blob: 9458ec54932557f8198e5dfe66b998a18bc5cab4 [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani15b8ef52016-02-02 17:35:05 -08003 *
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 */
16package org.onosproject.store.primitives.impl;
17
Madan Jampani15b8ef52016-02-02 17:35:05 -080018import java.io.File;
19import java.util.Collection;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import java.util.Map;
Madan Jampani15b8ef52016-02-02 17:35:05 -080021import java.util.Optional;
Madan Jampanif172d402016-03-04 00:56:38 -080022import java.util.Set;
Madan Jampani15b8ef52016-02-02 17:35:05 -080023import java.util.concurrent.CompletableFuture;
24import java.util.concurrent.atomic.AtomicBoolean;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070025import java.util.function.Supplier;
Madan Jampanif172d402016-03-04 00:56:38 -080026import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027
Jordan Halterman2bf177c2017-06-29 01:49:08 -070028import com.google.common.collect.Collections2;
29import com.google.common.collect.ImmutableMap;
30import io.atomix.protocols.raft.cluster.MemberId;
31import io.atomix.protocols.raft.service.RaftService;
Jordan Halterman28183ee2017-10-17 17:29:10 -070032import org.onosproject.cluster.ClusterService;
Madan Jampani15b8ef52016-02-02 17:35:05 -080033import org.onosproject.cluster.NodeId;
34import org.onosproject.cluster.Partition;
Madan Jampani33547452016-02-29 16:45:04 -080035import org.onosproject.cluster.PartitionId;
Jordan Halterman980a8c12017-09-22 18:01:19 -070036import org.onosproject.core.Version;
Jordan Halterman28183ee2017-10-17 17:29:10 -070037import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070038import org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapService;
39import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService;
40import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapService;
41import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapService;
42import org.onosproject.store.primitives.resources.impl.AtomixCounterService;
Jordan Halterman47432582018-01-25 16:56:45 -080043import org.onosproject.store.primitives.resources.impl.AtomixDistributedLockService;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070044import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeService;
45import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorService;
46import org.onosproject.store.primitives.resources.impl.AtomixWorkQueueService;
47import org.onosproject.store.service.DistributedPrimitive;
Jordan Haltermand0d80352017-08-10 15:08:27 -070048import org.onosproject.store.service.Ordering;
Madan Jampanie14a09c2016-02-11 10:43:21 -080049import org.onosproject.store.service.PartitionInfo;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070050import org.onosproject.store.service.Serializer;
Madan Jampani15b8ef52016-02-02 17:35:05 -080051
52/**
53 * Storage partition.
54 */
Jordan Halterman07f052b2017-10-08 14:22:41 -070055public abstract class StoragePartition implements Managed<StoragePartition> {
Madan Jampani15b8ef52016-02-02 17:35:05 -080056
Jordan Halterman07f052b2017-10-08 14:22:41 -070057 static final String PARTITIONS_DIR =
58 System.getProperty("karaf.data") + "/db/partitions/";
59
60 protected final AtomicBoolean isOpened = new AtomicBoolean(false);
61 protected final ClusterCommunicationService clusterCommunicator;
62 protected Partition partition;
63 protected NodeId localNodeId;
64 protected StoragePartitionServer server;
65 protected StoragePartitionClient client;
Madan Jampani15b8ef52016-02-02 17:35:05 -080066
Jordan Halterman2bf177c2017-06-29 01:49:08 -070067 public static final Map<String, Supplier<RaftService>> RAFT_SERVICES =
68 ImmutableMap.<String, Supplier<RaftService>>builder()
69 .put(DistributedPrimitive.Type.CONSISTENT_MAP.name(), AtomixConsistentMapService::new)
70 .put(DistributedPrimitive.Type.CONSISTENT_TREEMAP.name(), AtomixConsistentTreeMapService::new)
71 .put(DistributedPrimitive.Type.CONSISTENT_MULTIMAP.name(), AtomixConsistentSetMultimapService::new)
72 .put(DistributedPrimitive.Type.COUNTER_MAP.name(), AtomixAtomicCounterMapService::new)
73 .put(DistributedPrimitive.Type.COUNTER.name(), AtomixCounterService::new)
74 .put(DistributedPrimitive.Type.LEADER_ELECTOR.name(), AtomixLeaderElectorService::new)
75 .put(DistributedPrimitive.Type.WORK_QUEUE.name(), AtomixWorkQueueService::new)
Jordan Haltermand0d80352017-08-10 15:08:27 -070076 .put(DistributedPrimitive.Type.DOCUMENT_TREE.name(),
77 () -> new AtomixDocumentTreeService(Ordering.NATURAL))
78 .put(String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), Ordering.NATURAL),
79 () -> new AtomixDocumentTreeService(Ordering.NATURAL))
80 .put(String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), Ordering.INSERTION),
81 () -> new AtomixDocumentTreeService(Ordering.INSERTION))
Jordan Halterman47432582018-01-25 16:56:45 -080082 .put(DistributedPrimitive.Type.LOCK.name(), AtomixDistributedLockService::new)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070083 .build();
Madan Jampani65f24bb2016-03-15 15:16:18 -070084
Jordan Halterman980a8c12017-09-22 18:01:19 -070085 public StoragePartition(
86 Partition partition,
Jordan Halterman28183ee2017-10-17 17:29:10 -070087 ClusterCommunicationService clusterCommunicator,
Jordan Halterman07f052b2017-10-08 14:22:41 -070088 ClusterService clusterService) {
Madan Jampani33547452016-02-29 16:45:04 -080089 this.partition = partition;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070090 this.clusterCommunicator = clusterCommunicator;
Madan Jampani15b8ef52016-02-02 17:35:05 -080091 this.localNodeId = clusterService.getLocalNode().id();
Madan Jampani15b8ef52016-02-02 17:35:05 -080092 }
93
Madan Jampani3a9911c2016-02-21 11:25:45 -080094 /**
95 * Returns the partition client instance.
96 * @return client
97 */
Madan Jampani15b8ef52016-02-02 17:35:05 -080098 public StoragePartitionClient client() {
99 return client;
100 }
101
102 @Override
103 public CompletableFuture<Void> open() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700104 if (partition.getMembers().contains(localNodeId)) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700105 return openServer()
106 .thenCompose(v -> openClient())
107 .thenAccept(v -> isOpened.set(true))
108 .thenApply(v -> null);
Madan Jampanif172d402016-03-04 00:56:38 -0800109 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700110 return openClient()
111 .thenAccept(v -> isOpened.set(true))
112 .thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800113 }
114
115 @Override
116 public CompletableFuture<Void> close() {
Madan Jampani33547452016-02-29 16:45:04 -0800117 // We do not explicitly close the server and instead let the cluster
118 // deal with this as an unclean exit.
Madan Jampani65f24bb2016-03-15 15:16:18 -0700119 return closeClient();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800120 }
121
Madan Jampani33547452016-02-29 16:45:04 -0800122 /**
Jordan Halterman07f052b2017-10-08 14:22:41 -0700123 * Deletes the partition.
124 *
125 * @return future to be completed once the partition has been deleted
126 */
127 public CompletableFuture<Void> delete() {
128 return closeServer().thenCompose(v -> closeClient()).thenRun(() -> deleteServer());
129 }
130
131 /**
132 * Returns the partition data folder.
133 *
134 * @return the partition data folder
135 */
136 public abstract File getDataFolder();
137
138 /**
Jordan Halterman980a8c12017-09-22 18:01:19 -0700139 * Returns the partition name.
140 *
141 * @return the partition name
142 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700143 public abstract String getName();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700144
145 /**
Jordan Halterman07f052b2017-10-08 14:22:41 -0700146 * Returns the identifier of the {@link Partition partition} associated with this instance.
Jordan Halterman980a8c12017-09-22 18:01:19 -0700147 *
Jordan Halterman07f052b2017-10-08 14:22:41 -0700148 * @return partition identifier
Jordan Halterman980a8c12017-09-22 18:01:19 -0700149 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700150 public PartitionId getId() {
151 return partition.getId();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700152 }
153
154 /**
155 * Returns the partition version.
156 *
157 * @return the partition version
158 */
159 public Version getVersion() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700160 return partition.getVersion();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800161 }
162
Madan Jampani33547452016-02-29 16:45:04 -0800163 /**
164 * Returns the identifiers of partition members.
165 * @return partition member instance ids
166 */
167 public Collection<NodeId> getMembers() {
168 return partition.getMembers();
169 }
170
171 /**
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700172 * Returns the {@link MemberId identifiers} of partition members.
173 * @return partition member identifiers
Madan Jampani33547452016-02-29 16:45:04 -0800174 */
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700175 public Collection<MemberId> getMemberIds() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700176 return Collections2.transform(getMembers(), n -> MemberId.from(n.id()));
Madan Jampani33547452016-02-29 16:45:04 -0800177 }
178
Madan Jampanif172d402016-03-04 00:56:38 -0800179 /**
180 * Attempts to rejoin the partition.
181 * @return future that is completed after the operation is complete
182 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700183 protected abstract CompletableFuture<Void> openServer();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700184
185 /**
Madan Jampanif172d402016-03-04 00:56:38 -0800186 * Attempts to join the partition as a new member.
187 * @return future that is completed after the operation is complete
188 */
189 private CompletableFuture<Void> joinCluster() {
190 Set<NodeId> otherMembers = partition.getMembers()
191 .stream()
192 .filter(nodeId -> !nodeId.equals(localNodeId))
193 .collect(Collectors.toSet());
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700194 StoragePartitionServer server = new StoragePartitionServer(this,
195 MemberId.from(localNodeId.id()),
Jordan Halterman980a8c12017-09-22 18:01:19 -0700196 clusterCommunicator);
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700197 return server.join(Collections2.transform(otherMembers, n -> MemberId.from(n.id())))
198 .thenRun(() -> this.server = server);
Madan Jampanif172d402016-03-04 00:56:38 -0800199 }
200
Madan Jampani15b8ef52016-02-02 17:35:05 -0800201 private CompletableFuture<StoragePartitionClient> openClient() {
202 client = new StoragePartitionClient(this,
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700203 MemberId.from(localNodeId.id()),
204 new RaftClientCommunicator(
Jordan Halterman07f052b2017-10-08 14:22:41 -0700205 String.format("partition-%s-%s", partition.getId(), partition.getVersion()),
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700206 Serializer.using(StorageNamespaces.RAFT_PROTOCOL),
207 clusterCommunicator));
Madan Jampani15b8ef52016-02-02 17:35:05 -0800208 return client.open().thenApply(v -> client);
209 }
210
Madan Jampani33547452016-02-29 16:45:04 -0800211 /**
212 * Closes the partition server if it was previously opened.
213 * @return future that is completed when the operation completes
214 */
Madan Jampanif172d402016-03-04 00:56:38 -0800215 public CompletableFuture<Void> leaveCluster() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700216 return server != null
217 ? server.closeAndExit().thenRun(() -> server.delete())
218 : CompletableFuture.completedFuture(null);
Madan Jampani33547452016-02-29 16:45:04 -0800219 }
220
221 @Override
222 public boolean isOpen() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700223 return isOpened.get();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800224 }
225
Jordan Halterman07f052b2017-10-08 14:22:41 -0700226 private CompletableFuture<Void> closeServer() {
227 if (server != null) {
228 return server.close();
229 }
230 return CompletableFuture.completedFuture(null);
231 }
232
233 private void deleteServer() {
234 if (server != null) {
235 server.delete();
236 }
237 }
238
Madan Jampani15b8ef52016-02-02 17:35:05 -0800239 private CompletableFuture<Void> closeClient() {
240 if (client != null) {
241 return client.close();
242 }
243 return CompletableFuture.completedFuture(null);
244 }
245
Madan Jampanie14a09c2016-02-11 10:43:21 -0800246 /**
247 * Returns the partition information if this partition is locally managed i.e.
248 * this node is a active member of the partition.
249 * @return partition info
250 */
251 public Optional<PartitionInfo> info() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700252 return server != null && server.isOpen() ? Optional.of(server.info()) : Optional.empty();
Madan Jampani33547452016-02-29 16:45:04 -0800253 }
254
Jon Hall1195afb2016-06-28 18:54:07 -0700255 /**
256 * Process updates to partitions and handles joining or leaving a partition.
257 * @param newValue new Partition
258 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700259 void onUpdate(Partition newValue) {
Jon Hall1195afb2016-06-28 18:54:07 -0700260 boolean wasPresent = partition.getMembers().contains(localNodeId);
261 boolean isPresent = newValue.getMembers().contains(localNodeId);
Madan Jampanif172d402016-03-04 00:56:38 -0800262 this.partition = newValue;
Jon Hall1195afb2016-06-28 18:54:07 -0700263 if ((wasPresent && isPresent) || (!wasPresent && !isPresent)) {
264 // no action needed
265 return;
266 }
Jordan Halterman07f052b2017-10-08 14:22:41 -0700267 // Only need to do action if our membership changed
Jon Hall1195afb2016-06-28 18:54:07 -0700268 if (wasPresent) {
Madan Jampanif172d402016-03-04 00:56:38 -0800269 leaveCluster();
Ray Milkeyfe0e0852018-01-18 11:14:05 -0800270 } else {
Jon Hall1195afb2016-06-28 18:54:07 -0700271 joinCluster();
Madan Jampani33547452016-02-29 16:45:04 -0800272 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800273 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800274}