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.controller.cluster.access.client;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
14 import java.util.concurrent.ConcurrentHashMap;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
20 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.RequestException;
22 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
23 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
24 import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
25 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
26 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.concepts.WritableIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * A behavior, which handles messages sent to a {@link AbstractClientActor}.
36 * @author Robert Varga
39 public abstract class ClientActorBehavior<T extends BackendInfo> extends
40 RecoveredClientActorBehavior<ClientActorContext> implements Identifiable<ClientIdentifier> {
42 * Connection reconnect cohort, driven by this class.
45 protected interface ConnectionConnectCohort {
47 * Finish the connection by replaying previous messages onto the new connection.
49 * @param enqueuedEntries Previously-enqueued entries
50 * @return A {@link ReconnectForwarder} to handle any straggler messages which arrive after this method returns.
52 @Nonnull ReconnectForwarder finishReconnect(@Nonnull Iterable<ConnectionEntry> enqueuedEntries);
55 private static final Logger LOG = LoggerFactory.getLogger(ClientActorBehavior.class);
58 * Map of connections to the backend. This map is concurrent to allow lookups, but given complex operations
59 * involved in connection transitions it is protected by a {@link InversibleLock}. Write-side of the lock is taken
60 * during connection transitions. Optimistic read-side of the lock is taken when new connections are introduced
64 * The lock detects potential AB/BA deadlock scenarios and will force the reader side out by throwing
65 * a {@link InversibleLockException} -- which must be propagated up, releasing locks as it propagates. The initial
66 * entry point causing the the conflicting lookup must then call {@link InversibleLockException#awaitResolution()}
67 * before retrying the operation.
69 // TODO: it should be possible to move these two into ClientActorContext
70 private final Map<Long, AbstractClientConnection<T>> connections = new ConcurrentHashMap<>();
71 private final InversibleLock connectionsLock = new InversibleLock();
72 private final BackendInfoResolver<T> resolver;
74 protected ClientActorBehavior(@Nonnull final ClientActorContext context,
75 @Nonnull final BackendInfoResolver<T> resolver) {
77 this.resolver = Preconditions.checkNotNull(resolver);
82 public final ClientIdentifier getIdentifier() {
83 return context().getIdentifier();
87 * Get a connection to a shard.
89 * @param shard Shard cookie
90 * @return Connection to a shard
91 * @throws InversibleLockException if the shard is being reconnected
93 public final AbstractClientConnection<T> getConnection(final Long shard) {
95 final long stamp = connectionsLock.optimisticRead();
96 final AbstractClientConnection<T> conn = connections.computeIfAbsent(shard, this::createConnection);
97 if (connectionsLock.validate(stamp)) {
98 // No write-lock in-between, return success
104 @SuppressWarnings("unchecked")
106 final ClientActorBehavior<T> onReceiveCommand(final Object command) {
107 if (command instanceof InternalCommand) {
108 return ((InternalCommand<T>) command).execute(this);
110 if (command instanceof SuccessEnvelope) {
111 return onRequestSuccess((SuccessEnvelope) command);
113 if (command instanceof FailureEnvelope) {
114 return internalOnRequestFailure((FailureEnvelope) command);
117 return onCommand(command);
120 private static long extractCookie(final WritableIdentifier id) {
121 if (id instanceof TransactionIdentifier) {
122 return ((TransactionIdentifier) id).getHistoryId().getCookie();
123 } else if (id instanceof LocalHistoryIdentifier) {
124 return ((LocalHistoryIdentifier) id).getCookie();
126 throw new IllegalArgumentException("Unhandled identifier " + id);
130 private void onResponse(final ResponseEnvelope<?> response) {
131 final long cookie = extractCookie(response.getMessage().getTarget());
132 final AbstractClientConnection<T> connection = connections.get(cookie);
133 if (connection != null) {
134 connection.receiveResponse(response);
136 LOG.info("{}: Ignoring unknown response {}", persistenceId(), response);
140 private ClientActorBehavior<T> onRequestSuccess(final SuccessEnvelope success) {
145 private ClientActorBehavior<T> onRequestFailure(final FailureEnvelope failure) {
150 private ClientActorBehavior<T> internalOnRequestFailure(final FailureEnvelope command) {
151 final RequestFailure<?, ?> failure = command.getMessage();
152 final RequestException cause = failure.getCause();
153 if (cause instanceof RetiredGenerationException) {
154 LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
160 return onRequestFailure(command);
163 private void poison(final RequestException cause) {
164 final long stamp = connectionsLock.writeLock();
166 for (AbstractClientConnection<T> q : connections.values()) {
172 connectionsLock.unlockWrite(stamp);
177 * Halt And Catch Fire. Halt processing on this client. Implementations need to ensure they initiate state flush
178 * procedures. No attempt to use this instance should be made after this method returns. Any such use may result
179 * in undefined behavior.
181 * @param cause Failure cause
183 protected abstract void haltClient(@Nonnull Throwable cause);
186 * Override this method to handle any command which is not handled by the base behavior.
188 * @param command the command to process
189 * @return Next behavior to use, null if this actor should shut down.
192 protected abstract ClientActorBehavior<T> onCommand(@Nonnull Object command);
195 * Override this method to provide a backend resolver instance.
197 * @return a backend resolver instance
199 protected final @Nonnull BackendInfoResolver<T> resolver() {
204 * Callback invoked when a new connection has been established. Implementations are expected perform preparatory
205 * tasks before the previous connection is frozen.
207 * @param newConn New connection
208 * @return ConnectionConnectCohort which will be used to complete the process of bringing the connection up.
210 @GuardedBy("connectionsLock")
211 @Nonnull protected abstract ConnectionConnectCohort connectionUp(@Nonnull ConnectedClientConnection<T> newConn);
213 private void backendConnectFinished(final Long shard, final AbstractClientConnection<T> conn,
214 final T backend, final Throwable failure) {
215 if (failure != null) {
216 LOG.error("{}: failed to resolve shard {}", persistenceId(), shard, failure);
217 conn.poison(new RuntimeRequestException("Failed to resolve shard " + shard, failure));
221 LOG.debug("{}: resolved shard {} to {}", persistenceId(), shard, backend);
222 final long stamp = connectionsLock.writeLock();
224 // Create a new connected connection
225 final ConnectedClientConnection<T> newConn = new ConnectedClientConnection<>(conn.context(),
226 conn.cookie(), backend);
227 LOG.debug("{}: resolving connection {} to {}", persistenceId(), conn, newConn);
229 // Start reconnecting without the old connection lock held
230 final ConnectionConnectCohort cohort = Verify.verifyNotNull(connectionUp(newConn));
232 // Lock the old connection and get a reference to its entries
233 final Iterable<ConnectionEntry> replayIterable = conn.startReplay();
235 // Finish the connection attempt
236 final ReconnectForwarder forwarder = Verify.verifyNotNull(cohort.finishReconnect(replayIterable));
238 // Install the forwarder, unlocking the old connection
239 conn.finishReplay(forwarder);
241 // Make sure new lookups pick up the new connection
242 connections.replace(shard, conn, newConn);
243 LOG.debug("{}: replaced connection {} with {}", persistenceId(), conn, newConn);
245 connectionsLock.unlockWrite(stamp);
249 void removeConnection(final AbstractClientConnection<?> conn) {
250 connections.remove(conn.cookie(), conn);
251 LOG.debug("{}: removed connection {}", persistenceId(), conn);
254 @SuppressWarnings("unchecked")
255 void reconnectConnection(final ConnectedClientConnection<?> oldConn,
256 final ReconnectingClientConnection<?> newConn) {
257 final ReconnectingClientConnection<T> conn = (ReconnectingClientConnection<T>)newConn;
258 connections.replace(oldConn.cookie(), (AbstractClientConnection<T>)oldConn, conn);
259 LOG.debug("{}: connection {} reconnecting as {}", persistenceId(), oldConn, newConn);
261 final Long shard = oldConn.cookie();
262 resolver().refreshBackendInfo(shard, conn.getBackendInfo().get()).whenComplete(
263 (backend, failure) -> context().executeInActor(behavior -> {
264 backendConnectFinished(shard, conn, backend, failure);
269 private ConnectingClientConnection<T> createConnection(final Long shard) {
270 final ConnectingClientConnection<T> conn = new ConnectingClientConnection<>(context(), shard);
272 resolver().getBackendInfo(shard).whenComplete((backend, failure) -> context().executeInActor(behavior -> {
273 backendConnectFinished(shard, conn, backend, failure);