2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.opendaylight.controller.cluster.ActorSystemProvider;
18 import org.opendaylight.controller.cluster.datastore.config.Configuration;
19 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
20 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.osgi.framework.FrameworkUtil;
24 import org.osgi.service.component.ComponentFactory;
25 import org.osgi.service.component.ComponentInstance;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Modified;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Global bootstrap component. It is responsible to start all distributed datastore instances and activate
36 * {@link OSGiDOMStore} as appropriate. It also provides routing of datastore proprerties towards AbstractDataStore.
39 @Component(immediate = true, configurationPid = "org.opendaylight.controller.cluster.datastore")
40 public final class OSGiDistributedDataStore {
42 * Internal state associated with a particular datastore. An instance is created for each datastore and once the
43 * datastore settles, we create a new component configuration of {@link OSGiDOMStore}. This indirection is needed
44 * to not block Service Component Runtime from activating other components while we are waiting for the datastore
45 * to settle (which can take a long time).
47 private final class DatastoreState implements FutureCallback<Object> {
48 private final DatastoreContextIntrospector introspector;
49 private final LogicalDatastoreType datastoreType;
50 private final AbstractDataStore datastore;
51 private final String serviceType;
54 private ComponentInstance<OSGiDOMStore> component;
56 private boolean stopped;
58 DatastoreState(final DatastoreContextIntrospector introspector, final LogicalDatastoreType datastoreType,
59 final AbstractDataStore datastore, final String serviceType) {
60 this.introspector = requireNonNull(introspector);
61 this.datastoreType = requireNonNull(datastoreType);
62 this.datastore = requireNonNull(datastore);
63 this.serviceType = requireNonNull(serviceType);
66 synchronized void updateProperties(final Map<String, Object> properties) {
67 if (introspector.update(properties)) {
68 LOG.info("Distributed Datastore type {} updating context", datastoreType);
69 datastore.onDatastoreContextUpdated(introspector.newContextFactory());
74 LOG.info("Distributed Datastore type {} stopping", datastoreType);
78 if (component != null) {
83 LOG.info("Distributed Datastore type {} stopped", datastoreType);
88 public void onSuccess(final Object result) {
89 LOG.debug("Distributed Datastore type {} reached initial settle", datastoreType);
93 component = datastoreFactory.newInstance(FrameworkUtil.asDictionary(Map.of(
94 OSGiDOMStore.DATASTORE_TYPE_PROP, datastoreType,
95 OSGiDOMStore.DATASTORE_INST_PROP, datastore,
96 "type", serviceType)));
97 LOG.info("Distributed Datastore type {} started", datastoreType);
103 public synchronized void onFailure(final Throwable cause) {
104 LOG.error("Distributed Datastore type {} failed to settle", datastoreType, cause);
108 private static final Logger LOG = LoggerFactory.getLogger(OSGiDistributedDataStore.class);
111 DOMSchemaService schemaService = null;
113 ActorSystemProvider actorSystemProvider = null;
115 DatastoreContextIntrospectorFactory introspectorFactory = null;
117 DatastoreSnapshotRestore snapshotRestore = null;
119 ModuleShardConfigProvider configProvider = null;
120 @Reference(target = "(component.factory=" + OSGiDOMStore.FACTORY_NAME + ")")
121 ComponentFactory<OSGiDOMStore> datastoreFactory = null;
123 private DatastoreState configDatastore;
124 private DatastoreState operDatastore;
127 void activate(final Map<String, Object> properties) {
128 configDatastore = createDatastore(LogicalDatastoreType.CONFIGURATION, "distributed-config", properties, null);
129 operDatastore = createDatastore(LogicalDatastoreType.OPERATIONAL, "distributed-operational", properties,
130 new ConfigurationImpl(configProvider));
134 void modified(final Map<String, Object> properties) {
135 LOG.debug("Overlaying settings: {}", properties);
136 configDatastore.updateProperties(properties);
137 operDatastore.updateProperties(properties);
142 operDatastore.stop();
143 operDatastore = null;
144 configDatastore.stop();
145 configDatastore = null;
148 private DatastoreState createDatastore(final LogicalDatastoreType datastoreType, final String serviceType,
149 final Map<String, Object> properties, final Configuration config) {
150 LOG.info("Distributed Datastore type {} starting", datastoreType);
151 final DatastoreContextIntrospector introspector = introspectorFactory.newInstance(datastoreType, properties);
152 final AbstractDataStore datastore = DistributedDataStoreFactory.createInstance(actorSystemProvider,
153 introspector.getContext(), introspector, snapshotRestore, config);
154 datastore.setCloseable(schemaService.registerSchemaContextListener(datastore));
155 final DatastoreState state = new DatastoreState(introspector, datastoreType, datastore, serviceType);
157 Futures.addCallback(datastore.initialSettleFuture(), state,
158 // Note we are invoked from shard manager and therefore could block it, hence the round-trip to executor
159 datastore.getActorUtils().getClientDispatcher()::execute);