Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 20 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 21 | import net.kuujo.copycat.cluster.ClusterConfig; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 22 | import net.kuujo.copycat.cluster.Member; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 23 | import net.kuujo.copycat.log.FileLog; |
| 24 | import net.kuujo.copycat.netty.NettyTcpProtocol; |
| 25 | import net.kuujo.copycat.protocol.Consistency; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 26 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Activate; |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Deactivate; |
| 30 | import org.apache.felix.scr.annotations.Reference; |
| 31 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 34 | import org.onosproject.store.cluster.impl.NodeInfo; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 35 | import org.onosproject.store.service.ConsistentMap; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 36 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 37 | import org.onosproject.store.service.Serializer; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 38 | import org.onosproject.store.service.StorageAdminService; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 39 | import org.onosproject.store.service.StorageService; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 40 | import org.onosproject.store.service.TransactionContext; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 43 | import java.io.File; |
| 44 | import java.io.IOException; |
| 45 | import java.util.List; |
| 46 | import java.util.Map; |
| 47 | import java.util.Set; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 48 | import java.util.concurrent.CountDownLatch; |
| 49 | import java.util.concurrent.TimeUnit; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 50 | import java.util.stream.Collectors; |
| 51 | |
| 52 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Database manager. |
| 56 | */ |
| 57 | @Component(immediate = true, enabled = true) |
| 58 | @Service |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 59 | public class DatabaseManager implements StorageService, StorageAdminService { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 60 | |
| 61 | private final Logger log = getLogger(getClass()); |
| 62 | private PartitionedDatabase partitionedDatabase; |
| 63 | public static final int COPYCAT_TCP_PORT = 7238; // 7238 = RAFT |
| 64 | private static final String CONFIG_DIR = "../config"; |
| 65 | private static final String PARTITION_DEFINITION_FILE = "tablets.json"; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 66 | private static final int DATABASE_STARTUP_TIMEOUT_SEC = 60; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 67 | |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 68 | private final PartitionedDatabaseConfig databaseConfig = new PartitionedDatabaseConfig(); |
| 69 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected ClusterService clusterService; |
| 72 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 73 | protected String nodeToUri(NodeInfo node) { |
| 74 | return String.format("tcp://%s:%d", node.getIp(), COPYCAT_TCP_PORT); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
| 79 | |
| 80 | final String logDir = System.getProperty("karaf.data", "./data"); |
| 81 | |
| 82 | // load database configuration |
| 83 | File file = new File(CONFIG_DIR, PARTITION_DEFINITION_FILE); |
| 84 | log.info("Loading database definition: {}", file.getAbsolutePath()); |
| 85 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 86 | Map<String, Set<NodeInfo>> partitionMap; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 87 | try { |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 88 | DatabaseDefinitionStore databaseDef = new DatabaseDefinitionStore(file); |
| 89 | partitionMap = databaseDef.read().getPartitions(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 90 | } catch (IOException e) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 91 | throw new IllegalStateException("Failed to load database config", e); |
| 92 | } |
| 93 | |
| 94 | String[] activeNodeUris = partitionMap.values() |
| 95 | .stream() |
| 96 | .reduce((s1, s2) -> Sets.union(s1, s2)) |
| 97 | .get() |
| 98 | .stream() |
| 99 | .map(this::nodeToUri) |
| 100 | .toArray(String[]::new); |
| 101 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 102 | String localNodeUri = nodeToUri(NodeInfo.of(clusterService.getLocalNode())); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 103 | |
| 104 | ClusterConfig clusterConfig = new ClusterConfig() |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 105 | .withProtocol(new NettyTcpProtocol() |
| 106 | .withSsl(false) |
| 107 | .withConnectTimeout(60000) |
| 108 | .withAcceptBacklog(1024) |
| 109 | .withTrafficClass(-1) |
| 110 | .withSoLinger(-1) |
| 111 | .withReceiveBufferSize(32768) |
| 112 | .withSendBufferSize(8192) |
| 113 | .withThreads(1)) |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 114 | .withElectionTimeout(3000) |
| 115 | .withHeartbeatInterval(1500) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 116 | .withMembers(activeNodeUris) |
| 117 | .withLocalMember(localNodeUri); |
| 118 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 119 | partitionMap.forEach((name, nodes) -> { |
| 120 | Set<String> replicas = nodes.stream().map(this::nodeToUri).collect(Collectors.toSet()); |
| 121 | DatabaseConfig partitionConfig = new DatabaseConfig() |
Thomas Vachuska | 0249b53 | 2015-02-20 16:46:18 -0800 | [diff] [blame] | 122 | .withElectionTimeout(3000) |
| 123 | .withHeartbeatInterval(1500) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 124 | .withConsistency(Consistency.STRONG) |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 125 | .withLog(new FileLog() |
| 126 | .withDirectory(logDir) |
| 127 | .withSegmentSize(1073741824) // 1GB |
| 128 | .withFlushOnWrite(true) |
| 129 | .withSegmentInterval(Long.MAX_VALUE)) |
| 130 | .withDefaultSerializer(new DatabaseSerializer()) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 131 | .withReplicas(replicas); |
| 132 | databaseConfig.addPartition(name, partitionConfig); |
| 133 | }); |
| 134 | |
| 135 | partitionedDatabase = PartitionedDatabaseManager.create("onos-store", clusterConfig, databaseConfig); |
| 136 | |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 137 | CountDownLatch latch = new CountDownLatch(1); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 138 | partitionedDatabase.open().whenComplete((db, error) -> { |
| 139 | if (error != null) { |
| 140 | log.warn("Failed to open database.", error); |
| 141 | } else { |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 142 | latch.countDown(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 143 | log.info("Successfully opened database."); |
| 144 | } |
| 145 | }); |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 146 | try { |
| 147 | if (!latch.await(DATABASE_STARTUP_TIMEOUT_SEC, TimeUnit.SECONDS)) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 148 | log.warn("Timed out waiting for database to initialize."); |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 149 | } |
| 150 | } catch (InterruptedException e) { |
| 151 | Thread.currentThread().interrupt(); |
| 152 | log.warn("Failed to complete database initialization."); |
| 153 | } |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 154 | log.info("Started"); |
| 155 | } |
| 156 | |
| 157 | @Deactivate |
| 158 | public void deactivate() { |
| 159 | partitionedDatabase.close().whenComplete((result, error) -> { |
| 160 | if (error != null) { |
| 161 | log.warn("Failed to cleanly close database.", error); |
| 162 | } else { |
| 163 | log.info("Successfully closed database."); |
| 164 | } |
| 165 | }); |
| 166 | log.info("Stopped"); |
| 167 | } |
| 168 | |
| 169 | @Override |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 170 | public <K, V> ConsistentMap<K , V> createConsistentMap(String name, Serializer serializer) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 171 | return new ConsistentMapImpl<K, V>(name, partitionedDatabase, serializer); |
| 172 | } |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 173 | |
| 174 | @Override |
| 175 | public TransactionContext createTransactionContext() { |
| 176 | return new DefaultTransactionContext(partitionedDatabase); |
| 177 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 178 | |
| 179 | @Override |
| 180 | public List<PartitionInfo> getPartitionInfo() { |
| 181 | return partitionedDatabase.getRegisteredPartitions() |
| 182 | .values() |
| 183 | .stream() |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 184 | .map(db -> toPartitionInfo(db, databaseConfig.partitions().get(db.name()))) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 185 | .collect(Collectors.toList()); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Maps a Raft Database object to a PartitionInfo object. |
| 190 | * |
| 191 | * @param database database containing input data |
| 192 | * @return PartitionInfo object |
| 193 | */ |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 194 | private static PartitionInfo toPartitionInfo(Database database, DatabaseConfig dbConfig) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 195 | return new PartitionInfo(database.name(), |
| 196 | database.cluster().term(), |
| 197 | database.cluster().members().stream() |
| 198 | .map(Member::uri) |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 199 | .filter(uri -> dbConfig.getReplicas().contains(uri)) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 200 | .collect(Collectors.toList()), |
| 201 | database.cluster().leader() != null ? |
| 202 | database.cluster().leader().uri() : null); |
| 203 | } |
| 204 | } |