Search in sources :

Example 1 with PseudoLock

use of com.netflix.exhibitor.core.config.PseudoLock 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 PseudoLock

use of com.netflix.exhibitor.core.config.PseudoLock in project exhibitor by soabase.

the class ConfigResource method setConfigRolling.

@Path("set-rolling")
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response setConfigRolling(String newConfigJson) throws Exception {
    InstanceConfig wrapped = parseToConfig(newConfigJson);
    Result result = null;
    try {
        PseudoLock lock = context.getExhibitor().getConfigManager().newConfigBasedLock();
        try {
            if (// TODO consider making configurable in the future
            lock.lock(context.getExhibitor().getLog(), 10, TimeUnit.SECONDS)) {
                if (context.getExhibitor().getConfigManager().startRollingConfig(wrapped, null)) {
                    result = new Result("OK", true);
                }
            }
        } finally {
            lock.unlock();
        }
        if (result == null) {
            result = new Result("Another process has updated the config.", false);
        }
        context.getExhibitor().resetLocalConnection();
    } catch (Exception e) {
        result = new Result(e);
    }
    return Response.ok(result).build();
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) IOException(java.io.IOException) Result(com.netflix.exhibitor.core.entities.Result) PseudoLock(com.netflix.exhibitor.core.config.PseudoLock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with PseudoLock

use of com.netflix.exhibitor.core.config.PseudoLock in project exhibitor by soabase.

the class ConfigResource method setConfig.

@Path("set")
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response setConfig(String newConfigJson) throws Exception {
    InstanceConfig wrapped = parseToConfig(newConfigJson);
    Result result = null;
    try {
        PseudoLock lock = context.getExhibitor().getConfigManager().newConfigBasedLock();
        try {
            if (// TODO consider making configurable in the future
            lock.lock(context.getExhibitor().getLog(), 10, TimeUnit.SECONDS)) {
                if (context.getExhibitor().getConfigManager().updateConfig(wrapped)) {
                    result = new Result("OK", true);
                }
            }
        } finally {
            lock.unlock();
        }
        if (result == null) {
            result = new Result(CANT_UPDATE_CONFIG_MESSAGE, false);
        }
        context.getExhibitor().resetLocalConnection();
    } catch (Exception e) {
        result = new Result(e);
    }
    return Response.ok(result).build();
}
Also used : InstanceConfig(com.netflix.exhibitor.core.config.InstanceConfig) IOException(java.io.IOException) Result(com.netflix.exhibitor.core.entities.Result) PseudoLock(com.netflix.exhibitor.core.config.PseudoLock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

PseudoLock (com.netflix.exhibitor.core.config.PseudoLock)3 InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)2 Result (com.netflix.exhibitor.core.entities.Result)2 IOException (java.io.IOException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ServerStatus (com.netflix.exhibitor.core.entities.ServerStatus)1 ServerList (com.netflix.exhibitor.core.state.ServerList)1