Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}