Search in sources :

Example 1 with ServerStatus

use of com.netflix.exhibitor.core.entities.ServerStatus in project exhibitor by soabase.

the class AutomaticInstanceManagement method call.

@Override
public Boolean call() throws Exception {
    if (exhibitor.getConfigManager().getConfig().getInt(IntConfigs.AUTO_MANAGE_INSTANCES) == 0) {
        // auto manage is turned off
        return true;
    }
    if (exhibitor.getConfigManager().isRolling()) {
        return true;
    }
    if (exhibitor.getMonitorRunningInstance().getCurrentInstanceState() == InstanceStateTypes.LATENT) {
        // this instance hasn't warmed up yet
        return true;
    }
    ServerList serverList = new ServerList(exhibitor.getConfigManager().getConfig().getString(StringConfigs.SERVERS_SPEC));
    List<ServerStatus> statuses = getStatuses(serverList);
    clusterState.update(serverList, statuses);
    EnsembleBuilder ensembleBuilder = (exhibitor.getConfigManager().getConfig().getInt(IntConfigs.AUTO_MANAGE_INSTANCES_FIXED_ENSEMBLE_SIZE) > 0) ? new FixedEnsembleBuilder(exhibitor, clusterState) : new FlexibleEnsembleBuilder(exhibitor, clusterState);
    if (!ensembleBuilder.newEnsembleNeeded()) {
        return true;
    }
    if (!applyAllAtOnce() && !clusterState.isInQuorum()) {
        exhibitor.getLog().add(ActivityLog.Type.INFO, "Ensemble is not currently in quorum. Automatic Instance Management will wait for quorum. NOTE: if \"Apply All At Once\" is set to \"yes\", this quorum check is not needed.");
        return true;
    }
    int settlingPeriodMs = exhibitor.getConfigManager().getConfig().getInt(IntConfigs.AUTO_MANAGE_INSTANCES_SETTLING_PERIOD_MS);
    if (!clusterState.isStable(settlingPeriodMs)) {
        exhibitor.getLog().add(ActivityLog.Type.INFO, "Ensemble state is not yet stable. Automatic Instance Management will wait for stability.");
        return true;
    }
    PseudoLock lock = exhibitor.getConfigManager().newConfigBasedLock();
    try {
        if (lock.lock(exhibitor.getLog(), Exhibitor.AUTO_INSTANCE_MANAGEMENT_PERIOD_MS / 2, TimeUnit.MILLISECONDS)) {
            ServerList potentialServerList = ensembleBuilder.createPotentialServerList();
            if (// otherwise, no change
            !potentialServerList.equals(serverList)) {
                if (potentialServerList.getSpecs().size() == 0) {
                    exhibitor.getLog().add(ActivityLog.Type.INFO, "Automatic Instance Management skipped because new potential server list is empty");
                } else {
                    exhibitor.getLog().add(ActivityLog.Type.INFO, "Automatic Instance Management will change the server list: " + serverList + " ==> " + potentialServerList);
                    adjustConfig(potentialServerList.toSpecString(), clusterState.getLeaderHostname());
                }
            }
        }
    } finally {
        lock.unlock();
    }
    return true;
}
Also used : ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) ServerList(com.netflix.exhibitor.core.state.ServerList) PseudoLock(com.netflix.exhibitor.core.config.PseudoLock)

Example 2 with ServerStatus

use of com.netflix.exhibitor.core.entities.ServerStatus in project exhibitor by soabase.

the class ClusterState method getLiveInstances.

List<ServerStatus> getLiveInstances() {
    List<ServerStatus> live = Lists.newArrayList();
    List<ServerStatus> currentStatuses = statuses.get();
    for (ServerStatus status : currentStatuses) {
        InstanceStateTypes type = InstanceStateTypes.fromCode(status.getCode());
        if (type != InstanceStateTypes.DOWN) {
            live.add(status);
        }
    }
    return live;
}
Also used : ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) InstanceStateTypes(com.netflix.exhibitor.core.state.InstanceStateTypes)

Example 3 with ServerStatus

use of com.netflix.exhibitor.core.entities.ServerStatus in project exhibitor by soabase.

the class FlexibleEnsembleBuilder method createPotentialServerList.

public ServerList createPotentialServerList() {
    ServerList configuredServerList = clusterState.getConfiguredServerList();
    int existingMaxId = getExistingMaxId(configuredServerList);
    List<ServerSpec> newList = Lists.newArrayList();
    Set<String> addedHostnames = Sets.newHashSet();
    for (ServerStatus status : clusterState.getLiveInstances()) {
        ServerSpec spec = configuredServerList.getSpec(status.getHostname());
        if (spec == null) {
            spec = new ServerSpec(status.getHostname(), ++existingMaxId, ServerType.STANDARD);
            addedHostnames.add(spec.getHostname());
        }
        newList.add(spec);
    }
    if (usState.getUs() == null) {
        ServerSpec spec = new ServerSpec(exhibitor.getThisJVMHostname(), ++existingMaxId, ServerType.STANDARD);
        addedHostnames.add(spec.getHostname());
        newList.add(spec);
    }
    int standardTypeCount = 0;
    for (ServerSpec spec : newList) {
        if (spec.getServerType() == ServerType.STANDARD) {
            ++standardTypeCount;
        }
    }
    int observerThreshold = exhibitor.getConfigManager().getConfig().getInt(IntConfigs.OBSERVER_THRESHOLD);
    if (observerThreshold > 0) {
        for (int i = 0; (standardTypeCount >= observerThreshold) && (i < newList.size()); ++i) {
            ServerSpec spec = newList.get(i);
            if (// i.e. don't change existing instances to observer
            addedHostnames.contains(spec.getHostname())) {
                newList.set(i, new ServerSpec(spec.getHostname(), spec.getServerId(), ServerType.OBSERVER));
                --standardTypeCount;
            }
        }
    }
    return new ServerList(newList);
}
Also used : ServerSpec(com.netflix.exhibitor.core.state.ServerSpec) ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) ServerList(com.netflix.exhibitor.core.state.ServerList)

