Search in sources :

Example 1 with InstanceConfig

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));
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) Exhibitor(com.netflix.exhibitor.core.Exhibitor) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) Test(org.testng.annotations.Test)

Example 2 with InstanceConfig

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));
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Exhibitor(com.netflix.exhibitor.core.Exhibitor) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) Semaphore(java.util.concurrent.Semaphore) Test(org.testng.annotations.Test)

Example 3 with InstanceConfig

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);
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) FourLetterWord(com.netflix.exhibitor.core.state.FourLetterWord) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with InstanceConfig

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);
}
Also used : ServerSpec(com.netflix.exhibitor.core.state.ServerSpec) InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) ObjectNode(org.codehaus.jackson.node.ObjectNode) ServerList(com.netflix.exhibitor.core.state.ServerList) ArrayNode(org.codehaus.jackson.node.ArrayNode) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with InstanceConfig

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();
        }
    };
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) EncodedConfigParser(com.netflix.exhibitor.core.config.EncodedConfigParser) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) JsonNode(org.codehaus.jackson.JsonNode) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)15 IntConfigs (com.netflix.exhibitor.core.config.IntConfigs)7 StringConfigs (com.netflix.exhibitor.core.config.StringConfigs)7 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 GET (javax.ws.rs.GET)5 ServerList (com.netflix.exhibitor.core.state.ServerList)4 ServerSpec (com.netflix.exhibitor.core.state.ServerSpec)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Exhibitor (com.netflix.exhibitor.core.Exhibitor)2 ConfigCollection (com.netflix.exhibitor.core.config.ConfigCollection)2 EncodedConfigParser (com.netflix.exhibitor.core.config.EncodedConfigParser)2 LoadedInstanceConfig (com.netflix.exhibitor.core.config.LoadedInstanceConfig)2 PropertyBasedInstanceConfig (com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig)2 PseudoLock (com.netflix.exhibitor.core.config.PseudoLock)2 RollingConfigState (com.netflix.exhibitor.core.config.RollingConfigState)2 Result (com.netflix.exhibitor.core.entities.Result)2 FourLetterWord (com.netflix.exhibitor.core.state.FourLetterWord)2 IOException (java.io.IOException)2 POST (javax.ws.rs.POST)2