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 getFourLetterWord.
@Path("4ltr/{word}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getFourLetterWord(@PathParam("word") String word) throws Exception {
InstanceConfig config = context.getExhibitor().getConfigManager().getConfig();
String value;
try {
FourLetterWord.Word wordEnum = FourLetterWord.Word.valueOf(word.toUpperCase());
value = new FourLetterWord(wordEnum, config, context.getExhibitor().getConnectionTimeOutMs()).getResponse();
} catch (IllegalArgumentException e) {
value = "* unknown *";
}
return JsonUtil.writeValueAsString(value);
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class ClusterResource method getClusterAsJson.
@Path("list")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getClusterAsJson() throws Exception {
InstanceConfig config = context.getExhibitor().getConfigManager().getConfig();
ObjectNode node = JsonNodeFactory.instance.objectNode();
ArrayNode serversNode = JsonNodeFactory.instance.arrayNode();
ServerList serverList = new ServerList(config.getString(StringConfigs.SERVERS_SPEC));
for (ServerSpec spec : serverList.getSpecs()) {
serversNode.add(spec.getHostname());
}
node.put("servers", serversNode);
node.put("port", config.getInt(IntConfigs.CLIENT_PORT));
return JsonUtil.writeValueAsString(node);
}
use of com.netflix.exhibitor.core.config.InstanceConfig in project exhibitor by soabase.
the class ConfigResource method parseToConfig.
private InstanceConfig parseToConfig(String newConfigJson) throws IOException {
ObjectMapper mapper = new ObjectMapper();
final JsonNode tree = mapper.readTree(mapper.getJsonFactory().createJsonParser(newConfigJson));
String backupExtraValue = "";
if (tree.get("backupExtra") != null) {
List<EncodedConfigParser.FieldValue> values = Lists.newArrayList();
JsonNode backupExtra = tree.get("backupExtra");
Iterator<String> fieldNames = backupExtra.getFieldNames();
while (fieldNames.hasNext()) {
String name = fieldNames.next();
String value = backupExtra.get(name).getTextValue();
values.add(new EncodedConfigParser.FieldValue(name, value));
}
backupExtraValue = new EncodedConfigParser(values).toEncoded();
}
List<EncodedConfigParser.FieldValue> zooCfgValues = Lists.newArrayList();
JsonNode zooCfgExtra = tree.get("zooCfgExtra");
Iterator<String> fieldNames = zooCfgExtra.getFieldNames();
while (fieldNames.hasNext()) {
String name = fieldNames.next();
String value = zooCfgExtra.get(name).getTextValue();
zooCfgValues.add(new EncodedConfigParser.FieldValue(name, value));
}
final String zooCfgExtraValue = new EncodedConfigParser(zooCfgValues).toEncoded();
final String finalBackupExtraValue = backupExtraValue;
return new InstanceConfig() {
@Override
public String getString(StringConfigs config) {
switch(config) {
case BACKUP_EXTRA:
{
return finalBackupExtraValue;
}
case ZOO_CFG_EXTRA:
{
return zooCfgExtraValue;
}
default:
{
// NOP
break;
}
}
JsonNode node = tree.get(fixName(config));
if (node == null) {
return "";
}
return node.asText();
}
@Override
public int getInt(IntConfigs config) {
JsonNode node = tree.get(fixName(config));
if (node == null) {
return 0;
}
return node.asInt();
}
};
}
Aggregations