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;
15 import java.util.Dictionary;
16 import java.util.Hashtable;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.controller.cluster.ActorSystemProvider;
20 import org.opendaylight.controller.cluster.datastore.config.Configuration;
21 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
22 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfigProvider;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.osgi.service.component.ComponentFactory;
26 import org.osgi.service.component.ComponentInstance;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30 import org.osgi.service.component.annotations.Modified;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Global bootstrap component. It is responsible to start all distributed datastore instances and activate
37 * {@link OSGiDOMStore} as appropriate. It also provides routing of datastore proprerties towards AbstractDataStore.
40 @Component(immediate = true, configurationPid = "org.opendaylight.controller.cluster.datastore")
41 public final class OSGiDistributedDataStore {
43 * Internal state associated with a particular datastore. An instance is created for each datastore and once the
44 * datastore settles, we create a new component configuration of {@link OSGiDOMStore}. This indirection is needed
45 * to not block Service Component Runtime from activating other components while we are waiting for the datastore
46 * to settle (which can take a long time).
48 private final class DatastoreState implements FutureCallback<Object> {
49 private final DatastoreContextIntrospector introspector;
50 private final LogicalDatastoreType datastoreType;
51 private final AbstractDataStore datastore;
52 private final String serviceType;
55 private ComponentInstance component;
57 private boolean stopped;
59 DatastoreState(final DatastoreContextIntrospector introspector, final LogicalDatastoreType datastoreType,
60 final AbstractDataStore datastore, final String serviceType) {
61 this.introspector = requireNonNull(introspector);
62 this.datastoreType = requireNonNull(datastoreType);
63 this.datastore = requireNonNull(datastore);
64 this.serviceType = requireNonNull(serviceType);
67 synchronized void updateProperties(final Map<String, Object> properties) {
68 if (introspector.update(properties)) {
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 final Dictionary<String, Object> dict = new Hashtable<>();
94 dict.put(OSGiDOMStore.DATASTORE_TYPE_PROP, datastoreType);
95 dict.put(OSGiDOMStore.DATASTORE_INST_PROP, datastore);
96 dict.put("type", serviceType);
97 component = datastoreFactory.newInstance(dict);
98 LOG.info("Distributed Datastore type {} started", datastoreType);
104 public synchronized void onFailure(final Throwable cause) {
105 LOG.error("Distributed Datastore type {} failed to settle", datastoreType, cause);
109 private static final Logger LOG = LoggerFactory.getLogger(OSGiDistributedDataStore.class);
112 DOMSchemaService schemaService = null;
114 ActorSystemProvider actorSystemProvider = null;
116 DatastoreContextIntrospectorFactory introspectorFactory = null;
118 DatastoreSnapshotRestore snapshotRestore = null;
120 ModuleShardConfigProvider configProvider = null;
121 @Reference(target = "(component.factory=" + OSGiDOMStore.FACTORY_NAME + ")")
122 ComponentFactory datastoreFactory = null;
124 private DatastoreState configDatastore;
125 private DatastoreState operDatastore;
128 void activate(final Map<String, Object> properties) {
129 configDatastore = createDatastore(LogicalDatastoreType.CONFIGURATION, "distributed-config", null);
130 operDatastore = createDatastore(LogicalDatastoreType.OPERATIONAL, "distributed-operational",
131 new ConfigurationImpl(configProvider));
132 modified(properties);
136 void modified(final Map<String, Object> properties) {
137 LOG.debug("Overlaying settings: {}", properties);
138 configDatastore.updateProperties(properties);
139 operDatastore.updateProperties(properties);
144 operDatastore.stop();
145 operDatastore = null;
146 configDatastore.stop();
147 configDatastore = null;
150 private DatastoreState createDatastore(final LogicalDatastoreType datastoreType, final String serviceType,
151 final Configuration config) {
152 LOG.info("Distributed Datastore type {} starting", datastoreType);
153 final DatastoreContextIntrospector introspector = introspectorFactory.newInstance(datastoreType);
154 final AbstractDataStore datastore = DistributedDataStoreFactory.createInstance(actorSystemProvider,
155 introspector.getContext(), introspector, snapshotRestore, config);
156 datastore.setCloseable(schemaService.registerSchemaContextListener(datastore));
157 final DatastoreState state = new DatastoreState(introspector, datastoreType, datastore, serviceType);
159 Futures.addCallback(datastore.initialSettleFuture(), state,
160 // Note we are invoked from shard manager and therefore could block it, hence the round-trip to executor
161 datastore.getActorUtils().getClientDispatcher()::execute);