Skip to content

Commit 84a09e1

Browse files
Merge pull request elastic#7 from henningandersen/spacetime_transactions_prepare_more
Spacetime transactions prepare more
2 parents 6d9d61c + 70aa582 commit 84a09e1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

server/src/main/java/org/elasticsearch/index/shard/ShardTransactionRegistry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public synchronized Map<TxID, Boolean> prepare(TxID txID) {
5151
Set<String> conflictingKeys = conflictingKeysByTxID.get(txID);
5252
if (conflictingKeys != null) {
5353
return conflictingKeys.stream().flatMap(id -> byKey.get(id).stream())
54-
.filter(conflict -> conflict.equals(txID) == false).collect(Collectors.toMap(Function.identity(),
54+
.filter(conflict -> conflict.equals(txID) == false).distinct().collect(Collectors.toMap(Function.identity(),
5555
this::winConflict));
5656
} else {
5757
return Map.of();

server/src/test/java/org/elasticsearch/index/shard/ShardTransactionRegistryTests.java

+34
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import org.elasticsearch.action.bulk.TxID;
1212
import org.elasticsearch.test.ESTestCase;
1313

14+
import java.util.Map;
1415
import java.util.Set;
1516
import java.util.stream.Collectors;
1617
import java.util.stream.IntStream;
1718

19+
import static org.hamcrest.Matchers.empty;
1820
import static org.hamcrest.Matchers.equalTo;
21+
import static org.hamcrest.Matchers.is;
1922

2023
public class ShardTransactionRegistryTests extends ESTestCase {
2124
private final ShardTransactionRegistry registry = new ShardTransactionRegistry();
@@ -42,6 +45,37 @@ public void testRegisterAndUnregister() {
4245
assertThat(registry.size(), equalTo(0));
4346
}
4447

48+
public void testPrepare() {
49+
Set<String> ids1 = ids(0, 100);
50+
Set<String> ids2 = ids(50, 150);
51+
Set<String> ids3 = ids(100, 200);
52+
Set<String> ids4 = ids(200, 300);
53+
54+
TxID txID1 = TxID.create();
55+
TxID txID2 = TxID.create();
56+
TxID txID3 = TxID.create();
57+
TxID txID4 = TxID.create();
58+
registry.registerTransaction(txID1, ids1);
59+
registry.registerTransaction(txID2, ids2);
60+
registry.registerTransaction(txID3, ids3);
61+
registry.registerTransaction(txID4, ids4);
62+
63+
Map<TxID, Boolean> prepared1 = registry.prepare(txID1);
64+
Map<TxID, Boolean> prepared2 = registry.prepare(txID2);
65+
Map<TxID, Boolean> prepared3 = registry.prepare(txID3);
66+
Map<TxID, Boolean> prepared4 = registry.prepare(txID4);
67+
68+
assertThat(prepared1.entrySet().size(), equalTo(1));
69+
assertThat(prepared1.get(txID2), is(true));
70+
assertThat(prepared2.entrySet().size(), equalTo(2));
71+
assertThat(prepared2.get(txID1), is(false));
72+
assertThat(prepared2.get(txID3), is(true));
73+
assertThat(prepared3.entrySet().size(), equalTo(1));
74+
assertThat(prepared3.get(txID2), is(false));
75+
assertThat(prepared4.entrySet(), empty());
76+
// need some more around already conflicted but now removed txs
77+
}
78+
4579
private Set<String> ids(int startInclusive, int endExclusive) {
4680
return IntStream.range(startInclusive, endExclusive).mapToObj(String::valueOf).collect(Collectors.toSet());
4781
}

0 commit comments

Comments
 (0)