blob: 354068ba4d6ab83991eaa3ff8272b97e22cc7ecf [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
Jonathan Hart054da972015-02-18 17:30:28 -080019import com.google.common.collect.Sets;
Madan Jampani09342702015-02-05 23:32:40 -080020import net.kuujo.copycat.cluster.ClusterConfig;
Jonathan Hart054da972015-02-18 17:30:28 -080021import net.kuujo.copycat.cluster.Member;
Madan Jampani09342702015-02-05 23:32:40 -080022import net.kuujo.copycat.log.FileLog;
23import net.kuujo.copycat.netty.NettyTcpProtocol;
24import net.kuujo.copycat.protocol.Consistency;
Madan Jampani09342702015-02-05 23:32:40 -080025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.ControllerNode;
33import org.onosproject.cluster.DefaultControllerNode;
Madan Jampani393e0f02015-02-12 07:35:39 +053034import org.onosproject.store.service.ConsistentMap;
Jonathan Hart054da972015-02-18 17:30:28 -080035import org.onosproject.store.service.PartitionInfo;
Madan Jampani393e0f02015-02-12 07:35:39 +053036import org.onosproject.store.service.Serializer;
Jonathan Hart054da972015-02-18 17:30:28 -080037import org.onosproject.store.service.StorageAdminService;
Madan Jampani393e0f02015-02-12 07:35:39 +053038import org.onosproject.store.service.StorageService;
Madan Jampani64689552015-02-17 10:00:27 -080039import org.onosproject.store.service.TransactionContext;
Madan Jampani09342702015-02-05 23:32:40 -080040import org.slf4j.Logger;
41
Jonathan Hart054da972015-02-18 17:30:28 -080042import java.io.File;
43import java.io.IOException;
44import java.util.List;
45import java.util.Map;
46import java.util.Set;
47import java.util.stream.Collectors;
48
49import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani09342702015-02-05 23:32:40 -080050
51/**
52 * Database manager.
53 */
54@Component(immediate = true, enabled = true)
55@Service
Jonathan Hart054da972015-02-18 17:30:28 -080056public class DatabaseManager implements StorageService, StorageAdminService {
Madan Jampani09342702015-02-05 23:32:40 -080057
58 private final Logger log = getLogger(getClass());
59 private PartitionedDatabase partitionedDatabase;
60 public static final int COPYCAT_TCP_PORT = 7238; // 7238 = RAFT
61 private static final String CONFIG_DIR = "../config";
62 private static final String PARTITION_DEFINITION_FILE = "tablets.json";
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ClusterService clusterService;
66
67 protected String nodeToUri(ControllerNode node) {
Madan Jampani393e0f02015-02-12 07:35:39 +053068 return String.format("tcp://%s:%d", node.ip(), COPYCAT_TCP_PORT);
Madan Jampani09342702015-02-05 23:32:40 -080069 }
70
71 @Activate
72 public void activate() {
73
74 final String logDir = System.getProperty("karaf.data", "./data");
75
76 // load database configuration
77 File file = new File(CONFIG_DIR, PARTITION_DEFINITION_FILE);
78 log.info("Loading database definition: {}", file.getAbsolutePath());
79
80 DatabaseDefinitionStore databaseDef = new DatabaseDefinitionStore(file);
81 Map<String, Set<DefaultControllerNode>> partitionMap;
82 try {
83 partitionMap = databaseDef.read();
84 } catch (IOException e) {
85 log.error("Failed to load database config {}", file);
86 throw new IllegalStateException("Failed to load database config", e);
87 }
88
89 String[] activeNodeUris = partitionMap.values()
90 .stream()
91 .reduce((s1, s2) -> Sets.union(s1, s2))
92 .get()
93 .stream()
94 .map(this::nodeToUri)
95 .toArray(String[]::new);
96
97 String localNodeUri = nodeToUri(clusterService.getLocalNode());
98
99 ClusterConfig clusterConfig = new ClusterConfig()
Madan Jampani393e0f02015-02-12 07:35:39 +0530100 .withProtocol(new NettyTcpProtocol()
101 .withSsl(false)
102 .withConnectTimeout(60000)
103 .withAcceptBacklog(1024)
104 .withTrafficClass(-1)
105 .withSoLinger(-1)
106 .withReceiveBufferSize(32768)
107 .withSendBufferSize(8192)
108 .withThreads(1))
Thomas Vachuska0249b532015-02-20 16:46:18 -0800109 .withElectionTimeout(3000)
110 .withHeartbeatInterval(1500)
Madan Jampani09342702015-02-05 23:32:40 -0800111 .withMembers(activeNodeUris)
112 .withLocalMember(localNodeUri);
113
114 PartitionedDatabaseConfig databaseConfig = new PartitionedDatabaseConfig();
115
116 partitionMap.forEach((name, nodes) -> {
117 Set<String> replicas = nodes.stream().map(this::nodeToUri).collect(Collectors.toSet());
118 DatabaseConfig partitionConfig = new DatabaseConfig()
Thomas Vachuska0249b532015-02-20 16:46:18 -0800119 .withElectionTimeout(3000)
120 .withHeartbeatInterval(1500)
Madan Jampani09342702015-02-05 23:32:40 -0800121 .withConsistency(Consistency.STRONG)
Madan Jampani393e0f02015-02-12 07:35:39 +0530122 .withLog(new FileLog()
123 .withDirectory(logDir)
124 .withSegmentSize(1073741824) // 1GB
125 .withFlushOnWrite(true)
126 .withSegmentInterval(Long.MAX_VALUE))
127 .withDefaultSerializer(new DatabaseSerializer())
Madan Jampani09342702015-02-05 23:32:40 -0800128 .withReplicas(replicas);
129 databaseConfig.addPartition(name, partitionConfig);
130 });
131
132 partitionedDatabase = PartitionedDatabaseManager.create("onos-store", clusterConfig, databaseConfig);
133
134 partitionedDatabase.open().whenComplete((db, error) -> {
135 if (error != null) {
136 log.warn("Failed to open database.", error);
137 } else {
138 log.info("Successfully opened database.");
139 }
140 });
141 log.info("Started");
142 }
143
144 @Deactivate
145 public void deactivate() {
146 partitionedDatabase.close().whenComplete((result, error) -> {
147 if (error != null) {
148 log.warn("Failed to cleanly close database.", error);
149 } else {
150 log.info("Successfully closed database.");
151 }
152 });
153 log.info("Stopped");
154 }
155
156 @Override
Madan Jampani393e0f02015-02-12 07:35:39 +0530157 public <K, V> ConsistentMap<K , V> createConsistentMap(String name, Serializer serializer) {
Madan Jampani09342702015-02-05 23:32:40 -0800158 return new ConsistentMapImpl<K, V>(name, partitionedDatabase, serializer);
159 }
Madan Jampani64689552015-02-17 10:00:27 -0800160
161 @Override
162 public TransactionContext createTransactionContext() {
163 return new DefaultTransactionContext(partitionedDatabase);
164 }
Jonathan Hart054da972015-02-18 17:30:28 -0800165
166 @Override
167 public List<PartitionInfo> getPartitionInfo() {
168 return partitionedDatabase.getRegisteredPartitions()
169 .values()
170 .stream()
171 .map(DatabaseManager::toPartitionInfo)
172 .collect(Collectors.toList());
173 }
174
175 /**
176 * Maps a Raft Database object to a PartitionInfo object.
177 *
178 * @param database database containing input data
179 * @return PartitionInfo object
180 */
181 private static PartitionInfo toPartitionInfo(Database database) {
182 return new PartitionInfo(database.name(),
183 database.cluster().term(),
184 database.cluster().members().stream()
185 .map(Member::uri)
186 .collect(Collectors.toList()),
187 database.cluster().leader() != null ?
188 database.cluster().leader().uri() : null);
189 }
190}