Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,13 @@
package org.evomaster.client.java.controller.internal.db;
Copy link
Collaborator

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 for internal/db/redis, internal/db/mongo and internal/db/opensearch

Copy link
Collaborator Author

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


import org.evomaster.client.java.instrumentation.RedisCommand;

public class RedisCommandEvaluation {
public final RedisCommand redisCommand;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those should be private, with public getters. usually, we have public fields only for DTOs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are private now.

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are private now.


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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace all "...." string constants with constant definitions

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace "hash" with string constant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added constant Strings.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace 1d with constant value

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added constant value for MAX_REDIS_DISTANCE in RedisHeuristicCalculator, and that is the value imported in RedisHandler.

}
} 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);
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