Visitar URL original
Fixed - check synced slaves in RTransaction commit #6292 · redisson/redisson@0feeec5 · GitHub
Skip to content

Commit 0feeec5

Browse files
committed
Fixed - check synced slaves in RTransaction commit #6292
Signed-off-by: Ronit Gupta <ronitgupta138@gmail.com>
1 parent adf15d3 commit 0feeec5

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎redisson/src/main/java/org/redisson/transaction/RedissonTransaction.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,19 @@ public RFuture<Void> commitAsync() {
264264

265265
enableLocalCacheAsync(id, (Map<HashKey, HashValue>) hashes);
266266
executed.set(true);
267+
268+
if (batchOptions.getSyncSlaves() > 0 && r.getSyncedSlaves() < batchOptions.getSyncSlaves()) {
269+
throw new CompletionException(new TransactionException("Unable to sync " + batchOptions.getSyncSlaves()
270+
+ " slaves within " + options.getSyncTimeout() + " ms"));
271+
}
272+
267273
return null;
268274
});
269275
});
270276
return new CompletableFutureWrapper<>(ff);
271277
}
272278

273-
private BatchOptions createOptions() {
279+
BatchOptions createOptions() {
274280
MasterSlaveEntry entry = commandExecutor.getConnectionManager().getEntrySet().iterator().next();
275281
int syncSlaves = entry.getAvailableSlaves();
276282
if (options.getSyncSlaves() == -1) {
@@ -315,16 +321,21 @@ public void commit(Set<String> localCaches, List<TransactionalOperation> operati
315321
throw e;
316322
}
317323

324+
BatchResult<?> result;
318325
try {
319-
320-
transactionExecutor.execute();
326+
result = transactionExecutor.execute();
321327
} catch (Exception e) {
322328
throw new TransactionException("Unable to execute transaction", e);
323329
}
324-
330+
325331
enableLocalCache(id, hashes);
326-
332+
327333
executed.set(true);
334+
335+
if (batchOptions.getSyncSlaves() > 0 && result.getSyncedSlaves() < batchOptions.getSyncSlaves()) {
336+
throw new TransactionException("Unable to sync " + batchOptions.getSyncSlaves()
337+
+ " slaves within " + options.getSyncTimeout() + " ms");
338+
}
328339
}
329340

330341
private void checkTimeout() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) 2013-2026 Nikita Koksharov
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.redisson.transaction;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.redisson.api.TransactionOptions;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class RedissonTransactionSyncSlavesTest {
26+
27+
@Test
28+
public void testTransactionOptionsSyncSlaves() {
29+
TransactionOptions options = TransactionOptions.defaults()
30+
.syncSlaves(3, 10, TimeUnit.SECONDS);
31+
32+
assertThat(options.getSyncSlaves()).isEqualTo(3);
33+
assertThat(options.getSyncTimeout()).isEqualTo(10000L);
34+
35+
TransactionOptions optionsCopy = TransactionOptions.defaults()
36+
.syncSlaves(3, 10, TimeUnit.SECONDS);
37+
assertThat(options).isEqualTo(optionsCopy);
38+
assertThat(options.hashCode()).isEqualTo(optionsCopy.hashCode());
39+
40+
TransactionOptions noSync = TransactionOptions.defaults()
41+
.syncSlaves(-1, 5, TimeUnit.SECONDS);
42+
assertThat(noSync.getSyncSlaves()).isEqualTo(-1);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)