blob: 2df9b4ca7503060e1cd96e7982b93ab3d0a3cbf4 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Madan Jampani09342702015-02-05 23:32:40 -080017package org.onosproject.store.consistent.impl;
18
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.File;
22import java.io.IOException;
23import java.util.Map;
24import java.util.Set;
25import java.util.stream.Collectors;
26
27import net.kuujo.copycat.cluster.ClusterConfig;
28import net.kuujo.copycat.log.FileLog;
29import net.kuujo.copycat.netty.NettyTcpProtocol;
30import net.kuujo.copycat.protocol.Consistency;
31
32import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Component;
34import org.apache.felix.scr.annotations.Deactivate;
35import org.apache.felix.scr.annotations.Reference;
36import org.apache.felix.scr.annotations.ReferenceCardinality;
37import org.apache.felix.scr.annotations.Service;
38import org.onosproject.cluster.ClusterService;
39import org.onosproject.cluster.ControllerNode;
40import org.onosproject.cluster.DefaultControllerNode;
Madan Jampani393e0f02015-02-12 07:35:39 +053041import org.onosproject.store.service.ConsistentMap;
42import org.onosproject.store.service.Serializer;
43import org.onosproject.store.service.StorageService;
Madan Jampani64689552015-02-17 10:00:27 -080044import org.onosproject.store.service.TransactionContext;
Madan Jampani09342702015-02-05 23:32:40 -080045import org.slf4j.Logger;
46
47import com.google.common.collect.Sets;
48
49/**
50 * Database manager.
51 */
52@Component(immediate = true, enabled = true)
53@Service
Madan Jampani393e0f02015-02-12 07:35:39 +053054public class DatabaseManager implements StorageService {
Madan Jampani09342702015-02-05 23:32:40 -080055
56 private final Logger log = getLogger(getClass());
57 private PartitionedDatabase partitionedDatabase;
58 public static final int COPYCAT_TCP_PORT = 7238; // 7238 = RAFT
59 private static final String CONFIG_DIR = "../config";
60 private static final String PARTITION_DEFINITION_FILE = "tablets.json";
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ClusterService clusterService;
64
65 protected String nodeToUri(ControllerNode node) {
Madan Jampani393e0f02015-02-12 07:35:39 +053066 return String.format("tcp://%s:%d", node.ip(), COPYCAT_TCP_PORT);
Madan Jampani09342702015-02-05 23:32:40 -080067 }
68
69 @Activate
70 public void activate() {
71
72 final String logDir = System.getProperty("karaf.data", "./data");
73
74 // load database configuration
75 File file = new File(CONFIG_DIR, PARTITION_DEFINITION_FILE);
76 log.info("Loading database definition: {}", file.getAbsolutePath());
77
78 DatabaseDefinitionStore databaseDef = new DatabaseDefinitionStore(file);
79 Map<String, Set<DefaultControllerNode>> partitionMap;
80 try {
81 partitionMap = databaseDef.read();
82 } catch (IOException e) {
83 log.error("Failed to load database config {}", file);
84 throw new IllegalStateException("Failed to load database config", e);
85 }
86
87 String[] activeNodeUris = partitionMap.values()
88 .stream()
89 .reduce((s1, s2) -> Sets.union(s1, s2))
90 .get()
91 .stream()
92 .map(this::nodeToUri)
93 .toArray(String[]::new);
94
95 String localNodeUri = nodeToUri(clusterService.getLocalNode());
96
97 ClusterConfig clusterConfig = new ClusterConfig()
Madan Jampani393e0f02015-02-12 07:35:39 +053098 .withProtocol(new NettyTcpProtocol()
99 .withSsl(false)
100 .withConnectTimeout(60000)
101 .withAcceptBacklog(1024)
102 .withTrafficClass(-1)
103 .withSoLinger(-1)
104 .withReceiveBufferSize(32768)
105 .withSendBufferSize(8192)
106 .withThreads(1))
107 .withElectionTimeout(300)
108 .withHeartbeatInterval(150)
Madan Jampani09342702015-02-05 23:32:40 -0800109 .withMembers(activeNodeUris)
110 .withLocalMember(localNodeUri);
111
112 PartitionedDatabaseConfig databaseConfig = new PartitionedDatabaseConfig();
113
114 partitionMap.forEach((name, nodes) -> {
115 Set<String> replicas = nodes.stream().map(this::nodeToUri).collect(Collectors.toSet());
116 DatabaseConfig partitionConfig = new DatabaseConfig()
Madan Jampani393e0f02015-02-12 07:35:39 +0530117 .withElectionTimeout(300)
118 .withHeartbeatInterval(150)
Madan Jampani09342702015-02-05 23:32:40 -0800119 .withConsistency(Consistency.STRONG)
Madan Jampani393e0f02015-02-12 07:35:39 +0530120 .withLog(new FileLog()
121 .withDirectory(logDir)
122 .withSegmentSize(1073741824) // 1GB
123 .withFlushOnWrite(true)
124 .withSegmentInterval(Long.MAX_VALUE))
125 .withDefaultSerializer(new DatabaseSerializer())
Madan Jampani09342702015-02-05 23:32:40 -0800126 .withReplicas(replicas);
127 databaseConfig.addPartition(name, partitionConfig);
128 });
129
130 partitionedDatabase = PartitionedDatabaseManager.create("onos-store", clusterConfig, databaseConfig);
131
132 partitionedDatabase.open().whenComplete((db, error) -> {
133 if (error != null) {
134 log.warn("Failed to open database.", error);
135 } else {
136 log.info("Successfully opened database.");
137 }
138 });
139 log.info("Started");
140 }
141
142 @Deactivate
143 public void deactivate() {
144 partitionedDatabase.close().whenComplete((result, error) -> {
145 if (error != null) {
146 log.warn("Failed to cleanly close database.", error);
147 } else {
148 log.info("Successfully closed database.");
149 }
150 });
151 log.info("Stopped");
152 }
153
154 @Override
Madan Jampani393e0f02015-02-12 07:35:39 +0530155 public <K, V> ConsistentMap<K , V> createConsistentMap(String name, Serializer serializer) {
Madan Jampani09342702015-02-05 23:32:40 -0800156 return new ConsistentMapImpl<K, V>(name, partitionedDatabase, serializer);
157 }
Madan Jampani64689552015-02-17 10:00:27 -0800158
159 @Override
160 public TransactionContext createTransactionContext() {
161 return new DefaultTransactionContext(partitionedDatabase);
162 }
Madan Jampani09342702015-02-05 23:32:40 -0800163}