This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Sync check added to broadcast stage (#1856)
* Added inSyncService for use in BroadcastStage * Changed to atomicboolean
- Loading branch information
Brord van Wierst
authored
May 12, 2020
1 parent
4f8e07d
commit 6828cc4
Showing
12 changed files
with
299 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/iota/iri/service/milestone/InSyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.iota.iri.service.milestone; | ||
|
||
/** | ||
* | ||
* Service for checking our node status | ||
* | ||
*/ | ||
public interface InSyncService { | ||
|
||
/** | ||
* Verifies if this node is currently considered in sync | ||
* | ||
* @return <code>true</code> if we are in sync, otherwise <code>false</code> | ||
*/ | ||
boolean isInSync(); | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/iota/iri/service/milestone/impl/MilestoneInSyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.iota.iri.service.milestone.impl; | ||
|
||
import com.iota.iri.service.milestone.InSyncService; | ||
import com.iota.iri.service.milestone.MilestoneSolidifier; | ||
import com.iota.iri.service.snapshot.SnapshotProvider; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* | ||
* 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. | ||
* | ||
*/ | ||
public class MilestoneInSyncService implements InSyncService { | ||
|
||
/** | ||
* 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 | ||
*/ | ||
@VisibleForTesting | ||
static final int LOCAL_SNAPSHOT_SYNC_BUFFER = 5; | ||
|
||
/** | ||
* If this node is currently seen as in sync | ||
*/ | ||
private AtomicBoolean isInSync; | ||
|
||
/** | ||
* Data provider for the latest solid index | ||
*/ | ||
private SnapshotProvider snapshotProvider; | ||
|
||
/** | ||
* Data provider for the latest index | ||
*/ | ||
private MilestoneSolidifier milestoneSolidifier; | ||
|
||
/** | ||
* @param snapshotProvider data provider for the snapshots that are relevant for the node | ||
*/ | ||
public MilestoneInSyncService(SnapshotProvider snapshotProvider, MilestoneSolidifier milestoneSolidifier) { | ||
this.snapshotProvider = snapshotProvider; | ||
this.milestoneSolidifier = milestoneSolidifier; | ||
this.isInSync = new AtomicBoolean(false); | ||
} | ||
|
||
@Override | ||
public boolean isInSync() { | ||
if (!milestoneSolidifier.isInitialScanComplete()) { | ||
return false; | ||
} | ||
|
||
int latestIndex = milestoneSolidifier.getLatestMilestoneIndex(); | ||
int latestSnapshot = snapshotProvider.getLatestSnapshot().getIndex(); | ||
|
||
// If we are out of sync, only a full sync will get us in | ||
if (!isInSync.get() && latestIndex == latestSnapshot) { | ||
isInSync.set(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.set(false); | ||
} | ||
|
||
return isInSync.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.