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 | |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | import java.util.stream.Collectors; |
| 26 | |
| 27 | import net.kuujo.copycat.cluster.ClusterConfig; |
| 28 | import net.kuujo.copycat.log.FileLog; |
| 29 | import net.kuujo.copycat.netty.NettyTcpProtocol; |
| 30 | import net.kuujo.copycat.protocol.Consistency; |
| 31 | |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Reference; |
| 36 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 37 | import org.apache.felix.scr.annotations.Service; |
| 38 | import org.onosproject.cluster.ClusterService; |
| 39 | import org.onosproject.cluster.ControllerNode; |
| 40 | import org.onosproject.cluster.DefaultControllerNode; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 41 | import org.onosproject.store.service.ConsistentMap; |
| 42 | import org.onosproject.store.service.Serializer; |
| 43 | import org.onosproject.store.service.StorageService; |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame^] | 44 | import org.onosproject.store.service.TransactionContext; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import com.google.common.collect.Sets; |
| 48 | |
| 49 | /** |
| 50 | * Database manager. |
| 51 | */ |
| 52 | @Component(immediate = true, enabled = true) |
| 53 | @Service |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 54 | public class DatabaseManager implements StorageService { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 55 | |
| 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 Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 66 | return String.format("tcp://%s:%d", node.ip(), COPYCAT_TCP_PORT); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 67 | } |
| 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 Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 98 | .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 Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 109 | .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 Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 117 | .withElectionTimeout(300) |
| 118 | .withHeartbeatInterval(150) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 119 | .withConsistency(Consistency.STRONG) |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 120 | .withLog(new FileLog() |
| 121 | .withDirectory(logDir) |
| 122 | .withSegmentSize(1073741824) // 1GB |
| 123 | .withFlushOnWrite(true) |
| 124 | .withSegmentInterval(Long.MAX_VALUE)) |
| 125 | .withDefaultSerializer(new DatabaseSerializer()) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 126 | .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 Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 155 | public <K, V> ConsistentMap<K , V> createConsistentMap(String name, Serializer serializer) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 156 | return new ConsistentMapImpl<K, V>(name, partitionedDatabase, serializer); |
| 157 | } |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame^] | 158 | |
| 159 | @Override |
| 160 | public TransactionContext createTransactionContext() { |
| 161 | return new DefaultTransactionContext(partitionedDatabase); |
| 162 | } |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 163 | } |