Example 4 with ServerStatus

use of com.netflix.exhibitor.core.entities.ServerStatus in project exhibitor by soabase.

the class TestFixedAutoInstanceManagement method testBecomesObserver.

@Test
public void testBecomesObserver() throws Exception {
    MockExhibitorInstance mockExhibitorInstance = new MockExhibitorInstance("new");
    mockExhibitorInstance.getMockConfigProvider().setConfig(StringConfigs.SERVERS_SPEC, "1:a,2:b,3:c");
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES, 1);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_SETTLING_PERIOD_MS, 0);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_FIXED_ENSEMBLE_SIZE, 4);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.OBSERVER_THRESHOLD, 3);
    List<ServerStatus> statuses = Lists.newArrayList();
    statuses.add(new ServerStatus("a", InstanceStateTypes.SERVING.getCode(), "", true));
    statuses.add(new ServerStatus("b", InstanceStateTypes.SERVING.getCode(), "", false));
    statuses.add(new ServerStatus("c", InstanceStateTypes.SERVING.getCode(), "", false));
    Mockito.when(mockExhibitorInstance.getMockForkJoinPool().invoke(Mockito.isA(ClusterStatusTask.class))).thenReturn(statuses);
    AutomaticInstanceManagement management = new AutomaticInstanceManagement(mockExhibitorInstance.getMockExhibitor());
    management.call();
    Assert.assertEquals(mockExhibitorInstance.getMockExhibitor().getConfigManager().getConfig().getString(StringConfigs.SERVERS_SPEC), "1:a,2:b,3:c,O:4:new");
}
Also used : ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) Test(org.testng.annotations.Test)

Example 5 with ServerStatus

use of com.netflix.exhibitor.core.entities.ServerStatus in project exhibitor by soabase.

the class TestFlexibleAutoInstanceManagement method testBecomesObserver.

@Test
public void testBecomesObserver() throws Exception {
    MockExhibitorInstance mockExhibitorInstance = new MockExhibitorInstance("new");
    mockExhibitorInstance.getMockConfigProvider().setConfig(StringConfigs.SERVERS_SPEC, "1:a,2:b,3:c");
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES, 1);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_SETTLING_PERIOD_MS, 0);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.AUTO_MANAGE_INSTANCES_FIXED_ENSEMBLE_SIZE, 0);
    mockExhibitorInstance.getMockConfigProvider().setConfig(IntConfigs.OBSERVER_THRESHOLD, 3);
    List<ServerStatus> statuses = Lists.newArrayList();
    statuses.add(new ServerStatus("a", InstanceStateTypes.SERVING.getCode(), "", true));
    statuses.add(new ServerStatus("b", InstanceStateTypes.SERVING.getCode(), "", false));
    statuses.add(new ServerStatus("c", InstanceStateTypes.SERVING.getCode(), "", false));
    Mockito.when(mockExhibitorInstance.getMockForkJoinPool().invoke(Mockito.isA(ClusterStatusTask.class))).thenReturn(statuses);
    AutomaticInstanceManagement management = new AutomaticInstanceManagement(mockExhibitorInstance.getMockExhibitor());
    management.call();
    Assert.assertEquals(mockExhibitorInstance.getMockExhibitor().getConfigManager().getConfig().getString(StringConfigs.SERVERS_SPEC), "1:a,2:b,3:c,O:4:new");
}
Also used : ServerStatus(com.netflix.exhibitor.core.entities.ServerStatus) Test(org.testng.annotations.Test)

Aggregations

ServerStatus (com.netflix.exhibitor.core.entities.ServerStatus)18 Test (org.testng.annotations.Test)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 ServerList (com.netflix.exhibitor.core.state.ServerList)4 InstanceStateTypes (com.netflix.exhibitor.core.state.InstanceStateTypes)3 ServerSpec (com.netflix.exhibitor.core.state.ServerSpec)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ClusterStatusTask (com.netflix.exhibitor.core.automanage.ClusterStatusTask)1 InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)1 LoadedInstanceConfig (com.netflix.exhibitor.core.config.LoadedInstanceConfig)1 PseudoLock (com.netflix.exhibitor.core.config.PseudoLock)1 IOException (java.io.IOException)1 List (java.util.List)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 GenericEntity (javax.ws.rs.core.GenericEntity)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1