-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Introduced core and cli subprojects
- Loading branch information
Julien Ruaux
committed
Apr 21, 2023
1 parent
55bc8d4
commit be6cba7
Showing
72 changed files
with
622 additions
and
3,598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ hs_err_pid*.log | |
*.iml | ||
.vscode | ||
/README.html | ||
/spring-shell.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
project_description=Redis Smart Cache Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
dependencies { | ||
implementation group: 'com.redis', name: 'lettucemod', version: lettucemodVersion | ||
implementation 'org.springframework:spring-beans' | ||
implementation 'com.fasterxml.jackson.core:jackson-databind' | ||
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-properties' | ||
implementation group: 'io.airlift', name: 'units', version: airliftVersion | ||
implementation 'io.micrometer:micrometer-registry-jmx' | ||
implementation group: 'com.redis', name: 'micrometer-registry-redis', version: micrometerRedisVersion | ||
testImplementation group: 'org.awaitility', name: 'awaitility', version: awaitilityVersion | ||
testImplementation group: 'com.redis.testcontainers', name: 'testcontainers-redis-junit', version: testcontainersRedisVersion | ||
} | ||
|
||
bootJar { | ||
enabled = false | ||
} | ||
|
||
jar { | ||
archiveClassifier = '' | ||
} | ||
|
||
compileJava { | ||
options.release = 8 | ||
} |
50 changes: 50 additions & 0 deletions
50
core/redis-smart-cache-core/src/main/java/com/redis/smartcache/core/ClientManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.redis.smartcache.core; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import com.redis.lettucemod.util.ClientBuilder; | ||
import com.redis.lettucemod.util.RedisURIBuilder; | ||
import com.redis.smartcache.core.Config.RedisConfig; | ||
|
||
import io.lettuce.core.AbstractRedisClient; | ||
import io.lettuce.core.RedisURI; | ||
|
||
public class ClientManager implements AutoCloseable { | ||
|
||
private static final Logger log = Logger.getLogger(ClientManager.class.getName()); | ||
|
||
private final Map<Config, AbstractRedisClient> clients = new HashMap<>(); | ||
|
||
public AbstractRedisClient getClient(Config config) { | ||
return clients.computeIfAbsent(config, this::client); | ||
} | ||
|
||
private AbstractRedisClient client(Config config) { | ||
RedisURI redisURI = redisURI(config.getRedis()); | ||
log.log(Level.FINE, "Creating Redis client with URI {0}", redisURI); | ||
return ClientBuilder.create(redisURI).cluster(config.getRedis().isCluster()).build(); | ||
} | ||
|
||
private RedisURI redisURI(RedisConfig config) { | ||
RedisURIBuilder builder = RedisURIBuilder.create(); | ||
builder.uri(config.getUri()); | ||
builder.username(config.getUsername()); | ||
builder.password(config.getPassword()); | ||
builder.ssl(config.isTls()); | ||
builder.sslVerifyMode(config.getTlsVerify()); | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
clients.values().forEach(c -> { | ||
c.shutdown(); | ||
c.getResources().shutdown(); | ||
}); | ||
clients.clear(); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
core/redis-smart-cache-core/src/main/java/com/redis/smartcache/core/Fields.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.redis.smartcache.core; | ||
|
||
public interface Fields { | ||
|
||
public static final String METER_QUERY = "query"; | ||
public static final String TAG_ID = "id"; | ||
public static final String TAG_TABLE = "table"; | ||
public static final String TAG_SQL = "sql"; | ||
public static final String TAG_TYPE = "type"; | ||
|
||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
core/redis-smart-cache-core/src/main/java/com/redis/smartcache/core/Mappers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.redis.smartcache.core; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper; | ||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsSchema; | ||
|
||
public class Mappers { | ||
|
||
public static final String PROPERTY_PREFIX = "smartcache"; | ||
public static final String PROPERTY_PREFIX_DRIVER = PROPERTY_PREFIX + ".driver"; | ||
public static final String PROPERTY_PREFIX_REDIS = PROPERTY_PREFIX + ".redis"; | ||
|
||
private static final JavaPropsSchema PROPS_SCHEMA = JavaPropsSchema.emptySchema().withPrefix(PROPERTY_PREFIX); | ||
private static final JavaPropsMapper PROPS_MAPPER = propsMapper(); | ||
|
||
private Mappers() { | ||
} | ||
|
||
public static JavaPropsMapper propsMapper() { | ||
return JavaPropsMapper.builder().serializationInclusion(Include.NON_NULL) | ||
.propertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE) | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build(); | ||
} | ||
|
||
public static Properties properties(Config config) throws IOException { | ||
return PROPS_MAPPER.writeValueAsProperties(config, PROPS_SCHEMA); | ||
} | ||
|
||
public static Config config(Properties properties) throws IOException { | ||
return PROPS_MAPPER.readPropertiesAs(properties, PROPS_SCHEMA, Config.class); | ||
} | ||
|
||
} |
197 changes: 197 additions & 0 deletions
197
.../redis-smart-cache-core/src/main/java/com/redis/smartcache/core/MeterRegistryManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package com.redis.smartcache.core; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
import com.redis.lettucemod.api.StatefulRedisModulesConnection; | ||
import com.redis.lettucemod.util.RedisModulesUtils; | ||
import com.redis.micrometer.RediSearchMeterRegistry; | ||
import com.redis.micrometer.RediSearchRegistryConfig; | ||
import com.redis.micrometer.RedisRegistryConfig; | ||
import com.redis.micrometer.RedisTimeSeriesMeterRegistry; | ||
|
||
import io.lettuce.core.AbstractRedisClient; | ||
import io.micrometer.core.instrument.Clock; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.simple.SimpleConfig; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import io.micrometer.jmx.JmxConfig; | ||
import io.micrometer.jmx.JmxMeterRegistry; | ||
|
||
public class MeterRegistryManager implements AutoCloseable { | ||
|
||
private static final Logger log = Logger.getLogger(MeterRegistryManager.class.getName()); | ||
|
||
public static final String KEYSPACE_METRICS = "metrics"; | ||
|
||
private final ClientManager clientManager; | ||
private final Map<Config, MeterRegistry> registries = new HashMap<>(); | ||
|
||
public MeterRegistryManager(ClientManager clientManager) { | ||
this.clientManager = clientManager; | ||
} | ||
|
||
public MeterRegistry getRegistry(Config config) { | ||
return registries.computeIfAbsent(config, this::meterRegistry); | ||
} | ||
|
||
private MeterRegistry meterRegistry(Config config) { | ||
switch (config.getMetrics().getRegistry()) { | ||
case JMX: | ||
return jmxMeterRegistry(config); | ||
case REDIS: | ||
return redisMeterRegistry(config); | ||
default: | ||
return simpleMeterRegistry(config); | ||
} | ||
} | ||
|
||
private SimpleMeterRegistry simpleMeterRegistry(Config config) { | ||
Duration step = step(config); | ||
return new SimpleMeterRegistry(new SimpleConfig() { | ||
|
||
@Override | ||
public String get(String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Duration step() { | ||
return step; | ||
} | ||
|
||
}, Clock.SYSTEM); | ||
} | ||
|
||
private JmxMeterRegistry jmxMeterRegistry(Config config) { | ||
Duration step = step(config); | ||
return new JmxMeterRegistry(new JmxConfig() { | ||
|
||
@Override | ||
public String get(String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Duration step() { | ||
return step; | ||
} | ||
|
||
@Override | ||
public String domain() { | ||
return config.getName(); | ||
} | ||
|
||
}, Clock.SYSTEM); | ||
} | ||
|
||
private Duration duration(io.airlift.units.Duration duration) { | ||
return Duration.ofMillis(duration.toMillis()); | ||
} | ||
|
||
private Duration step(Config config) { | ||
return duration(config.getMetrics().getStep()); | ||
} | ||
|
||
private MeterRegistry redisMeterRegistry(Config config) { | ||
AbstractRedisClient client = clientManager.getClient(config); | ||
StatefulRedisModulesConnection<String, String> connection = RedisModulesUtils.connection(client); | ||
try { | ||
connection.sync().ftList(); | ||
} catch (Exception e) { | ||
// Looks like we don't have Redis Modules | ||
log.info("No RediSearch found, downgrading to simple meter registry"); | ||
return simpleMeterRegistry(config); | ||
} | ||
log.fine("Creating meter registry"); | ||
Duration step = step(config); | ||
KeyBuilder keyBuilder = KeyBuilder.of(config); | ||
String tsKeyspace = keyBuilder.build(KEYSPACE_METRICS); | ||
RedisTimeSeriesMeterRegistry tsRegistry = new RedisTimeSeriesMeterRegistry(new RedisRegistryConfig() { | ||
|
||
@Override | ||
public String get(String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String keyspace() { | ||
return tsKeyspace; | ||
} | ||
|
||
@Override | ||
public String keySeparator() { | ||
return keyBuilder.separator(); | ||
} | ||
|
||
@Override | ||
public Duration step() { | ||
return step; | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return config.getMetrics().isEnabled(); | ||
} | ||
|
||
}, Clock.SYSTEM, client); | ||
tsRegistry.config().meterFilter(MeterFilter.ignoreTags(Fields.TAG_SQL, Fields.TAG_TABLE)); | ||
RediSearchMeterRegistry searchRegistry = new RediSearchMeterRegistry(new RediSearchRegistryConfig() { | ||
|
||
@Override | ||
public String get(String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String keyspace() { | ||
return keyBuilder.keyspace().orElse(null); | ||
} | ||
|
||
@Override | ||
public String keySeparator() { | ||
return keyBuilder.separator(); | ||
} | ||
|
||
@Override | ||
public Duration step() { | ||
return step; | ||
} | ||
|
||
@Override | ||
public String[] nonKeyTags() { | ||
return new String[] { Fields.TAG_SQL, Fields.TAG_TABLE, Fields.TAG_TYPE }; | ||
} | ||
|
||
@Override | ||
public String indexPrefix() { | ||
return keyBuilder.keyspace().orElse(null); | ||
} | ||
|
||
@Override | ||
public String indexSuffix() { | ||
return "idx"; | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return config.getMetrics().isEnabled(); | ||
} | ||
|
||
}, Clock.SYSTEM, client); | ||
searchRegistry.config().meterFilter(MeterFilter.acceptNameStartsWith(Fields.METER_QUERY)) | ||
.meterFilter(MeterFilter.deny()); | ||
return new CompositeMeterRegistry().add(tsRegistry).add(searchRegistry); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
registries.values().forEach(MeterRegistry::close); | ||
registries.clear(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
core/redis-smart-cache-core/src/main/java/com/redis/smartcache/core/RulesetManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.redis.smartcache.core; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper; | ||
import com.redis.smartcache.core.Config.RulesetConfig; | ||
|
||
import io.lettuce.core.AbstractRedisClient; | ||
|
||
public class RulesetManager implements AutoCloseable { | ||
|
||
public static final String KEY_CONFIG = "config"; | ||
|
||
private final JavaPropsMapper mapper = Mappers.propsMapper(); | ||
private final Map<Config, ConfigManager<RulesetConfig>> configManagers = new HashMap<>(); | ||
private final ClientManager clientManager; | ||
|
||
public RulesetManager(ClientManager clientManager) { | ||
this.clientManager = clientManager; | ||
} | ||
|
||
public RulesetConfig getRuleset(Config config) { | ||
return configManagers.computeIfAbsent(config, this::createConfigManager).get(); | ||
} | ||
|
||
private ConfigManager<RulesetConfig> createConfigManager(Config config) { | ||
AbstractRedisClient client = clientManager.getClient(config); | ||
String key = KeyBuilder.of(config).build(KEY_CONFIG); | ||
RulesetConfig ruleset = config.getRuleset(); | ||
StreamConfigManager<RulesetConfig> configManager = new StreamConfigManager<>(client, key, ruleset, mapper); | ||
try { | ||
configManager.start(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Could not start config manager", e); | ||
} | ||
return configManager; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
for (ConfigManager<RulesetConfig> configManager : configManagers.values()) { | ||
configManager.close(); | ||
} | ||
configManagers.clear(); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.