Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
Fix: added a buffer to LSManager (#1286)
Browse files Browse the repository at this point in the history
* added a buffer to LSManager

* Added the out of sync buffer when we are in sync
  • Loading branch information
kwek20 authored and GalRogozinski committed Jan 30, 2019
1 parent 88558c1 commit bf40222
Showing 1 changed file with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class LocalSnapshotManagerImpl implements LocalSnapshotManager {
* due.
*/
private static final int LOCAL_SNAPSHOT_RESCAN_INTERVAL = 10000;

/**
* To prevent jumping back and forth in and out of sync, there is a buffer in between.
* Only when the latest milestone and latest snapshot differ more than this number, we fall out of sync
*/
private static final int LOCAL_SNAPSHOT_SYNC_BUFFER = 5;

/**
* Logger for this class allowing us to dump debug and status messages.
Expand All @@ -51,6 +57,11 @@ public class LocalSnapshotManagerImpl implements LocalSnapshotManager {
* Configuration with important snapshot related parameters.
*/
private SnapshotConfig config;

/**
* If this node is currently seen as in sync
*/
private boolean isInSync;

/**
* Holds a reference to the {@link ThreadIdentifier} for the monitor thread.
Expand Down Expand Up @@ -85,6 +96,8 @@ public LocalSnapshotManagerImpl init(SnapshotProvider snapshotProvider, Snapshot
this.snapshotService = snapshotService;
this.transactionPruner = transactionPruner;
this.config = config;

this.isInSync = false;

return this;
}
Expand Down Expand Up @@ -117,10 +130,7 @@ public void shutdown() {
*/
private void monitorThread(LatestMilestoneTracker latestMilestoneTracker) {
while (!Thread.currentThread().isInterrupted()) {
int localSnapshotInterval = latestMilestoneTracker.isInitialScanComplete() &&
snapshotProvider.getLatestSnapshot().getIndex() == latestMilestoneTracker.getLatestMilestoneIndex()
? config.getLocalSnapshotsIntervalSynced()
: config.getLocalSnapshotsIntervalUnsynced();
int localSnapshotInterval = getSnapshotInterval(isInSync(latestMilestoneTracker));

int latestSnapshotIndex = snapshotProvider.getLatestSnapshot().getIndex();
int initialSnapshotIndex = snapshotProvider.getInitialSnapshot().getIndex();
Expand All @@ -136,4 +146,46 @@ private void monitorThread(LatestMilestoneTracker latestMilestoneTracker) {
ThreadUtils.sleep(LOCAL_SNAPSHOT_RESCAN_INTERVAL);
}
}

/**
* A snapshot is taken in an interval.
* This interval changes based on the state of the node.
*
* @param inSync if this node is in sync
* @return the current interval in which we take local snapshots
*/
private int getSnapshotInterval(boolean inSync) {
return inSync
? config.getLocalSnapshotsIntervalSynced()
: config.getLocalSnapshotsIntervalUnsynced();
}

/**
* A node is defined in sync when the latest snapshot milestone index and the latest milestone index are equal.
* In order to prevent a bounce between in and out of sync, a buffer is added when a node became in sync.
*
* This will always return false if we are not done scanning milestone candidates during initialization.
*
* @param latestMilestoneTracker tracker we use to determine milestones
* @return <code>true</code> if we are in sync, otherwise <code>false</code>
*/
private boolean isInSync(LatestMilestoneTracker latestMilestoneTracker) {
if (!latestMilestoneTracker.isInitialScanComplete()) {
return false;
}

int latestIndex = latestMilestoneTracker.getLatestMilestoneIndex();
int latestSnapshot = snapshotProvider.getLatestSnapshot().getIndex();

// If we are out of sync, only a full sync will get us in
if (!isInSync && latestIndex == latestSnapshot) {
isInSync = true;

// When we are in sync, only dropping below the buffer gets us out of sync
} else if (latestSnapshot < latestIndex - LOCAL_SNAPSHOT_SYNC_BUFFER) {
isInSync = false;
}

return isInSync;
}
}

0 comments on commit bf40222

Please sign in to comment.