Skip to content

Commit

Permalink
GH-2998 back off when many concurrent writes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed May 9, 2024
1 parent 1896407 commit 610eed1
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
class SailSourceBranch implements SailSource {

private static final long BACKOFF_START = Runtime.getRuntime().maxMemory() / 1024 / 1024 / 1024 * 10;

private static final Logger logger = LoggerFactory.getLogger(SailSourceBranch.class);

/**
Expand Down Expand Up @@ -404,6 +406,31 @@ private boolean isChanged(Changeset change) {
private SailDataset derivedFromSerializable(IsolationLevel level) throws SailException {
try {
semaphore.lock();

if (changes.size() > BACKOFF_START) {
int size = changes.size();
Changeset peekLast = changes.peekLast();
compressChanges();
int backoff = Math.min(5, changes.size());
while (peekLast == changes.peekLast() && backoff-- > 0) {
peekLast = changes.peekLast();
try {
semaphore.unlock();
if (semaphore.isHeldByCurrentThread()) {
System.out.println();
}
System.out.println("Backing off: " + changes.size());
Thread.sleep(10);
compressChanges();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SailException(e);
} finally {
semaphore.lock();
}
}
}

if (serializable == null && level.isCompatibleWith(IsolationLevels.SERIALIZABLE)) {
serializable = backingSource.sink(level);
}
Expand Down

0 comments on commit 610eed1

Please sign in to comment.