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.databroker.actors.dds;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.concurrent.CompletionStage;
14 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
15 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Unlike the full
22 * {@link ModuleShardBackendResolver}, this resolver is used in situations where the client corresponds exactly to one
23 * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all. This
24 * class is thread-safe.
26 * @author Robert Varga
28 final class SimpleShardBackendResolver extends AbstractShardBackendResolver {
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardBackendResolver.class);
31 private final String shardName;
33 private volatile ShardState state;
35 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
36 SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils,
37 final String shardName) {
38 super(clientId, actorUtils);
39 this.shardName = requireNonNull(shardName);
42 private CompletionStage<ShardBackendInfo> getBackendInfo(final long cookie) {
43 checkArgument(cookie == 0);
45 final ShardState existing = state;
46 if (existing != null) {
47 return existing.getStage();
51 final ShardState recheck = state;
52 if (recheck != null) {
53 return recheck.getStage();
56 final ShardState newState = resolveBackendInfo(shardName, 0);
59 final CompletionStage<ShardBackendInfo> stage = newState.getStage();
60 stage.whenComplete((info, failure) -> {
61 if (failure != null) {
62 synchronized (SimpleShardBackendResolver.this) {
63 if (state == newState) {
75 public CompletionStage<ShardBackendInfo> getBackendInfo(final Long cookie) {
76 return getBackendInfo(cookie.longValue());
80 public CompletionStage<? extends ShardBackendInfo> refreshBackendInfo(final Long cookie,
81 final ShardBackendInfo staleInfo) {
83 final ShardState existing = state;
84 if (existing != null) {
85 if (!staleInfo.equals(existing.getResult())) {
86 return existing.getStage();
90 LOG.debug("Invalidating backend information {}", staleInfo);
91 flushCache(shardName);
92 LOG.trace("Invalidated cache {}", staleInfo);
97 return getBackendInfo(cookie);
101 public String resolveCookieName(Long cookie) {