Search in sources :

Example 11 with ServerList

use of com.netflix.exhibitor.core.state.ServerList in project exhibitor by soabase.

the class TestRollingConfigChange method testDownMiddleInstance.

@Test
public void testDownMiddleInstance() throws Exception {
    ServerList serverList = new ServerList("1:aaa,2:two,3:zzz");
    RemoteInstanceRequestClient mockClient = new RemoteInstanceRequestClient() {

        @Override
        public void close() throws IOException {
        }

        @Override
        public <T> T getWebResource(URI remoteUri, MediaType type, Class<T> clazz) throws Exception {
            if (remoteUri.getHost().equals("two")) {
                throw new Exception();
            }
            return clazz.cast("foo");
        }
    };
    ActivityLog log = new ActivityLog(100);
    ActivityQueue activityQueue = new ActivityQueue();
    Exhibitor mockExhibitor = Mockito.mock(Exhibitor.class);
    MonitorRunningInstance mockMonitorRunningInstance = makeMockMonitorRunningInstance();
    Mockito.when(mockExhibitor.getMonitorRunningInstance()).thenReturn(mockMonitorRunningInstance);
    Mockito.when(mockExhibitor.getLog()).thenReturn(log);
    Mockito.when(mockExhibitor.getActivityQueue()).thenReturn(activityQueue);
    Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn("one");
    Mockito.when(mockExhibitor.getRemoteInstanceRequestClient()).thenReturn(mockClient);
    final AtomicLong modified = new AtomicLong(1);
    ConfigProvider provider = new ConfigWrapper(modified);
    InstanceState state = new InstanceState(serverList, InstanceStateTypes.SERVING, new RestartSignificantConfig(null));
    ConfigManager manager = new ConfigManager(mockExhibitor, provider, 10);
    manager.start();
    try {
        Properties properties = new Properties();
        properties.setProperty(PropertyBasedInstanceConfig.toName(StringConfigs.SERVERS_SPEC, PropertyBasedInstanceConfig.ROOT_PROPERTY_PREFIX), serverList.toSpecString());
        PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, DefaultProperties.get(null));
        manager.startRollingConfig(config.getRootConfig(), null);
        for (String hostname : manager.getRollingConfigState().getRollingHostNames()) {
            if (hostname.equals("two")) {
                continue;
            }
            Assert.assertTrue(manager.isRolling());
            RollingReleaseState rollingState = new RollingReleaseState(state, manager.getCollection());
            Assert.assertEquals(rollingState.getCurrentRollingHostname(), hostname);
            Assert.assertNull(manager.getRollingConfigAdvanceAttempt());
            Mockito.when(mockExhibitor.getThisJVMHostname()).thenReturn(hostname);
            long lastModified = modified.get();
            manager.checkRollingConfig(state);
            if (// the next will be the down instance "two"
            hostname.equals("aaa")) {
                for (// don't check last time as it's cleared on MAX
                int i = 1; // don't check last time as it's cleared on MAX
                i < ConfigManager.DEFAULT_MAX_ATTEMPTS; // don't check last time as it's cleared on MAX
                ++i) {
                    Assert.assertNotNull(manager.getRollingConfigAdvanceAttempt());
                    Assert.assertEquals(manager.getRollingConfigAdvanceAttempt().getAttemptCount(), i);
                    manager.checkRollingConfig(state);
                }
            }
            Assert.assertTrue(modified.get() > lastModified);
        }
        Assert.assertFalse(manager.isRolling());
    } finally {
        CloseableUtils.closeQuietly(manager);
    }
}
Also used : ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Properties(java.util.Properties) URI(java.net.URI) IOException(java.io.IOException) MonitorRunningInstance(com.netflix.exhibitor.core.state.MonitorRunningInstance) AtomicLong(java.util.concurrent.atomic.AtomicLong) InstanceState(com.netflix.exhibitor.core.state.InstanceState) ActivityQueue(com.netflix.exhibitor.core.activity.ActivityQueue) Exhibitor(com.netflix.exhibitor.core.Exhibitor) ServerList(com.netflix.exhibitor.core.state.ServerList) MediaType(javax.ws.rs.core.MediaType) RemoteInstanceRequestClient(com.netflix.exhibitor.core.automanage.RemoteInstanceRequestClient) RestartSignificantConfig(com.netflix.exhibitor.core.state.RestartSignificantConfig) Test(org.testng.annotations.Test)

