Skip to content
Merged
Show file tree
Hide file tree
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 Oct 10, 2025
4dcdb55
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
ed511c8
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
3b657bc
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
d3dd421
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej Oct 21, 2025
059b63f
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
b45545d
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
c27af0f
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
fa6eac4
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
0365c95
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej Oct 24, 2025
924fea2
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
5ed7fda
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
6e43d73
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
eb0450e
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
4f39fee
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
99687e2
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
5c859d4
Redis handler and Redis heuristic calculator, plus tests.
aszyrej Oct 10, 2025
d14b33f
Modified Redis Heuristic Calculator to decouple it from RedisDB. Redi…
aszyrej Oct 21, 2025
08dfae7
Changes from PR review
aszyrej Oct 26, 2025
3146af9
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej Oct 26, 2025
f381512
Merge branch 'master' into feature/redis-distance-calculation
aszyrej Oct 27, 2025
19648f5
Merge branch 'master' into feature/redis-distance-calculation
aszyrej Oct 27, 2025
06d98e5
Refactor for PR review
aszyrej Nov 2, 2025
e3651c3
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej Nov 2, 2025
203db54
Merge branch 'master' into feature/redis-distance-calculation
aszyrej Nov 2, 2025
847f905
Merge branch 'feature/redis-distance-calculation' of https://github.c…
aszyrej Nov 2, 2025
6661044
Added Lettuce again after merge with main
aszyrej Nov 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client-java/controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
<artifactId>mongodb-driver-sync</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<scope>test</scope>
</dependency>

<!--gRPC RPC test-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ default void extractRPCSchema(){}

default Object getOpenSearchConnection() {return null;}

default Object getRedisConnection() {return null;}

/**
* <p>
* register or execute specified SQL script for initializing data in database
Expand Down
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;
}
}
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 {
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;
}
}
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;
}
}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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");
}
}
Loading