2 * Copyright (c) 2014 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;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15 import akka.actor.PoisonPill;
16 import akka.actor.Props;
17 import com.google.common.annotations.Beta;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Throwables;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.TimeoutException;
29 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
30 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
31 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
32 import org.opendaylight.controller.cluster.databroker.actors.dds.DistributedDataStoreClientActor;
33 import org.opendaylight.controller.cluster.datastore.config.Configuration;
34 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
35 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
36 import org.opendaylight.controller.cluster.datastore.shardmanager.AbstractShardManagerCreator;
37 import org.opendaylight.controller.cluster.datastore.shardmanager.ShardManagerCreator;
38 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
39 import org.opendaylight.controller.cluster.datastore.utils.PrimaryShardInfoFutureCache;
40 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
42 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohort;
43 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistration;
44 import org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry;
45 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
46 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
50 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
51 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import scala.concurrent.duration.Duration;
57 * Base implementation of a distributed DOMStore.
59 public abstract class AbstractDataStore implements DistributedDataStoreInterface, EffectiveModelContextListener,
60 DatastoreContextPropertiesUpdater.Listener, DOMStoreTreeChangePublisher,
61 DOMDataTreeCommitCohortRegistry, AutoCloseable {
63 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataStore.class);
65 private final SettableFuture<Void> readinessFuture = SettableFuture.create();
66 private final ClientIdentifier identifier;
67 private final DataStoreClient client;
68 private final ActorUtils actorUtils;
70 private AutoCloseable closeable;
71 private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
72 private DatastoreInfoMXBeanImpl datastoreInfoMXBean;
74 @SuppressWarnings("checkstyle:IllegalCatch")
75 @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR", justification = "Testing overrides")
76 protected AbstractDataStore(final ActorSystem actorSystem, final ClusterWrapper cluster,
77 final Configuration configuration, final DatastoreContextFactory datastoreContextFactory,
78 final DatastoreSnapshot restoreFromSnapshot) {
79 requireNonNull(actorSystem, "actorSystem should not be null");
80 requireNonNull(cluster, "cluster should not be null");
81 requireNonNull(configuration, "configuration should not be null");
82 requireNonNull(datastoreContextFactory, "datastoreContextFactory should not be null");
84 String shardManagerId = ShardManagerIdentifier.builder()
85 .type(datastoreContextFactory.getBaseDatastoreContext().getDataStoreName()).build().toString();
87 LOG.info("Creating ShardManager : {}", shardManagerId);
89 String shardDispatcher =
90 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
92 PrimaryShardInfoFutureCache primaryShardInfoCache = new PrimaryShardInfoFutureCache();
94 AbstractShardManagerCreator<?> creator = getShardManagerCreator().cluster(cluster).configuration(configuration)
95 .datastoreContextFactory(datastoreContextFactory)
96 .readinessFuture(readinessFuture)
97 .primaryShardInfoCache(primaryShardInfoCache)
98 .restoreFromSnapshot(restoreFromSnapshot)
99 .distributedDataStore(this);
101 actorUtils = new ActorUtils(actorSystem, createShardManager(actorSystem, creator, shardDispatcher,
102 shardManagerId), cluster, configuration, datastoreContextFactory.getBaseDatastoreContext(),
103 primaryShardInfoCache);
105 final Props clientProps = DistributedDataStoreClientActor.props(cluster.getCurrentMemberName(),
106 datastoreContextFactory.getBaseDatastoreContext().getDataStoreName(), actorUtils);
107 final ActorRef clientActor = actorSystem.actorOf(clientProps);
109 client = DistributedDataStoreClientActor.getDistributedDataStoreClient(clientActor, 30, TimeUnit.SECONDS);
110 } catch (Exception e) {
111 LOG.error("Failed to get actor for {}", clientProps, e);
112 clientActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
113 Throwables.throwIfUnchecked(e);
114 throw new RuntimeException(e);
117 identifier = client.getIdentifier();
118 LOG.debug("Distributed data store client {} started", identifier);
120 datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(
121 datastoreContextFactory.getBaseDatastoreContext().getDataStoreMXBeanType());
122 datastoreConfigMXBean.setContext(datastoreContextFactory.getBaseDatastoreContext());
123 datastoreConfigMXBean.registerMBean();
125 datastoreInfoMXBean = new DatastoreInfoMXBeanImpl(datastoreContextFactory.getBaseDatastoreContext()
126 .getDataStoreMXBeanType(), actorUtils);
127 datastoreInfoMXBean.registerMBean();
131 protected AbstractDataStore(final ActorUtils actorUtils, final ClientIdentifier identifier) {
132 this.actorUtils = requireNonNull(actorUtils, "actorContext should not be null");
134 this.identifier = requireNonNull(identifier);
138 protected AbstractDataStore(final ActorUtils actorUtils, final ClientIdentifier identifier,
139 final DataStoreClient clientActor) {
140 this.actorUtils = requireNonNull(actorUtils, "actorContext should not be null");
141 client = clientActor;
142 this.identifier = requireNonNull(identifier);
145 protected AbstractShardManagerCreator<?> getShardManagerCreator() {
146 return new ShardManagerCreator();
149 protected final DataStoreClient getClient() {
153 final ClientIdentifier getIdentifier() {
157 public void setCloseable(final AutoCloseable closeable) {
158 this.closeable = closeable;
162 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
163 final YangInstanceIdentifier treeId, final L listener) {
164 requireNonNull(treeId, "treeId should not be null");
165 requireNonNull(listener, "listener should not be null");
168 * We need to potentially deal with multi-shard composition for registration targeting the root of the data
169 * store. If that is the case, we delegate to a more complicated setup invol
171 if (treeId.isEmpty()) {
172 // User is targeting root of the datastore. If there is more than one shard, we have to register with them
173 // all and perform data composition.
174 final Set<String> shardNames = actorUtils.getConfiguration().getAllShardNames();
175 if (shardNames.size() > 1) {
176 checkArgument(listener instanceof ClusteredDOMDataTreeChangeListener,
177 "Cannot listen on root without non-clustered listener %s", listener);
178 return new RootDataTreeChangeListenerProxy<>(actorUtils, listener, shardNames);
182 final String shardName = actorUtils.getShardStrategyFactory().getStrategy(treeId).findShard(treeId);
183 LOG.debug("Registering tree listener: {} for tree: {} shard: {}", listener, treeId, shardName);
185 final DataTreeChangeListenerProxy<L> listenerRegistrationProxy =
186 new DataTreeChangeListenerProxy<>(actorUtils, listener, treeId);
187 listenerRegistrationProxy.init(shardName);
189 return listenerRegistrationProxy;
193 public <C extends DOMDataTreeCommitCohort> DOMDataTreeCommitCohortRegistration<C> registerCommitCohort(
194 final DOMDataTreeIdentifier subtree, final C cohort) {
195 YangInstanceIdentifier treeId = requireNonNull(subtree, "subtree should not be null").getRootIdentifier();
196 requireNonNull(cohort, "listener should not be null");
199 final String shardName = actorUtils.getShardStrategyFactory().getStrategy(treeId).findShard(treeId);
200 LOG.debug("Registering cohort: {} for tree: {} shard: {}", cohort, treeId, shardName);
202 DataTreeCohortRegistrationProxy<C> cohortProxy =
203 new DataTreeCohortRegistrationProxy<>(actorUtils, subtree, cohort);
204 cohortProxy.init(shardName);
209 public void onModelContextUpdated(final EffectiveModelContext newModelContext) {
210 actorUtils.setSchemaContext(newModelContext);
214 public void onDatastoreContextUpdated(final DatastoreContextFactory contextFactory) {
215 LOG.info("DatastoreContext updated for data store {}", actorUtils.getDataStoreName());
217 actorUtils.setDatastoreContext(contextFactory);
218 datastoreConfigMXBean.setContext(contextFactory.getBaseDatastoreContext());
222 @SuppressWarnings("checkstyle:IllegalCatch")
223 public void close() {
224 LOG.info("Closing data store {}", identifier);
226 if (datastoreConfigMXBean != null) {
227 datastoreConfigMXBean.unregisterMBean();
229 if (datastoreInfoMXBean != null) {
230 datastoreInfoMXBean.unregisterMBean();
233 if (closeable != null) {
236 } catch (Exception e) {
237 LOG.debug("Error closing instance", e);
241 actorUtils.shutdown();
243 if (client != null) {
249 public final ActorUtils getActorUtils() {
253 // TODO: consider removing this in favor of awaitReadiness()
255 public void waitTillReady() {
256 LOG.info("Beginning to wait for data store to become ready : {}", identifier);
258 final Duration toWait = initialSettleTime();
260 if (!awaitReadiness(toWait)) {
261 LOG.error("Shard leaders failed to settle in {}, giving up", toWait);
264 } catch (InterruptedException e) {
265 LOG.error("Interrupted while waiting for shards to settle", e);
269 LOG.debug("Data store {} is now ready", identifier);
274 public boolean awaitReadiness() throws InterruptedException {
275 return awaitReadiness(initialSettleTime());
280 public boolean awaitReadiness(final Duration toWait) throws InterruptedException {
282 if (toWait.isFinite()) {
284 readinessFuture.get(toWait.toNanos(), TimeUnit.NANOSECONDS);
285 } catch (TimeoutException e) {
286 LOG.debug("Timed out waiting for shards to settle", e);
290 readinessFuture.get();
292 } catch (ExecutionException e) {
293 LOG.warn("Unexpected readiness failure, assuming convergence", e);
301 public void awaitReadiness(final long timeout, final TimeUnit unit) throws InterruptedException, TimeoutException {
302 if (!awaitReadiness(Duration.create(timeout, unit))) {
303 throw new TimeoutException("Shard leaders failed to settle");
307 @SuppressWarnings("checkstyle:IllegalCatch")
308 private static ActorRef createShardManager(final ActorSystem actorSystem,
309 final AbstractShardManagerCreator<?> creator, final String shardDispatcher,
310 final String shardManagerId) {
311 Exception lastException = null;
313 for (int i = 0; i < 100; i++) {
315 return actorSystem.actorOf(creator.props().withDispatcher(shardDispatcher), shardManagerId);
316 } catch (Exception e) {
318 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
319 LOG.debug("Could not create actor {} because of {} - waiting for sometime before retrying "
320 + "(retry count = {})", shardManagerId, e.getMessage(), i);
324 throw new IllegalStateException("Failed to create Shard Manager", lastException);
328 * Future which completes when all shards settle for the first time.
330 * @return A Listenable future.
332 public final ListenableFuture<?> initialSettleFuture() {
333 return readinessFuture;
337 SettableFuture<Void> readinessFuture() {
338 return readinessFuture;
342 @SuppressWarnings("unchecked")
343 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerProxyListener(
344 final YangInstanceIdentifier shardLookup, final YangInstanceIdentifier insideShard,
345 final DOMDataTreeChangeListener delegate) {
347 requireNonNull(shardLookup, "shardLookup should not be null");
348 requireNonNull(insideShard, "insideShard should not be null");
349 requireNonNull(delegate, "delegate should not be null");
351 final String shardName = actorUtils.getShardStrategyFactory().getStrategy(shardLookup).findShard(shardLookup);
352 LOG.debug("Registering tree listener: {} for tree: {} shard: {}, path inside shard: {}",
353 delegate,shardLookup, shardName, insideShard);
355 // wrap this in the ClusteredDOMDataTreeChangeLister interface
356 // since we always want clustered registration
357 final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> listenerRegistrationProxy =
358 new DataTreeChangeListenerProxy<>(actorUtils, new ClusteredDOMDataTreeChangeListener() {
360 public void onDataTreeChanged(final List<DataTreeCandidate> changes) {
361 delegate.onDataTreeChanged(changes);
365 public void onInitialData() {
366 delegate.onInitialData();
369 listenerRegistrationProxy.init(shardName);
371 return (ListenerRegistration<L>) listenerRegistrationProxy;
374 private Duration initialSettleTime() {
375 final DatastoreContext context = actorUtils.getDatastoreContext();
376 final int multiplier = context.getInitialSettleTimeoutMultiplier();
377 return multiplier == 0 ? Duration.Inf() : context.getShardLeaderElectionTimeout().duration().$times(multiplier);