blob: c419d981843e9027ebf738b69cb4ae8c77158f99 [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;
43import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeService;
44import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorService;
45import org.onosproject.store.primitives.resources.impl.AtomixWorkQueueService;
46import org.onosproject.store.service.DistributedPrimitive;
Jordan Haltermand0d80352017-08-10 15:08:27 -070047import org.onosproject.store.service.Ordering;
Madan Jampanie14a09c2016-02-11 10:43:21 -080048import org.onosproject.store.service.PartitionInfo;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049import org.onosproject.store.service.Serializer;
Madan Jampani15b8ef52016-02-02 17:35:05 -080050
51/**
52 * Storage partition.
53 */
Jordan Halterman07f052b2017-10-08 14:22:41 -070054public abstract class StoragePartition implements Managed<StoragePartition> {
Madan Jampani15b8ef52016-02-02 17:35:05 -080055
Jordan Halterman07f052b2017-10-08 14:22:41 -070056 static final String PARTITIONS_DIR =
57 System.getProperty("karaf.data") + "/db/partitions/";
58
59 protected final AtomicBoolean isOpened = new AtomicBoolean(false);
60 protected final ClusterCommunicationService clusterCommunicator;
61 protected Partition partition;
62 protected NodeId localNodeId;
63 protected StoragePartitionServer server;
64 protected StoragePartitionClient client;
Madan Jampani15b8ef52016-02-02 17:35:05 -080065
Jordan Halterman2bf177c2017-06-29 01:49:08 -070066 public static final Map<String, Supplier<RaftService>> RAFT_SERVICES =
67 ImmutableMap.<String, Supplier<RaftService>>builder()
68 .put(DistributedPrimitive.Type.CONSISTENT_MAP.name(), AtomixConsistentMapService::new)
69 .put(DistributedPrimitive.Type.CONSISTENT_TREEMAP.name(), AtomixConsistentTreeMapService::new)
70 .put(DistributedPrimitive.Type.CONSISTENT_MULTIMAP.name(), AtomixConsistentSetMultimapService::new)
71 .put(DistributedPrimitive.Type.COUNTER_MAP.name(), AtomixAtomicCounterMapService::new)
72 .put(DistributedPrimitive.Type.COUNTER.name(), AtomixCounterService::new)
73 .put(DistributedPrimitive.Type.LEADER_ELECTOR.name(), AtomixLeaderElectorService::new)
74 .put(DistributedPrimitive.Type.WORK_QUEUE.name(), AtomixWorkQueueService::new)
Jordan Haltermand0d80352017-08-10 15:08:27 -070075 .put(DistributedPrimitive.Type.DOCUMENT_TREE.name(),
76 () -> new AtomixDocumentTreeService(Ordering.NATURAL))
77 .put(String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), Ordering.NATURAL),
78 () -> new AtomixDocumentTreeService(Ordering.NATURAL))
79 .put(String.format("%s-%s", DistributedPrimitive.Type.DOCUMENT_TREE.name(), Ordering.INSERTION),
80 () -> new AtomixDocumentTreeService(Ordering.INSERTION))
Jordan Halterman2bf177c2017-06-29 01:49:08 -070081 .build();
Madan Jampani65f24bb2016-03-15 15:16:18 -070082
Jordan Halterman980a8c12017-09-22 18:01:19 -070083 public StoragePartition(
84 Partition partition,
Jordan Halterman28183ee2017-10-17 17:29:10 -070085 ClusterCommunicationService clusterCommunicator,
Jordan Halterman07f052b2017-10-08 14:22:41 -070086 ClusterService clusterService) {
Madan Jampani33547452016-02-29 16:45:04 -080087 this.partition = partition;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070088 this.clusterCommunicator = clusterCommunicator;
Madan Jampani15b8ef52016-02-02 17:35:05 -080089 this.localNodeId = clusterService.getLocalNode().id();
Madan Jampani15b8ef52016-02-02 17:35:05 -080090 }
91
Madan Jampani3a9911c2016-02-21 11:25:45 -080092 /**
93 * Returns the partition client instance.
94 * @return client
95 */
Madan Jampani15b8ef52016-02-02 17:35:05 -080096 public StoragePartitionClient client() {
97 return client;
98 }
99
100 @Override
101 public CompletableFuture<Void> open() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700102 if (partition.getMembers().contains(localNodeId)) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700103 return openServer()
104 .thenCompose(v -> openClient())
105 .thenAccept(v -> isOpened.set(true))
106 .thenApply(v -> null);
Madan Jampanif172d402016-03-04 00:56:38 -0800107 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700108 return openClient()
109 .thenAccept(v -> isOpened.set(true))
110 .thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800111 }
112
113 @Override
114 public CompletableFuture<Void> close() {
Madan Jampani33547452016-02-29 16:45:04 -0800115 // We do not explicitly close the server and instead let the cluster
116 // deal with this as an unclean exit.
Madan Jampani65f24bb2016-03-15 15:16:18 -0700117 return closeClient();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800118 }
119
Madan Jampani33547452016-02-29 16:45:04 -0800120 /**
Jordan Halterman07f052b2017-10-08 14:22:41 -0700121 * Deletes the partition.
122 *
123 * @return future to be completed once the partition has been deleted
124 */
125 public CompletableFuture<Void> delete() {
126 return closeServer().thenCompose(v -> closeClient()).thenRun(() -> deleteServer());
127 }
128
129 /**
130 * Returns the partition data folder.
131 *
132 * @return the partition data folder
133 */
134 public abstract File getDataFolder();
135
136 /**
Jordan Halterman980a8c12017-09-22 18:01:19 -0700137 * Returns the partition name.
138 *
139 * @return the partition name
140 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700141 public abstract String getName();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700142
143 /**
Jordan Halterman07f052b2017-10-08 14:22:41 -0700144 * Returns the identifier of the {@link Partition partition} associated with this instance.
Jordan Halterman980a8c12017-09-22 18:01:19 -0700145 *
Jordan Halterman07f052b2017-10-08 14:22:41 -0700146 * @return partition identifier
Jordan Halterman980a8c12017-09-22 18:01:19 -0700147 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700148 public PartitionId getId() {
149 return partition.getId();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700150 }
151
152 /**
153 * Returns the partition version.
154 *
155 * @return the partition version
156 */
157 public Version getVersion() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700158 return partition.getVersion();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800159 }
160
Madan Jampani33547452016-02-29 16:45:04 -0800161 /**
162 * Returns the identifiers of partition members.
163 * @return partition member instance ids
164 */
165 public Collection<NodeId> getMembers() {
166 return partition.getMembers();
167 }
168
169 /**
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700170 * Returns the {@link MemberId identifiers} of partition members.
171 * @return partition member identifiers
Madan Jampani33547452016-02-29 16:45:04 -0800172 */
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700173 public Collection<MemberId> getMemberIds() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700174 return Collections2.transform(getMembers(), n -> MemberId.from(n.id()));
Madan Jampani33547452016-02-29 16:45:04 -0800175 }
176
Madan Jampanif172d402016-03-04 00:56:38 -0800177 /**
178 * Attempts to rejoin the partition.
179 * @return future that is completed after the operation is complete
180 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700181 protected abstract CompletableFuture<Void> openServer();
Jordan Halterman980a8c12017-09-22 18:01:19 -0700182
183 /**
Madan Jampanif172d402016-03-04 00:56:38 -0800184 * Attempts to join the partition as a new member.
185 * @return future that is completed after the operation is complete
186 */
187 private CompletableFuture<Void> joinCluster() {
188 Set<NodeId> otherMembers = partition.getMembers()
189 .stream()
190 .filter(nodeId -> !nodeId.equals(localNodeId))
191 .collect(Collectors.toSet());
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700192 StoragePartitionServer server = new StoragePartitionServer(this,
193 MemberId.from(localNodeId.id()),
Jordan Halterman980a8c12017-09-22 18:01:19 -0700194 clusterCommunicator);
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700195 return server.join(Collections2.transform(otherMembers, n -> MemberId.from(n.id())))
196 .thenRun(() -> this.server = server);
Madan Jampanif172d402016-03-04 00:56:38 -0800197 }
198
Madan Jampani15b8ef52016-02-02 17:35:05 -0800199 private CompletableFuture<StoragePartitionClient> openClient() {
200 client = new StoragePartitionClient(this,
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700201 MemberId.from(localNodeId.id()),
202 new RaftClientCommunicator(
Jordan Halterman07f052b2017-10-08 14:22:41 -0700203 String.format("partition-%s-%s", partition.getId(), partition.getVersion()),
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700204 Serializer.using(StorageNamespaces.RAFT_PROTOCOL),
205 clusterCommunicator));
Madan Jampani15b8ef52016-02-02 17:35:05 -0800206 return client.open().thenApply(v -> client);
207 }
208
Madan Jampani33547452016-02-29 16:45:04 -0800209 /**
210 * Closes the partition server if it was previously opened.
211 * @return future that is completed when the operation completes
212 */
Madan Jampanif172d402016-03-04 00:56:38 -0800213 public CompletableFuture<Void> leaveCluster() {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700214 return server != null
215 ? server.closeAndExit().thenRun(() -> server.delete())
216 : CompletableFuture.completedFuture(null);
Madan Jampani33547452016-02-29 16:45:04 -0800217 }
218
219 @Override
220 public boolean isOpen() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700221 return isOpened.get();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800222 }
223
Jordan Halterman07f052b2017-10-08 14:22:41 -0700224 private CompletableFuture<Void> closeServer() {
225 if (server != null) {
226 return server.close();
227 }
228 return CompletableFuture.completedFuture(null);
229 }
230
231 private void deleteServer() {
232 if (server != null) {
233 server.delete();
234 }
235 }
236
Madan Jampani15b8ef52016-02-02 17:35:05 -0800237 private CompletableFuture<Void> closeClient() {
238 if (client != null) {
239 return client.close();
240 }
241 return CompletableFuture.completedFuture(null);
242 }
243
Madan Jampanie14a09c2016-02-11 10:43:21 -0800244 /**
245 * Returns the partition information if this partition is locally managed i.e.
246 * this node is a active member of the partition.
247 * @return partition info
248 */
249 public Optional<PartitionInfo> info() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700250 return server != null && server.isOpen() ? Optional.of(server.info()) : Optional.empty();
Madan Jampani33547452016-02-29 16:45:04 -0800251 }
252
Jon Hall1195afb2016-06-28 18:54:07 -0700253 /**
254 * Process updates to partitions and handles joining or leaving a partition.
255 * @param newValue new Partition
256 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700257 void onUpdate(Partition newValue) {
Jon Hall1195afb2016-06-28 18:54:07 -0700258 boolean wasPresent = partition.getMembers().contains(localNodeId);
259 boolean isPresent = newValue.getMembers().contains(localNodeId);
Madan Jampanif172d402016-03-04 00:56:38 -0800260 this.partition = newValue;
Jon Hall1195afb2016-06-28 18:54:07 -0700261 if ((wasPresent && isPresent) || (!wasPresent && !isPresent)) {
262 // no action needed
263 return;
264 }
Jordan Halterman07f052b2017-10-08 14:22:41 -0700265 // Only need to do action if our membership changed
Jon Hall1195afb2016-06-28 18:54:07 -0700266 if (wasPresent) {
Madan Jampanif172d402016-03-04 00:56:38 -0800267 leaveCluster();
Jon Hall1195afb2016-06-28 18:54:07 -0700268 } else if (isPresent) {
269 joinCluster();
Madan Jampani33547452016-02-29 16:45:04 -0800270 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800271 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800272}