f4d88a9b6d317443681c4a91addf49b83ac7722a
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / OSGiDistributedDataStore.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static java.util.Objects.requireNonNull;
11
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;
17 import java.util.Map;
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;
34
35 /**
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.
38  */
39 @Beta
40 @Component(immediate = true, configurationPid = "org.opendaylight.controller.cluster.datastore")
41 public final class OSGiDistributedDataStore {
42     /**
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).
47      */
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;
53
54         @GuardedBy("this")
55         private ComponentInstance component;
56         @GuardedBy("this")
57         private boolean stopped;
58
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);
65         }
66
67         synchronized void updateProperties(final Map<String, Object> properties) {
68             if (introspector.update(properties)) {
69                 datastore.onDatastoreContextUpdated(introspector.newContextFactory());
70             }
71         }
72
73         void stop() {
74             LOG.info("Distributed Datastore type {} stopping", datastoreType);
75
76             synchronized (this) {
77                 stopped = true;
78                 if (component != null) {
79                     component.dispose();
80                     component = null;
81                 }
82                 datastore.close();
83                 LOG.info("Distributed Datastore type {} stopped", datastoreType);
84             }
85         }
86
87         @Override
88         public void onSuccess(final Object result) {
89             LOG.debug("Distributed Datastore type {} reached initial settle", datastoreType);
90
91             synchronized (this) {
92                 if (!stopped) {
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);
99                 }
100             }
101         }
102
103         @Override
104         public synchronized void onFailure(final Throwable cause) {
105             LOG.error("Distributed Datastore type {} failed to settle", datastoreType, cause);
106         }
107     }
108
109     private static final Logger LOG = LoggerFactory.getLogger(OSGiDistributedDataStore.class);
110
111     @Reference
112     DOMSchemaService schemaService = null;
113     @Reference
114     ActorSystemProvider actorSystemProvider = null;
115     @Reference
116     DatastoreContextIntrospectorFactory introspectorFactory = null;
117     @Reference
118     DatastoreSnapshotRestore snapshotRestore = null;
119     @Reference
120     ModuleShardConfigProvider configProvider = null;
121     @Reference(target = "(component.factory=" + OSGiDOMStore.FACTORY_NAME + ")")
122     ComponentFactory datastoreFactory = null;
123
124     private DatastoreState configDatastore;
125     private DatastoreState operDatastore;
126
127     @Activate
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);
133     }
134
135     @Modified
136     void modified(final Map<String, Object> properties) {
137         LOG.debug("Overlaying settings: {}", properties);
138         configDatastore.updateProperties(properties);
139         operDatastore.updateProperties(properties);
140     }
141
142     @Deactivate
143     void deactivate() {
144         operDatastore.stop();
145         operDatastore = null;
146         configDatastore.stop();
147         configDatastore = null;
148     }
149
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);
158
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);
162         return state;
163     }
164 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.