use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class MockConfigProvider method setConfig.
void setConfig(final StringConfigs type, final String value) {
final InstanceConfig config = collection.getRootConfig();
final InstanceConfig newConfig = new InstanceConfig() {
@Override
public String getString(StringConfigs configType) {
if (configType == type) {
return value;
}
return config.getString(configType);
}
@Override
public int getInt(IntConfigs configType) {
return config.getInt(configType);
}
};
collection = new ConfigCollection() {
@Override
public InstanceConfig getConfigForThisInstance(String hostname) {
return getRootConfig();
}
@Override
public InstanceConfig getRootConfig() {
return newConfig;
}
@Override
public InstanceConfig getRollingConfig() {
return null;
}
@Override
public boolean isRolling() {
return false;
}
@Override
public RollingConfigState getRollingConfigState() {
return null;
}
};
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class Checker method calculateState.
public StateAndLeader calculateState() throws Exception {
InstanceConfig config = exhibitor.getConfigManager().getConfig();
if (!isSet(config, StringConfigs.ZOOKEEPER_DATA_DIRECTORY) || !isSet(config, StringConfigs.ZOOKEEPER_INSTALL_DIRECTORY)) {
return new StateAndLeader(InstanceStateTypes.LATENT, false);
}
InstanceStateTypes actualState = InstanceStateTypes.DOWN;
boolean isLeader = false;
String ruok = new FourLetterWord(FourLetterWord.Word.RUOK, hostname, config, exhibitor.getConnectionTimeOutMs()).getResponse();
if ("imok".equals(ruok)) {
// The following code depends on inside knowledge of the "srvr" response. If they change it
// this code might break
List<String> lines = new FourLetterWord(FourLetterWord.Word.SRVR, hostname, config, exhibitor.getConnectionTimeOutMs()).getResponseLines();
for (String line : lines) {
if (line.contains("not currently serving")) {
actualState = InstanceStateTypes.NOT_SERVING;
break;
}
if (line.toLowerCase().startsWith("mode")) {
actualState = InstanceStateTypes.SERVING;
String[] parts = line.split(":");
if (parts.length > 1) {
String mode = parts[1].trim();
if (mode.equalsIgnoreCase("leader") || mode.equalsIgnoreCase("standalone")) {
isLeader = true;
}
}
break;
}
}
}
return new StateAndLeader(actualState, isLeader);
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class TestMonitorRunningInstance method testServerListHasChanged.
@Test
public void testServerListHasChanged() throws Exception {
InstanceConfig config = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
return "S:1:foo,S:2:bar";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
return 0;
}
};
Exhibitor mockExhibitor = makeMockExhibitor(config, "foo");
MonitorRunningInstance monitor = new MonitorRunningInstance(mockExhibitor);
StateAndLeader stateAndLeader = monitor.getStateAndLeader();
InstanceState localCurrentInstanceState = new InstanceState(new ServerList(config.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(config));
InstanceConfig newConfig = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
// observer added
return "S:1:foo,S:2:bar,O:3:snafu";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
return 0;
}
};
InstanceState instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
Assert.assertFalse(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
newConfig = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
// standard added
return "S:1:foo,S:2:bar,S:3:snafu";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
return 0;
}
};
instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
newConfig = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
// "us" changed to observer
return "O:1:foo,S:2:bar";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
return 0;
}
};
instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
newConfig = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
// not-us changed to observer
return "S:1:foo,O:2:bar";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
return 0;
}
};
instanceState = new InstanceState(new ServerList(newConfig.getString(StringConfigs.SERVERS_SPEC)), stateAndLeader.getState(), new RestartSignificantConfig(newConfig));
Assert.assertTrue(monitor.serverListHasChanged(instanceState, localCurrentInstanceState));
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class TestMonitorRunningInstance method testTempDownInstance.
@Test
public void testTempDownInstance() throws Exception {
final AtomicInteger checkMs = new AtomicInteger(10000);
InstanceConfig config = new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case SERVERS_SPEC:
{
return "1:foo,2:bar";
}
case ZOOKEEPER_DATA_DIRECTORY:
case ZOOKEEPER_INSTALL_DIRECTORY:
{
return "/";
}
}
return null;
}
@Override
public int getInt(IntConfigs config) {
switch(config) {
case CHECK_MS:
{
return checkMs.get();
}
}
return 0;
}
};
Exhibitor mockExhibitor = makeMockExhibitor(config, "foo");
final Semaphore semaphore = new Semaphore(0);
MonitorRunningInstance monitor = new MonitorRunningInstance(mockExhibitor) {
@Override
protected void restartZooKeeper(InstanceState currentInstanceState) throws Exception {
semaphore.release();
}
};
monitor.doWork();
Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
monitor.doWork();
// no instance state change, should not try restart
Assert.assertFalse(semaphore.tryAcquire(3, TimeUnit.SECONDS));
checkMs.set(1);
// should do restart now as 10 times checkMs has elapsed
monitor.doWork();
Assert.assertTrue(semaphore.tryAcquire(10, TimeUnit.SECONDS));
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class ClusterResource method getClusterAsExhibitor.
@Path("list")
@GET
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
public String getClusterAsExhibitor() throws Exception {
InstanceConfig config = context.getExhibitor().getConfigManager().getConfig();
StringBuilder response = new StringBuilder();
ServerList serverList = new ServerList(config.getString(StringConfigs.SERVERS_SPEC));
response.append("count=").append(serverList.getSpecs().size());
int index = 0;
for (ServerSpec spec : serverList.getSpecs()) {
response.append("&server").append(index++).append("=").append(URLEncoder.encode(spec.getHostname(), "UTF-8"));
}
response.append("&port=").append(config.getInt(IntConfigs.CLIENT_PORT));
return response.toString();
}
Aggregations