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 java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.FailureEnvelope;
22 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
23 import org.opendaylight.controller.cluster.access.concepts.RequestException;
24 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
25 import org.opendaylight.controller.cluster.access.concepts.ResponseEnvelope;
26 import org.opendaylight.controller.cluster.access.concepts.RetiredGenerationException;
27 import org.opendaylight.controller.cluster.access.concepts.RuntimeRequestException;
28 import org.opendaylight.controller.cluster.access.concepts.SuccessEnvelope;
29 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
30 import org.opendaylight.yangtools.concepts.Identifiable;
31 import org.opendaylight.yangtools.concepts.WritableIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import scala.concurrent.duration.FiniteDuration;
37 * A behavior, which handles messages sent to a {@link AbstractClientActor}.
39 * @author Robert Varga
42 public abstract class ClientActorBehavior<T extends BackendInfo> extends
43 RecoveredClientActorBehavior<ClientActorContext> implements Identifiable<ClientIdentifier> {
45 * Connection reconnect cohort, driven by this class.
48 protected interface ConnectionConnectCohort {
50 * Finish the connection by replaying previous messages onto the new connection.
52 * @param enqueuedEntries Previously-enqueued entries
53 * @return A {@link ReconnectForwarder} to handle any straggler messages which arrive after this method returns.
55 @Nonnull ReconnectForwarder finishReconnect(@Nonnull Iterable<ConnectionEntry> enqueuedEntries);
58 private static final Logger LOG = LoggerFactory.getLogger(ClientActorBehavior.class);
59 private static final FiniteDuration RESOLVE_RETRY_DURATION = FiniteDuration.apply(5, TimeUnit.SECONDS);
62 * Map of connections to the backend. This map is concurrent to allow lookups, but given complex operations
63 * involved in connection transitions it is protected by a {@link InversibleLock}. Write-side of the lock is taken
64 * during connection transitions. Optimistic read-side of the lock is taken when new connections are introduced
68 * The lock detects potential AB/BA deadlock scenarios and will force the reader side out by throwing
69 * a {@link InversibleLockException} -- which must be propagated up, releasing locks as it propagates. The initial
70 * entry point causing the the conflicting lookup must then call {@link InversibleLockException#awaitResolution()}
71 * before retrying the operation.
73 // TODO: it should be possible to move these two into ClientActorContext
74 private final Map<Long, AbstractClientConnection<T>> connections = new ConcurrentHashMap<>();
75 private final InversibleLock connectionsLock = new InversibleLock();
76 private final BackendInfoResolver<T> resolver;
78 protected ClientActorBehavior(@Nonnull final ClientActorContext context,
79 @Nonnull final BackendInfoResolver<T> resolver) {
81 this.resolver = Preconditions.checkNotNull(resolver);
86 public final ClientIdentifier getIdentifier() {
87 return context().getIdentifier();
91 * Get a connection to a shard.
93 * @param shard Shard cookie
94 * @return Connection to a shard
95 * @throws InversibleLockException if the shard is being reconnected
97 public final AbstractClientConnection<T> getConnection(final Long shard) {
99 final long stamp = connectionsLock.optimisticRead();
100 final AbstractClientConnection<T> conn = connections.computeIfAbsent(shard, this::createConnection);
101 if (connectionsLock.validate(stamp)) {
102 // No write-lock in-between, return success
108 @SuppressWarnings("unchecked")
110 final ClientActorBehavior<T> onReceiveCommand(final Object command) {
111 if (command instanceof InternalCommand) {
112 return ((InternalCommand<T>) command).execute(this);
114 if (command instanceof SuccessEnvelope) {
115 return onRequestSuccess((SuccessEnvelope) command);
117 if (command instanceof FailureEnvelope) {
118 return internalOnRequestFailure((FailureEnvelope) command);
121 return onCommand(command);
124 private static long extractCookie(final WritableIdentifier id) {
125 if (id instanceof TransactionIdentifier) {
126 return ((TransactionIdentifier) id).getHistoryId().getCookie();
127 } else if (id instanceof LocalHistoryIdentifier) {
128 return ((LocalHistoryIdentifier) id).getCookie();
130 throw new IllegalArgumentException("Unhandled identifier " + id);
134 private void onResponse(final ResponseEnvelope<?> response) {
135 final long cookie = extractCookie(response.getMessage().getTarget());
136 final AbstractClientConnection<T> connection = connections.get(cookie);
137 if (connection != null) {
138 connection.receiveResponse(response);
140 LOG.info("{}: Ignoring unknown response {}", persistenceId(), response);
144 private ClientActorBehavior<T> onRequestSuccess(final SuccessEnvelope success) {
149 private ClientActorBehavior<T> onRequestFailure(final FailureEnvelope failure) {
154 private ClientActorBehavior<T> internalOnRequestFailure(final FailureEnvelope command) {
155 final RequestFailure<?, ?> failure = command.getMessage();
156 final RequestException cause = failure.getCause();
157 if (cause instanceof RetiredGenerationException) {
158 LOG.error("{}: current generation {} has been superseded", persistenceId(), getIdentifier(), cause);
164 return onRequestFailure(command);
167 private void poison(final RequestException cause) {
168 final long stamp = connectionsLock.writeLock();
170 for (AbstractClientConnection<T> q : connections.values()) {
176 connectionsLock.unlockWrite(stamp);
181 * Halt And Catch Fire. Halt processing on this client. Implementations need to ensure they initiate state flush
182 * procedures. No attempt to use this instance should be made after this method returns. Any such use may result
183 * in undefined behavior.
185 * @param cause Failure cause
187 protected abstract void haltClient(@Nonnull Throwable cause);
190 * Override this method to handle any command which is not handled by the base behavior.
192 * @param command the command to process
193 * @return Next behavior to use, null if this actor should shut down.
196 protected abstract ClientActorBehavior<T> onCommand(@Nonnull Object command);
199 * Override this method to provide a backend resolver instance.
201 * @return a backend resolver instance
203 protected final @Nonnull BackendInfoResolver<T> resolver() {
208 * Callback invoked when a new connection has been established. Implementations are expected perform preparatory
209 * tasks before the previous connection is frozen.
211 * @param newConn New connection
212 * @return ConnectionConnectCohort which will be used to complete the process of bringing the connection up.
214 @GuardedBy("connectionsLock")
215 @Nonnull protected abstract ConnectionConnectCohort connectionUp(@Nonnull ConnectedClientConnection<T> newConn);
217 private void backendConnectFinished(final Long shard, final AbstractClientConnection<T> conn,
218 final T backend, final Throwable failure) {
219 if (failure != null) {
220 if (failure instanceof TimeoutException) {
221 if (!conn.equals(connections.get(shard))) {
222 // AbstractClientConnection will remove itself when it decides there is no point in continuing,
223 // at which point we want to stop retrying
224 LOG.info("{}: stopping resolution of shard {} on stale connection {}", persistenceId(), shard, conn,
229 LOG.debug("{}: timed out resolving shard {}, scheduling retry in {}", persistenceId(), shard,
230 RESOLVE_RETRY_DURATION, failure);
231 context().executeInActor(b -> {
232 resolveConnection(shard, conn);
234 }, RESOLVE_RETRY_DURATION);
238 LOG.error("{}: failed to resolve shard {}", persistenceId(), shard, failure);
239 final RequestException cause;
240 if (failure instanceof RequestException) {
241 cause = (RequestException) failure;
243 cause = new RuntimeRequestException("Failed to resolve shard " + shard, failure);
250 LOG.debug("{}: resolved shard {} to {}", persistenceId(), shard, backend);
251 final long stamp = connectionsLock.writeLock();
253 // Create a new connected connection
254 final ConnectedClientConnection<T> newConn = new ConnectedClientConnection<>(conn.context(),
255 conn.cookie(), backend);
256 LOG.debug("{}: resolving connection {} to {}", persistenceId(), conn, newConn);
258 // Start reconnecting without the old connection lock held
259 final ConnectionConnectCohort cohort = Verify.verifyNotNull(connectionUp(newConn));
261 // Lock the old connection and get a reference to its entries
262 final Iterable<ConnectionEntry> replayIterable = conn.startReplay();
264 // Finish the connection attempt
265 final ReconnectForwarder forwarder = Verify.verifyNotNull(cohort.finishReconnect(replayIterable));
267 // Install the forwarder, unlocking the old connection
268 conn.finishReplay(forwarder);
270 // Make sure new lookups pick up the new connection
271 connections.replace(shard, conn, newConn);
272 LOG.debug("{}: replaced connection {} with {}", persistenceId(), conn, newConn);
274 connectionsLock.unlockWrite(stamp);
278 void removeConnection(final AbstractClientConnection<?> conn) {
279 connections.remove(conn.cookie(), conn);
280 LOG.debug("{}: removed connection {}", persistenceId(), conn);
283 @SuppressWarnings("unchecked")
284 void reconnectConnection(final ConnectedClientConnection<?> oldConn,
285 final ReconnectingClientConnection<?> newConn) {
286 final ReconnectingClientConnection<T> conn = (ReconnectingClientConnection<T>)newConn;
287 connections.replace(oldConn.cookie(), (AbstractClientConnection<T>)oldConn, conn);
288 LOG.debug("{}: connection {} reconnecting as {}", persistenceId(), oldConn, newConn);
290 final Long shard = oldConn.cookie();
291 resolver().refreshBackendInfo(shard, conn.getBackendInfo().get()).whenComplete(
292 (backend, failure) -> context().executeInActor(behavior -> {
293 backendConnectFinished(shard, conn, backend, failure);
298 private ConnectingClientConnection<T> createConnection(final Long shard) {
299 final ConnectingClientConnection<T> conn = new ConnectingClientConnection<>(context(), shard);
300 resolveConnection(shard, conn);
304 private void resolveConnection(final Long shard, final AbstractClientConnection<T> conn) {
305 LOG.debug("{}: resolving shard {} connection {}", persistenceId(), shard, conn);
306 resolver().getBackendInfo(shard).whenComplete((backend, failure) -> context().executeInActor(behavior -> {
307 backendConnectFinished(shard, conn, backend, failure);