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 javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.controller.cluster.access.client.BackendInfoResolver;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Unlike the full
23 * {@link ModuleShardBackendResolver}, this resolver is used in situations where the client corresponds exactly to one
24 * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all.
26 * @author Robert Varga
29 final class SimpleShardBackendResolver extends AbstractShardBackendResolver {
30 private static final Logger LOG = LoggerFactory.getLogger(SimpleShardBackendResolver.class);
32 private final String shardName;
34 private volatile ShardState state;
36 // FIXME: we really need just ActorContext.findPrimaryShardAsync()
37 SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorUtils actorUtils,
38 final String shardName) {
39 super(clientId, actorUtils);
40 this.shardName = requireNonNull(shardName);
43 private CompletionStage<ShardBackendInfo> getBackendInfo(final long cookie) {
44 checkArgument(cookie == 0);
46 final ShardState existing = state;
47 if (existing != null) {
48 return existing.getStage();
52 final ShardState recheck = state;
53 if (recheck != null) {
54 return recheck.getStage();
57 final ShardState newState = resolveBackendInfo(shardName, 0);
60 final CompletionStage<ShardBackendInfo> stage = newState.getStage();
61 stage.whenComplete((info, failure) -> {
62 if (failure != null) {
63 synchronized (SimpleShardBackendResolver.this) {
64 if (state == newState) {
76 public CompletionStage<ShardBackendInfo> getBackendInfo(final Long cookie) {
77 return getBackendInfo(cookie.longValue());
81 public CompletionStage<? extends ShardBackendInfo> refreshBackendInfo(final Long cookie,
82 final ShardBackendInfo staleInfo) {
84 final ShardState existing = state;
85 if (existing != null) {
86 if (!staleInfo.equals(existing.getResult())) {
87 return existing.getStage();
91 LOG.debug("Invalidating backend information {}", staleInfo);
92 flushCache(shardName);
93 LOG.trace("Invalidated cache {}", staleInfo);
98 return getBackendInfo(cookie);
102 public String resolveCookieName(Long cookie) {