diff options
Diffstat (limited to 'orchestrator/src/main/java/com')
11 files changed, 168 insertions, 155 deletions
diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorImpl.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorImpl.java index d9d41cdaecc..964d2875496 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorImpl.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorImpl.java @@ -34,7 +34,7 @@ import com.yahoo.vespa.orchestrator.status.ApplicationInstanceStatus; import com.yahoo.vespa.orchestrator.status.HostInfo; import com.yahoo.vespa.orchestrator.status.HostInfos; import com.yahoo.vespa.orchestrator.status.HostStatus; -import com.yahoo.vespa.orchestrator.status.MutableStatusRegistry; +import com.yahoo.vespa.orchestrator.status.MutableStatusService; import com.yahoo.vespa.orchestrator.status.StatusService; import java.io.IOException; @@ -135,8 +135,8 @@ public class OrchestratorImpl implements Orchestrator { public void setNodeStatus(HostName hostName, HostStatus status) throws OrchestrationException { ApplicationInstanceReference reference = getApplicationInstance(hostName).reference(); OrchestratorContext context = OrchestratorContext.createContextForSingleAppOp(clock); - try (MutableStatusRegistry statusRegistry = statusService - .lockApplicationInstance_forCurrentThreadOnly(context, reference)) { + try (MutableStatusService statusRegistry = statusService + .lockApplication(context, reference)) { statusRegistry.setHostState(hostName, status); } } @@ -165,8 +165,8 @@ public class OrchestratorImpl implements Orchestrator { ApplicationInstance appInstance = getApplicationInstance(hostName); OrchestratorContext context = OrchestratorContext.createContextForSingleAppOp(clock); - try (MutableStatusRegistry statusRegistry = statusService - .lockApplicationInstance_forCurrentThreadOnly(context, appInstance.reference())) { + try (MutableStatusService statusRegistry = statusService + .lockApplication(context, appInstance.reference())) { HostStatus currentHostState = statusRegistry.getHostInfos().getOrNoRemarks(hostName).status(); if (currentHostState == HostStatus.NO_REMARKS) { return; @@ -202,8 +202,8 @@ public class OrchestratorImpl implements Orchestrator { .with(FetchVector.Dimension.HOSTNAME, hostName.s()) .value(); OrchestratorContext context = OrchestratorContext.createContextForSingleAppOp(clock, usePermanentlyDownStatus); - try (MutableStatusRegistry statusRegistry = statusService - .lockApplicationInstance_forCurrentThreadOnly(context, appInstance.reference())) { + try (MutableStatusService statusRegistry = statusService + .lockApplication(context, appInstance.reference())) { ApplicationApi applicationApi = applicationApiFactory.create(nodeGroup, statusRegistry, clusterControllerClientFactory); @@ -220,8 +220,8 @@ public class OrchestratorImpl implements Orchestrator { void suspendGroup(OrchestratorContext context, NodeGroup nodeGroup) throws HostStateChangeDeniedException { ApplicationInstanceReference applicationReference = nodeGroup.getApplicationReference(); - try (MutableStatusRegistry hostStatusRegistry = - statusService.lockApplicationInstance_forCurrentThreadOnly(context, applicationReference)) { + try (MutableStatusService hostStatusRegistry = + statusService.lockApplication(context, applicationReference)) { ApplicationInstanceStatus appStatus = hostStatusRegistry.getStatus(); if (appStatus == ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN) { return; @@ -354,8 +354,8 @@ public class OrchestratorImpl implements Orchestrator { throws ApplicationStateChangeDeniedException, ApplicationIdNotFoundException{ OrchestratorContext context = OrchestratorContext.createContextForSingleAppOp(clock); ApplicationInstanceReference appRef = OrchestratorUtil.toApplicationInstanceReference(appId, instanceLookupService); - try (MutableStatusRegistry statusRegistry = - statusService.lockApplicationInstance_forCurrentThreadOnly(context, appRef)) { + try (MutableStatusService statusRegistry = + statusService.lockApplication(context, appRef)) { // Short-circuit if already in wanted state if (status == statusRegistry.getStatus()) return; diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorUtil.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorUtil.java index 91da046840d..5294a841412 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorUtil.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/OrchestratorUtil.java @@ -65,7 +65,7 @@ public class OrchestratorUtil { private static final Pattern APPLICATION_INSTANCE_REFERENCE_REST_FORMAT_PATTERN = Pattern.compile("^([^:]+):(.+)$"); /** Returns an ApplicationInstanceReference constructed from the serialized format used in the REST API. */ - public static ApplicationInstanceReference parseAppInstanceReference(String restFormat) { + public static ApplicationInstanceReference parseApplicationInstanceReference(String restFormat) { if (restFormat == null) { throw new IllegalArgumentException("Could not construct instance id from null string"); } diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiFactory.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiFactory.java index 5e6ec59c28d..ef14a99d7db 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiFactory.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiFactory.java @@ -2,7 +2,7 @@ package com.yahoo.vespa.orchestrator.model; import com.yahoo.vespa.orchestrator.controller.ClusterControllerClientFactory; -import com.yahoo.vespa.orchestrator.status.MutableStatusRegistry; +import com.yahoo.vespa.orchestrator.status.MutableStatusService; /** * @author mpolden @@ -16,7 +16,7 @@ public class ApplicationApiFactory { } public ApplicationApi create(NodeGroup nodeGroup, - MutableStatusRegistry hostStatusService, + MutableStatusService hostStatusService, ClusterControllerClientFactory clusterControllerClientFactory) { return new ApplicationApiImpl(nodeGroup, hostStatusService, clusterControllerClientFactory, numberOfConfigServers); } diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiImpl.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiImpl.java index cf6946fa7f8..92867cf3258 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiImpl.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/model/ApplicationApiImpl.java @@ -12,7 +12,7 @@ import com.yahoo.vespa.orchestrator.controller.ClusterControllerClientFactory; import com.yahoo.vespa.orchestrator.status.ApplicationInstanceStatus; import com.yahoo.vespa.orchestrator.status.HostInfos; import com.yahoo.vespa.orchestrator.status.HostStatus; -import com.yahoo.vespa.orchestrator.status.MutableStatusRegistry; +import com.yahoo.vespa.orchestrator.status.MutableStatusService; import java.util.Collection; import java.util.Comparator; @@ -32,12 +32,12 @@ public class ApplicationApiImpl implements ApplicationApi { private final ApplicationInstance applicationInstance; private final NodeGroup nodeGroup; - private final MutableStatusRegistry hostStatusService; + private final MutableStatusService hostStatusService; private final List<ClusterApi> clusterInOrder; private final HostInfos hostInfos; public ApplicationApiImpl(NodeGroup nodeGroup, - MutableStatusRegistry hostStatusService, + MutableStatusService hostStatusService, ClusterControllerClientFactory clusterControllerClientFactory, int numberOfConfigServers) { this.applicationInstance = nodeGroup.getApplication(); diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicy.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicy.java index f6a1e4f91f0..95ce34a8a61 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicy.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/HostedVespaPolicy.java @@ -13,7 +13,7 @@ import com.yahoo.vespa.orchestrator.model.NodeGroup; import com.yahoo.vespa.orchestrator.model.StorageNode; import com.yahoo.vespa.orchestrator.status.ApplicationInstanceStatus; import com.yahoo.vespa.orchestrator.status.HostStatus; -import com.yahoo.vespa.orchestrator.status.MutableStatusRegistry; +import com.yahoo.vespa.orchestrator.status.MutableStatusService; /** * @author oyving @@ -113,7 +113,7 @@ public class HostedVespaPolicy implements Policy { OrchestratorContext context, ApplicationInstance applicationInstance, HostName hostName, - MutableStatusRegistry hostStatusService) throws HostStateChangeDeniedException { + MutableStatusService hostStatusService) throws HostStateChangeDeniedException { NodeGroup nodeGroup = new NodeGroup(applicationInstance, hostName); ApplicationApi applicationApi = applicationApiFactory.create(nodeGroup, hostStatusService, clusterControllerClientFactory); releaseSuspensionGrant(context, applicationApi); diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/Policy.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/Policy.java index aa7636227c8..6e7a3552828 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/Policy.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/policy/Policy.java @@ -5,8 +5,7 @@ import com.yahoo.vespa.applicationmodel.ApplicationInstance; import com.yahoo.vespa.applicationmodel.HostName; import com.yahoo.vespa.orchestrator.OrchestratorContext; import com.yahoo.vespa.orchestrator.model.ApplicationApi; -import com.yahoo.vespa.orchestrator.status.MutableStatusRegistry; -import com.yahoo.vespa.orchestrator.status.StatusService; +import com.yahoo.vespa.orchestrator.status.MutableStatusService; /** * @author oyving @@ -33,6 +32,6 @@ public interface Policy { void releaseSuspensionGrant( OrchestratorContext context, ApplicationInstance applicationInstance, HostName hostName, - MutableStatusRegistry hostStatusService) throws HostStateChangeDeniedException; + MutableStatusService hostStatusService) throws HostStateChangeDeniedException; } diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/InstanceResource.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/InstanceResource.java index cfdda3250f6..2ade81ff0f5 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/InstanceResource.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/InstanceResource.java @@ -37,7 +37,7 @@ import java.util.TreeMap; import java.util.stream.Collectors; import static com.yahoo.vespa.orchestrator.OrchestratorUtil.getHostsUsedByApplicationInstance; -import static com.yahoo.vespa.orchestrator.OrchestratorUtil.parseAppInstanceReference; +import static com.yahoo.vespa.orchestrator.OrchestratorUtil.parseApplicationInstanceReference; /** * Provides a read-only API for looking into the current state as seen by the Orchestrator. @@ -152,7 +152,7 @@ public class InstanceResource { static ApplicationInstanceReference parseInstanceId(String instanceIdString) { try { - return parseAppInstanceReference(instanceIdString); + return parseApplicationInstanceReference(instanceIdString); } catch (IllegalArgumentException e) { throwBadRequest(e.getMessage()); return null; // Necessary for compiler diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/MutableStatusRegistry.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/MutableStatusService.java index 24da83364aa..ad68652e968 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/MutableStatusRegistry.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/MutableStatusService.java @@ -3,8 +3,6 @@ package com.yahoo.vespa.orchestrator.status; import com.yahoo.vespa.applicationmodel.HostName; -import java.util.Set; - /** * Registry of the suspension and host statuses for an application instance. * @@ -12,7 +10,7 @@ import java.util.Set; * @author Tony Vaagenes * @author bakksjo */ -public interface MutableStatusRegistry extends AutoCloseable { +public interface MutableStatusService extends AutoCloseable { /** Returns the status of this application. */ ApplicationInstanceStatus getStatus(); diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/StatusService.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/StatusService.java index e2be5ec7eb6..346d0523333 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/StatusService.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/StatusService.java @@ -44,9 +44,8 @@ public interface StatusService { * this case, subsequent mutating operations will fail, but previous mutating operations are NOT rolled back. * This may leave the registry in an inconsistent state (as judged by the client code). */ - MutableStatusRegistry lockApplicationInstance_forCurrentThreadOnly( - OrchestratorContext context, - ApplicationInstanceReference applicationInstanceReference) throws UncheckedTimeoutException; + MutableStatusService lockApplication(OrchestratorContext context, ApplicationInstanceReference reference) + throws UncheckedTimeoutException; /** * Returns all application instances that are allowed to be down. The intention is to use this @@ -68,5 +67,5 @@ public interface StatusService { ApplicationInstanceStatus getApplicationInstanceStatus(ApplicationInstanceReference application); /** Get host info for hostname in application. This is consistent if its lock is held. */ - HostInfo getHostInfo(ApplicationInstanceReference applicationInstanceReference, HostName hostName); + HostInfo getHostInfo(ApplicationInstanceReference reference, HostName hostName); } diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZkMutableStatusService.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZkMutableStatusService.java new file mode 100644 index 00000000000..e855f78a03c --- /dev/null +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZkMutableStatusService.java @@ -0,0 +1,104 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.vespa.orchestrator.status; + +import com.yahoo.log.LogLevel; +import com.yahoo.vespa.applicationmodel.ApplicationInstanceReference; +import com.yahoo.vespa.applicationmodel.HostName; +import com.yahoo.vespa.curator.Curator; +import org.apache.zookeeper.KeeperException; + +import java.util.logging.Logger; + +class ZkMutableStatusService implements MutableStatusService { + + private static final Logger log = Logger.getLogger(ZkMutableStatusService.class.getName()); + + private final ZkStatusService statusService; + private final Curator curator; + private final Runnable onClose; + private final ApplicationInstanceReference reference; + private final boolean probe; + private final HostInfosCache hostInfosCache; + + ZkMutableStatusService(ZkStatusService statusService, + Curator curator, + Runnable onClose, + ApplicationInstanceReference reference, + boolean probe, + HostInfosCache hostInfosCache) { + this.statusService = statusService; + this.curator = curator; + this.onClose = onClose; + this.reference = reference; + this.probe = probe; + this.hostInfosCache = hostInfosCache; + } + + @Override + public ApplicationInstanceStatus getStatus() { + return statusService.getApplicationInstanceStatus(reference); + } + + @Override + public HostInfos getHostInfos() { + return hostInfosCache.getHostInfos(reference); + } + + @Override + public void setHostState(final HostName hostName, final HostStatus status) { + if (probe) return; + log.log(LogLevel.INFO, "Setting host " + hostName + " to status " + status); + hostInfosCache.setHostStatus(reference, hostName, status); + } + + @Override + public void setApplicationInstanceStatus(ApplicationInstanceStatus applicationInstanceStatus) { + if (probe) return; + + log.log(LogLevel.INFO, "Setting app " + reference.asString() + " to status " + applicationInstanceStatus); + + String path = statusService.applicationInstanceSuspendedPath(reference); + switch (applicationInstanceStatus) { + case NO_REMARKS: + deleteNode_ignoreNoNodeException(path); + break; + case ALLOWED_TO_BE_DOWN: + createNode_ignoreNodeExistsException(path); + break; + } + } + + @Override + public void close() { + try { + onClose.run(); + } catch (RuntimeException e) { + // We may want to avoid logging some exceptions that may be expected, like when session expires. + log.log(LogLevel.WARNING, + "Failed close application lock in " + + ZkMutableStatusService.class.getSimpleName() + ", will ignore and continue", + e); + } + } + + void deleteNode_ignoreNoNodeException(String path) { + try { + curator.framework().delete().forPath(path); + } catch (KeeperException.NoNodeException e) { + // ok + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + void createNode_ignoreNodeExistsException(String path) { + try { + curator.framework().create().creatingParentsIfNeeded().forPath(path); + } catch (KeeperException.NodeExistsException e) { + // ok + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZookeeperStatusService.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZkStatusService.java index ba363b73973..4be629c6d21 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZookeeperStatusService.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/status/ZkStatusService.java @@ -13,8 +13,6 @@ import com.yahoo.vespa.curator.Curator; import com.yahoo.vespa.curator.Lock; import com.yahoo.vespa.orchestrator.OrchestratorContext; import com.yahoo.vespa.orchestrator.OrchestratorUtil; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.apache.zookeeper.KeeperException.NodeExistsException; import org.apache.zookeeper.data.Stat; import javax.inject.Inject; @@ -33,9 +31,9 @@ import java.util.logging.Logger; * TODO: expiry of old application instances * @author Tony Vaagenes */ -public class ZookeeperStatusService implements StatusService { +public class ZkStatusService implements StatusService { - private static final Logger log = Logger.getLogger(ZookeeperStatusService.class.getName()); + private static final Logger log = Logger.getLogger(ZkStatusService.class.getName()); final static String HOST_STATUS_BASE_PATH = "/vespa/host-status-service"; final static String APPLICATION_STATUS_BASE_PATH = "/vespa/application-status-service"; @@ -52,7 +50,7 @@ public class ZookeeperStatusService implements StatusService { private final ConcurrentHashMap<Map<String, String>, Metric.Context> cachedContexts = new ConcurrentHashMap<>(); @Inject - public ZookeeperStatusService(@Component Curator curator, @Component Metric metric, @Component Timer timer) { + public ZkStatusService(@Component Curator curator, @Component Metric metric, @Component Timer timer) { this.curator = curator; this.metric = metric; this.timer = timer; @@ -60,7 +58,7 @@ public class ZookeeperStatusService implements StatusService { } /** Non-private for testing only. */ - ZookeeperStatusService(Curator curator, Metric metric, Timer timer, HostInfosCache hostInfosCache) { + ZkStatusService(Curator curator, Metric metric, Timer timer, HostInfosCache hostInfosCache) { this.curator = curator; this.metric = metric; this.timer = timer; @@ -77,9 +75,9 @@ public class ZookeeperStatusService implements StatusService { if (stat == null) return resultSet; // The path exist and we may have children - for (String appRefStr : curator.framework().getChildren().forPath(APPLICATION_STATUS_BASE_PATH)) { - ApplicationInstanceReference appRef = OrchestratorUtil.parseAppInstanceReference(appRefStr); - resultSet.add(appRef); + for (String referenceString : curator.framework().getChildren().forPath(APPLICATION_STATUS_BASE_PATH)) { + ApplicationInstanceReference reference = OrchestratorUtil.parseApplicationInstanceReference(referenceString); + resultSet.add(reference); } return resultSet; @@ -112,20 +110,20 @@ public class ZookeeperStatusService implements StatusService { * (i.e. the request is for another applicationInstanceReference) */ @Override - public MutableStatusRegistry lockApplicationInstance_forCurrentThreadOnly( + public MutableStatusService lockApplication( OrchestratorContext context, - ApplicationInstanceReference applicationInstanceReference) throws UncheckedTimeoutException { + ApplicationInstanceReference reference) throws UncheckedTimeoutException { Runnable onRegistryClose; // A multi-application operation, aka batch suspension, will first issue a probe // then a non-probe. With "large locks", the lock is not release in between - // no lock is taken on the non-probe. Instead, the release is done on the multi-application // context close. - if (context.hasLock(applicationInstanceReference)) { + if (context.hasLock(reference)) { onRegistryClose = () -> {}; } else { - Runnable unlock = acquireLock(context, applicationInstanceReference); - if (context.registerLockAcquisition(applicationInstanceReference, unlock)) { + Runnable unlock = acquireLock(context, reference); + if (context.registerLockAcquisition(reference, unlock)) { onRegistryClose = () -> {}; } else { onRegistryClose = unlock; @@ -133,7 +131,13 @@ public class ZookeeperStatusService implements StatusService { } try { - return new ZkMutableStatusRegistry(onRegistryClose, applicationInstanceReference, context.isProbe()); + return new ZkMutableStatusService( + this, + curator, + onRegistryClose, + reference, + context.isProbe(), + hostInfosCache); } catch (Throwable t) { // In case the constructor throws an exception. onRegistryClose.run(); @@ -142,9 +146,9 @@ public class ZookeeperStatusService implements StatusService { } private Runnable acquireLock(OrchestratorContext context, - ApplicationInstanceReference applicationInstanceReference) + ApplicationInstanceReference reference) throws UncheckedTimeoutException { - ApplicationId applicationId = OrchestratorUtil.toApplicationId(applicationInstanceReference); + ApplicationId applicationId = OrchestratorUtil.toApplicationId(reference); String app = applicationId.application().value() + "." + applicationId.instance().value(); Map<String, String> dimensions = Map.of( "tenantName", applicationId.tenant().value(), @@ -153,7 +157,7 @@ public class ZookeeperStatusService implements StatusService { Metric.Context metricContext = cachedContexts.computeIfAbsent(dimensions, metric::createContext); Duration duration = context.getTimeLeft(); - String lockPath = applicationInstanceLock2Path(applicationInstanceReference); + String lockPath = applicationInstanceLock2Path(reference); Lock lock = new Lock(lockPath, curator); Instant startTime = timer.currentTime(); @@ -180,7 +184,7 @@ public class ZookeeperStatusService implements StatusService { // We may want to avoid logging some exceptions that may be expected, like when session expires. log.log(LogLevel.WARNING, "Failed to close application lock for " + - ZookeeperStatusService.class.getSimpleName() + ", will ignore and continue", + ZkStatusService.class.getSimpleName() + ", will ignore and continue", e); } @@ -194,42 +198,16 @@ public class ZookeeperStatusService implements StatusService { return Duration.between(startInstant, endInstant).toMillis() / 1000.0; } - private boolean deleteNode_ignoreNoNodeException(String path, String debugLogMessageIfNotExists) { - try { - curator.framework().delete().forPath(path); - return true; - } catch (NoNodeException e) { - log.log(LogLevel.DEBUG, debugLogMessageIfNotExists, e); - return false; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - private boolean createNode_ignoreNodeExistsException(String path, String debugLogMessageIfExists) { - try { - curator.framework().create() - .creatingParentsIfNeeded() - .forPath(path); - return true; - } catch (NodeExistsException e) { - log.log(LogLevel.DEBUG, debugLogMessageIfExists, e); - return false; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - @Override - public HostInfo getHostInfo(ApplicationInstanceReference applicationInstanceReference, HostName hostName) { - return hostInfosCache.getHostInfos(applicationInstanceReference).getOrNoRemarks(hostName); + public HostInfo getHostInfo(ApplicationInstanceReference reference, HostName hostName) { + return hostInfosCache.getHostInfos(reference).getOrNoRemarks(hostName); } @Override - public ApplicationInstanceStatus getApplicationInstanceStatus(ApplicationInstanceReference applicationInstanceReference) { + public ApplicationInstanceStatus getApplicationInstanceStatus(ApplicationInstanceReference reference) { try { Stat statOrNull = curator.framework().checkExists().forPath( - applicationInstanceSuspendedPath(applicationInstanceReference)); + applicationInstanceSuspendedPath(reference)); return (statOrNull == null) ? ApplicationInstanceStatus.NO_REMARKS : ApplicationInstanceStatus.ALLOWED_TO_BE_DOWN; } catch (Exception e) { @@ -237,89 +215,24 @@ public class ZookeeperStatusService implements StatusService { } } - static String applicationInstanceReferencePath(ApplicationInstanceReference applicationInstanceReference) { - return HOST_STATUS_BASE_PATH + '/' + - applicationInstanceReference.tenantId() + ":" + applicationInstanceReference.applicationInstanceId(); - } - - private static String hostsAllowedDownPath(ApplicationInstanceReference applicationInstanceReference) { - return applicationInstanceReferencePath(applicationInstanceReference) + "/hosts-allowed-down"; + static String applicationInstanceReferencePath(ApplicationInstanceReference reference) { + return HOST_STATUS_BASE_PATH + '/' + reference.tenantId() + ":" + reference.applicationInstanceId(); } - private static String applicationInstanceLock2Path(ApplicationInstanceReference applicationInstanceReference) { - return applicationInstanceReferencePath(applicationInstanceReference) + "/lock2"; + private static String hostsAllowedDownPath(ApplicationInstanceReference reference) { + return applicationInstanceReferencePath(reference) + "/hosts-allowed-down"; } - private String applicationInstanceSuspendedPath(ApplicationInstanceReference applicationInstanceReference) { - return APPLICATION_STATUS_BASE_PATH + "/" + OrchestratorUtil.toRestApiFormat(applicationInstanceReference); + private static String applicationInstanceLock2Path(ApplicationInstanceReference reference) { + return applicationInstanceReferencePath(reference) + "/lock2"; } - private static String hostAllowedDownPath(ApplicationInstanceReference applicationInstanceReference, HostName hostname) { - return hostsAllowedDownPath(applicationInstanceReference) + '/' + hostname.s(); + String applicationInstanceSuspendedPath(ApplicationInstanceReference reference) { + return APPLICATION_STATUS_BASE_PATH + "/" + OrchestratorUtil.toRestApiFormat(reference); } - private class ZkMutableStatusRegistry implements MutableStatusRegistry { - - private final Runnable onClose; - private final ApplicationInstanceReference applicationInstanceReference; - private final boolean probe; - - public ZkMutableStatusRegistry(Runnable onClose, - ApplicationInstanceReference applicationInstanceReference, - boolean probe) { - this.onClose = onClose; - this.applicationInstanceReference = applicationInstanceReference; - this.probe = probe; - } - - @Override - public ApplicationInstanceStatus getStatus() { - return getApplicationInstanceStatus(applicationInstanceReference); - } - - @Override - public HostInfos getHostInfos() { - return hostInfosCache.getHostInfos(applicationInstanceReference); - } - - @Override - public void setHostState(final HostName hostName, final HostStatus status) { - if (probe) return; - log.log(LogLevel.INFO, "Setting host " + hostName + " to status " + status); - hostInfosCache.setHostStatus(applicationInstanceReference, hostName, status); - } - - @Override - public void setApplicationInstanceStatus(ApplicationInstanceStatus applicationInstanceStatus) { - if (probe) return; - - log.log(LogLevel.INFO, "Setting app " + applicationInstanceReference.asString() + " to status " + applicationInstanceStatus); - - String path = applicationInstanceSuspendedPath(applicationInstanceReference); - switch (applicationInstanceStatus) { - case NO_REMARKS: - deleteNode_ignoreNoNodeException(path, - "Instance is already in state NO_REMARKS, path = " + path); - break; - case ALLOWED_TO_BE_DOWN: - createNode_ignoreNodeExistsException(path, - "Instance is already in state ALLOWED_TO_BE_DOWN, path = " + path); - break; - } - } - - @Override - public void close() { - try { - onClose.run(); - } catch (RuntimeException e) { - // We may want to avoid logging some exceptions that may be expected, like when session expires. - log.log(LogLevel.WARNING, - "Failed close application lock in " + - ZookeeperStatusService.class.getSimpleName() + ", will ignore and continue", - e); - } - } + private static String hostAllowedDownPath(ApplicationInstanceReference reference, HostName hostname) { + return hostsAllowedDownPath(reference) + '/' + hostname.s(); } } |