X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2Factors%2Fdds%2FSimpleShardBackendResolver.java;fp=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2Factors%2Fdds%2FSimpleShardBackendResolver.java;h=056a1ea7e226deb49a6ee12e916769bf7e6dbb0c;hb=32b322afd58f120a78208c939a01422aa224d0cf;hp=0000000000000000000000000000000000000000;hpb=c7846405c83f680660852f299d8051b420b3cddd;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java new file mode 100644 index 0000000000..056a1ea7e2 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/SimpleShardBackendResolver.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.databroker.actors.dds; + +import com.google.common.base.Preconditions; +import java.util.concurrent.CompletionStage; +import javax.annotation.concurrent.ThreadSafe; +import org.opendaylight.controller.cluster.access.client.BackendInfoResolver; +import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; +import org.opendaylight.controller.cluster.datastore.utils.ActorContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link BackendInfoResolver} implementation for static shard configuration based on ShardManager. Unlike the full + * {@link ModuleShardBackendResolver}, this resolver is used in situations where the client corresponds exactly to one + * backend shard, e.g. there is only one fixed cookie assigned and the operation path is not consulted at all. + * + * @author Robert Varga + */ +@ThreadSafe +final class SimpleShardBackendResolver extends AbstractShardBackendResolver { + private static final Logger LOG = LoggerFactory.getLogger(SimpleShardBackendResolver.class); + + private final String shardName; + + private volatile ShardState state; + + // FIXME: we really need just ActorContext.findPrimaryShardAsync() + SimpleShardBackendResolver(final ClientIdentifier clientId, final ActorContext actorContext, + final String shardName) { + super(clientId, actorContext); + this.shardName = Preconditions.checkNotNull(shardName); + } + + private CompletionStage getBackendInfo(final long cookie) { + Preconditions.checkArgument(cookie == 0); + + ShardState local = state; + if (local == null) { + synchronized (this) { + local = state; + if (local == null) { + local = resolveBackendInfo(shardName, 0); + state = local; + } + } + } + + return local.getStage(); + } + + @Override + public CompletionStage getBackendInfo(final Long cookie) { + return getBackendInfo(cookie.longValue()); + } + + @Override + public CompletionStage refreshBackendInfo(final Long cookie, + final ShardBackendInfo staleInfo) { + + final ShardState existing = state; + if (existing != null) { + if (!staleInfo.equals(existing.getResult())) { + return existing.getStage(); + } + + synchronized (this) { + LOG.debug("Invalidating backend information {}", staleInfo); + flushCache(shardName); + LOG.trace("Invalidated cache %s", staleInfo); + state = null; + } + } + + return getBackendInfo(cookie); + } +}