use of org.projectnessie.versioned.ReferenceRetryFailureException in project nessie by projectnessie.
the class AbstractConcurrency method concurrency.
@ParameterizedTest
@MethodSource("concurrencyVariations")
void concurrency(Variation variation) throws Exception {
new ArrayList<>(globalRegistry.getRegistries()).forEach(globalRegistry::remove);
globalRegistry.add(new SimpleMeterRegistry());
ExecutorService executor = Executors.newFixedThreadPool(variation.threads);
AtomicInteger commitsOK = new AtomicInteger();
AtomicInteger retryFailures = new AtomicInteger();
AtomicBoolean stopFlag = new AtomicBoolean();
List<Runnable> tasks = new ArrayList<>(variation.threads);
Map<Key, ContentId> keyToContentId = new HashMap<>();
Map<ContentId, ByteString> globalStates = new ConcurrentHashMap<>();
Map<BranchName, Map<Key, ByteString>> onRefStates = new ConcurrentHashMap<>();
try {
CountDownLatch startLatch = new CountDownLatch(1);
Map<BranchName, Set<Key>> keysPerBranch = new HashMap<>();
for (int i = 0; i < variation.threads; i++) {
BranchName branch = BranchName.of("concurrency-" + ((variation.singleBranch ? "shared" : i)));
List<Key> keys = new ArrayList<>(variation.tables);
for (int k = 0; k < variation.tables; k++) {
String variationKey = variation.sharedKeys ? "shared" : Integer.toString(i);
Key key = Key.of("some", "key", variationKey, "table-" + k);
keys.add(key);
keyToContentId.put(key, ContentId.of(String.format("%s-table-%d", variationKey, k)));
keysPerBranch.computeIfAbsent(branch, x -> new HashSet<>()).add(key);
}
tasks.add(() -> {
try {
assertThat(startLatch.await(2, TimeUnit.SECONDS)).isTrue();
for (int commit = 0; ; commit++) {
if (stopFlag.get()) {
break;
}
List<ByteString> currentStates = databaseAdapter.values(databaseAdapter.hashOnReference(branch, Optional.empty()), keys, KeyFilterPredicate.ALLOW_ALL).values().stream().map(ContentAndState::getGlobalState).collect(Collectors.toList());
ImmutableCommitAttempt.Builder commitAttempt = ImmutableCommitAttempt.builder();
for (int ki = 0; ki < keys.size(); ki++) {
Key key = keys.get(ki);
ContentId contentId = keyToContentId.get(key);
String newGlobal = Integer.toString(Integer.parseInt(currentStates.get(ki).toStringUtf8()) + 1);
WithGlobalStateContent c = WithGlobalStateContent.withGlobal(newGlobal, "", contentId.getId());
commitAttempt.putGlobal(contentId, SimpleStoreWorker.INSTANCE.toStoreGlobalState(c));
if (!variation.sharedKeys) {
commitAttempt.putExpectedStates(contentId, Optional.of(currentStates.get(ki)));
}
commitAttempt.addPuts(KeyWithBytes.of(keys.get(ki), contentId, SimpleStoreWorker.INSTANCE.getPayload(c), SimpleStoreWorker.INSTANCE.toStoreOnReferenceState(c)));
}
try {
commitAttempt.commitToBranch(branch).commitMetaSerialized(ByteString.copyFromUtf8("commit #" + commit + " to " + branch.getName() + " something " + ThreadLocalRandom.current().nextLong()));
commitAndRecord(globalStates, onRefStates, branch, commitAttempt);
commitsOK.incrementAndGet();
} catch (ReferenceRetryFailureException retry) {
retryFailures.incrementAndGet();
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
});
}
for (Entry<BranchName, Set<Key>> branchKeys : keysPerBranch.entrySet()) {
BranchName branch = branchKeys.getKey();
databaseAdapter.create(branch, databaseAdapter.hashOnReference(BranchName.of("main"), Optional.empty()));
ImmutableCommitAttempt.Builder commitAttempt = ImmutableCommitAttempt.builder().commitToBranch(branchKeys.getKey()).commitMetaSerialized(ByteString.copyFromUtf8("initial commit for " + branch.getName()));
for (Key k : branchKeys.getValue()) {
ContentId contentId = keyToContentId.get(k);
WithGlobalStateContent c = WithGlobalStateContent.withGlobal("0", "", contentId.getId());
commitAttempt.addPuts(KeyWithBytes.of(k, contentId, SimpleStoreWorker.INSTANCE.getPayload(c), SimpleStoreWorker.INSTANCE.toStoreOnReferenceState(c)));
commitAttempt.putGlobal(contentId, SimpleStoreWorker.INSTANCE.toStoreGlobalState(c));
}
commitAndRecord(globalStates, onRefStates, branch, commitAttempt);
}
// Submit all 'Runnable's as 'CompletableFuture's and construct a combined 'CompletableFuture'
// that we can wait for.
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(tasks.stream().map(r -> CompletableFuture.runAsync(r, executor)).toArray((IntFunction<CompletableFuture<?>[]>) CompletableFuture[]::new));
startLatch.countDown();
Thread.sleep(2_000);
stopFlag.set(true);
// 30 seconds is long, but necessary to let transactional databases detect deadlocks, which
// cause Nessie-commit-retries.
combinedFuture.get(30, TimeUnit.SECONDS);
for (Entry<BranchName, Set<Key>> branchKeys : keysPerBranch.entrySet()) {
BranchName branch = branchKeys.getKey();
Hash hash = databaseAdapter.hashOnReference(branch, Optional.empty());
ArrayList<Key> keys = new ArrayList<>(branchKeys.getValue());
// Note: only fetch the values, cannot assert those here.
databaseAdapter.values(hash, keys, KeyFilterPredicate.ALLOW_ALL);
}
} finally {
stopFlag.set(true);
executor.shutdownNow();
// 30 seconds is long, but necessary to let transactional databases detect deadlocks, which
// cause Nessie-commit-retries.
assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
System.out.printf("AbstractConcurrency.concurrency - %s : Commits OK: %s Retry-Failures: %s%n", variation, commitsOK, retryFailures);
System.out.printf("AbstractConcurrency.concurrency - %s : try-loop success: count: %6d retries: %6d total-time-millis: %d%n", variation, (long) DatabaseAdapterMetrics.tryLoopCounts("success").count(), (long) DatabaseAdapterMetrics.tryLoopRetries("success").count(), (long) DatabaseAdapterMetrics.tryLoopDuration("success").totalTime(TimeUnit.MILLISECONDS));
System.out.printf("AbstractConcurrency.concurrency - %s : try-loop failure: count: %6d retries: %6d total-time-millis: %d%n", variation, (long) DatabaseAdapterMetrics.tryLoopCounts("fail").count(), (long) DatabaseAdapterMetrics.tryLoopRetries("fail").count(), (long) DatabaseAdapterMetrics.tryLoopDuration("fail").totalTime(TimeUnit.MILLISECONDS));
new ArrayList<>(globalRegistry.getRegistries()).forEach(globalRegistry::remove);
}
}
use of org.projectnessie.versioned.ReferenceRetryFailureException in project nessie by projectnessie.
the class CommitBench method doCommit.
private void doCommit(BenchmarkParam bp, BranchName branch, List<Key> keys, Map<Key, String> contentIds) throws Exception {
Map<Key, BaseContent> contentByKey = bp.versionStore.getValues(branch, keys);
try {
List<Operation<BaseContent>> operations = new ArrayList<>(bp.tablesPerCommit);
for (int i = 0; i < bp.tablesPerCommit; i++) {
Key key = keys.get(i);
BaseContent value = contentByKey.get(key);
if (value == null) {
throw new RuntimeException("no value for key " + key + " in " + branch);
}
String currentState = ((WithGlobalStateContent) value).getGlobal();
String newGlobalState = Integer.toString(Integer.parseInt(currentState) + 1);
String contentId = contentIds.get(key);
operations.add(Put.of(key, // hashes, because parent, "content", key are all the same.
withGlobal(newGlobalState, "commit value " + ThreadLocalRandom.current().nextLong(), contentId), withGlobal(currentState, "foo", contentId)));
}
bp.versionStore.commit(branch, Optional.empty(), commitMessage("commit meta data"), operations);
bp.success.incrementAndGet();
} catch (ReferenceRetryFailureException e) {
bp.retryFailures.incrementAndGet();
} catch (ReferenceConflictException e) {
bp.conflictsFailures.incrementAndGet();
}
}
Aggregations