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.Stopwatch;
13 import com.google.common.base.Verify;
14 import java.util.Collection;
16 import java.util.Optional;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.GuardedBy;
23 import org.opendaylight.controller.cluster.access.commands.NotLeaderException;
24 import org.opendaylight.controller.cluster.access.commands.OutOfSequenceEnvelopeException;
25 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
26 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
27 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
28 import org.opendaylight.controller.cluster.access.concepts.RequestException;
29 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
30 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
31 import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
32 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
33 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
34 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
35 import org.opendaylight.controller.cluster.common.actor.Dispatchers.DispatcherType;
36 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
37 import org.opendaylight.controller.cluster.messaging.MessageAssembler;
38 import org.opendaylight.yangtools.concepts.Identifiable;
39 import org.opendaylight.yangtools.concepts.Identifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import scala.concurrent.duration.FiniteDuration;
45 * A behavior, which handles messages sent to a {@link AbstractClientActor}.
47 * @author Robert Varga
50 public abstract class ClientActorBehavior<T extends BackendInfo> extends
51 RecoveredClientActorBehavior<ClientActorContext> implements Identifiable<ClientIdentifier> {
53 * Connection reconnect cohort, driven by this class.
56 protected interface ConnectionConnectCohort {
58 * Finish the connection by replaying previous messages onto the new connection.
60 * @param enqueuedEntries Previously-enqueued entries
61 * @return A {@link ReconnectForwarder} to handle any straggler messages which arrive after this method returns.
63 @Nonnull ReconnectForwarder finishReconnect(@Nonnull Collection<ConnectionEntry> enqueuedEntries);
66 private static final Logger LOG = LoggerFactory.getLogger(ClientActorBehavior.class);
67 private static final FiniteDuration RESOLVE_RETRY_DURATION = FiniteDuration.apply(1, TimeUnit.SECONDS);
70 * Map of connections to the backend. This map is concurrent to allow lookups, but given complex operations
71 * involved in connection transitions it is protected by a {@link InversibleLock}. Write-side of the lock is taken
72 * during connection transitions. Optimistic read-side of the lock is taken when new connections are introduced
76 * The lock detects potential AB/BA deadlock scenarios and will force the reader side out by throwing
77 * a {@link InversibleLockException} -- which must be propagated up, releasing locks as it propagates. The initial
78 * entry point causing the the conflicting lookup must then call {@link InversibleLockException#awaitResolution()}
79 * before retrying the operation.
81 // TODO: it should be possible to move these two into ClientActorContext
82 private final Map<Long, AbstractClientConnection<T>> connections = new ConcurrentHashMap<>();
83 private final InversibleLock connectionsLock = new InversibleLock();
84 private final BackendInfoResolver<T> resolver;
85 private final MessageAssembler responseMessageAssembler;
87 protected ClientActorBehavior(@Nonnull final ClientActorContext context,
88 @Nonnull final BackendInfoResolver<T> resolver) {
90 this.resolver = Preconditions.checkNotNull(resolver);
92 final ClientActorConfig config = context.config();
93 responseMessageAssembler = MessageAssembler.builder().logContext(persistenceId())
94 .fileBackedStreamFactory(new FileBackedOutputStreamFactory(config.getFileBackedStreamingThreshold(),
95 config.getTempFileDirectory()))
96 .assembledMessageCallback((message, sender) -> context.self().tell(message, sender)).build();
101 public final ClientIdentifier getIdentifier() {
102 return context().getIdentifier();
106 public void close() {
107 responseMessageAssembler.close();
111 * Get a connection to a shard.
113 * @param shard Shard cookie
114 * @return Connection to a shard
115 * @throws InversibleLockException if the shard is being reconnected
117 public final AbstractClientConnection<T> getConnection(final Long shard) {
119 final long stamp = connectionsLock.optimisticRead();
120 final AbstractClientConnection<T> conn = connections.computeIfAbsent(shard, this::createConnection);
121 if (connectionsLock.validate(stamp)) {
122 // No write-lock in-between, return success
128 private AbstractClientConnection<T> getConnection(final ResponseEnvelope<?> response) {
129 // Always called from actor context: no locking required
130 return connections.get(extractCookie(response.getMessage().getTarget()));
133 @SuppressWarnings("unchecked")
135 final ClientActorBehavior<T> onReceiveCommand(final Object command) {
136 if (command instanceof InternalCommand) {
137 return ((InternalCommand<T>) command).execute(this);
140 if (command instanceof SuccessEnvelope) {
141 return onRequestSuccess((SuccessEnvelope) command);
144 if (command instanceof FailureEnvelope) {
145 return internalOnRequestFailure((FailureEnvelope) command);
148 if (MessageAssembler.isHandledMessage(command)) {
149 context().dispatchers().getDispatcher(DispatcherType.Serialization).execute(
150 () -> responseMessageAssembler.handleMessage(command, context().self()));
154 if (context().messageSlicer().handleMessage(command)) {
158 return onCommand(command);
161 private static long extractCookie(final Identifier id) {
162 if (id instanceof TransactionIdentifier) {
163 return ((TransactionIdentifier) id).getHistoryId().getCookie();
164 } else if (id instanceof LocalHistoryIdentifier) {
165 return ((LocalHistoryIdentifier) id).getCookie();
167 throw new IllegalArgumentException("Unhandled identifier " + id);
171 private void onResponse(final ResponseEnvelope<?> response) {
172 final AbstractClientConnection<T> connection = getConnection(response);
173 if (connection != null) {
174 connection.receiveResponse(response);
176 LOG.info("{}: Ignoring unknown response {}", persistenceId(), response);
180 private ClientActorBehavior<T> onRequestSuccess(final SuccessEnvelope success) {
185 private ClientActorBehavior<T> onRequestFailure(final FailureEnvelope failure) {
190 private ClientActorBehavior<T> internalOnRequestFailure(final FailureEnvelope command) {
191 final AbstractClientConnection<T> conn = getConnection(command);
194 * We are talking to multiple actors, which may be lagging behind our state significantly. This has
195 * the effect that we may be receiving responses from a previous connection after we have created a new
196 * one to a different actor.
198 * Since we are already replaying requests to the new actor, we want to ignore errors reported on the old
199 * connection -- for example NotLeaderException, which must not cause a new reconnect. Check the envelope's
200 * sessionId and if it does not match our current connection just ignore it.
202 final Optional<T> optBackend = conn.getBackendInfo();
203 if (optBackend.isPresent() && optBackend.get().getSessionId() != command.getSessionId()) {
204 LOG.debug("{}: Mismatched current connection {} and envelope {}, ignoring response", persistenceId(),
210 final RequestFailure<?, ?> failure = command.getMessage();
211 final RequestException cause = failure.getCause();
212 if (cause instanceof RetiredGenerationException) {
213 LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
218 if (cause instanceof NotLeaderException) {
219 if (conn instanceof ReconnectingClientConnection) {
220 // Already reconnecting, do not churn the logs
222 } else if (conn != null) {
223 LOG.info("{}: connection {} indicated no leadership, reconnecting it", persistenceId(), conn, cause);
224 return conn.reconnect(this, cause);
227 if (cause instanceof OutOfSequenceEnvelopeException) {
228 if (conn instanceof ReconnectingClientConnection) {
229 // Already reconnecting, do not churn the logs
231 } else if (conn != null) {
232 LOG.info("{}: connection {} indicated sequencing mismatch on {} sequence {} ({}), reconnecting it",
233 persistenceId(), conn, failure.getTarget(), failure.getSequence(), command.getTxSequence(), cause);
234 return conn.reconnect(this, cause);
238 return onRequestFailure(command);
241 private void poison(final RequestException cause) {
242 final long stamp = connectionsLock.writeLock();
244 for (AbstractClientConnection<T> q : connections.values()) {
250 connectionsLock.unlockWrite(stamp);
253 context().messageSlicer().close();
257 * Halt And Catch Fire. Halt processing on this client. Implementations need to ensure they initiate state flush
258 * procedures. No attempt to use this instance should be made after this method returns. Any such use may result
259 * in undefined behavior.
261 * @param cause Failure cause
263 protected abstract void haltClient(@Nonnull Throwable cause);
266 * Override this method to handle any command which is not handled by the base behavior.
268 * @param command the command to process
269 * @return Next behavior to use, null if this actor should shut down.
272 protected abstract ClientActorBehavior<T> onCommand(@Nonnull Object command);
275 * Override this method to provide a backend resolver instance.
277 * @return a backend resolver instance
279 protected final @Nonnull BackendInfoResolver<T> resolver() {
284 * Callback invoked when a new connection has been established. Implementations are expected perform preparatory
285 * tasks before the previous connection is frozen.
287 * @param newConn New connection
288 * @return ConnectionConnectCohort which will be used to complete the process of bringing the connection up.
290 @GuardedBy("connectionsLock")
291 @Nonnull protected abstract ConnectionConnectCohort connectionUp(@Nonnull ConnectedClientConnection<T> newConn);
293 private void backendConnectFinished(final Long shard, final AbstractClientConnection<T> oldConn,
294 final T backend, final Throwable failure) {
295 if (failure != null) {
296 if (failure instanceof TimeoutException) {
297 if (!oldConn.equals(connections.get(shard))) {
298 // AbstractClientConnection will remove itself when it decides there is no point in continuing,
299 // at which point we want to stop retrying
300 LOG.info("{}: stopping resolution of shard {} on stale connection {}", persistenceId(), shard,
305 LOG.debug("{}: timed out resolving shard {}, scheduling retry in {}", persistenceId(), shard,
306 RESOLVE_RETRY_DURATION, failure);
307 context().executeInActor(b -> {
308 resolveConnection(shard, oldConn);
310 }, RESOLVE_RETRY_DURATION);
314 LOG.error("{}: failed to resolve shard {}", persistenceId(), shard, failure);
315 final RequestException cause;
316 if (failure instanceof RequestException) {
317 cause = (RequestException) failure;
319 cause = new RuntimeRequestException("Failed to resolve shard " + shard, failure);
322 oldConn.poison(cause);
326 LOG.info("{}: resolved shard {} to {}", persistenceId(), shard, backend);
327 final long stamp = connectionsLock.writeLock();
329 final Stopwatch sw = Stopwatch.createStarted();
331 // Create a new connected connection
332 final ConnectedClientConnection<T> newConn = new ConnectedClientConnection<>(oldConn, backend);
333 LOG.info("{}: resolving connection {} to {}", persistenceId(), oldConn, newConn);
335 // Start reconnecting without the old connection lock held
336 final ConnectionConnectCohort cohort = Verify.verifyNotNull(connectionUp(newConn));
338 // Lock the old connection and get a reference to its entries
339 final Collection<ConnectionEntry> replayIterable = oldConn.startReplay();
341 // Finish the connection attempt
342 final ReconnectForwarder forwarder = Verify.verifyNotNull(cohort.finishReconnect(replayIterable));
344 // Cancel sleep debt after entries were replayed, before new connection starts receiving.
345 newConn.cancelDebt();
347 // Install the forwarder, unlocking the old connection
348 oldConn.finishReplay(forwarder);
350 // Make sure new lookups pick up the new connection
351 if (!connections.replace(shard, oldConn, newConn)) {
352 final AbstractClientConnection<T> existing = connections.get(oldConn.cookie());
353 LOG.warn("{}: old connection {} does not match existing {}, new connection {} in limbo",
354 persistenceId(), oldConn, existing, newConn);
356 LOG.info("{}: replaced connection {} with {} in {}", persistenceId(), oldConn, newConn, sw);
359 connectionsLock.unlockWrite(stamp);
363 void removeConnection(final AbstractClientConnection<?> conn) {
364 final long stamp = connectionsLock.writeLock();
366 if (!connections.remove(conn.cookie(), conn)) {
367 final AbstractClientConnection<T> existing = connections.get(conn.cookie());
368 if (existing != null) {
369 LOG.warn("{}: failed to remove connection {}, as it was superseded by {}", persistenceId(), conn,
372 LOG.warn("{}: failed to remove connection {}, as it was not tracked", persistenceId(), conn);
375 LOG.info("{}: removed connection {}", persistenceId(), conn);
376 cancelSlicing(conn.cookie());
379 connectionsLock.unlockWrite(stamp);
383 @SuppressWarnings("unchecked")
384 void reconnectConnection(final ConnectedClientConnection<?> oldConn,
385 final ReconnectingClientConnection<?> newConn) {
386 final ReconnectingClientConnection<T> conn = (ReconnectingClientConnection<T>)newConn;
387 LOG.info("{}: connection {} reconnecting as {}", persistenceId(), oldConn, newConn);
389 final long stamp = connectionsLock.writeLock();
391 final boolean replaced = connections.replace(oldConn.cookie(), (AbstractClientConnection<T>)oldConn, conn);
393 final AbstractClientConnection<T> existing = connections.get(oldConn.cookie());
394 if (existing != null) {
395 LOG.warn("{}: failed to replace connection {}, as it was superseded by {}", persistenceId(), conn,
398 LOG.warn("{}: failed to replace connection {}, as it was not tracked", persistenceId(), conn);
401 cancelSlicing(oldConn.cookie());
404 connectionsLock.unlockWrite(stamp);
407 final Long shard = oldConn.cookie();
408 LOG.info("{}: refreshing backend for shard {}", persistenceId(), shard);
409 resolver().refreshBackendInfo(shard, conn.getBackendInfo().get()).whenComplete(
410 (backend, failure) -> context().executeInActor(behavior -> {
411 backendConnectFinished(shard, conn, backend, failure);
416 private void cancelSlicing(final Long cookie) {
417 context().messageSlicer().cancelSlicing(id -> {
419 return cookie.equals(extractCookie(id));
420 } catch (IllegalArgumentException e) {
421 LOG.debug("extractCookie failed while cancelling slicing for cookie {}: {}", cookie, e);
427 private ConnectingClientConnection<T> createConnection(final Long shard) {
428 final ConnectingClientConnection<T> conn = new ConnectingClientConnection<>(context(), shard);
429 resolveConnection(shard, conn);
433 private void resolveConnection(final Long shard, final AbstractClientConnection<T> conn) {
434 LOG.debug("{}: resolving shard {} connection {}", persistenceId(), shard, conn);
435 resolver().getBackendInfo(shard).whenComplete((backend, failure) -> context().executeInActor(behavior -> {
436 backendConnectFinished(shard, conn, backend, failure);