Skip to content

Commit

Permalink
Fixed counter base path getting deleted (apache#12193)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored Sep 26, 2021
1 parent a69611c commit 55e304b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public <T> LockManager<T> getLockManager(Class<T> clazz) {

@Override
public CompletableFuture<Long> getNextCounterValue(String path) {
return store.exists(path)
.thenCompose(exists -> {
if (exists) {
// The base path already exists
return incrementCounter(path);
} else {
return store.put(path, new byte[0], Optional.empty())
.thenCompose(__ -> incrementCounter(path));
}
});
}

private CompletableFuture<Long> incrementCounter(String path) {
String counterBasePath = path + "/-";
return store
.put(counterBasePath, new byte[0], Optional.of(-1L),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ public void basicTest(String provider, Supplier<String> urlSupplier) throws Exce
long l4 = cs1.getNextCounterValue("/my/path").join();
assertNotEquals(l3, l4);
}

@Test(dataProvider = "impl")
public void testCounterDoesNotAutoReset(String provider, Supplier<String> urlSupplier) throws Exception {
if (provider.equals("Memory")) {
// Test doesn't make sense for local memory since we're testing across different instances
return;
}

MetadataStoreExtended store1 = MetadataStoreExtended.create(urlSupplier.get(),
MetadataStoreConfig.builder().build());

CoordinationService cs1 = new CoordinationServiceImpl(store1);

long l1 = cs1.getNextCounterValue("/my/path").join();
long l2 = cs1.getNextCounterValue("/my/path").join();
long l3 = cs1.getNextCounterValue("/my/path").join();

assertNotEquals(l1, l2);
assertNotEquals(l2, l3);

cs1.close();
store1.close();;

// Delete all the empty container nodes
zks.checkContainers();

MetadataStoreExtended store2 = MetadataStoreExtended.create(urlSupplier.get(),
MetadataStoreConfig.builder().build());
@Cleanup
CoordinationService cs2 = new CoordinationServiceImpl(store2);

long l4 = cs2.getNextCounterValue("/my/path").join();
assertNotEquals(l1, l4);
assertNotEquals(l2, l4);
assertNotEquals(l3, l4);
}
}

0 comments on commit 55e304b

Please sign in to comment.