-
Notifications
You must be signed in to change notification settings - Fork 98
Redis handler and heuristic calculator. #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
fe430f8
4dcdb55
ed511c8
3b657bc
d3dd421
059b63f
b45545d
c27af0f
fa6eac4
0365c95
924fea2
5ed7fda
6e43d73
eb0450e
4f39fee
99687e2
5c859d4
d14b33f
08dfae7
3146af9
f381512
19648f5
06d98e5
e3651c3
203db54
847f905
6661044
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package org.evomaster.client.java.controller.internal.db; | ||
|
|
||
| import org.evomaster.client.java.instrumentation.RedisCommand; | ||
|
|
||
| public class RedisCommandEvaluation { | ||
| public final RedisCommand redisCommand; | ||
|
||
| public final RedisDistanceWithMetrics redisDistanceWithMetrics; | ||
|
|
||
| public RedisCommandEvaluation(RedisCommand redisCommand, RedisDistanceWithMetrics redisDistanceWithMetrics) { | ||
| this.redisCommand = redisCommand; | ||
| this.redisDistanceWithMetrics = redisDistanceWithMetrics; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package org.evomaster.client.java.controller.internal.db; | ||
|
|
||
| public class RedisDistanceWithMetrics { | ||
| public final double redisDistance; // A number between 0 and 1. | ||
| public final int numberOfEvaluatedKeys; | ||
|
||
|
|
||
| public RedisDistanceWithMetrics(double redisDistance, int numberOfEvaluatedKeys) { | ||
| if(redisDistance < 0){ | ||
| throw new IllegalArgumentException("Distance must be non-negative but value is " + redisDistance); | ||
| } | ||
| if(numberOfEvaluatedKeys < 0){ | ||
| throw new IllegalArgumentException("Number of evaluated keys must be non-negative but value is " | ||
| + numberOfEvaluatedKeys); | ||
| } | ||
| this.redisDistance = redisDistance; | ||
| this.numberOfEvaluatedKeys = numberOfEvaluatedKeys; | ||
| } | ||
|
|
||
| public int getNumberOfEvaluatedKeys() { | ||
| return numberOfEvaluatedKeys; | ||
| } | ||
|
|
||
| public double getDistance() { | ||
| return redisDistance; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package org.evomaster.client.java.controller.internal.db; | ||
|
|
||
| import org.evomaster.client.java.controller.internal.TaintHandlerExecutionTracer; | ||
| import org.evomaster.client.java.controller.redis.RedisClient; | ||
| import org.evomaster.client.java.controller.redis.RedisHeuristicsCalculator; | ||
| import org.evomaster.client.java.controller.redis.RedisInfo; | ||
| import org.evomaster.client.java.instrumentation.RedisCommand; | ||
| import org.evomaster.client.java.utils.SimpleLogger; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| /** | ||
| * Class used to act upon Redis commands executed by the SUT | ||
| */ | ||
| public class RedisHandler { | ||
|
|
||
| /** | ||
| * Info about the executed commands | ||
| */ | ||
| private final List<RedisCommand> operations; | ||
|
|
||
| /** | ||
| * The heuristics based on the Redis execution | ||
| */ | ||
| private final List<RedisCommandEvaluation> evaluatedRedisCommands = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Whether to calculate heuristics based on execution or not | ||
| */ | ||
| private volatile boolean calculateHeuristics; | ||
|
|
||
| /** | ||
| * Whether to use execution's info or not | ||
| */ | ||
| private volatile boolean extractRedisExecution; | ||
|
|
||
| /** | ||
| * The client must be created through a connection factory. | ||
| */ | ||
| private RedisClient redisClient = null; | ||
|
|
||
| private final RedisHeuristicsCalculator calculator = new RedisHeuristicsCalculator(new TaintHandlerExecutionTracer()); | ||
|
|
||
|
|
||
| public RedisHandler() { | ||
| operations = new ArrayList<>(); | ||
| extractRedisExecution = true; | ||
| calculateHeuristics = true; | ||
| } | ||
|
|
||
| public void reset() { | ||
| operations.clear(); | ||
| evaluatedRedisCommands.clear(); | ||
| } | ||
|
|
||
| public boolean isCalculateHeuristics() { | ||
| return calculateHeuristics; | ||
| } | ||
|
|
||
| public boolean isExtractRedisExecution() { | ||
| return extractRedisExecution; | ||
| } | ||
|
|
||
| public void setCalculateHeuristics(boolean calculateHeuristics) { | ||
| this.calculateHeuristics = calculateHeuristics; | ||
| } | ||
|
|
||
| public void setExtractRedisExecution(boolean extractRedisExecution) { | ||
| this.extractRedisExecution = extractRedisExecution; | ||
| } | ||
|
|
||
| public void handle(RedisCommand info) { | ||
| if (extractRedisExecution) { | ||
| operations.add(info); | ||
| } | ||
| } | ||
|
|
||
| public List<RedisCommandEvaluation> getEvaluatedRedisCommands() { | ||
| operations.stream() | ||
| .filter(command -> command.getType().shouldCalculateHeuristic()) | ||
| .forEach(redisCommand -> { | ||
| RedisDistanceWithMetrics distanceWithMetrics = computeDistance(redisCommand, redisClient); | ||
| evaluatedRedisCommands.add(new RedisCommandEvaluation(redisCommand, distanceWithMetrics)); | ||
| }); | ||
| operations.clear(); | ||
|
|
||
| return evaluatedRedisCommands; | ||
| } | ||
|
|
||
| private RedisDistanceWithMetrics computeDistance(RedisCommand redisCommand, RedisClient redisClient) { | ||
| RedisCommand.RedisCommandType type = redisCommand.getType(); | ||
| try { | ||
| switch (type) { | ||
| case KEYS: | ||
| case EXISTS: { | ||
| List<RedisInfo> redisInfo = createRedisInfoForAllKeys(redisClient); | ||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case GET: { | ||
| List<RedisInfo> redisInfo = createRedisInfoForKeysByType("string", redisClient); | ||
|
||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case HGET: { | ||
| String field = redisCommand.extractArgs().get(1); | ||
| List<RedisInfo> redisInfo = createRedisInfoForKeysByField(field, redisClient); | ||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case HGETALL: { | ||
| List<RedisInfo> redisInfo = createRedisInfoForKeysByType("hash", redisClient); | ||
|
||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case SMEMBERS: { | ||
| List<RedisInfo> redisInfo = createRedisInfoForKeysByType("set", redisClient); | ||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case SINTER: { | ||
| List<String> keys = redisCommand.extractArgs(); | ||
| List<RedisInfo> redisInfo = createRedisInfoForIntersection(keys, redisClient); | ||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| default: | ||
| return new RedisDistanceWithMetrics(1d, 0); | ||
|
||
| } | ||
| } catch (Exception e) { | ||
| SimpleLogger.warn("Could not compute distance for " + type + ": " + e.getMessage()); | ||
| return new RedisDistanceWithMetrics(1d, 0); | ||
| } | ||
| } | ||
|
|
||
| private List<RedisInfo> createRedisInfoForIntersection(List<String> keys, RedisClient redisClient) { | ||
| List<RedisInfo> redisData = new ArrayList<>(); | ||
| keys.forEach( | ||
| key -> redisData.add(new RedisInfo(key, redisClient.getType(key), redisClient.getSetMembers(key)) | ||
| )); | ||
| return redisData; | ||
| } | ||
|
|
||
| private List<RedisInfo> createRedisInfoForAllKeys(RedisClient redisClient) { | ||
| Set<String> keys = redisClient.getAllKeys(); | ||
| List<RedisInfo> redisData = new ArrayList<>(); | ||
| keys.forEach( | ||
| key -> redisData.add(new RedisInfo(key)) | ||
| ); | ||
| return redisData; | ||
| } | ||
|
|
||
| private List<RedisInfo> createRedisInfoForKeysByType(String type, RedisClient redisClient) { | ||
| Set<String> keys = redisClient.getKeysByType(type); | ||
| List<RedisInfo> redisData = new ArrayList<>(); | ||
| keys.forEach(key -> redisData.add(new RedisInfo(key))); | ||
| return redisData; | ||
| } | ||
|
|
||
| private List<RedisInfo> createRedisInfoForKeysByField(String field, RedisClient redisClient) { | ||
| Set<String> keys = redisClient.getKeysByType("hash"); | ||
| List<RedisInfo> redisData = new ArrayList<>(); | ||
| keys.forEach(key -> redisData.add(new RedisInfo(key, redisClient.hashFieldExists(key, field)))); | ||
| return redisData; | ||
| } | ||
|
|
||
| public void setRedisClient(RedisClient redisClient) { | ||
| this.redisClient = redisClient; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package org.evomaster.client.java.controller.redis; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * RedisClient that uses Lettuce dynamically via reflection, avoiding | ||
| * compile-time dependency on Spring or Lettuce. | ||
| */ | ||
| public class RedisClient { | ||
|
|
||
| private final Object redisClient; // io.lettuce.core.RedisClient | ||
| private final Object connection; // io.lettuce.core.api.StatefulRedisConnection | ||
| private final Object syncCommands; // io.lettuce.core.api.sync.RedisCommands | ||
|
|
||
| public RedisClient(String host, int port) { | ||
| try { | ||
| Class<?> redisClientClass = Class.forName("io.lettuce.core.RedisClient"); | ||
| Class<?> redisURIClass = Class.forName("io.lettuce.core.RedisURI"); | ||
|
|
||
| Method createUri = redisURIClass.getMethod("create", String.class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace strings with constants |
||
| Object uri = createUri.invoke(null, "redis://" + host + ":" + port); | ||
|
|
||
| Method createClient = redisClientClass.getMethod("create", redisURIClass); | ||
| this.redisClient = createClient.invoke(null, uri); | ||
|
|
||
| Method connectMethod = redisClientClass.getMethod("connect"); | ||
| this.connection = connectMethod.invoke(redisClient); | ||
|
|
||
| Class<?> statefulConnClass = Class.forName("io.lettuce.core.api.StatefulRedisConnection"); | ||
| Method syncMethod = statefulConnClass.getMethod("sync"); | ||
| this.syncCommands = syncMethod.invoke(connection); | ||
|
|
||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to initialize Lettuce Redis client via reflection", e); | ||
| } | ||
| } | ||
|
|
||
| public void close() { | ||
| try { | ||
| if (connection != null) { | ||
| Method close = connection.getClass().getMethod("close"); | ||
| close.invoke(connection); | ||
| } | ||
| if (redisClient != null) { | ||
| Method shutdown = redisClient.getClass().getMethod("shutdown"); | ||
| shutdown.invoke(redisClient); | ||
| } | ||
| } catch (Exception ignored) {} | ||
| } | ||
|
|
||
| /** Equivalent to SET key value */ | ||
| public void setValue(String key, String value) { | ||
| invoke("set", key, value); | ||
| } | ||
|
|
||
| /** Equivalent to GET key */ | ||
| public String getValue(String key) { | ||
| return (String) invoke("get", key); | ||
| } | ||
|
|
||
| /** Equivalent to KEYS * */ | ||
| public Set<String> getAllKeys() { | ||
| Object result = invoke("keys", "*"); | ||
| if (result instanceof Collection) | ||
| return new HashSet<>((Collection<String>) result); | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| /** Equivalent to TYPE key */ | ||
| public String getType(String key) { | ||
| Object result = invoke("type", key); | ||
| return result != null ? result.toString() : null; | ||
| } | ||
|
|
||
| /** HSET key field value */ | ||
| public void hashSet(String key, String field, String value) { | ||
| invoke("hset", key, field, value); | ||
| } | ||
|
|
||
| /** HEXISTS key field */ | ||
| public boolean hashFieldExists(String key, String field) { | ||
| Object result = invoke("hexists", key, field); | ||
| return result instanceof Boolean && (Boolean) result; | ||
| } | ||
|
|
||
| /** SMEMBERS key */ | ||
| public Set<String> getSetMembers(String key) { | ||
| Object result = invoke("smembers", key); | ||
| if (result instanceof Collection) | ||
| return new HashSet<>((Collection<String>) result); | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| private Object invoke(String methodName, Object... args) { | ||
| try { | ||
| Class<?>[] argTypes = Arrays.stream(args) | ||
| .map(Object::getClass) | ||
| .toArray(Class<?>[]::new); | ||
|
|
||
| Method method = findMethod(syncCommands.getClass(), methodName, argTypes); | ||
| if (method == null) | ||
| throw new RuntimeException("Method not found: " + methodName); | ||
| return method.invoke(syncCommands, args); | ||
|
|
||
| } catch (Exception e) { | ||
| throw new RuntimeException("Error invoking Redis command: " + methodName, e); | ||
| } | ||
| } | ||
|
|
||
| private Method findMethod(Class<?> clazz, String name, Class<?>[] argTypes) { | ||
| for (Method m : clazz.getMethods()) { | ||
| if (!m.getName().equals(name)) continue; | ||
| if (m.getParameterCount() != argTypes.length) continue; | ||
| return m; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public Set<String> getKeysByType(String expectedType) { | ||
| return getAllKeys().stream() | ||
| .filter(k -> expectedType.equalsIgnoreCase(getType(k))) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| public void flushAll() { | ||
| invoke("flushall"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as more files are added to
internal/db, should refactor, and have subfolders forinternal/db/redis,internal/db/mongoandinternal/db/opensearchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved all three Redis files from internal/db to internal/db/redis, and left mongo and opensearch files as they were for this specific PR, for a future refactor