|
| 1 | +package org.evomaster.client.java.controller.internal.db.redis; |
| 2 | + |
| 3 | +import org.evomaster.client.java.controller.internal.TaintHandlerExecutionTracer; |
| 4 | +import org.evomaster.client.java.controller.redis.RedisClient; |
| 5 | +import org.evomaster.client.java.controller.redis.RedisHeuristicsCalculator; |
| 6 | +import org.evomaster.client.java.controller.redis.RedisInfo; |
| 7 | +import org.evomaster.client.java.instrumentation.RedisCommand; |
| 8 | +import org.evomaster.client.java.utils.SimpleLogger; |
| 9 | + |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +import static org.evomaster.client.java.controller.redis.RedisHeuristicsCalculator.MAX_REDIS_DISTANCE; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class used to act upon Redis commands executed by the SUT |
| 16 | + */ |
| 17 | +public class RedisHandler { |
| 18 | + |
| 19 | + /** |
| 20 | + * Info about the executed commands |
| 21 | + */ |
| 22 | + private final List<RedisCommand> operations; |
| 23 | + |
| 24 | + /** |
| 25 | + * The heuristics based on the Redis execution |
| 26 | + */ |
| 27 | + private final List<RedisCommandEvaluation> evaluatedRedisCommands = new ArrayList<>(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Whether to calculate heuristics based on execution or not |
| 31 | + */ |
| 32 | + private volatile boolean calculateHeuristics; |
| 33 | + |
| 34 | + /** |
| 35 | + * Whether to use execution's info or not |
| 36 | + */ |
| 37 | + private volatile boolean extractRedisExecution; |
| 38 | + |
| 39 | + /** |
| 40 | + * The client must be created through a connection factory. |
| 41 | + */ |
| 42 | + private RedisClient redisClient = null; |
| 43 | + |
| 44 | + private final RedisHeuristicsCalculator calculator = new RedisHeuristicsCalculator(new TaintHandlerExecutionTracer()); |
| 45 | + |
| 46 | + private static final String REDIS_HASH_TYPE = "hash"; |
| 47 | + private static final String REDIS_SET_TYPE = "set"; |
| 48 | + private static final String REDIS_STRING_TYPE = "string"; |
| 49 | + |
| 50 | + public RedisHandler() { |
| 51 | + operations = new ArrayList<>(); |
| 52 | + extractRedisExecution = true; |
| 53 | + calculateHeuristics = true; |
| 54 | + } |
| 55 | + |
| 56 | + public void reset() { |
| 57 | + operations.clear(); |
| 58 | + evaluatedRedisCommands.clear(); |
| 59 | + } |
| 60 | + |
| 61 | + public boolean isCalculateHeuristics() { |
| 62 | + return calculateHeuristics; |
| 63 | + } |
| 64 | + |
| 65 | + public boolean isExtractRedisExecution() { |
| 66 | + return extractRedisExecution; |
| 67 | + } |
| 68 | + |
| 69 | + public void setCalculateHeuristics(boolean calculateHeuristics) { |
| 70 | + this.calculateHeuristics = calculateHeuristics; |
| 71 | + } |
| 72 | + |
| 73 | + public void setExtractRedisExecution(boolean extractRedisExecution) { |
| 74 | + this.extractRedisExecution = extractRedisExecution; |
| 75 | + } |
| 76 | + |
| 77 | + public void handle(RedisCommand info) { |
| 78 | + if (extractRedisExecution) { |
| 79 | + operations.add(info); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public List<RedisCommandEvaluation> getEvaluatedRedisCommands() { |
| 84 | + operations.stream() |
| 85 | + .filter(command -> command.getType().shouldCalculateHeuristic()) |
| 86 | + .forEach(redisCommand -> { |
| 87 | + RedisDistanceWithMetrics distanceWithMetrics = computeDistance(redisCommand, redisClient); |
| 88 | + evaluatedRedisCommands.add(new RedisCommandEvaluation(redisCommand, distanceWithMetrics)); |
| 89 | + }); |
| 90 | + operations.clear(); |
| 91 | + |
| 92 | + return evaluatedRedisCommands; |
| 93 | + } |
| 94 | + |
| 95 | + private RedisDistanceWithMetrics computeDistance(RedisCommand redisCommand, RedisClient redisClient) { |
| 96 | + RedisCommand.RedisCommandType type = redisCommand.getType(); |
| 97 | + try { |
| 98 | + switch (type) { |
| 99 | + case KEYS: |
| 100 | + case EXISTS: { |
| 101 | + List<RedisInfo> redisInfo = createRedisInfoForAllKeys(redisClient); |
| 102 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 103 | + } |
| 104 | + |
| 105 | + case GET: { |
| 106 | + List<RedisInfo> redisInfo = createRedisInfoForKeysByType(REDIS_STRING_TYPE, redisClient); |
| 107 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 108 | + } |
| 109 | + |
| 110 | + case HGET: { |
| 111 | + String field = redisCommand.extractArgs().get(1); |
| 112 | + List<RedisInfo> redisInfo = createRedisInfoForKeysByField(field, redisClient); |
| 113 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 114 | + } |
| 115 | + |
| 116 | + case HGETALL: { |
| 117 | + List<RedisInfo> redisInfo = createRedisInfoForKeysByType(REDIS_HASH_TYPE, redisClient); |
| 118 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 119 | + } |
| 120 | + |
| 121 | + case SMEMBERS: { |
| 122 | + List<RedisInfo> redisInfo = createRedisInfoForKeysByType(REDIS_SET_TYPE, redisClient); |
| 123 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 124 | + } |
| 125 | + |
| 126 | + case SINTER: { |
| 127 | + List<String> keys = redisCommand.extractArgs(); |
| 128 | + List<RedisInfo> redisInfo = createRedisInfoForIntersection(keys, redisClient); |
| 129 | + return calculator.computeDistance(redisCommand, redisInfo); |
| 130 | + } |
| 131 | + |
| 132 | + default: |
| 133 | + return new RedisDistanceWithMetrics(MAX_REDIS_DISTANCE, 0); |
| 134 | + } |
| 135 | + } catch (Exception e) { |
| 136 | + SimpleLogger.warn("Could not compute distance for " + type + ": " + e.getMessage()); |
| 137 | + return new RedisDistanceWithMetrics(MAX_REDIS_DISTANCE, 0); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private List<RedisInfo> createRedisInfoForIntersection(List<String> keys, RedisClient redisClient) { |
| 142 | + List<RedisInfo> redisData = new ArrayList<>(); |
| 143 | + keys.forEach( |
| 144 | + key -> redisData.add(new RedisInfo(key, redisClient.getType(key), redisClient.getSetMembers(key)) |
| 145 | + )); |
| 146 | + return redisData; |
| 147 | + } |
| 148 | + |
| 149 | + private List<RedisInfo> createRedisInfoForAllKeys(RedisClient redisClient) { |
| 150 | + Set<String> keys = redisClient.getAllKeys(); |
| 151 | + List<RedisInfo> redisData = new ArrayList<>(); |
| 152 | + keys.forEach( |
| 153 | + key -> redisData.add(new RedisInfo(key)) |
| 154 | + ); |
| 155 | + return redisData; |
| 156 | + } |
| 157 | + |
| 158 | + private List<RedisInfo> createRedisInfoForKeysByType(String type, RedisClient redisClient) { |
| 159 | + Set<String> keys = redisClient.getKeysByType(type); |
| 160 | + List<RedisInfo> redisData = new ArrayList<>(); |
| 161 | + keys.forEach(key -> redisData.add(new RedisInfo(key))); |
| 162 | + return redisData; |
| 163 | + } |
| 164 | + |
| 165 | + private List<RedisInfo> createRedisInfoForKeysByField(String field, RedisClient redisClient) { |
| 166 | + Set<String> keys = redisClient.getKeysByType(REDIS_HASH_TYPE); |
| 167 | + List<RedisInfo> redisData = new ArrayList<>(); |
| 168 | + keys.forEach(key -> redisData.add(new RedisInfo(key, redisClient.hashFieldExists(key, field)))); |
| 169 | + return redisData; |
| 170 | + } |
| 171 | + |
| 172 | + public void setRedisClient(RedisClient redisClient) { |
| 173 | + this.redisClient = redisClient; |
| 174 | + } |
| 175 | +} |
0 commit comments