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 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 23 | import com.google.common.util.concurrent.Futures; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 24 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 25 | import net.kuujo.copycat.CopycatConfig; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 26 | import net.kuujo.copycat.cluster.ClusterConfig; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 27 | import net.kuujo.copycat.cluster.Member; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 28 | import net.kuujo.copycat.cluster.Member.Type; |
| 29 | import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator; |
| 30 | import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator; |
| 31 | import net.kuujo.copycat.log.BufferedLog; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 32 | import net.kuujo.copycat.log.FileLog; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 33 | import net.kuujo.copycat.log.Log; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 34 | import net.kuujo.copycat.protocol.Consistency; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 35 | import net.kuujo.copycat.protocol.Protocol; |
| 36 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 37 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 38 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 39 | import org.apache.felix.scr.annotations.Activate; |
| 40 | import org.apache.felix.scr.annotations.Component; |
| 41 | import org.apache.felix.scr.annotations.Deactivate; |
| 42 | import org.apache.felix.scr.annotations.Reference; |
| 43 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 44 | import org.apache.felix.scr.annotations.Service; |
| 45 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 46 | import org.onosproject.core.IdGenerator; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 47 | import org.onosproject.store.cluster.impl.ClusterDefinitionManager; |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 48 | import org.onosproject.store.cluster.impl.NodeInfo; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 49 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 50 | import org.onosproject.store.ecmap.EventuallyConsistentMapBuilderImpl; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 51 | import org.onosproject.store.service.AtomicCounterBuilder; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 52 | import org.onosproject.store.service.ConsistentMapBuilder; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 53 | import org.onosproject.store.service.ConsistentMapException; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 54 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 55 | import org.onosproject.store.service.MapInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 56 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 57 | import org.onosproject.store.service.SetBuilder; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 58 | import org.onosproject.store.service.StorageAdminService; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 59 | import org.onosproject.store.service.StorageService; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 60 | import org.onosproject.store.service.Transaction; |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 61 | import org.onosproject.store.service.TransactionContextBuilder; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 64 | import java.io.File; |
| 65 | import java.io.IOException; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 66 | import java.util.Collection; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 67 | import java.util.List; |
| 68 | import java.util.Map; |
| 69 | import java.util.Set; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 70 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 71 | import java.util.concurrent.ExecutionException; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 72 | import java.util.concurrent.Executors; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 73 | import java.util.concurrent.TimeUnit; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 74 | import java.util.concurrent.TimeoutException; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 75 | import java.util.stream.Collectors; |
| 76 | |
| 77 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Database manager. |
| 81 | */ |
| 82 | @Component(immediate = true, enabled = true) |
| 83 | @Service |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 84 | public class DatabaseManager implements StorageService, StorageAdminService { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 85 | |
| 86 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 87 | |
| 88 | public static final int COPYCAT_TCP_PORT = 7238; // 7238 = RAFT |
| 89 | public static final String PARTITION_DEFINITION_FILE = "../config/tablets.json"; |
| 90 | public static final String BASE_PARTITION_NAME = "p0"; |
| 91 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 92 | private static final int RAFT_ELECTION_TIMEOUT_MILLIS = 3000; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 93 | private static final int DATABASE_OPERATION_TIMEOUT_MILLIS = 5000; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 94 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 95 | private ClusterCoordinator coordinator; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 96 | private PartitionedDatabase partitionedDatabase; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 97 | private Database inMemoryDatabase; |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 98 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 99 | private TransactionManager transactionManager; |
| 100 | private final IdGenerator transactionIdGenerator = () -> RandomUtils.nextLong(); |
| 101 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected ClusterService clusterService; |
| 104 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected ClusterCommunicationService clusterCommunicator; |
| 107 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 108 | protected String nodeToUri(NodeInfo node) { |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 109 | return String.format("onos://%s:%d", node.getIp(), node.getTcpPort()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Activate |
| 113 | public void activate() { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 114 | // load database configuration |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 115 | File databaseDefFile = new File(PARTITION_DEFINITION_FILE); |
| 116 | log.info("Loading database definition: {}", databaseDefFile.getAbsolutePath()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 117 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 118 | Map<String, Set<NodeInfo>> partitionMap; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 119 | try { |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 120 | DatabaseDefinitionStore databaseDefStore = new DatabaseDefinitionStore(databaseDefFile); |
| 121 | if (!databaseDefFile.exists()) { |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 122 | createDefaultDatabaseDefinition(databaseDefStore); |
| 123 | } |
| 124 | partitionMap = databaseDefStore.read().getPartitions(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 125 | } catch (IOException e) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 126 | throw new IllegalStateException("Failed to load database config", e); |
| 127 | } |
| 128 | |
| 129 | String[] activeNodeUris = partitionMap.values() |
| 130 | .stream() |
| 131 | .reduce((s1, s2) -> Sets.union(s1, s2)) |
| 132 | .get() |
| 133 | .stream() |
| 134 | .map(this::nodeToUri) |
| 135 | .toArray(String[]::new); |
| 136 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 137 | String localNodeUri = nodeToUri(NodeInfo.of(clusterService.getLocalNode())); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 138 | Protocol protocol = new CopycatCommunicationProtocol(clusterService, clusterCommunicator); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 139 | |
| 140 | ClusterConfig clusterConfig = new ClusterConfig() |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 141 | .withProtocol(protocol) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 142 | .withElectionTimeout(electionTimeoutMillis(activeNodeUris)) |
| 143 | .withHeartbeatInterval(heartbeatTimeoutMillis(activeNodeUris)) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 144 | .withMembers(activeNodeUris) |
| 145 | .withLocalMember(localNodeUri); |
| 146 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 147 | CopycatConfig copycatConfig = new CopycatConfig() |
| 148 | .withName("onos") |
| 149 | .withClusterConfig(clusterConfig) |
| 150 | .withDefaultSerializer(new DatabaseSerializer()) |
| 151 | .withDefaultExecutor(Executors.newSingleThreadExecutor(new NamedThreadFactory("copycat-coordinator-%d"))); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 152 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 153 | coordinator = new DefaultClusterCoordinator(copycatConfig.resolve()); |
| 154 | |
| 155 | DatabaseConfig inMemoryDatabaseConfig = |
| 156 | newDatabaseConfig(BASE_PARTITION_NAME, newInMemoryLog(), activeNodeUris); |
| 157 | inMemoryDatabase = coordinator |
| 158 | .getResource(inMemoryDatabaseConfig.getName(), inMemoryDatabaseConfig.resolve(clusterConfig) |
| 159 | .withSerializer(copycatConfig.getDefaultSerializer()) |
| 160 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())); |
| 161 | |
| 162 | List<Database> partitions = partitionMap.entrySet() |
| 163 | .stream() |
| 164 | .map(entry -> { |
| 165 | String[] replicas = entry.getValue().stream().map(this::nodeToUri).toArray(String[]::new); |
| 166 | return newDatabaseConfig(entry.getKey(), newPersistentLog(), replicas); |
| 167 | }) |
| 168 | .map(config -> { |
| 169 | Database db = coordinator.getResource(config.getName(), config.resolve(clusterConfig) |
| 170 | .withSerializer(copycatConfig.getDefaultSerializer()) |
| 171 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())); |
| 172 | return db; |
| 173 | }) |
| 174 | .collect(Collectors.toList()); |
| 175 | |
| 176 | partitionedDatabase = new PartitionedDatabase("onos-store", partitions); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 177 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 178 | CompletableFuture<Void> status = coordinator.open() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 179 | .thenCompose(v -> CompletableFuture.allOf(inMemoryDatabase.open(), partitionedDatabase.open()) |
| 180 | .whenComplete((db, error) -> { |
| 181 | if (error != null) { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 182 | log.error("Failed to initialize database.", error); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 183 | } else { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 184 | log.info("Successfully initialized database."); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 185 | } |
| 186 | })); |
| 187 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 188 | Futures.getUnchecked(status); |
| 189 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 190 | transactionManager = new TransactionManager(partitionedDatabase); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 191 | log.info("Started"); |
| 192 | } |
| 193 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 194 | private void createDefaultDatabaseDefinition(DatabaseDefinitionStore store) { |
| 195 | // Assumes IPv4 is returned. |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 196 | String ip = ClusterDefinitionManager.getSiteLocalAddress(); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 197 | NodeInfo node = NodeInfo.from(ip, ip, COPYCAT_TCP_PORT); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 198 | try { |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 199 | store.write(DatabaseDefinition.from(ImmutableSet.of(node))); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 200 | } catch (IOException e) { |
| 201 | log.warn("Unable to write default cluster definition", e); |
| 202 | } |
| 203 | } |
| 204 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 205 | @Deactivate |
| 206 | public void deactivate() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 207 | CompletableFuture.allOf(inMemoryDatabase.close(), partitionedDatabase.close()) |
| 208 | .thenCompose(v -> coordinator.close()) |
| 209 | .whenComplete((result, error) -> { |
| 210 | if (error != null) { |
| 211 | log.warn("Failed to cleanly close databases.", error); |
| 212 | } else { |
| 213 | log.info("Successfully closed databases."); |
| 214 | } |
| 215 | }); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 216 | log.info("Stopped"); |
| 217 | } |
| 218 | |
| 219 | @Override |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 220 | public TransactionContextBuilder transactionContextBuilder() { |
| 221 | return new DefaultTransactionContextBuilder( |
| 222 | inMemoryDatabase, partitionedDatabase, transactionIdGenerator.getNewId()); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 223 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 224 | |
| 225 | @Override |
| 226 | public List<PartitionInfo> getPartitionInfo() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 227 | return Lists.asList( |
| 228 | inMemoryDatabase, |
| 229 | partitionedDatabase.getPartitions().toArray(new Database[]{})) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 230 | .stream() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 231 | .map(DatabaseManager::toPartitionInfo) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 232 | .collect(Collectors.toList()); |
| 233 | } |
| 234 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 235 | private Log newPersistentLog() { |
| 236 | String logDir = System.getProperty("karaf.data", "./data"); |
| 237 | return new FileLog() |
| 238 | .withDirectory(logDir) |
| 239 | .withSegmentSize(1073741824) // 1GB |
| 240 | .withFlushOnWrite(true) |
| 241 | .withSegmentInterval(Long.MAX_VALUE); |
| 242 | } |
| 243 | |
| 244 | private Log newInMemoryLog() { |
| 245 | return new BufferedLog() |
| 246 | .withFlushOnWrite(false) |
| 247 | .withFlushInterval(Long.MAX_VALUE) |
| 248 | .withSegmentSize(10485760) // 10MB |
| 249 | .withSegmentInterval(Long.MAX_VALUE); |
| 250 | } |
| 251 | |
| 252 | private DatabaseConfig newDatabaseConfig(String name, Log log, String[] replicas) { |
| 253 | return new DatabaseConfig() |
| 254 | .withName(name) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 255 | .withElectionTimeout(electionTimeoutMillis(replicas)) |
| 256 | .withHeartbeatInterval(heartbeatTimeoutMillis(replicas)) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 257 | .withConsistency(Consistency.STRONG) |
| 258 | .withLog(log) |
| 259 | .withDefaultSerializer(new DatabaseSerializer()) |
| 260 | .withReplicas(replicas); |
| 261 | } |
| 262 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 263 | private long electionTimeoutMillis(String[] replicas) { |
| 264 | return replicas.length == 1 ? 10L : RAFT_ELECTION_TIMEOUT_MILLIS; |
| 265 | } |
| 266 | |
| 267 | private long heartbeatTimeoutMillis(String[] replicas) { |
| 268 | return electionTimeoutMillis(replicas) / 2; |
| 269 | } |
| 270 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 271 | /** |
| 272 | * Maps a Raft Database object to a PartitionInfo object. |
| 273 | * |
| 274 | * @param database database containing input data |
| 275 | * @return PartitionInfo object |
| 276 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 277 | private static PartitionInfo toPartitionInfo(Database database) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 278 | return new PartitionInfo(database.name(), |
| 279 | database.cluster().term(), |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 280 | database.cluster().members() |
| 281 | .stream() |
| 282 | .filter(member -> Type.ACTIVE.equals(member.type())) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 283 | .map(Member::uri) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 284 | .sorted() |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 285 | .collect(Collectors.toList()), |
| 286 | database.cluster().leader() != null ? |
| 287 | database.cluster().leader().uri() : null); |
| 288 | } |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 289 | |
| 290 | |
| 291 | @Override |
| 292 | public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() { |
| 293 | return new EventuallyConsistentMapBuilderImpl<>(clusterService, |
| 294 | clusterCommunicator); |
| 295 | } |
| 296 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 297 | @Override |
| 298 | public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() { |
| 299 | return new DefaultConsistentMapBuilder<>(inMemoryDatabase, partitionedDatabase); |
| 300 | } |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 301 | |
| 302 | @Override |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 303 | public <E> SetBuilder<E> setBuilder() { |
| 304 | return new DefaultSetBuilder<>(partitionedDatabase); |
| 305 | } |
| 306 | |
| 307 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 308 | public AtomicCounterBuilder atomicCounterBuilder() { |
| 309 | return new DefaultAtomicCounterBuilder(inMemoryDatabase, partitionedDatabase); |
| 310 | } |
| 311 | |
| 312 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 313 | public List<MapInfo> getMapInfo() { |
| 314 | List<MapInfo> maps = Lists.newArrayList(); |
| 315 | maps.addAll(getMapInfo(inMemoryDatabase)); |
| 316 | maps.addAll(getMapInfo(partitionedDatabase)); |
| 317 | return maps; |
| 318 | } |
| 319 | |
| 320 | private List<MapInfo> getMapInfo(Database database) { |
| 321 | return complete(database.tableNames()) |
| 322 | .stream() |
| 323 | .map(name -> new MapInfo(name, complete(database.size(name)))) |
| 324 | .filter(info -> info.size() > 0) |
| 325 | .collect(Collectors.toList()); |
| 326 | } |
| 327 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 328 | |
| 329 | @Override |
| 330 | public Map<String, Long> getCounters() { |
| 331 | Map<String, Long> counters = Maps.newHashMap(); |
| 332 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 333 | counters.putAll(complete(partitionedDatabase.counters())); |
| 334 | return counters; |
| 335 | } |
| 336 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 337 | @Override |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 338 | public Map<String, Long> getPartitionedDatabaseCounters() { |
| 339 | Map<String, Long> counters = Maps.newHashMap(); |
| 340 | counters.putAll(complete(partitionedDatabase.counters())); |
| 341 | return counters; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public Map<String, Long> getInMemoryDatabaseCounters() { |
| 346 | Map<String, Long> counters = Maps.newHashMap(); |
| 347 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 348 | return counters; |
| 349 | } |
| 350 | |
| 351 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 352 | public Collection<Transaction> getTransactions() { |
| 353 | return complete(transactionManager.getTransactions()); |
| 354 | } |
| 355 | |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 356 | private static <T> T complete(CompletableFuture<T> future) { |
| 357 | try { |
| 358 | return future.get(DATABASE_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 359 | } catch (InterruptedException e) { |
| 360 | Thread.currentThread().interrupt(); |
| 361 | throw new ConsistentMapException.Interrupted(); |
| 362 | } catch (TimeoutException e) { |
| 363 | throw new ConsistentMapException.Timeout(); |
| 364 | } catch (ExecutionException e) { |
| 365 | throw new ConsistentMapException(e.getCause()); |
| 366 | } |
| 367 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 368 | |
| 369 | @Override |
| 370 | public void redriveTransactions() { |
| 371 | getTransactions().stream().forEach(transactionManager::execute); |
| 372 | } |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 373 | } |