blob: 28a9c5a9689eb3aa12c1c4ecd42a275392d4eb6a [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 Jampani09342702015-02-05 23:32:40 -080044import org.slf4j.Logger;
45
46import com.google.common.collect.Sets;
47
48/**
49 * Database manager.
50 */
51@Component(immediate = true, enabled = true)
52@Service
Madan Jampani393e0f02015-02-12 07:35:39 +053053public class DatabaseManager implements StorageService {
Madan Jampani09342702015-02-05 23:32:40 -080054
55 private final Logger log = getLogger(getClass());
56 private PartitionedDatabase partitionedDatabase;
57 public static final int COPYCAT_TCP_PORT = 7238; // 7238 = RAFT
58 private static final String CONFIG_DIR = "../config";
59 private static final String PARTITION_DEFINITION_FILE = "tablets.json";
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected ClusterService clusterService;
63
64 protected String nodeToUri(ControllerNode node) {
Madan Jampani393e0f02015-02-12 07:35:39 +053065 return String.format("tcp://%s:%d", node.ip(), COPYCAT_TCP_PORT);
Madan Jampani09342702015-02-05 23:32:40 -080066 }
67
68 @Activate
69 public void activate() {
70
71 final String logDir = System.getProperty("karaf.data", "./data");
72
73 // load database configuration
74 File file = new File(CONFIG_DIR, PARTITION_DEFINITION_FILE);
75 log.info("Loading database definition: {}", file.getAbsolutePath());
76
77 DatabaseDefinitionStore databaseDef = new DatabaseDefinitionStore(file);
78 Map<String, Set<DefaultControllerNode>> partitionMap;
79 try {
80 partitionMap = databaseDef.read();
81 } catch (IOException e) {
82 log.error("Failed to load database config {}", file);
83 throw new IllegalStateException("Failed to load database config", e);
84 }
85
86 String[] activeNodeUris = partitionMap.values()
87 .stream()
88 .reduce((s1, s2) -> Sets.union(s1, s2))
89 .get()
90 .stream()
91 .map(this::nodeToUri)
92 .toArray(String[]::new);
93
94 String localNodeUri = nodeToUri(clusterService.getLocalNode());
95
96 ClusterConfig clusterConfig = new ClusterConfig()
Madan Jampani393e0f02015-02-12 07:35:39 +053097 .withProtocol(new NettyTcpProtocol()
98 .withSsl(false)
99 .withConnectTimeout(60000)
100 .withAcceptBacklog(1024)
101 .withTrafficClass(-1)
102 .withSoLinger(-1)
103 .withReceiveBufferSize(32768)
104 .withSendBufferSize(8192)
105 .withThreads(1))
106 .withElectionTimeout(300)
107 .withHeartbeatInterval(150)
Madan Jampani09342702015-02-05 23:32:40 -0800108 .withMembers(activeNodeUris)
109 .withLocalMember(localNodeUri);
110
111 PartitionedDatabaseConfig databaseConfig = new PartitionedDatabaseConfig();
112
113 partitionMap.forEach((name, nodes) -> {
114 Set<String> replicas = nodes.stream().map(this::nodeToUri).collect(Collectors.toSet());
115 DatabaseConfig partitionConfig = new DatabaseConfig()
Madan Jampani393e0f02015-02-12 07:35:39 +0530116 .withElectionTimeout(300)
117 .withHeartbeatInterval(150)
Madan Jampani09342702015-02-05 23:32:40 -0800118 .withConsistency(Consistency.STRONG)
Madan Jampani393e0f02015-02-12 07:35:39 +0530119 .withLog(new FileLog()
120 .withDirectory(logDir)
121 .withSegmentSize(1073741824) // 1GB
122 .withFlushOnWrite(true)
123 .withSegmentInterval(Long.MAX_VALUE))
124 .withDefaultSerializer(new DatabaseSerializer())
Madan Jampani09342702015-02-05 23:32:40 -0800125 .withReplicas(replicas);
126 databaseConfig.addPartition(name, partitionConfig);
127 });
128
129 partitionedDatabase = PartitionedDatabaseManager.create("onos-store", clusterConfig, databaseConfig);
130
131 partitionedDatabase.open().whenComplete((db, error) -> {
132 if (error != null) {
133 log.warn("Failed to open database.", error);
134 } else {
135 log.info("Successfully opened database.");
136 }
137 });
138 log.info("Started");
139 }
140
141 @Deactivate
142 public void deactivate() {
143 partitionedDatabase.close().whenComplete((result, error) -> {
144 if (error != null) {
145 log.warn("Failed to cleanly close database.", error);
146 } else {
147 log.info("Successfully closed database.");
148 }
149 });
150 log.info("Stopped");
151 }
152
153 @Override
Madan Jampani393e0f02015-02-12 07:35:39 +0530154 public <K, V> ConsistentMap<K , V> createConsistentMap(String name, Serializer serializer) {
Madan Jampani09342702015-02-05 23:32:40 -0800155 return new ConsistentMapImpl<K, V>(name, partitionedDatabase, serializer);
156 }
157}