Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 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 | package org.onosproject.store.primitives.impl; |
| 17 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 18 | import java.io.File; |
| 19 | import java.util.Collection; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | import java.util.Map; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 21 | import java.util.Optional; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 22 | import java.util.Set; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 23 | import java.util.concurrent.CompletableFuture; |
| 24 | import java.util.concurrent.atomic.AtomicBoolean; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 25 | import java.util.function.Supplier; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 27 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Collections2; |
| 29 | import com.google.common.collect.ImmutableMap; |
| 30 | import io.atomix.protocols.raft.cluster.MemberId; |
| 31 | import io.atomix.protocols.raft.service.RaftService; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterService; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 33 | import org.onosproject.cluster.NodeId; |
| 34 | import org.onosproject.cluster.Partition; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 35 | import org.onosproject.cluster.PartitionId; |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 36 | import org.onosproject.core.Version; |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 37 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 38 | import org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapService; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapService; |
| 41 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapService; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixCounterService; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeService; |
| 44 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorService; |
| 45 | import org.onosproject.store.primitives.resources.impl.AtomixWorkQueueService; |
| 46 | import org.onosproject.store.service.DistributedPrimitive; |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 47 | import org.onosproject.store.service.Ordering; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 48 | import org.onosproject.store.service.PartitionInfo; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 49 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Storage partition. |
| 53 | */ |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 54 | public abstract class StoragePartition implements Managed<StoragePartition> { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 55 | |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 56 | 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 Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 65 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 66 | 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 Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 75 | .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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 81 | .build(); |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 82 | |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 83 | public StoragePartition( |
| 84 | Partition partition, |
Jordan Halterman | 28183ee | 2017-10-17 17:29:10 -0700 | [diff] [blame] | 85 | ClusterCommunicationService clusterCommunicator, |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 86 | ClusterService clusterService) { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 87 | this.partition = partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 88 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 89 | this.localNodeId = clusterService.getLocalNode().id(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Returns the partition client instance. |
| 94 | * @return client |
| 95 | */ |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 96 | public StoragePartitionClient client() { |
| 97 | return client; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public CompletableFuture<Void> open() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 102 | if (partition.getMembers().contains(localNodeId)) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 103 | return openServer() |
| 104 | .thenCompose(v -> openClient()) |
| 105 | .thenAccept(v -> isOpened.set(true)) |
| 106 | .thenApply(v -> null); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 107 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 108 | return openClient() |
| 109 | .thenAccept(v -> isOpened.set(true)) |
| 110 | .thenApply(v -> null); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public CompletableFuture<Void> close() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 115 | // We do not explicitly close the server and instead let the cluster |
| 116 | // deal with this as an unclean exit. |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 117 | return closeClient(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 120 | /** |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 121 | * 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 Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 137 | * Returns the partition name. |
| 138 | * |
| 139 | * @return the partition name |
| 140 | */ |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 141 | public abstract String getName(); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 144 | * Returns the identifier of the {@link Partition partition} associated with this instance. |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 145 | * |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 146 | * @return partition identifier |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 147 | */ |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 148 | public PartitionId getId() { |
| 149 | return partition.getId(); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the partition version. |
| 154 | * |
| 155 | * @return the partition version |
| 156 | */ |
| 157 | public Version getVersion() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 158 | return partition.getVersion(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 161 | /** |
| 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 170 | * Returns the {@link MemberId identifiers} of partition members. |
| 171 | * @return partition member identifiers |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 172 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 173 | public Collection<MemberId> getMemberIds() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 174 | return Collections2.transform(getMembers(), n -> MemberId.from(n.id())); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Attempts to rejoin the partition. |
| 179 | * @return future that is completed after the operation is complete |
| 180 | */ |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 181 | protected abstract CompletableFuture<Void> openServer(); |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 184 | * 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 192 | StoragePartitionServer server = new StoragePartitionServer(this, |
| 193 | MemberId.from(localNodeId.id()), |
Jordan Halterman | 980a8c1 | 2017-09-22 18:01:19 -0700 | [diff] [blame] | 194 | clusterCommunicator); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 195 | return server.join(Collections2.transform(otherMembers, n -> MemberId.from(n.id()))) |
| 196 | .thenRun(() -> this.server = server); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 199 | private CompletableFuture<StoragePartitionClient> openClient() { |
| 200 | client = new StoragePartitionClient(this, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 201 | MemberId.from(localNodeId.id()), |
| 202 | new RaftClientCommunicator( |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 203 | String.format("partition-%s-%s", partition.getId(), partition.getVersion()), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 204 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
| 205 | clusterCommunicator)); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 206 | return client.open().thenApply(v -> client); |
| 207 | } |
| 208 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 209 | /** |
| 210 | * Closes the partition server if it was previously opened. |
| 211 | * @return future that is completed when the operation completes |
| 212 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 213 | public CompletableFuture<Void> leaveCluster() { |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 214 | return server != null |
| 215 | ? server.closeAndExit().thenRun(() -> server.delete()) |
| 216 | : CompletableFuture.completedFuture(null); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public boolean isOpen() { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 221 | return isOpened.get(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 224 | 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 Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 237 | private CompletableFuture<Void> closeClient() { |
| 238 | if (client != null) { |
| 239 | return client.close(); |
| 240 | } |
| 241 | return CompletableFuture.completedFuture(null); |
| 242 | } |
| 243 | |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 244 | /** |
| 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 Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 250 | return server != null && server.isOpen() ? Optional.of(server.info()) : Optional.empty(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Process updates to partitions and handles joining or leaving a partition. |
| 255 | * @param newValue new Partition |
| 256 | */ |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 257 | void onUpdate(Partition newValue) { |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 258 | boolean wasPresent = partition.getMembers().contains(localNodeId); |
| 259 | boolean isPresent = newValue.getMembers().contains(localNodeId); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 260 | this.partition = newValue; |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 261 | if ((wasPresent && isPresent) || (!wasPresent && !isPresent)) { |
| 262 | // no action needed |
| 263 | return; |
| 264 | } |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 265 | // Only need to do action if our membership changed |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 266 | if (wasPresent) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 267 | leaveCluster(); |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 268 | } else if (isPresent) { |
| 269 | joinCluster(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 270 | } |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 271 | } |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 272 | } |