/** * */ public InstanceSpec getInstanceSpec() { return configBuilder.getInstanceSpec(thisInstanceIndex); }
/** * */ public Collection<InstanceSpec> getInstanceSpecs() { return configBuilder.getInstanceSpecs(); }
public void run() { try { QuorumPeerConfig config = configBuilder.buildConfig(thisInstanceIndex); main.runFromConfig(config); } catch (Exception e) { logger.error(String.format("From testing server (random state: %s) for instance: %s", String.valueOf(configBuilder.isFromRandom()), getInstanceSpec()), e); } } }).start();
/** * @param configBuilder Config builder. * @param thisInstanceIndex This instance index. */ public TestingZooKeeperServer(QuorumConfigBuilder configBuilder, int thisInstanceIndex) { this.configBuilder = configBuilder; this.thisInstanceIndex = thisInstanceIndex; main = (configBuilder.size() > 1) ? new FixedTestingQuorumPeerMain() : new TestingZooKeeperMain(); }
/** * Creates an ensemble using the given server specs * * @param specs map of an instance spec to its set of quorum instances. Allows simulation of an ensemble with * instances having different config peers */ public TestingCluster(Map<InstanceSpec, Collection<InstanceSpec>> specs) { ImmutableList.Builder<TestingZooKeeperServer> serverBuilder = ImmutableList.builder(); for (Map.Entry<InstanceSpec, Collection<InstanceSpec>> entry : specs.entrySet()) { List<InstanceSpec> instanceSpecs = Lists.newArrayList(entry.getValue()); int index = instanceSpecs.indexOf(entry.getKey()); Preconditions.checkState(index >= 0, entry.getKey() + " not found in specs"); QuorumConfigBuilder builder = new QuorumConfigBuilder(instanceSpecs); serverBuilder.add(new TestingZooKeeperServer(builder, index)); } servers = serverBuilder.build(); }
/** * Restart the server. If the server is running it will be stopped and then * started again. If it is not running (in a LATENT or STOPPED state) then * it will be restarted. If it is in a CLOSED state then an exception will * be thrown. * * @throws Exception */ public void restart() throws Exception { // Can't restart from a closed state as all the temporary data is gone if (state.get() == State.CLOSED) throw new IllegalStateException("Cannot restart a closed instance"); // If the server's currently running then stop it. if (state.get() == State.STARTED) stop(); // Set to a LATENT state so we can restart state.set(State.LATENT); main = (configBuilder.size() > 1) ? new FixedTestingQuorumPeerMain() : new TestingZooKeeperMain(); start(); }