Example 12 with ServerList

use of com.netflix.exhibitor.core.state.ServerList in project exhibitor by soabase.

the class FixedEnsembleBuilder method createPotentialServerList.

@Override
public ServerList createPotentialServerList() {
    ServerList configuredServerList = clusterState.getConfiguredServerList();
    Map<ServerSpec, InstanceStateTypes> statusMap = clusterState.buildStatusMap();
    List<ServerSpec> newList = Lists.newArrayList();
    for (ServerSpec spec : configuredServerList.getSpecs()) {
        if (statusMap.get(spec) != InstanceStateTypes.DOWN) {
            newList.add(spec);
        }
    }
    if (newList.size() >= fixedEnsembleSize) {
        // no room for us
        return configuredServerList;
    }
    int standardTypeCount = 0;
    for (ServerSpec spec : newList) {
        if (spec.getServerType() == ServerType.STANDARD) {
            ++standardTypeCount;
        }
    }
    int observerThreshold = exhibitor.getConfigManager().getConfig().getInt(IntConfigs.OBSERVER_THRESHOLD);
    ServerType serverType = ((observerThreshold > 0) && (standardTypeCount >= observerThreshold)) ? ServerType.OBSERVER : ServerType.STANDARD;
    int existingMaxId = FlexibleEnsembleBuilder.getExistingMaxId(configuredServerList);
    ServerSpec us = new ServerSpec(exhibitor.getThisJVMHostname(), existingMaxId + 1, serverType);
    newList.add(us);
    return new ServerList(newList);
}
Also used : ServerType(com.netflix.exhibitor.core.state.ServerType) ServerSpec(com.netflix.exhibitor.core.state.ServerSpec) ServerList(com.netflix.exhibitor.core.state.ServerList) InstanceStateTypes(com.netflix.exhibitor.core.state.InstanceStateTypes)

Example 13 with ServerList

use of com.netflix.exhibitor.core.state.ServerList in project exhibitor by soabase.

the class ClusterState method buildStatusMap.

Map<ServerSpec, InstanceStateTypes> buildStatusMap() {
    ServerList serverList = configuredServerList.get();
    ImmutableMap.Builder<ServerSpec, InstanceStateTypes> builder = ImmutableMap.builder();
    List<ServerStatus> currentStatuses = statuses.get();
    for (ServerStatus status : currentStatuses) {
        ServerSpec spec = serverList.getSpec(status.getHostname());
        if (spec != null) {
            builder.put(spec, status.getInstanceStateType());
        } else {
            log.error("No configured spec found for hostname: " + status.getHostname());
        }
    }
    return builder.build();
}
Also used : ServerSpec(com.netflix.exhibitor.core.state.ServerSpec) ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) ServerList(com.netflix.exhibitor.core.state.ServerList) InstanceStateTypes(com.netflix.exhibitor.core.state.InstanceStateTypes) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with ServerList

use of com.netflix.exhibitor.core.state.ServerList in project exhibitor by soabase.

the class ConfigResource method getSystemState.

