2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onosproject.store.consistent.impl;
19 import com.google.common.collect.ArrayListMultimap;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24 import com.google.common.collect.Multimap;
25 import com.google.common.collect.Multimaps;
26 import com.google.common.collect.Sets;
27 import com.google.common.util.concurrent.Futures;
29 import net.kuujo.copycat.CopycatConfig;
30 import net.kuujo.copycat.cluster.ClusterConfig;
31 import net.kuujo.copycat.cluster.Member;
32 import net.kuujo.copycat.cluster.Member.Type;
33 import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
34 import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
35 import net.kuujo.copycat.log.BufferedLog;
36 import net.kuujo.copycat.log.FileLog;
37 import net.kuujo.copycat.log.Log;
38 import net.kuujo.copycat.protocol.Consistency;
39 import net.kuujo.copycat.protocol.Protocol;
40 import net.kuujo.copycat.util.concurrent.NamedThreadFactory;
42 import org.apache.commons.lang.math.RandomUtils;
43 import org.apache.felix.scr.annotations.Activate;
44 import org.apache.felix.scr.annotations.Component;
45 import org.apache.felix.scr.annotations.Deactivate;
46 import org.apache.felix.scr.annotations.Reference;
47 import org.apache.felix.scr.annotations.ReferenceCardinality;
48 import org.apache.felix.scr.annotations.ReferencePolicy;
49 import org.apache.felix.scr.annotations.Service;
51 import org.onosproject.app.ApplicationEvent;
52 import org.onosproject.app.ApplicationListener;
53 import org.onosproject.app.ApplicationService;
54 import org.onosproject.cluster.ClusterService;
55 import org.onosproject.cluster.NodeId;
56 import org.onosproject.core.ApplicationId;
57 import org.onosproject.core.IdGenerator;
58 import org.onosproject.store.cluster.impl.ClusterDefinitionManager;
59 import org.onosproject.store.cluster.impl.NodeInfo;
60 import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
61 import org.onosproject.store.ecmap.EventuallyConsistentMapBuilderImpl;
62 import org.onosproject.store.service.AtomicCounterBuilder;
63 import org.onosproject.store.service.AtomicValueBuilder;
64 import org.onosproject.store.service.ConsistentMapBuilder;
65 import org.onosproject.store.service.ConsistentMapException;
66 import org.onosproject.store.service.DistributedQueueBuilder;
67 import org.onosproject.store.service.EventuallyConsistentMapBuilder;
68 import org.onosproject.store.service.MapInfo;
69 import org.onosproject.store.service.PartitionInfo;
70 import org.onosproject.store.service.DistributedSetBuilder;
71 import org.onosproject.store.service.StorageAdminService;
72 import org.onosproject.store.service.StorageService;
73 import org.onosproject.store.service.Transaction;
74 import org.onosproject.store.service.TransactionContextBuilder;
75 import org.slf4j.Logger;
78 import java.io.IOException;
79 import java.util.Collection;
80 import java.util.List;
83 import java.util.concurrent.CompletableFuture;
84 import java.util.concurrent.ExecutionException;
85 import java.util.concurrent.Executors;
86 import java.util.concurrent.TimeUnit;
87 import java.util.concurrent.TimeoutException;
88 import java.util.stream.Collectors;
90 import static org.slf4j.LoggerFactory.getLogger;
91 import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
92 import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
97 @Component(immediate = true, enabled = true)
99 public class DatabaseManager implements StorageService, StorageAdminService {
101 private final Logger log = getLogger(getClass());
103 public static final int COPYCAT_TCP_PORT = 9876;
104 public static final String PARTITION_DEFINITION_FILE = "../config/tablets.json";
105 public static final String BASE_PARTITION_NAME = "p0";
107 private static final int RAFT_ELECTION_TIMEOUT_MILLIS = 3000;
108 private static final int DATABASE_OPERATION_TIMEOUT_MILLIS = 5000;
110 private ClusterCoordinator coordinator;
111 protected PartitionedDatabase partitionedDatabase;
112 protected Database inMemoryDatabase;
113 protected NodeId localNodeId;
115 private TransactionManager transactionManager;
116 private final IdGenerator transactionIdGenerator = () -> RandomUtils.nextLong();
118 private ApplicationListener appListener = new InternalApplicationListener();
120 private final Multimap<String, DefaultAsyncConsistentMap> maps =
121 Multimaps.synchronizedMultimap(ArrayListMultimap.create());
122 private final Multimap<ApplicationId, DefaultAsyncConsistentMap> mapsByApplication =
123 Multimaps.synchronizedMultimap(ArrayListMultimap.create());
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected ClusterService clusterService;
128 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
129 protected ApplicationService applicationService;
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected ClusterCommunicationService clusterCommunicator;
134 protected String nodeToUri(NodeInfo node) {
135 return String.format("onos://%s:%d", node.getIp(), node.getTcpPort());
138 protected void bindApplicationService(ApplicationService service) {
139 applicationService = service;
140 applicationService.addListener(appListener);
143 protected void unbindApplicationService(ApplicationService service) {
144 applicationService.removeListener(appListener);
145 this.applicationService = null;
149 public void activate() {
150 localNodeId = clusterService.getLocalNode().id();
151 // load database configuration
152 File databaseDefFile = new File(PARTITION_DEFINITION_FILE);
153 log.info("Loading database definition: {}", databaseDefFile.getAbsolutePath());
155 Map<String, Set<NodeInfo>> partitionMap;
157 DatabaseDefinitionStore databaseDefStore = new DatabaseDefinitionStore(databaseDefFile);
158 if (!databaseDefFile.exists()) {
159 createDefaultDatabaseDefinition(databaseDefStore);
161 partitionMap = databaseDefStore.read().getPartitions();
162 } catch (IOException e) {
163 throw new IllegalStateException("Failed to load database config", e);
166 String[] activeNodeUris = partitionMap.values()
168 .reduce((s1, s2) -> Sets.union(s1, s2))
171 .map(this::nodeToUri)
172 .toArray(String[]::new);
174 String localNodeUri = nodeToUri(NodeInfo.of(clusterService.getLocalNode()));
175 Protocol protocol = new CopycatCommunicationProtocol(clusterService, clusterCommunicator);
177 ClusterConfig clusterConfig = new ClusterConfig()
178 .withProtocol(protocol)
179 .withElectionTimeout(electionTimeoutMillis(activeNodeUris))
180 .withHeartbeatInterval(heartbeatTimeoutMillis(activeNodeUris))
181 .withMembers(activeNodeUris)
182 .withLocalMember(localNodeUri);
184 CopycatConfig copycatConfig = new CopycatConfig()
186 .withClusterConfig(clusterConfig)
187 .withDefaultSerializer(new DatabaseSerializer())
188 .withDefaultExecutor(Executors.newSingleThreadExecutor(new NamedThreadFactory("copycat-coordinator-%d")));
190 coordinator = new DefaultClusterCoordinator(copycatConfig.resolve());
192 DatabaseConfig inMemoryDatabaseConfig =
193 newDatabaseConfig(BASE_PARTITION_NAME, newInMemoryLog(), activeNodeUris);
194 inMemoryDatabase = coordinator
195 .getResource(inMemoryDatabaseConfig.getName(), inMemoryDatabaseConfig.resolve(clusterConfig)
196 .withSerializer(copycatConfig.getDefaultSerializer())
197 .withDefaultExecutor(copycatConfig.getDefaultExecutor()));
199 List<Database> partitions = partitionMap.entrySet()
202 String[] replicas = entry.getValue().stream().map(this::nodeToUri).toArray(String[]::new);
203 return newDatabaseConfig(entry.getKey(), newPersistentLog(), replicas);
206 Database db = coordinator.getResource(config.getName(), config.resolve(clusterConfig)
207 .withSerializer(copycatConfig.getDefaultSerializer())
208 .withDefaultExecutor(copycatConfig.getDefaultExecutor()));
211 .collect(Collectors.toList());
213 partitionedDatabase = new PartitionedDatabase("onos-store", partitions);
215 CompletableFuture<Void> status = coordinator.open()
216 .thenCompose(v -> CompletableFuture.allOf(inMemoryDatabase.open(), partitionedDatabase.open())
217 .whenComplete((db, error) -> {
219 log.error("Failed to initialize database.", error);
221 log.info("Successfully initialized database.");
225 Futures.getUnchecked(status);
227 transactionManager = new TransactionManager(partitionedDatabase, consistentMapBuilder());
228 partitionedDatabase.setTransactionManager(transactionManager);
233 private void createDefaultDatabaseDefinition(DatabaseDefinitionStore store) {
234 // Assumes IPv4 is returned.
235 String ip = ClusterDefinitionManager.getSiteLocalAddress();
236 NodeInfo node = NodeInfo.from(ip, ip, COPYCAT_TCP_PORT);
238 store.write(DatabaseDefinition.from(ImmutableSet.of(node)));
239 } catch (IOException e) {
240 log.warn("Unable to write default cluster definition", e);
245 public void deactivate() {
246 CompletableFuture.allOf(inMemoryDatabase.close(), partitionedDatabase.close())
247 .thenCompose(v -> coordinator.close())
248 .whenComplete((result, error) -> {
250 log.warn("Failed to cleanly close databases.", error);
252 log.info("Successfully closed databases.");
255 maps.values().forEach(this::unregisterMap);
256 if (applicationService != null) {
257 applicationService.removeListener(appListener);
263 public TransactionContextBuilder transactionContextBuilder() {
264 return new DefaultTransactionContextBuilder(this, transactionIdGenerator.getNewId());
268 public List<PartitionInfo> getPartitionInfo() {
271 partitionedDatabase.getPartitions().toArray(new Database[]{}))
273 .map(DatabaseManager::toPartitionInfo)
274 .collect(Collectors.toList());
277 private Log newPersistentLog() {
278 String logDir = System.getProperty("karaf.data", "./data");
280 .withDirectory(logDir)
281 .withSegmentSize(1073741824) // 1GB
282 .withFlushOnWrite(true)
283 .withSegmentInterval(Long.MAX_VALUE);
286 private Log newInMemoryLog() {
287 return new BufferedLog()
288 .withFlushOnWrite(false)
289 .withFlushInterval(Long.MAX_VALUE)
290 .withSegmentSize(10485760) // 10MB
291 .withSegmentInterval(Long.MAX_VALUE);
294 private DatabaseConfig newDatabaseConfig(String name, Log log, String[] replicas) {
295 return new DatabaseConfig()
297 .withElectionTimeout(electionTimeoutMillis(replicas))
298 .withHeartbeatInterval(heartbeatTimeoutMillis(replicas))
299 .withConsistency(Consistency.DEFAULT)
301 .withDefaultSerializer(new DatabaseSerializer())
302 .withReplicas(replicas);
305 private long electionTimeoutMillis(String[] replicas) {
306 return replicas.length == 1 ? 10L : RAFT_ELECTION_TIMEOUT_MILLIS;
309 private long heartbeatTimeoutMillis(String[] replicas) {
310 return electionTimeoutMillis(replicas) / 2;
314 * Maps a Raft Database object to a PartitionInfo object.
316 * @param database database containing input data
317 * @return PartitionInfo object
319 private static PartitionInfo toPartitionInfo(Database database) {
320 return new PartitionInfo(database.name(),
321 database.cluster().term(),
322 database.cluster().members()
324 .filter(member -> Type.ACTIVE.equals(member.type()))
327 .collect(Collectors.toList()),
328 database.cluster().leader() != null ?
329 database.cluster().leader().uri() : null);
334 public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() {
335 return new EventuallyConsistentMapBuilderImpl<>(clusterService,
336 clusterCommunicator);
340 public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() {
341 return new DefaultConsistentMapBuilder<>(this);
345 public <E> DistributedSetBuilder<E> setBuilder() {
346 return new DefaultDistributedSetBuilder<>(this);
351 public <E> DistributedQueueBuilder<E> queueBuilder() {
352 return new DefaultDistributedQueueBuilder<>(this);
356 public AtomicCounterBuilder atomicCounterBuilder() {
357 return new DefaultAtomicCounterBuilder(inMemoryDatabase, partitionedDatabase);
361 public <V> AtomicValueBuilder<V> atomicValueBuilder() {
362 return new DefaultAtomicValueBuilder<>(this);
366 public List<MapInfo> getMapInfo() {
367 List<MapInfo> maps = Lists.newArrayList();
368 maps.addAll(getMapInfo(inMemoryDatabase));
369 maps.addAll(getMapInfo(partitionedDatabase));
373 private List<MapInfo> getMapInfo(Database database) {
374 return complete(database.maps())
376 .map(name -> new MapInfo(name, complete(database.mapSize(name))))
377 .filter(info -> info.size() > 0)
378 .collect(Collectors.toList());
383 public Map<String, Long> getCounters() {
384 Map<String, Long> counters = Maps.newHashMap();
385 counters.putAll(complete(inMemoryDatabase.counters()));
386 counters.putAll(complete(partitionedDatabase.counters()));
391 public Map<String, Long> getPartitionedDatabaseCounters() {
392 Map<String, Long> counters = Maps.newHashMap();
393 counters.putAll(complete(partitionedDatabase.counters()));
398 public Map<String, Long> getInMemoryDatabaseCounters() {
399 Map<String, Long> counters = Maps.newHashMap();
400 counters.putAll(complete(inMemoryDatabase.counters()));
405 public Collection<Transaction> getTransactions() {
406 return complete(transactionManager.getTransactions());
409 private static <T> T complete(CompletableFuture<T> future) {
411 return future.get(DATABASE_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
412 } catch (InterruptedException e) {
413 Thread.currentThread().interrupt();
414 throw new ConsistentMapException.Interrupted();
415 } catch (TimeoutException e) {
416 throw new ConsistentMapException.Timeout();
417 } catch (ExecutionException e) {
418 throw new ConsistentMapException(e.getCause());
423 public void redriveTransactions() {
424 getTransactions().stream().forEach(transactionManager::execute);
427 protected <K, V> DefaultAsyncConsistentMap<K, V> registerMap(DefaultAsyncConsistentMap<K, V> map) {
428 maps.put(map.name(), map);
429 if (map.applicationId() != null) {
430 mapsByApplication.put(map.applicationId(), map);
435 protected <K, V> void unregisterMap(DefaultAsyncConsistentMap<K, V> map) {
436 maps.remove(map.name(), map);
437 if (map.applicationId() != null) {
438 mapsByApplication.remove(map.applicationId(), map);
442 private class InternalApplicationListener implements ApplicationListener {
444 public void event(ApplicationEvent event) {
445 if (event.type() == APP_UNINSTALLED || event.type() == APP_DEACTIVATED) {
446 ApplicationId appId = event.subject().id();
447 List<DefaultAsyncConsistentMap> mapsToRemove = ImmutableList.copyOf(mapsByApplication.get(appId));
448 mapsToRemove.forEach(DatabaseManager.this::unregisterMap);
449 if (event.type() == APP_UNINSTALLED) {
450 mapsToRemove.stream().filter(map -> map.purgeOnUninstall()).forEach(map -> map.clear());