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