* @param clusterFormationTimeout
* @param reachable
*/
public static void start(SlaveState slaveState, boolean validate, Integer expectedSlaves, long clusterFormationTimeout,
Set<Integer> reachable) {
Lifecycle lifecycle = slaveState.getTrait(Lifecycle.class);
Clustered clustered = slaveState.getTrait(Clustered.class);
Partitionable partitionable = slaveState.getTrait(Partitionable.class);
try {
if (partitionable != null) {
partitionable.setStartWithReachable(slaveState.getSlaveIndex(), reachable);
}
for (ServiceListener listener : slaveState.getServiceListeners()) {
listener.beforeServiceStart();
}
long startingTime = System.currentTimeMillis();
lifecycle.start();
long startedTime = System.currentTimeMillis();
slaveState.getTimeline().addEvent(LifecycleHelper.LIFECYCLE, new Timeline.IntervalEvent(startingTime, "Start", startedTime - startingTime));
if (validate && clustered != null) {
int expectedNumberOfSlaves = expectedSlaves != null ? expectedSlaves : slaveState.getGroupSize();
long clusterFormationDeadline = System.currentTimeMillis() + clusterFormationTimeout;
for (;;) {
int numMembers = clustered.getClusteredNodes();
if (numMembers != expectedNumberOfSlaves) {
String msg = "Number of members=" + numMembers + " is not the one expected: " + expectedNumberOfSlaves;
log.info(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
if (System.currentTimeMillis() > clusterFormationDeadline) {
throw new ClusterFormationTimeoutException(msg);
}
} else {
log.info("Number of members is the one expected: " + clustered.getClusteredNodes());
break;
}
}
}
for (ServiceListener listener : slaveState.getServiceListeners()) {
try {
listener.afterServiceStart();
} catch (Exception e) {
log.error("Failed to run listener " + listener, e);
}
}
} catch (RuntimeException e) {
log.trace("Failed to start", e);
try {
lifecycle.stop();
} catch (Exception ignored) {
log.trace("Failed to stop after start failed", ignored);
}
throw e;
}