|
4 | 4 | import io.vertx.core.Context; |
5 | 5 | import io.vertx.core.Future; |
6 | 6 | import io.vertx.core.buffer.Buffer; |
| 7 | +import io.vertx.core.json.JsonObject; |
7 | 8 | import io.vertx.ext.unit.Async; |
8 | 9 | import io.vertx.ext.unit.TestContext; |
9 | 10 | import io.vertx.ext.unit.junit.RunTestOnContext; |
10 | 11 | import io.vertx.ext.unit.junit.VertxUnitRunner; |
11 | 12 | import io.vertx.redis.client.*; |
12 | | -import java.util.stream.Collectors; |
| 13 | +import io.vertx.redis.client.impl.ZModem; |
13 | 14 | import org.junit.*; |
14 | 15 | import org.junit.runner.RunWith; |
15 | 16 | import org.testcontainers.containers.FixedHostPortGenericContainer; |
16 | 17 | import org.testcontainers.containers.GenericContainer; |
17 | 18 |
|
18 | 19 | import java.util.*; |
19 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
| 21 | +import java.util.stream.Collectors; |
20 | 22 |
|
21 | 23 | import static io.vertx.redis.client.Command.*; |
22 | 24 | import static io.vertx.redis.client.Request.cmd; |
@@ -1163,6 +1165,98 @@ public void setAndWaitEmptyBatch(TestContext should) { |
1163 | 1165 | )); |
1164 | 1166 | } |
1165 | 1167 |
|
| 1168 | + @Test(timeout = 30_000) |
| 1169 | + @SuppressWarnings("unchecked") |
| 1170 | + public void batchSameSlotGroupByMultipleSlotsCommands(TestContext should) { |
| 1171 | + final Async test = should.async(); |
| 1172 | + |
| 1173 | + Redis.createClient(rule.vertx(), options) |
| 1174 | + .connect() |
| 1175 | + .onComplete(should.asyncAssertSuccess(cluster -> { |
| 1176 | + cluster |
| 1177 | + .send(cmd(CLUSTER).arg("SLOTS")) |
| 1178 | + .compose(reply -> { |
| 1179 | + if (reply == null || reply.size() == 0) { |
| 1180 | + // no slots available we can't really proceed |
| 1181 | + return Future.failedFuture("CLUSTER SLOTS No slots available in the cluster."); |
| 1182 | + } |
| 1183 | + |
| 1184 | + Map<Integer, JsonObject> slotRangeMap = new HashMap<>(); |
| 1185 | + for (int i = 0; i < reply.size(); i++) { |
| 1186 | + Response s = reply.get(i); |
| 1187 | + JsonObject slotRange = new JsonObject() |
| 1188 | + .put("start", s.get(0).toInteger()) |
| 1189 | + .put("end", s.get(1).toInteger()); |
| 1190 | + slotRangeMap.put(i, slotRange); |
| 1191 | + } |
| 1192 | + |
| 1193 | + Map<Integer, List<Request>> mapCommands = new HashMap<>(); |
| 1194 | + for (int i = 0; i < 100000; i++) { |
| 1195 | + String key = "key-" + i; |
| 1196 | + String value = "value-" + i; |
| 1197 | + int group; |
| 1198 | + int slot = ZModem.generate(key); |
| 1199 | + for (Map.Entry<Integer, JsonObject> entry : slotRangeMap.entrySet()) { |
| 1200 | + if (slot >= entry.getValue().getInteger("start") && slot <= entry.getValue().getInteger("end")) { |
| 1201 | + mapCommands.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(cmd(SET).arg(key).arg(value)); |
| 1202 | + break; |
| 1203 | + } |
| 1204 | + } |
| 1205 | + } |
| 1206 | + |
| 1207 | + List<Future<List<Response>>> futures = new ArrayList<>(); |
| 1208 | + for (Map.Entry<Integer, List<Request>> entry : mapCommands.entrySet()) { |
| 1209 | + futures.add(cluster.batch(entry.getValue())); |
| 1210 | + } |
| 1211 | + |
| 1212 | + return Future.all(futures) |
| 1213 | + .onComplete(should.asyncAssertSuccess(responses -> { |
| 1214 | + should.assertEquals(mapCommands.values().stream().map(List::size).reduce(0, Integer::sum), responses.result().list().stream().map(item -> ((List<Request>) item).size()).reduce(0, Integer::sum)); |
| 1215 | + test.complete(); |
| 1216 | + })); |
| 1217 | + }).onFailure(should::fail); |
| 1218 | + })).onFailure(should::fail); |
| 1219 | + } |
| 1220 | + |
| 1221 | + @Test(timeout = 30_000) |
| 1222 | + public void batchSameSlotsCommands(TestContext should) { |
| 1223 | + final Async test = should.async(); |
| 1224 | + |
| 1225 | + Redis.createClient(rule.vertx(), options) |
| 1226 | + .connect() |
| 1227 | + .onComplete(should.asyncAssertSuccess(cluster -> { |
| 1228 | + cluster |
| 1229 | + .send(cmd(CLUSTER).arg("SLOTS")) |
| 1230 | + .compose(reply -> { |
| 1231 | + if (reply == null || reply.size() == 0) { |
| 1232 | + // no slots available we can't really proceed |
| 1233 | + return Future.failedFuture("CLUSTER SLOTS No slots available in the cluster."); |
| 1234 | + } |
| 1235 | + |
| 1236 | + //take random slot |
| 1237 | + Response s = reply.get(0); |
| 1238 | + JsonObject slotRange = new JsonObject() |
| 1239 | + .put("start", s.get(0).toInteger()) |
| 1240 | + .put("end", s.get(1).toInteger()); |
| 1241 | + |
| 1242 | + List<Request> rawCommands = new ArrayList<>(); |
| 1243 | + for (int i = 0; i < 100000; i++) { |
| 1244 | + String key = "key-" + i; |
| 1245 | + String value = "value-" + i; |
| 1246 | + int endpoint; |
| 1247 | + int slot = ZModem.generate(key); |
| 1248 | + if (slot >= slotRange.getInteger("start") && slot <= slotRange.getInteger("end")) { |
| 1249 | + rawCommands.add(Request.cmd(Command.SET).arg(key).arg(value)); |
| 1250 | + } |
| 1251 | + } |
| 1252 | + return cluster.batch(rawCommands).onComplete(should.asyncAssertSuccess(responses -> { |
| 1253 | + should.assertEquals(rawCommands.size(), responses.size()); |
| 1254 | + test.complete(); |
| 1255 | + })); |
| 1256 | + }).onFailure(should::fail); |
| 1257 | + })).onFailure(should::fail); |
| 1258 | + } |
| 1259 | + |
1166 | 1260 | @Test(timeout = 30_000) |
1167 | 1261 | public void clusterInfoReturnsVerbatimString(TestContext should) { |
1168 | 1262 | final Async test = should.async(); |
|
0 commit comments