@Path("get-state")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSystemState(@Context Request request) throws Exception {
    InstanceConfig config = context.getExhibitor().getConfigManager().getConfig();
    String response = new FourLetterWord(FourLetterWord.Word.RUOK, config, context.getExhibitor().getConnectionTimeOutMs()).getResponse();
    ServerList serverList = new ServerList(config.getString(StringConfigs.SERVERS_SPEC));
    ServerSpec us = UsState.findUs(context.getExhibitor(), serverList.getSpecs());
    ObjectNode mainNode = JsonNodeFactory.instance.objectNode();
    ObjectNode configNode = JsonNodeFactory.instance.objectNode();
    ObjectNode controlPanelNode = JsonNodeFactory.instance.objectNode();
    mainNode.put("version", context.getExhibitor().getVersion());
    mainNode.put("running", "imok".equals(response));
    mainNode.put("backupActive", context.getExhibitor().getBackupManager().isActive());
    mainNode.put("standaloneMode", context.getExhibitor().getConfigManager().isStandaloneMode());
    mainNode.put("extraHeadingText", context.getExhibitor().getExtraHeadingText());
    mainNode.put("nodeMutationsAllowed", context.getExhibitor().nodeMutationsAllowed());
    configNode.put("rollInProgress", context.getExhibitor().getConfigManager().isRolling());
    configNode.put("rollStatus", context.getExhibitor().getConfigManager().getRollingConfigState().getRollingStatus());
    configNode.put("rollPercentDone", context.getExhibitor().getConfigManager().getRollingConfigState().getRollingPercentDone());
    configNode.put("hostname", context.getExhibitor().getThisJVMHostname());
    configNode.put("serverId", (us != null) ? us.getServerId() : -1);
    for (StringConfigs c : StringConfigs.values()) {
        configNode.put(fixName(c), config.getString(c));
    }
    for (IntConfigs c : IntConfigs.values()) {
        String fixedName = fixName(c);
        int value = config.getInt(c);
        configNode.put(fixedName, value);
    }
    EncodedConfigParser zooCfgParser = new EncodedConfigParser(config.getString(StringConfigs.ZOO_CFG_EXTRA));
    ObjectNode zooCfgNode = JsonNodeFactory.instance.objectNode();
    for (EncodedConfigParser.FieldValue fv : zooCfgParser.getFieldValues()) {
        zooCfgNode.put(fv.getField(), fv.getValue());
    }
    configNode.put("zooCfgExtra", zooCfgNode);
    if (context.getExhibitor().getBackupManager().isActive()) {
        ObjectNode backupExtraNode = JsonNodeFactory.instance.objectNode();
        EncodedConfigParser parser = context.getExhibitor().getBackupManager().getBackupConfigParser();
        List<BackupConfigSpec> configs = context.getExhibitor().getBackupManager().getConfigSpecs();
        for (BackupConfigSpec c : configs) {
            String value = parser.getValue(c.getKey());
            backupExtraNode.put(c.getKey(), (value != null) ? value : "");
        }
        configNode.put("backupExtra", backupExtraNode);
    }
    configNode.put("controlPanel", controlPanelNode);
    mainNode.put("config", configNode);
    String json = JsonUtil.writeValueAsString(mainNode);
    EntityTag tag = new EntityTag(Hashing.sha1().hashString(json, Charsets.UTF_8).toString());
    Response.ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder == null) {
        builder = Response.ok(json).tag(tag);
    }
    return builder.build();
}
Also used : ServerSpec(com.netflix.exhibitor.core.state.ServerSpec) ObjectNode(org.codehaus.jackson.node.ObjectNode) IntConfigs(com.netflix.exhibitor.core.config.IntConfigs) Response(javax.ws.rs.core.Response) InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) EncodedConfigParser(com.netflix.exhibitor.core.config.EncodedConfigParser) FourLetterWord(com.netflix.exhibitor.core.state.FourLetterWord) BackupConfigSpec(com.netflix.exhibitor.core.backup.BackupConfigSpec) ServerList(com.netflix.exhibitor.core.state.ServerList) EntityTag(javax.ws.rs.core.EntityTag) StringConfigs(com.netflix.exhibitor.core.config.StringConfigs) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with ServerList

use of com.netflix.exhibitor.core.state.ServerList 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

ServerList (com.netflix.exhibitor.core.state.ServerList)16 ServerSpec (com.netflix.exhibitor.core.state.ServerSpec)7 Exhibitor (com.netflix.exhibitor.core.Exhibitor)6 ActivityLog (com.netflix.exhibitor.core.activity.ActivityLog)6 ActivityQueue (com.netflix.exhibitor.core.activity.ActivityQueue)6 RemoteInstanceRequestClient (com.netflix.exhibitor.core.automanage.RemoteInstanceRequestClient)6 MonitorRunningInstance (com.netflix.exhibitor.core.state.MonitorRunningInstance)6 URI (java.net.URI)6 Properties (java.util.Properties)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 MediaType (javax.ws.rs.core.MediaType)6 Test (org.testng.annotations.Test)6 InstanceState (com.netflix.exhibitor.core.state.InstanceState)5 RestartSignificantConfig (com.netflix.exhibitor.core.state.RestartSignificantConfig)5 InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)4 ServerStatus (com.netflix.exhibitor.core.entities.ServerStatus)4 IOException (java.io.IOException)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4