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 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 19 | import com.google.common.base.Charsets; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ArrayListMultimap; |
| 21 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ListMultimap; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 26 | import com.google.common.collect.Sets; |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 27 | import com.google.common.util.concurrent.Futures; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 28 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 29 | import net.kuujo.copycat.CopycatConfig; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 30 | import net.kuujo.copycat.cluster.ClusterConfig; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 31 | import net.kuujo.copycat.cluster.Member; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 32 | import net.kuujo.copycat.cluster.Member.Type; |
| 33 | import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator; |
| 34 | import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator; |
| 35 | import net.kuujo.copycat.log.BufferedLog; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 36 | import net.kuujo.copycat.log.FileLog; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 37 | import net.kuujo.copycat.log.Log; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 38 | import net.kuujo.copycat.protocol.Consistency; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 39 | import net.kuujo.copycat.protocol.Protocol; |
| 40 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 41 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 42 | import org.apache.commons.lang.math.RandomUtils; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 43 | import org.apache.felix.scr.annotations.Activate; |
| 44 | import org.apache.felix.scr.annotations.Component; |
| 45 | import org.apache.felix.scr.annotations.Deactivate; |
| 46 | import org.apache.felix.scr.annotations.Reference; |
| 47 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 48 | import org.apache.felix.scr.annotations.ReferencePolicy; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 49 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 50 | |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
| 52 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 53 | import org.onosproject.app.ApplicationEvent; |
| 54 | import org.onosproject.app.ApplicationListener; |
| 55 | import org.onosproject.app.ApplicationService; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 56 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 57 | import org.onosproject.cluster.NodeId; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 58 | import org.onosproject.core.ApplicationId; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 59 | import org.onosproject.core.IdGenerator; |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 60 | import org.onosproject.store.cluster.impl.ClusterDefinitionManager; |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 61 | import org.onosproject.store.cluster.impl.NodeInfo; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 62 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 63 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 64 | import org.onosproject.store.ecmap.EventuallyConsistentMapBuilderImpl; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 65 | import org.onosproject.store.service.AtomicCounterBuilder; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 66 | import org.onosproject.store.service.ConsistentMapBuilder; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 67 | import org.onosproject.store.service.ConsistentMapException; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 68 | import org.onosproject.store.service.DistributedQueueBuilder; |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 69 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 70 | import org.onosproject.store.service.MapEvent; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 71 | import org.onosproject.store.service.MapInfo; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 72 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 73 | import org.onosproject.store.service.DistributedSetBuilder; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 74 | import org.onosproject.store.service.StorageAdminService; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 75 | import org.onosproject.store.service.StorageService; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 76 | import org.onosproject.store.service.Transaction; |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 77 | import org.onosproject.store.service.TransactionContextBuilder; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 78 | import org.slf4j.Logger; |
| 79 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 80 | import java.io.File; |
| 81 | import java.io.IOException; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 82 | import java.util.Collection; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 83 | import java.util.List; |
| 84 | import java.util.Map; |
| 85 | import java.util.Set; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 86 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 87 | import java.util.concurrent.ExecutionException; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 88 | import java.util.concurrent.ExecutorService; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 89 | import java.util.concurrent.Executors; |
Madan Jampani | d14166a | 2015-02-24 17:37:51 -0800 | [diff] [blame] | 90 | import java.util.concurrent.TimeUnit; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 91 | import java.util.concurrent.TimeoutException; |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 92 | import java.util.stream.Collectors; |
| 93 | |
| 94 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 95 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
| 96 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Database manager. |
| 100 | */ |
| 101 | @Component(immediate = true, enabled = true) |
| 102 | @Service |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 103 | public class DatabaseManager implements StorageService, StorageAdminService { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 104 | |
| 105 | private final Logger log = getLogger(getClass()); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 106 | |
Thomas Vachuska | 06f44e1 | 2015-06-01 16:38:05 -0700 | [diff] [blame] | 107 | public static final int COPYCAT_TCP_PORT = 9876; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 108 | public static final String PARTITION_DEFINITION_FILE = "../config/tablets.json"; |
| 109 | public static final String BASE_PARTITION_NAME = "p0"; |
| 110 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 111 | private static final int RAFT_ELECTION_TIMEOUT_MILLIS = 3000; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 112 | private static final int DATABASE_OPERATION_TIMEOUT_MILLIS = 5000; |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 113 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 114 | protected static final MessageSubject QUEUE_UPDATED_TOPIC = new MessageSubject("distributed-queue-updated"); |
| 115 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 116 | private ClusterCoordinator coordinator; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 117 | protected PartitionedDatabase partitionedDatabase; |
| 118 | protected Database inMemoryDatabase; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 119 | protected NodeId localNodeId; |
Madan Jampani | fe3a9a7 | 2015-03-13 16:32:26 -0700 | [diff] [blame] | 120 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 121 | private TransactionManager transactionManager; |
| 122 | private final IdGenerator transactionIdGenerator = () -> RandomUtils.nextLong(); |
| 123 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 124 | private ExecutorService eventDispatcher; |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 125 | private ExecutorService queuePollExecutor; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 126 | private ApplicationListener appListener = new InternalApplicationListener(); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 127 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 128 | private final Map<String, DefaultAsyncConsistentMap> maps = Maps.newConcurrentMap(); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 129 | private final ListMultimap<ApplicationId, DefaultAsyncConsistentMap> mapsByApplication = ArrayListMultimap.create(); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 130 | private final Map<String, DefaultDistributedQueue> queues = Maps.newConcurrentMap(); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 131 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | protected ClusterService clusterService; |
| 134 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 135 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC) |
| 136 | protected ApplicationService applicationService; |
| 137 | |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected ClusterCommunicationService clusterCommunicator; |
| 140 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 141 | protected String nodeToUri(NodeInfo node) { |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 142 | return String.format("onos://%s:%d", node.getIp(), node.getTcpPort()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 145 | protected void bindApplicationService(ApplicationService service) { |
| 146 | applicationService = service; |
| 147 | applicationService.addListener(appListener); |
| 148 | } |
| 149 | |
| 150 | protected void unbindApplicationService(ApplicationService service) { |
| 151 | applicationService.removeListener(appListener); |
| 152 | this.applicationService = null; |
| 153 | } |
| 154 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 155 | @Activate |
| 156 | public void activate() { |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 157 | localNodeId = clusterService.getLocalNode().id(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 158 | // load database configuration |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 159 | File databaseDefFile = new File(PARTITION_DEFINITION_FILE); |
| 160 | log.info("Loading database definition: {}", databaseDefFile.getAbsolutePath()); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 161 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 162 | Map<String, Set<NodeInfo>> partitionMap; |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 163 | try { |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 164 | DatabaseDefinitionStore databaseDefStore = new DatabaseDefinitionStore(databaseDefFile); |
| 165 | if (!databaseDefFile.exists()) { |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 166 | createDefaultDatabaseDefinition(databaseDefStore); |
| 167 | } |
| 168 | partitionMap = databaseDefStore.read().getPartitions(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 169 | } catch (IOException e) { |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 170 | throw new IllegalStateException("Failed to load database config", e); |
| 171 | } |
| 172 | |
| 173 | String[] activeNodeUris = partitionMap.values() |
| 174 | .stream() |
| 175 | .reduce((s1, s2) -> Sets.union(s1, s2)) |
| 176 | .get() |
| 177 | .stream() |
| 178 | .map(this::nodeToUri) |
| 179 | .toArray(String[]::new); |
| 180 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 181 | String localNodeUri = nodeToUri(NodeInfo.of(clusterService.getLocalNode())); |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 182 | Protocol protocol = new CopycatCommunicationProtocol(clusterService, clusterCommunicator); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 183 | |
| 184 | ClusterConfig clusterConfig = new ClusterConfig() |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 185 | .withProtocol(protocol) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 186 | .withElectionTimeout(electionTimeoutMillis(activeNodeUris)) |
| 187 | .withHeartbeatInterval(heartbeatTimeoutMillis(activeNodeUris)) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 188 | .withMembers(activeNodeUris) |
| 189 | .withLocalMember(localNodeUri); |
| 190 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 191 | CopycatConfig copycatConfig = new CopycatConfig() |
| 192 | .withName("onos") |
| 193 | .withClusterConfig(clusterConfig) |
| 194 | .withDefaultSerializer(new DatabaseSerializer()) |
| 195 | .withDefaultExecutor(Executors.newSingleThreadExecutor(new NamedThreadFactory("copycat-coordinator-%d"))); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 196 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 197 | coordinator = new DefaultClusterCoordinator(copycatConfig.resolve()); |
| 198 | |
| 199 | DatabaseConfig inMemoryDatabaseConfig = |
| 200 | newDatabaseConfig(BASE_PARTITION_NAME, newInMemoryLog(), activeNodeUris); |
| 201 | inMemoryDatabase = coordinator |
| 202 | .getResource(inMemoryDatabaseConfig.getName(), inMemoryDatabaseConfig.resolve(clusterConfig) |
| 203 | .withSerializer(copycatConfig.getDefaultSerializer()) |
| 204 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())); |
| 205 | |
| 206 | List<Database> partitions = partitionMap.entrySet() |
| 207 | .stream() |
| 208 | .map(entry -> { |
| 209 | String[] replicas = entry.getValue().stream().map(this::nodeToUri).toArray(String[]::new); |
| 210 | return newDatabaseConfig(entry.getKey(), newPersistentLog(), replicas); |
| 211 | }) |
| 212 | .map(config -> { |
| 213 | Database db = coordinator.getResource(config.getName(), config.resolve(clusterConfig) |
| 214 | .withSerializer(copycatConfig.getDefaultSerializer()) |
| 215 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())); |
| 216 | return db; |
| 217 | }) |
| 218 | .collect(Collectors.toList()); |
| 219 | |
| 220 | partitionedDatabase = new PartitionedDatabase("onos-store", partitions); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 221 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 222 | CompletableFuture<Void> status = coordinator.open() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 223 | .thenCompose(v -> CompletableFuture.allOf(inMemoryDatabase.open(), partitionedDatabase.open()) |
| 224 | .whenComplete((db, error) -> { |
| 225 | if (error != null) { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 226 | log.error("Failed to initialize database.", error); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 227 | } else { |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 228 | log.info("Successfully initialized database."); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 229 | } |
| 230 | })); |
| 231 | |
Madan Jampani | 0d03d60 | 2015-04-20 12:03:56 -0700 | [diff] [blame] | 232 | Futures.getUnchecked(status); |
| 233 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 234 | transactionManager = new TransactionManager(partitionedDatabase, consistentMapBuilder()); |
Ayaka Koshibe | 94cc01b | 2015-06-26 15:39:11 -0700 | [diff] [blame] | 235 | partitionedDatabase.setTransactionManager(transactionManager); |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 236 | |
| 237 | eventDispatcher = Executors.newSingleThreadExecutor( |
| 238 | groupedThreads("onos/store/manager", "map-event-dispatcher")); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 239 | |
| 240 | queuePollExecutor = Executors.newFixedThreadPool(4, |
| 241 | groupedThreads("onos/store/manager", "queue-poll-handler")); |
| 242 | |
| 243 | clusterCommunicator.<String>addSubscriber(QUEUE_UPDATED_TOPIC, |
| 244 | data -> new String(data, Charsets.UTF_8), |
| 245 | name -> { |
| 246 | DefaultDistributedQueue q = queues.get(name); |
| 247 | if (q != null) { |
| 248 | q.tryPoll(); |
| 249 | } |
| 250 | }, |
| 251 | queuePollExecutor); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 252 | log.info("Started"); |
| 253 | } |
| 254 | |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 255 | private void createDefaultDatabaseDefinition(DatabaseDefinitionStore store) { |
| 256 | // Assumes IPv4 is returned. |
Madan Jampani | afeebbd | 2015-05-19 15:26:01 -0700 | [diff] [blame] | 257 | String ip = ClusterDefinitionManager.getSiteLocalAddress(); |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 258 | NodeInfo node = NodeInfo.from(ip, ip, COPYCAT_TCP_PORT); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 259 | try { |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 260 | store.write(DatabaseDefinition.from(ImmutableSet.of(node))); |
Thomas Vachuska | 8dc1a69 | 2015-03-31 01:01:37 -0700 | [diff] [blame] | 261 | } catch (IOException e) { |
| 262 | log.warn("Unable to write default cluster definition", e); |
| 263 | } |
| 264 | } |
| 265 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 266 | @Deactivate |
| 267 | public void deactivate() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 268 | CompletableFuture.allOf(inMemoryDatabase.close(), partitionedDatabase.close()) |
| 269 | .thenCompose(v -> coordinator.close()) |
| 270 | .whenComplete((result, error) -> { |
| 271 | if (error != null) { |
| 272 | log.warn("Failed to cleanly close databases.", error); |
| 273 | } else { |
| 274 | log.info("Successfully closed databases."); |
| 275 | } |
| 276 | }); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 277 | clusterCommunicator.removeSubscriber(QUEUE_UPDATED_TOPIC); |
| 278 | maps.values().forEach(this::unregisterMap); |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 279 | if (applicationService != null) { |
| 280 | applicationService.removeListener(appListener); |
| 281 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 282 | eventDispatcher.shutdown(); |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 283 | queuePollExecutor.shutdown(); |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 284 | log.info("Stopped"); |
| 285 | } |
| 286 | |
| 287 | @Override |
Ayaka Koshibe | 3a32156 | 2015-04-29 13:24:07 -0700 | [diff] [blame] | 288 | public TransactionContextBuilder transactionContextBuilder() { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 289 | return new DefaultTransactionContextBuilder(this, transactionIdGenerator.getNewId()); |
Madan Jampani | 6468955 | 2015-02-17 10:00:27 -0800 | [diff] [blame] | 290 | } |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 291 | |
| 292 | @Override |
| 293 | public List<PartitionInfo> getPartitionInfo() { |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 294 | return Lists.asList( |
| 295 | inMemoryDatabase, |
| 296 | partitionedDatabase.getPartitions().toArray(new Database[]{})) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 297 | .stream() |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 298 | .map(DatabaseManager::toPartitionInfo) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 299 | .collect(Collectors.toList()); |
| 300 | } |
| 301 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 302 | private Log newPersistentLog() { |
| 303 | String logDir = System.getProperty("karaf.data", "./data"); |
| 304 | return new FileLog() |
| 305 | .withDirectory(logDir) |
| 306 | .withSegmentSize(1073741824) // 1GB |
| 307 | .withFlushOnWrite(true) |
| 308 | .withSegmentInterval(Long.MAX_VALUE); |
| 309 | } |
| 310 | |
| 311 | private Log newInMemoryLog() { |
| 312 | return new BufferedLog() |
| 313 | .withFlushOnWrite(false) |
| 314 | .withFlushInterval(Long.MAX_VALUE) |
| 315 | .withSegmentSize(10485760) // 10MB |
| 316 | .withSegmentInterval(Long.MAX_VALUE); |
| 317 | } |
| 318 | |
| 319 | private DatabaseConfig newDatabaseConfig(String name, Log log, String[] replicas) { |
| 320 | return new DatabaseConfig() |
| 321 | .withName(name) |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 322 | .withElectionTimeout(electionTimeoutMillis(replicas)) |
| 323 | .withHeartbeatInterval(heartbeatTimeoutMillis(replicas)) |
Madan Jampani | e9b781a | 2015-07-07 16:23:20 -0700 | [diff] [blame] | 324 | .withConsistency(Consistency.STRONG) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 325 | .withLog(log) |
| 326 | .withDefaultSerializer(new DatabaseSerializer()) |
| 327 | .withReplicas(replicas); |
| 328 | } |
| 329 | |
Madan Jampani | 1a4eca0 | 2015-04-02 15:29:26 -0700 | [diff] [blame] | 330 | private long electionTimeoutMillis(String[] replicas) { |
| 331 | return replicas.length == 1 ? 10L : RAFT_ELECTION_TIMEOUT_MILLIS; |
| 332 | } |
| 333 | |
| 334 | private long heartbeatTimeoutMillis(String[] replicas) { |
| 335 | return electionTimeoutMillis(replicas) / 2; |
| 336 | } |
| 337 | |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 338 | /** |
| 339 | * Maps a Raft Database object to a PartitionInfo object. |
| 340 | * |
| 341 | * @param database database containing input data |
| 342 | * @return PartitionInfo object |
| 343 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 344 | private static PartitionInfo toPartitionInfo(Database database) { |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 345 | return new PartitionInfo(database.name(), |
| 346 | database.cluster().term(), |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 347 | database.cluster().members() |
| 348 | .stream() |
| 349 | .filter(member -> Type.ACTIVE.equals(member.type())) |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 350 | .map(Member::uri) |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 351 | .sorted() |
Jonathan Hart | 054da97 | 2015-02-18 17:30:28 -0800 | [diff] [blame] | 352 | .collect(Collectors.toList()), |
| 353 | database.cluster().leader() != null ? |
| 354 | database.cluster().leader().uri() : null); |
| 355 | } |
Jonathan Hart | 6ec029a | 2015-03-24 17:12:35 -0700 | [diff] [blame] | 356 | |
| 357 | |
| 358 | @Override |
| 359 | public <K, V> EventuallyConsistentMapBuilder<K, V> eventuallyConsistentMapBuilder() { |
| 360 | return new EventuallyConsistentMapBuilderImpl<>(clusterService, |
| 361 | clusterCommunicator); |
| 362 | } |
| 363 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 364 | @Override |
| 365 | public <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder() { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 366 | return new DefaultConsistentMapBuilder<>(this); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 367 | } |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 368 | |
| 369 | @Override |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 370 | public <E> DistributedSetBuilder<E> setBuilder() { |
| 371 | return new DefaultDistributedSetBuilder<>(this); |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 374 | |
| 375 | @Override |
| 376 | public <E> DistributedQueueBuilder<E> queueBuilder() { |
| 377 | return new DefaultDistributedQueueBuilder<>(this); |
| 378 | } |
| 379 | |
Madan Jampani | 08706ce | 2015-04-01 14:49:28 -0700 | [diff] [blame] | 380 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 381 | public AtomicCounterBuilder atomicCounterBuilder() { |
| 382 | return new DefaultAtomicCounterBuilder(inMemoryDatabase, partitionedDatabase); |
| 383 | } |
| 384 | |
| 385 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 386 | public List<MapInfo> getMapInfo() { |
| 387 | List<MapInfo> maps = Lists.newArrayList(); |
| 388 | maps.addAll(getMapInfo(inMemoryDatabase)); |
| 389 | maps.addAll(getMapInfo(partitionedDatabase)); |
| 390 | return maps; |
| 391 | } |
| 392 | |
| 393 | private List<MapInfo> getMapInfo(Database database) { |
| 394 | return complete(database.tableNames()) |
| 395 | .stream() |
| 396 | .map(name -> new MapInfo(name, complete(database.size(name)))) |
| 397 | .filter(info -> info.size() > 0) |
| 398 | .collect(Collectors.toList()); |
| 399 | } |
| 400 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 401 | |
| 402 | @Override |
| 403 | public Map<String, Long> getCounters() { |
| 404 | Map<String, Long> counters = Maps.newHashMap(); |
| 405 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 406 | counters.putAll(complete(partitionedDatabase.counters())); |
| 407 | return counters; |
| 408 | } |
| 409 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 410 | @Override |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 411 | public Map<String, Long> getPartitionedDatabaseCounters() { |
| 412 | Map<String, Long> counters = Maps.newHashMap(); |
| 413 | counters.putAll(complete(partitionedDatabase.counters())); |
| 414 | return counters; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public Map<String, Long> getInMemoryDatabaseCounters() { |
| 419 | Map<String, Long> counters = Maps.newHashMap(); |
| 420 | counters.putAll(complete(inMemoryDatabase.counters())); |
| 421 | return counters; |
| 422 | } |
| 423 | |
| 424 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 425 | public Collection<Transaction> getTransactions() { |
| 426 | return complete(transactionManager.getTransactions()); |
| 427 | } |
| 428 | |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 429 | private static <T> T complete(CompletableFuture<T> future) { |
| 430 | try { |
| 431 | return future.get(DATABASE_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 432 | } catch (InterruptedException e) { |
| 433 | Thread.currentThread().interrupt(); |
| 434 | throw new ConsistentMapException.Interrupted(); |
| 435 | } catch (TimeoutException e) { |
| 436 | throw new ConsistentMapException.Timeout(); |
| 437 | } catch (ExecutionException e) { |
| 438 | throw new ConsistentMapException(e.getCause()); |
| 439 | } |
| 440 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 441 | |
| 442 | @Override |
| 443 | public void redriveTransactions() { |
| 444 | getTransactions().stream().forEach(transactionManager::execute); |
| 445 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 446 | |
Brian O'Connor | 24cf656 | 2015-06-11 18:42:30 -0700 | [diff] [blame] | 447 | protected <K, V> DefaultAsyncConsistentMap<K, V> registerMap(DefaultAsyncConsistentMap<K, V> map) { |
| 448 | DefaultAsyncConsistentMap<K, V> existing = maps.putIfAbsent(map.name(), map); |
| 449 | if (existing != null) { |
| 450 | // FIXME: We need to cleanly support different map instances with same name. |
| 451 | log.info("Map by name {} already exists", map.name()); |
| 452 | return existing; |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 453 | } else { |
| 454 | if (map.applicationId() != null) { |
| 455 | mapsByApplication.put(map.applicationId(), map); |
| 456 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | clusterCommunicator.<MapEvent<K, V>>addSubscriber(mapUpdatesSubject(map.name()), |
| 460 | map.serializer()::decode, |
| 461 | map::notifyLocalListeners, |
| 462 | eventDispatcher); |
Brian O'Connor | 24cf656 | 2015-06-11 18:42:30 -0700 | [diff] [blame] | 463 | return map; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 466 | protected <K, V> void unregisterMap(DefaultAsyncConsistentMap<K, V> map) { |
| 467 | if (maps.remove(map.name()) != null) { |
| 468 | clusterCommunicator.removeSubscriber(mapUpdatesSubject(map.name())); |
| 469 | } |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 470 | if (map.applicationId() != null) { |
| 471 | mapsByApplication.remove(map.applicationId(), map); |
| 472 | } |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | protected <E> void registerQueue(DefaultDistributedQueue<E> queue) { |
| 476 | // TODO: Support multiple local instances of the same queue. |
| 477 | if (queues.putIfAbsent(queue.name(), queue) != null) { |
| 478 | throw new IllegalStateException("Queue by name " + queue.name() + " already exists"); |
| 479 | } |
| 480 | } |
| 481 | |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 482 | protected static MessageSubject mapUpdatesSubject(String mapName) { |
| 483 | return new MessageSubject(mapName + "-map-updates"); |
| 484 | } |
Madan Jampani | e8af1cc | 2015-06-23 14:23:31 -0700 | [diff] [blame] | 485 | |
| 486 | private class InternalApplicationListener implements ApplicationListener { |
| 487 | @Override |
| 488 | public void event(ApplicationEvent event) { |
| 489 | if (event.type() == APP_UNINSTALLED || event.type() == APP_DEACTIVATED) { |
| 490 | ApplicationId appId = event.subject().id(); |
| 491 | List<DefaultAsyncConsistentMap> mapsToRemove = ImmutableList.copyOf(mapsByApplication.get(appId)); |
| 492 | mapsToRemove.forEach(DatabaseManager.this::unregisterMap); |
| 493 | if (event.type() == APP_UNINSTALLED) { |
| 494 | mapsToRemove.stream().filter(map -> map.purgeOnUninstall()).forEach(map -> map.clear()); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
Madan Jampani | e9b781a | 2015-07-07 16:23:20 -0700 | [diff] [blame] | 499 | } |