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