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 | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 17 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 18 | |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 19 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 20 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.CompletableFuture; |
| 28 | import java.util.concurrent.ExecutionException; |
| 29 | import java.util.concurrent.Executors; |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | import java.util.concurrent.TimeoutException; |
| 32 | import java.util.function.Function; |
| 33 | import java.util.stream.Collectors; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 34 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 35 | import net.kuujo.copycat.CopycatConfig; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 36 | import net.kuujo.copycat.cluster.ClusterConfig; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 37 | import net.kuujo.copycat.cluster.Member; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 38 | import net.kuujo.copycat.cluster.Member.Type; |
| 39 | import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator; |
| 40 | import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator; |
| 41 | import net.kuujo.copycat.log.BufferedLog; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 42 | import net.kuujo.copycat.log.FileLog; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 43 | import net.kuujo.copycat.log.Log; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 44 | import net.kuujo.copycat.protocol.Consistency; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 45 | import net.kuujo.copycat.protocol.Protocol; |
| 46 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 47 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 48 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 49 | import org.apache.felix.scr.annotations.Activate; |
| 50 | import org.apache.felix.scr.annotations.Component; |
| 51 | import org.apache.felix.scr.annotations.Deactivate; |
| 52 | import org.apache.felix.scr.annotations.Reference; |
| 53 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 54 | import org.apache.felix.scr.annotations.ReferencePolicy; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 55 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 56 | import org.onosproject.app.ApplicationEvent; |
| 57 | import org.onosproject.app.ApplicationListener; |
| 58 | import org.onosproject.app.ApplicationService; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 59 | import org.onosproject.cluster.ClusterMetadataService; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 60 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 61 | import org.onosproject.cluster.ControllerNode; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 62 | import org.onosproject.cluster.NodeId; |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 63 | import org.onosproject.cluster.PartitionId; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 64 | import org.onosproject.core.ApplicationId; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 65 | import org.onosproject.core.IdGenerator; |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 66 | import org.onosproject.persistence.PersistenceService; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 67 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 68 | import org.onosproject.store.service.AtomicCounterBuilder; |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 69 | import org.onosproject.store.service.AtomicValueBuilder; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 70 | import org.onosproject.store.service.ConsistentMapBuilder; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 71 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 72 | import org.onosproject.store.service.DistributedQueueBuilder; |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 73 | import org.onosproject.store.service.DistributedSetBuilder; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 74 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 75 | import org.onosproject.store.service.MapInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 76 | import org.onosproject.store.service.PartitionInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 77 | import org.onosproject.store.service.StorageAdminService; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 78 | import org.onosproject.store.service.StorageService; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 79 | import org.onosproject.store.service.Transaction; |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 80 | import org.onosproject.store.service.TransactionContextBuilder; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 81 | import org.slf4j.Logger; |
| 82 | |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 83 | import com.google.common.collect.ArrayListMultimap; |
| 84 | import com.google.common.collect.ImmutableList; |
| 85 | import com.google.common.collect.Lists; |
| 86 | import com.google.common.collect.Maps; |
| 87 | import com.google.common.collect.Multimap; |
| 88 | import com.google.common.collect.Multimaps; |
| 89 | import com.google.common.collect.Sets; |
| 90 | import com.google.common.util.concurrent.Futures; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Database manager. |
| 94 | */ |
| 95 | @Component(immediate = true, enabled = true) |
| 96 | @Service |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 97 | public class DatabaseManager implements StorageService, StorageAdminService { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 98 | |
| 99 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 100 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 101 | public static final String BASE_PARTITION_NAME = "p0"; |
| 102 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 103 | private static final int RAFT_ELECTION_TIMEOUT_MILLIS = 3000; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 104 | private static final int DATABASE_OPERATION_TIMEOUT_MILLIS = 5000; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 105 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 106 | private ClusterCoordinator coordinator; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 107 | protected PartitionedDatabase partitionedDatabase; |
| 108 | protected Database inMemoryDatabase; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 109 | protected NodeId localNodeId; |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 110 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 111 | private TransactionManager transactionManager; |
| 112 | private final IdGenerator transactionIdGenerator = () -> RandomUtils.nextLong(); |
| 113 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 114 | private ApplicationListener appListener = new InternalApplicationListener(); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 115 | |
Madan Jampani | 15687d1 | 2015-08-11 07:19:35 -0700 | [diff] [blame] | 116 | private final Multimap<String, DefaultAsyncConsistentMap> maps = |
| 117 | Multimaps.synchronizedMultimap(ArrayListMultimap.create()); |
| 118 | private final Multimap<ApplicationId, DefaultAsyncConsistentMap> mapsByApplication = |
| 119 | Multimaps.synchronizedMultimap(ArrayListMultimap.create()); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 120 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 122 | protected ClusterMetadataService clusterMetadataService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 125 | protected ClusterService clusterService; |
| 126 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC) |
| 128 | protected ApplicationService applicationService; |
| 129 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected ClusterCommunicationService clusterCommunicator; |
| 132 | |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected PersistenceService persistenceService; |
| 135 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 136 | protected String nodeIdToUri(NodeId nodeId) { |
| 137 | ControllerNode node = clusterService.getNode(nodeId); |
| 138 | return String.format("onos://%s:%d", node.ip(), node.tcpPort()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 141 | protected void bindApplicationService(ApplicationService service) { |
| 142 | applicationService = service; |
| 143 | applicationService.addListener(appListener); |
| 144 | } |
| 145 | |
| 146 | protected void unbindApplicationService(ApplicationService service) { |
| 147 | applicationService.removeListener(appListener); |
| 148 | this.applicationService = null; |
| 149 | } |
| 150 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 151 | @Activate |
| 152 | public void activate() { |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 153 | localNodeId = clusterService.getLocalNode().id(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 154 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 155 | Map<PartitionId, Set<NodeId>> partitionMap = Maps.newHashMap(); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 156 | clusterMetadataService.getClusterMetadata().getPartitions().forEach(p -> { |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 157 | partitionMap.put(p.getId(), Sets.newHashSet(p.getMembers())); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 158 | }); |
| 159 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 160 | String[] activeNodeUris = partitionMap.values() |
| 161 | .stream() |
| 162 | .reduce((s1, s2) -> Sets.union(s1, s2)) |
| 163 | .get() |
| 164 | .stream() |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 165 | .map(this::nodeIdToUri) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 166 | .toArray(String[]::new); |
| 167 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 168 | String localNodeUri = nodeIdToUri(clusterMetadataService.getLocalNode().id()); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 169 | Protocol protocol = new CopycatCommunicationProtocol(clusterService, clusterCommunicator); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 170 | |
| 171 | ClusterConfig clusterConfig = new ClusterConfig() |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 172 | .withProtocol(protocol) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 173 | .withElectionTimeout(electionTimeoutMillis(activeNodeUris)) |
| 174 | .withHeartbeatInterval(heartbeatTimeoutMillis(activeNodeUris)) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 175 | .withMembers(activeNodeUris) |
| 176 | .withLocalMember(localNodeUri); |
| 177 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 178 | CopycatConfig copycatConfig = new CopycatConfig() |
| 179 | .withName("onos") |
| 180 | .withClusterConfig(clusterConfig) |
| 181 | .withDefaultSerializer(new DatabaseSerializer()) |
| 182 | .withDefaultExecutor(Executors.newSingleThreadExecutor(new NamedThreadFactory("copycat-coordinator-%d"))); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 183 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 184 | coordinator = new DefaultClusterCoordinator(copycatConfig.resolve()); |
| 185 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 186 | Function<PartitionId, Log> logFunction = id -> id.asInt() == 0 ? newInMemoryLog() : newPersistentLog(); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 187 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 188 | Map<PartitionId, Database> databases = Maps.transformEntries(partitionMap, (k, v) -> { |
| 189 | String[] replicas = v.stream().map(this::nodeIdToUri).toArray(String[]::new); |
| 190 | DatabaseConfig config = newDatabaseConfig(String.format("p%s", k), logFunction.apply(k), replicas); |
| 191 | return coordinator.getResource(config.getName(), config.resolve(clusterConfig) |
| 192 | .withSerializer(copycatConfig.getDefaultSerializer()) |
| 193 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())); |
| 194 | }); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 195 | |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 196 | inMemoryDatabase = databases.remove(PartitionId.from(0)); |
| 197 | |
| 198 | partitionedDatabase = new PartitionedDatabase("onos-store", databases.values()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 199 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 200 | CompletableFuture<Void> status = coordinator.open() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 201 | .thenCompose(v -> CompletableFuture.allOf(inMemoryDatabase.open(), partitionedDatabase.open()) |
| 202 | .whenComplete((db, error) -> { |
| 203 | if (error != null) { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 204 | log.error("Failed to initialize database.", error); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 205 | } else { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 206 | log.info("Successfully initialized database."); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 207 | } |
| 208 | })); |
| 209 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 210 | Futures.getUnchecked(status); |
| 211 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 212 | transactionManager = new TransactionManager(partitionedDatabase, consistentMapBuilder()); |
Ayaka Koshibe | 94cc01b | 2015-06-26 15:39:11 -0700 | [diff] [blame] | 213 | partitionedDatabase.setTransactionManager(transactionManager); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 214 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 215 | log.info("Started"); |
| 216 | } |
| 217 | |
| 218 | @Deactivate |
| 219 | public void deactivate() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 220 | CompletableFuture.allOf(inMemoryDatabase.close(), partitionedDatabase.close()) |
| 221 | .thenCompose(v -> coordinator.close()) |
| 222 | .whenComplete((result, error) -> { |
| 223 | if (error != null) { |
| 224 | log.warn("Failed to cleanly close databases.", error); |
| 225 | } else { |
| 226 | log.info("Successfully closed databases."); |
| 227 | } |
| 228 | }); |
Madan Jampani | e81f1cf | 2015-09-15 11:00:41 -0700 | [diff] [blame] | 229 | ImmutableList.copyOf(maps.values()).forEach(this::unregisterMap); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 230 | if (applicationService != null) { |
| 231 | applicationService.removeListener(appListener); |
| 232 | } |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 233 | log.info("Stopped"); |
| 234 | } |
| 235 | |
| 236 | @Override |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 237 | public TransactionContextBuilder transactionContextBuilder() { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 238 | return new DefaultTransactionContextBuilder(this, transactionIdGenerator.getNewId()); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 239 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 240 | |
| 241 | @Override |
| 242 | public List<PartitionInfo> getPartitionInfo() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 243 | return Lists.asList( |
| 244 | inMemoryDatabase, |
| 245 | partitionedDatabase.getPartitions().toArray(new Database[]{})) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 246 | .stream() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 247 | .map(DatabaseManager::toPartitionInfo) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 248 | .collect(Collectors.toList()); |
| 249 | } |
| 250 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 251 | private Log newPersistentLog() { |
| 252 | String logDir = System.getProperty("karaf.data", "./data"); |
| 253 | return new FileLog() |
| 254 | .withDirectory(logDir) |
| 255 | .withSegmentSize(1073741824) // 1GB |
| 256 | .withFlushOnWrite(true) |
| 257 | .withSegmentInterval(Long.MAX_VALUE); |
| 258 | } |
| 259 | |
| 260 | private Log newInMemoryLog() { |
| 261 | return new BufferedLog() |
| 262 | .withFlushOnWrite(false) |
| 263 | .withFlushInterval(Long.MAX_VALUE) |
| 264 | .withSegmentSize(10485760) // 10MB |
| 265 | .withSegmentInterval(Long.MAX_VALUE); |
| 266 | } |
| 267 | |
| 268 | private DatabaseConfig newDatabaseConfig(String name, Log log, String[] replicas) { |
| 269 | return new DatabaseConfig() |
| 270 | .withName(name) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 271 | .withElectionTimeout(electionTimeoutMillis(replicas)) |
| 272 | .withHeartbeatInterval(heartbeatTimeoutMillis(replicas)) |
Madan Jampani | 40537ca | 2015-07-14 19:50:33 -0700 | [diff] [blame] | 273 | .withConsistency(Consistency.DEFAULT) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 274 | .withLog(log) |
| 275 | .withDefaultSerializer(new DatabaseSerializer()) |
| 276 | .withReplicas(replicas); |
| 277 | } |
| 278 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 279 | private long electionTimeoutMillis(String[] replicas) { |
| 280 | return replicas.length == 1 ? 10L : RAFT_ELECTION_TIMEOUT_MILLIS; |
| 281 | } |
| 282 | |
| 283 | private long heartbeatTimeoutMillis(String[] replicas) { |
| 284 | return electionTimeoutMillis(replicas) / 2; |
| 285 | } |
| 286 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Maps a Raft Database object to a PartitionInfo object. |
| 289 | * |
| 290 | * @param database database containing input data |
| 291 | * @return PartitionInfo object |
| 292 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 293 | private static PartitionInfo toPartitionInfo(Database database) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 294 | return new PartitionInfo(database.name(), |
| 295 | database.cluster().term(), |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 296 | database.cluster().members() |
| 297 | .stream() |
| 298 | .filter(member -> Type.ACTIVE.equals(member.type())) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 299 | .map(Member::uri) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 300 | .sorted() |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 301 | .collect(Collectors.toList()), |
| 302 | database.cluster().leader() != null ? |
| 303 | database.cluster().leader().uri() : null); |
| 304 | } |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 305 | |
| 306 | |
| 307 | @Override |
| 308 | public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() { |
| 309 | return new EventuallyConsistentMapBuilderImpl<>(clusterService, |
Aaron Kruglikov | 66cf0b9 | 2015-10-26 15:46:54 -0700 | [diff] [blame] | 310 | clusterCommunicator, |
| 311 | persistenceService); |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 314 | @Override |
| 315 | public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 316 | return new DefaultConsistentMapBuilder<>(this); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 317 | } |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 318 | |
| 319 | @Override |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 320 | public <E> DistributedSetBuilder<E> setBuilder() { |
| 321 | return new DefaultDistributedSetBuilder<>(this); |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 324 | |
| 325 | @Override |
| 326 | public <E> DistributedQueueBuilder<E> queueBuilder() { |
| 327 | return new DefaultDistributedQueueBuilder<>(this); |
| 328 | } |
| 329 | |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 330 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 331 | public AtomicCounterBuilder atomicCounterBuilder() { |
| 332 | return new DefaultAtomicCounterBuilder(inMemoryDatabase, partitionedDatabase); |
| 333 | } |
| 334 | |
| 335 | @Override |
Madan Jampani | 762246d | 2015-07-21 15:40:59 -0700 | [diff] [blame] | 336 | public <V> AtomicValueBuilder<V> atomicValueBuilder() { |
| 337 | return new DefaultAtomicValueBuilder<>(this); |
| 338 | } |
| 339 | |
| 340 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 341 | public List<MapInfo> getMapInfo() { |
| 342 | List<MapInfo> maps = Lists.newArrayList(); |
| 343 | maps.addAll(getMapInfo(inMemoryDatabase)); |
| 344 | maps.addAll(getMapInfo(partitionedDatabase)); |
| 345 | return maps; |
| 346 | } |
| 347 | |
| 348 | private List<MapInfo> getMapInfo(Database database) { |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 349 | return complete(database.maps()) |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 350 | .stream() |
Madan Jampani | 7804c99 | 2015-07-20 13:20:19 -0700 | [diff] [blame] | 351 | .map(name -> new MapInfo(name, complete(database.mapSize(name)))) |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 352 | .filter(info -> info.size() > 0) |
| 353 | .collect(Collectors.toList()); |
| 354 | } |
| 355 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 356 | |
| 357 | @Override |
| 358 | public Map<String, Long> getCounters() { |
| 359 | Map<String, Long> counters = Maps.newHashMap(); |
| 360 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 361 | counters.putAll(complete(partitionedDatabase.counters())); |
| 362 | return counters; |
| 363 | } |
| 364 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 365 | @Override |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 366 | public Map<String, Long> getPartitionedDatabaseCounters() { |
| 367 | Map<String, Long> counters = Maps.newHashMap(); |
| 368 | counters.putAll(complete(partitionedDatabase.counters())); |
| 369 | return counters; |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public Map<String, Long> getInMemoryDatabaseCounters() { |
| 374 | Map<String, Long> counters = Maps.newHashMap(); |
| 375 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 376 | return counters; |
| 377 | } |
| 378 | |
| 379 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 380 | public Collection<Transaction> getTransactions() { |
| 381 | return complete(transactionManager.getTransactions()); |
| 382 | } |
| 383 | |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 384 | private static <T> T complete(CompletableFuture<T> future) { |
| 385 | try { |
| 386 | return future.get(DATABASE_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 387 | } catch (InterruptedException e) { |
| 388 | Thread.currentThread().interrupt(); |
| 389 | throw new ConsistentMapException.Interrupted(); |
| 390 | } catch (TimeoutException e) { |
| 391 | throw new ConsistentMapException.Timeout(); |
| 392 | } catch (ExecutionException e) { |
| 393 | throw new ConsistentMapException(e.getCause()); |
| 394 | } |
| 395 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 396 | |
| 397 | @Override |
| 398 | public void redriveTransactions() { |
| 399 | getTransactions().stream().forEach(transactionManager::execute); |
| 400 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 401 | |
Brian O'Connor | 24cf656 | 2015-06-11 18:42:30 -0700 | [diff] [blame] | 402 | protected <K, V> DefaultAsyncConsistentMap<K, V> registerMap(DefaultAsyncConsistentMap<K, V> map) { |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 403 | maps.put(map.name(), map); |
| 404 | if (map.applicationId() != null) { |
| 405 | mapsByApplication.put(map.applicationId(), map); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 406 | } |
Brian O'Connor | 24cf656 | 2015-06-11 18:42:30 -0700 | [diff] [blame] | 407 | return map; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 410 | protected <K, V> void unregisterMap(DefaultAsyncConsistentMap<K, V> map) { |
Madan Jampani | 8566863 | 2015-08-10 10:46:40 -0700 | [diff] [blame] | 411 | maps.remove(map.name(), map); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 412 | if (map.applicationId() != null) { |
| 413 | mapsByApplication.remove(map.applicationId(), map); |
| 414 | } |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 417 | private class InternalApplicationListener implements ApplicationListener { |
| 418 | @Override |
| 419 | public void event(ApplicationEvent event) { |
| 420 | if (event.type() == APP_UNINSTALLED || event.type() == APP_DEACTIVATED) { |
| 421 | ApplicationId appId = event.subject().id(); |
HIGUCHI Yuta | 99b7b34 | 2015-09-29 16:54:21 -0700 | [diff] [blame] | 422 | List<DefaultAsyncConsistentMap> mapsToRemove; |
| 423 | synchronized (mapsByApplication) { |
| 424 | mapsToRemove = ImmutableList.copyOf(mapsByApplication.get(appId)); |
| 425 | } |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 426 | mapsToRemove.forEach(DatabaseManager.this::unregisterMap); |
| 427 | if (event.type() == APP_UNINSTALLED) { |
| 428 | mapsToRemove.stream().filter(map -> map.purgeOnUninstall()).forEach(map -> map.clear()); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
Madan Jampani | e9b781a | 2015-07-07 16:23:20 -0700 | [diff] [blame] | 433 | } |