Search in sources :

Example 6 with InstanceConfig

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;
        }
    };
}
Also used : LoadedInstanceConfig(com.netflix.exhibitor.core.config.LoadedInstanceConfig) InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) PropertyBasedInstanceConfig(com.netflix.exhibitor.core.config.PropertyBasedInstanceConfig) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) ConfigCollection(com.netflix.exhibitor.core.config.ConfigCollection) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) RollingConfigState(com.netflix.exhibitor.core.config.RollingConfigState)

Example 7 with InstanceConfig

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);
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig)

Example 8 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 9 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 10 with InstanceConfig

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

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