2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.singleton.dom.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static com.google.common.base.Verify.verifyNotNull;
14 import static java.util.Objects.requireNonNull;
16 import com.google.common.annotations.VisibleForTesting;
17 import com.google.common.base.MoreObjects;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.util.concurrent.FutureCallback;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.common.util.concurrent.SettableFuture;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Iterator;
29 import java.util.Map.Entry;
31 import java.util.concurrent.ConcurrentHashMap;
32 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
33 import java.util.concurrent.atomic.AtomicReference;
34 import org.checkerframework.checker.lock.qual.GuardedBy;
35 import org.checkerframework.checker.lock.qual.Holding;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
38 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
39 import org.opendaylight.mdsal.eos.common.api.GenericEntity;
40 import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
41 import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipChange;
42 import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipListener;
43 import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipService;
44 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
45 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
46 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Implementation of {@link ClusterSingletonServiceGroup} on top of the Entity Ownership Service. Since EOS is atomic
52 * in its operation and singleton services incur startup and most notably cleanup, we need to do something smart here.
55 * The implementation takes advantage of the fact that EOS provides stable ownership, i.e. owners are not moved as
56 * a result on new candidates appearing. We use two entities:
57 * - service entity, to which all nodes register
58 * - cleanup entity, which only the service entity owner registers to
61 * Once the cleanup entity ownership is acquired, services are started. As long as the cleanup entity is registered,
62 * it should remain the owner. In case a new service owner emerges, the old owner will start the cleanup process,
63 * eventually releasing the cleanup entity. The new owner registers for the cleanup entity -- but will not see it
64 * granted until the old owner finishes the cleanup.
66 * @param <P> the instance identifier path type
67 * @param <E> the GenericEntity type
68 * @param <C> the GenericEntityOwnershipChange type
69 * @param <G> the GenericEntityOwnershipListener type
70 * @param <S> the GenericEntityOwnershipService type
72 final class ClusterSingletonServiceGroupImpl<P extends HierarchicalIdentifier<P>, E extends GenericEntity<P>,
73 C extends GenericEntityOwnershipChange<P, E>, G extends GenericEntityOwnershipListener<P, C>,
74 S extends GenericEntityOwnershipService<P, E, G>> extends ClusterSingletonServiceGroup<P, E, C> {
76 private enum EntityState {
78 * This entity was never registered.
82 * Registration exists, but we are waiting for it to resolve.
86 * Registration indicated we are the owner.
90 * Registration indicated we are the owner, but global state is uncertain -- meaning there can be owners in
91 * another partition, for example.
95 * Registration indicated we are not the owner. In this state we do not care about global state, therefore we
96 * do not need an UNOWNED_JEOPARDY state.
103 * Local service is up and running.
105 // FIXME: we should support async startup, which will require a STARTING state.
108 * Local service is being stopped.
113 private static final Logger LOG = LoggerFactory.getLogger(ClusterSingletonServiceGroupImpl.class);
115 private final S entityOwnershipService;
116 private final String identifier;
118 /* Entity instances */
119 private final E serviceEntity;
120 private final E cleanupEntity;
122 private final Set<ClusterSingletonServiceRegistration> members = ConcurrentHashMap.newKeySet();
124 private final Map<ClusterSingletonServiceRegistration, ServiceInfo> services = new HashMap<>();
126 // Marker for when any state changed
127 @SuppressWarnings("rawtypes")
128 private static final AtomicIntegerFieldUpdater<ClusterSingletonServiceGroupImpl> DIRTY_UPDATER =
129 AtomicIntegerFieldUpdater.newUpdater(ClusterSingletonServiceGroupImpl.class, "dirty");
130 private volatile int dirty;
132 // Simplified lock: non-reentrant, support tryLock() only
133 @SuppressWarnings("rawtypes")
134 private static final AtomicIntegerFieldUpdater<ClusterSingletonServiceGroupImpl> LOCK_UPDATER =
135 AtomicIntegerFieldUpdater.newUpdater(ClusterSingletonServiceGroupImpl.class, "lock");
136 @SuppressWarnings("unused")
137 private volatile int lock;
140 * State tracking is quite involved, as we are tracking up to four asynchronous sources of events:
141 * - user calling close()
142 * - service entity ownership
143 * - cleanup entity ownership
144 * - service shutdown future
146 * Absolutely correct solution would be a set of behaviors, which govern each state, remembering where we want to
147 * get to and what we are doing. That would result in ~15 classes which would quickly render this code unreadable
148 * due to boilerplate overhead.
150 * We therefore take a different approach, tracking state directly in this class and evaluate state transitions
151 * based on recorded bits -- without explicit representation of state machine state.
154 * Group close future. In can only go from null to non-null reference. Whenever it is non-null, it indicates that
155 * the user has closed the group and we are converging to termination.
157 // We are using volatile get-and-set to support non-blocking close(). It may be more efficient to inline it here,
158 // as we perform a volatile read after unlocking -- that volatile read may easier on L1 cache.
159 // XXX: above needs a microbenchmark contention ever becomes a problem.
160 private final AtomicReference<SettableFuture<Void>> closeFuture = new AtomicReference<>();
163 * Service (base) entity registration. This entity selects an owner candidate across nodes. Candidates proceed to
164 * acquire {@link #cleanupEntity}.
167 private GenericEntityOwnershipCandidateRegistration<P, E> serviceEntityReg = null;
169 * Service (base) entity last reported state.
172 private EntityState serviceEntityState = EntityState.UNREGISTERED;
175 * Cleanup (owner) entity registration. This entity guards access to service state and coordinates shutdown cleanup
179 private GenericEntityOwnershipCandidateRegistration<P, E> cleanupEntityReg;
181 * Cleanup (owner) entity last reported state.
184 private EntityState cleanupEntityState = EntityState.UNREGISTERED;
186 private volatile boolean initialized;
189 * Class constructor. Note: last argument is reused as-is.
191 * @param identifier non-empty string as identifier
192 * @param mainEntity as Entity instance
193 * @param closeEntity as Entity instance
194 * @param entityOwnershipService GenericEntityOwnershipService instance
195 * @param parent parent service
196 * @param services Services list
198 ClusterSingletonServiceGroupImpl(final String identifier, final S entityOwnershipService, final E mainEntity,
199 final E closeEntity, final Collection<ClusterSingletonServiceRegistration> services) {
200 checkArgument(!identifier.isEmpty(), "Identifier may not be empty");
201 this.identifier = identifier;
202 this.entityOwnershipService = requireNonNull(entityOwnershipService);
203 this.serviceEntity = requireNonNull(mainEntity);
204 this.cleanupEntity = requireNonNull(closeEntity);
205 members.addAll(services);
207 LOG.debug("Instantiated new service group for {}", identifier);
211 ClusterSingletonServiceGroupImpl(final String identifier, final E mainEntity,
212 final E closeEntity, final S entityOwnershipService) {
213 this(identifier, entityOwnershipService, mainEntity, closeEntity, ImmutableList.of());
217 public String getIdentifier() {
222 ListenableFuture<?> closeClusterSingletonGroup() {
223 final ListenableFuture<?> ret = destroyGroup();
230 LOG.debug("Service group {} postponing sync on close", identifier);
236 private boolean isClosed() {
237 return closeFuture.get() != null;
241 void initialize() throws CandidateAlreadyRegisteredException {
244 checkState(!initialized, "Singleton group %s was already initilized", identifier);
245 LOG.debug("Initializing service group {} with services {}", identifier, members);
246 synchronized (this) {
247 serviceEntityState = EntityState.REGISTERED;
248 serviceEntityReg = entityOwnershipService.registerCandidate(serviceEntity);
256 private void checkNotClosed() {
257 checkState(!isClosed(), "Service group %s has already been closed", identifier);
261 void registerService(final ClusterSingletonServiceRegistration reg) {
262 final ClusterSingletonService service = verifyRegistration(reg);
265 checkState(initialized, "Service group %s is not initialized yet", identifier);
267 // First put the service
268 LOG.debug("Adding service {} to service group {}", service, identifier);
269 verify(members.add(reg));
273 LOG.debug("Service group {} delayed register of {}", identifier, reg);
281 ListenableFuture<?> unregisterService(final ClusterSingletonServiceRegistration reg) {
282 verifyRegistration(reg);
285 verify(members.remove(reg));
287 if (members.isEmpty()) {
288 // We need to let AbstractClusterSingletonServiceProviderImpl know this group is to be shutdown
289 // before we start applying state, because while we do not re-enter, the user is free to do whatever,
290 // notably including registering a service with the same ID from the service shutdown hook. That
291 // registration request needs to hit the successor of this group.
292 return destroyGroup();
298 LOG.debug("Service group {} delayed unregister of {}", identifier, reg);
303 private ClusterSingletonService verifyRegistration(final ClusterSingletonServiceRegistration reg) {
304 final ClusterSingletonService service = reg.getInstance();
305 verify(identifier.equals(service.getIdentifier().getName()));
309 private synchronized @NonNull ListenableFuture<?> destroyGroup() {
310 final SettableFuture<Void> future = SettableFuture.create();
311 if (!closeFuture.compareAndSet(null, future)) {
312 return verifyNotNull(closeFuture.get());
315 if (serviceEntityReg != null) {
316 // We are still holding the service registration, close it now...
317 LOG.debug("Service group {} unregistering service entity {}", identifier, serviceEntity);
318 serviceEntityReg.close();
319 serviceEntityReg = null;
327 void ownershipChanged(final C ownershipChange) {
328 LOG.debug("Ownership change {} for ClusterSingletonServiceGroup {}", ownershipChange, identifier);
330 synchronized (this) {
331 lockedOwnershipChanged(ownershipChange);
336 LOG.debug("Service group {} postponing ownership change sync", identifier);
345 * Handle an ownership change with the lock held. Callers are expected to handle termination conditions, this method
346 * and anything it calls must not call {@link #lockedClose(SettableFuture)}.
348 * @param ownershipChange reported change
351 private void lockedOwnershipChanged(final C ownershipChange) {
352 final E entity = ownershipChange.getEntity();
353 if (serviceEntity.equals(entity)) {
354 serviceOwnershipChanged(ownershipChange.getState(), ownershipChange.inJeopardy());
356 } else if (cleanupEntity.equals(entity)) {
357 cleanupCandidateOwnershipChanged(ownershipChange.getState(), ownershipChange.inJeopardy());
360 LOG.warn("Group {} received unrecognized change {}", identifier, ownershipChange);
365 private void cleanupCandidateOwnershipChanged(final EntityOwnershipChangeState state, final boolean jeopardy) {
367 cleanupEntityState = switch (state) {
368 case LOCAL_OWNERSHIP_GRANTED, LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE -> {
369 LOG.warn("Service group {} cleanup entity owned without certainty", identifier);
370 yield EntityState.OWNED_JEOPARDY;
372 case LOCAL_OWNERSHIP_LOST_NEW_OWNER, LOCAL_OWNERSHIP_LOST_NO_OWNER, REMOTE_OWNERSHIP_CHANGED,
373 REMOTE_OWNERSHIP_LOST_NO_OWNER -> {
374 LOG.info("Service group {} cleanup entity ownership uncertain", identifier);
375 yield EntityState.UNOWNED;
381 if (cleanupEntityState == EntityState.OWNED_JEOPARDY) {
382 // Pair info message with previous jeopardy
383 LOG.info("Service group {} cleanup entity ownership ascertained", identifier);
386 cleanupEntityState = switch (state) {
387 case LOCAL_OWNERSHIP_GRANTED, LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE -> EntityState.OWNED;
388 case LOCAL_OWNERSHIP_LOST_NEW_OWNER, LOCAL_OWNERSHIP_LOST_NO_OWNER, REMOTE_OWNERSHIP_LOST_NO_OWNER,
389 REMOTE_OWNERSHIP_CHANGED -> EntityState.UNOWNED;
394 private void serviceOwnershipChanged(final EntityOwnershipChangeState state, final boolean jeopardy) {
396 LOG.info("Service group {} service entity ownership uncertain", identifier);
397 serviceEntityState = switch (state) {
398 case LOCAL_OWNERSHIP_GRANTED, LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE -> EntityState.OWNED_JEOPARDY;
399 case LOCAL_OWNERSHIP_LOST_NEW_OWNER, LOCAL_OWNERSHIP_LOST_NO_OWNER, REMOTE_OWNERSHIP_CHANGED,
400 REMOTE_OWNERSHIP_LOST_NO_OWNER -> EntityState.UNOWNED;
405 if (serviceEntityState == EntityState.OWNED_JEOPARDY) {
406 // Pair info message with previous jeopardy
407 LOG.info("Service group {} service entity ownership ascertained", identifier);
411 case LOCAL_OWNERSHIP_GRANTED:
412 case LOCAL_OWNERSHIP_RETAINED_WITH_NO_CHANGE:
413 LOG.debug("Service group {} acquired service entity ownership", identifier);
414 serviceEntityState = EntityState.OWNED;
416 case LOCAL_OWNERSHIP_LOST_NEW_OWNER:
417 case LOCAL_OWNERSHIP_LOST_NO_OWNER:
418 case REMOTE_OWNERSHIP_CHANGED:
419 case REMOTE_OWNERSHIP_LOST_NO_OWNER:
420 LOG.debug("Service group {} lost service entity ownership", identifier);
421 serviceEntityState = EntityState.UNOWNED;
424 LOG.warn("Service group {} ignoring unhandled cleanup entity change {}", identifier, state);
428 // has to be called with lock asserted, which will be released prior to returning
429 private void reconcileState() {
430 // Always check if there is any state change to be applied.
433 if (conditionalClean()) {
437 // We may have ran a round of reconciliation, but the either one of may have happened asynchronously:
440 // - service future completed
441 // - entity state changed
443 // We are dropping the lock, but we need to recheck dirty and try to apply state again if it is found to
444 // be dirty again. This closes the following race condition:
446 // A: runs these checks holding the lock
447 // B: modifies them, fails to acquire lock
448 // A: releases lock -> noone takes care of reconciliation
455 LOG.debug("Service group {} re-running reconciliation", identifier);
459 LOG.debug("Service group {} will be reconciled by someone else", identifier);
461 LOG.debug("Service group {} is completely reconciled", identifier);
468 private void serviceTransitionCompleted() {
475 // Has to be called with lock asserted
476 private void tryReconcileState() {
477 // First take a safe snapshot of current state on which we will base our decisions.
478 final Set<ClusterSingletonServiceRegistration> localMembers;
479 final boolean haveCleanup;
480 final boolean haveService;
481 synchronized (this) {
482 if (serviceEntityReg != null) {
483 haveService = switch (serviceEntityState) {
484 case OWNED, OWNED_JEOPARDY -> true;
485 case REGISTERED, UNOWNED, UNREGISTERED -> false;
491 if (haveService && cleanupEntityReg == null) {
492 // We have the service entity but have not registered for cleanup entity. Do that now and retry.
493 LOG.debug("Service group {} registering cleanup entity", identifier);
495 cleanupEntityState = EntityState.REGISTERED;
496 cleanupEntityReg = entityOwnershipService.registerCandidate(cleanupEntity);
497 } catch (CandidateAlreadyRegisteredException e) {
498 LOG.error("Service group {} failed to take ownership, aborting", identifier, e);
499 if (serviceEntityReg != null) {
500 serviceEntityReg.close();
501 serviceEntityReg = null;
508 if (cleanupEntityReg != null) {
509 haveCleanup = switch (cleanupEntityState) {
511 case OWNED_JEOPARDY, REGISTERED, UNOWNED, UNREGISTERED -> false;
517 localMembers = ImmutableSet.copyOf(members);
520 if (haveService && haveCleanup) {
521 ensureServicesStarting(localMembers);
525 ensureServicesStopping();
527 if (!haveService && services.isEmpty()) {
528 LOG.debug("Service group {} has no running services", identifier);
529 final boolean canFinishClose;
530 synchronized (this) {
531 if (cleanupEntityReg != null) {
532 LOG.debug("Service group {} releasing cleanup entity", identifier);
533 cleanupEntityReg.close();
534 cleanupEntityReg = null;
537 canFinishClose = switch (cleanupEntityState) {
538 case OWNED, OWNED_JEOPARDY, REGISTERED -> false;
539 case UNOWNED, UNREGISTERED -> true;
543 if (canFinishClose) {
544 final SettableFuture<Void> localFuture = closeFuture.get();
545 if (localFuture != null && !localFuture.isDone()) {
546 LOG.debug("Service group {} completing termination", identifier);
547 localFuture.set(null);
553 // Has to be called with lock asserted
554 @SuppressWarnings("illegalCatch")
555 private void ensureServicesStarting(final Set<ClusterSingletonServiceRegistration> localConfig) {
556 LOG.debug("Service group {} starting services", identifier);
558 // This may look counter-intuitive, but the localConfig may be missing some services that are started -- for
559 // example when this method is executed as part of unregisterService() call. In that case we need to ensure
560 // services in the list are stopping
561 final Iterator<Entry<ClusterSingletonServiceRegistration, ServiceInfo>> it = services.entrySet().iterator();
562 while (it.hasNext()) {
563 final Entry<ClusterSingletonServiceRegistration, ServiceInfo> entry = it.next();
564 final ClusterSingletonServiceRegistration reg = entry.getKey();
565 if (!localConfig.contains(reg)) {
566 final ServiceInfo newInfo = ensureStopping(reg, entry.getValue());
567 if (newInfo != null) {
568 entry.setValue(newInfo);
575 // Now make sure member services are being juggled around
576 for (ClusterSingletonServiceRegistration reg : localConfig) {
577 if (!services.containsKey(reg)) {
578 final ClusterSingletonService service = reg.getInstance();
579 LOG.debug("Starting service {}", service);
582 service.instantiateServiceInstance();
583 } catch (Exception e) {
584 LOG.warn("Service group {} service {} failed to start, attempting to continue", identifier, service,
589 services.put(reg, ServiceInfo.started());
594 // Has to be called with lock asserted
595 private void ensureServicesStopping() {
596 final Iterator<Entry<ClusterSingletonServiceRegistration, ServiceInfo>> it = services.entrySet().iterator();
597 while (it.hasNext()) {
598 final Entry<ClusterSingletonServiceRegistration, ServiceInfo> entry = it.next();
599 final ServiceInfo newInfo = ensureStopping(entry.getKey(), entry.getValue());
600 if (newInfo != null) {
601 entry.setValue(newInfo);
608 @SuppressWarnings("illegalCatch")
609 private ServiceInfo ensureStopping(final ClusterSingletonServiceRegistration reg, final ServiceInfo info) {
610 switch (info.getState()) {
612 final ClusterSingletonService service = reg.getInstance();
614 LOG.debug("Service group {} stopping service {}", identifier, service);
615 final @NonNull ListenableFuture<?> future;
617 future = verifyNotNull(service.closeServiceInstance());
618 } catch (Exception e) {
619 LOG.warn("Service group {} service {} failed to stop, attempting to continue", identifier, service,
624 Futures.addCallback(future, new FutureCallback<Object>() {
626 public void onSuccess(final Object result) {
627 LOG.debug("Service group {} service {} stopped successfully", identifier, service);
628 serviceTransitionCompleted();
632 public void onFailure(final Throwable cause) {
633 LOG.debug("Service group {} service {} stopped with error", identifier, service, cause);
634 serviceTransitionCompleted();
636 }, MoreExecutors.directExecutor());
637 return info.toState(ServiceState.STOPPING, future);
639 if (info.getFuture().isDone()) {
640 LOG.debug("Service group {} removed stopped service {}", identifier, reg.getInstance());
645 throw new IllegalStateException("Unhandled state " + info.getState());
649 private void markDirty() {
653 private boolean isDirty() {
657 private boolean conditionalClean() {
658 return DIRTY_UPDATER.compareAndSet(this, 1, 0);
661 private boolean tryLock() {
662 return LOCK_UPDATER.compareAndSet(this, 0, 1);
665 private boolean unlock() {
666 verify(LOCK_UPDATER.compareAndSet(this, 1, 0));
671 public String toString() {
672 return MoreObjects.toStringHelper(this).add("identifier", identifier).toString();