use of io.aeron.cluster.service.ClusterNodeControlProperties in project Aeron by real-logic.
the class ClusterTool method toggleClusterState.
@SuppressWarnings("MethodLength")
private static boolean toggleClusterState(final PrintStream out, final File clusterDir, final ConsensusModule.State expectedState, final ClusterControl.ToggleState toggleState, final boolean waitForToggleToComplete, final long defaultTimeoutMs) {
if (!markFileExists(clusterDir) && TIMEOUT_MS <= 0) {
out.println(ClusterMarkFile.FILENAME + " does not exist.");
return false;
}
final int clusterId;
final ClusterNodeControlProperties clusterNodeControlProperties;
try (ClusterMarkFile markFile = openMarkFile(clusterDir, out::println)) {
clusterId = markFile.clusterId();
clusterNodeControlProperties = markFile.loadControlProperties();
}
final ClusterMembership clusterMembership = new ClusterMembership();
final long queryTimeoutMs = Math.max(TimeUnit.SECONDS.toMillis(1), TIMEOUT_MS);
if (!queryClusterMembers(clusterNodeControlProperties, queryTimeoutMs, clusterMembership)) {
out.println("Timed out querying cluster.");
return false;
}
final String prefix = "Member [" + clusterMembership.memberId + "]: ";
if (clusterMembership.leaderMemberId != clusterMembership.memberId) {
out.println(prefix + "Current node is not the leader (leaderMemberId = " + clusterMembership.leaderMemberId + "), unable to " + toggleState);
return false;
}
final File cncFile = new File(clusterNodeControlProperties.aeronDirectoryName, CncFileDescriptor.CNC_FILE);
if (!cncFile.exists()) {
out.println(prefix + "Unable to locate media driver. C`n`C file [" + cncFile.getAbsolutePath() + "] does not exist.");
return false;
}
final CountersReader countersReader = ClusterControl.mapCounters(cncFile);
try {
final ConsensusModule.State moduleState = ConsensusModule.State.find(countersReader, clusterId);
if (null == moduleState) {
out.println(prefix + "Unable to resolve state of consensus module.");
return false;
}
if (expectedState != moduleState) {
out.println(prefix + "Unable to " + toggleState + " as the state of the consensus module is " + moduleState + ", but needs to be " + expectedState);
return false;
}
final AtomicCounter controlToggle = ClusterControl.findControlToggle(countersReader, clusterId);
if (null == controlToggle) {
out.println(prefix + "Failed to find control toggle");
return false;
}
if (!toggleState.toggle(controlToggle)) {
out.println(prefix + "Failed to apply " + toggleState + ", current toggle value = " + ClusterControl.ToggleState.get(controlToggle));
return false;
}
if (waitForToggleToComplete) {
final long toggleTimeoutMs = Math.max(defaultTimeoutMs, TIMEOUT_MS);
final long startTime = System.currentTimeMillis();
ClusterControl.ToggleState currentState = null;
do {
Thread.yield();
if ((System.currentTimeMillis() - startTime) > toggleTimeoutMs) {
break;
}
currentState = ClusterControl.ToggleState.get(controlToggle);
} while (currentState != ClusterControl.ToggleState.NEUTRAL);
if (currentState != ClusterControl.ToggleState.NEUTRAL) {
out.println(prefix + "Timed out after " + toggleTimeoutMs + "ms waiting for " + toggleState + " to complete.");
}
}
out.println(prefix + toggleState + " applied successfully");
return true;
} finally {
IoUtil.unmap(countersReader.valuesBuffer().byteBuffer());
}
}
Aggregations