-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
fe430f8
Redis handler and Redis heuristic calculator, plus tests.
aszyrej 4dcdb55
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej ed511c8
Redis handler and Redis heuristic calculator, plus tests.
aszyrej 3b657bc
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej d3dd421
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej 059b63f
Redis handler and Redis heuristic calculator, plus tests.
aszyrej b45545d
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej c27af0f
Redis handler and Redis heuristic calculator, plus tests.
aszyrej fa6eac4
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej 0365c95
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej 924fea2
Redis handler and Redis heuristic calculator, plus tests.
aszyrej 5ed7fda
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej 6e43d73
Redis handler and Redis heuristic calculator, plus tests.
aszyrej eb0450e
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej 4f39fee
Redis handler and Redis heuristic calculator, plus tests.
aszyrej 99687e2
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej 5c859d4
Redis handler and Redis heuristic calculator, plus tests.
aszyrej d14b33f
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej 08dfae7
Changes from PR review
aszyrej 3146af9
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej f381512
Merge branch 'master' into feature/redis-distance-calculation
aszyrej 19648f5
Merge branch 'master' into feature/redis-distance-calculation
aszyrej 06d98e5
Refactor for PR review
aszyrej e3651c3
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej 203db54
Merge branch 'master' into feature/redis-distance-calculation
aszyrej 847f905
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej 6661044
Added Lettuce again after merge with main
aszyrej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
...n/java/org/evomaster/client/java/controller/internal/db/redis/RedisCommandEvaluation.java
This file contains hidden or 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,24 @@ | ||
| package org.evomaster.client.java.controller.internal.db.redis; | ||
|
|
||
| import org.evomaster.client.java.instrumentation.RedisCommand; | ||
|
|
||
| /** | ||
| * This class will link a given RedisCommand to the result of the distance calculation for that commmand. | ||
| */ | ||
| public class RedisCommandEvaluation { | ||
| private final RedisCommand redisCommand; | ||
| private final RedisDistanceWithMetrics redisDistanceWithMetrics; | ||
|
|
||
| public RedisCommandEvaluation(RedisCommand redisCommand, RedisDistanceWithMetrics redisDistanceWithMetrics) { | ||
| this.redisCommand = redisCommand; | ||
| this.redisDistanceWithMetrics = redisDistanceWithMetrics; | ||
| } | ||
|
|
||
| public RedisCommand getRedisCommand() { | ||
| return redisCommand; | ||
| } | ||
|
|
||
| public RedisDistanceWithMetrics getRedisDistanceWithMetrics() { | ||
| return redisDistanceWithMetrics; | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
...java/org/evomaster/client/java/controller/internal/db/redis/RedisDistanceWithMetrics.java
This file contains hidden or 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,30 @@ | ||
| package org.evomaster.client.java.controller.internal.db.redis; | ||
|
|
||
| /** | ||
| * This class will have the distance for a RedisCommand (between 0 and 1) | ||
| * and the number of evaluated keys in that distance calculation. | ||
| */ | ||
| public class RedisDistanceWithMetrics { | ||
aszyrej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final double redisDistance; // A number between 0 and 1. | ||
| private 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; | ||
| } | ||
| } | ||
175 changes: 175 additions & 0 deletions
175
...er/src/main/java/org/evomaster/client/java/controller/internal/db/redis/RedisHandler.java
This file contains hidden or 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,175 @@ | ||
| package org.evomaster.client.java.controller.internal.db.redis; | ||
|
|
||
| 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.*; | ||
|
|
||
| import static org.evomaster.client.java.controller.redis.RedisHeuristicsCalculator.MAX_REDIS_DISTANCE; | ||
|
|
||
| /** | ||
| * 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()); | ||
|
|
||
| private static final String REDIS_HASH_TYPE = "hash"; | ||
| private static final String REDIS_SET_TYPE = "set"; | ||
| private static final String REDIS_STRING_TYPE = "string"; | ||
|
|
||
| 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(REDIS_STRING_TYPE, 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(REDIS_HASH_TYPE, redisClient); | ||
| return calculator.computeDistance(redisCommand, redisInfo); | ||
| } | ||
|
|
||
| case SMEMBERS: { | ||
| List<RedisInfo> redisInfo = createRedisInfoForKeysByType(REDIS_SET_TYPE, 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(MAX_REDIS_DISTANCE, 0); | ||
| } | ||
| } catch (Exception e) { | ||
| SimpleLogger.warn("Could not compute distance for " + type + ": " + e.getMessage()); | ||
| return new RedisDistanceWithMetrics(MAX_REDIS_DISTANCE, 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(REDIS_HASH_TYPE); | ||
| 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; | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
...java/controller/src/main/java/org/evomaster/client/java/controller/redis/RedisClient.java
This file contains hidden or 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,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"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.