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.datastore.actors.client;
10 import akka.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.CompletionStage;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import javax.annotation.Nonnull;
16 import javax.annotation.concurrent.ThreadSafe;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * Caching resolver which resolves a cookie to a leader {@link ActorRef}. This class needs to be specialized by the
22 * client. It is used by {@link ClientActorBehavior} for request dispatch. Results are cached until they are invalidated
23 * by either the client actor (when a message timeout is detected) and by the specific frontend (on explicit
24 * invalidation or when updated information becomes available).
26 * @author Robert Varga
29 public abstract class BackendInfoResolver<T extends BackendInfo> {
30 private static final Logger LOG = LoggerFactory.getLogger(BackendInfoResolver.class);
31 private final ConcurrentMap<Long, CompletionStage<T>> backends = new ConcurrentHashMap<>();
33 // This is what the client needs to start processing. For as long as we do not have this, we should not complete
34 // this stage until we have this information
35 public final CompletionStage<? extends T> getBackendInfo(final long cookie) {
36 return backends.computeIfAbsent(cookie, this::resolveBackendInfo);
40 * Invalidate a particular instance of {@link BackendInfo}, typically as a response to a request timing out. If
41 * the provided information is not the one currently cached this method does nothing.
43 * @param cookie Backend cookie
44 * @param info Previous information to be invalidated
46 public final void invalidateBackend(final long cookie, final @Nonnull CompletionStage<? extends BackendInfo> info) {
47 if (backends.remove(cookie, Preconditions.checkNotNull(info))) {
48 LOG.trace("Invalidated cache %s -> %s", Long.toUnsignedString(cookie), info);
49 invalidateBackendInfo(info);
54 * Request new resolution of a particular backend identified by a cookie. This method is invoked when a client
55 * requests information which is not currently cached.
57 * @param cookie Backend cookie
58 * @return A {@link CompletionStage} resulting in information about the backend
60 protected abstract @Nonnull CompletionStage<T> resolveBackendInfo(final @Nonnull Long cookie);
63 * Invalidate previously-resolved shard information. This method is invoked when a timeout is detected
64 * and the information may need to be refreshed.
66 * @param info Previous promise of backend information
68 protected abstract void invalidateBackendInfo(@Nonnull CompletionStage<? extends BackendInfo> info);