Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [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 | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 36 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 37 | import org.onosproject.store.primitives.resources.impl.AtomixAtomicCounterMapService; |
| 38 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService; |
| 39 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapService; |
| 40 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapService; |
| 41 | import org.onosproject.store.primitives.resources.impl.AtomixCounterService; |
| 42 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeService; |
| 43 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorService; |
| 44 | import org.onosproject.store.primitives.resources.impl.AtomixWorkQueueService; |
| 45 | import org.onosproject.store.service.DistributedPrimitive; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 46 | import org.onosproject.store.service.PartitionInfo; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 47 | import org.onosproject.store.service.Serializer; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Storage partition. |
| 51 | */ |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 52 | public class StoragePartition implements Managed<StoragePartition> { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 53 | |
| 54 | private final AtomicBoolean isOpened = new AtomicBoolean(false); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 55 | private final ClusterCommunicationService clusterCommunicator; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 56 | private final File logFolder; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 57 | private Partition partition; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 58 | private NodeId localNodeId; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 59 | private StoragePartitionServer server; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 60 | private StoragePartitionClient client; |
| 61 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | public static final Map<String, Supplier<RaftService>> RAFT_SERVICES = |
| 63 | ImmutableMap.<String, Supplier<RaftService>>builder() |
| 64 | .put(DistributedPrimitive.Type.CONSISTENT_MAP.name(), AtomixConsistentMapService::new) |
| 65 | .put(DistributedPrimitive.Type.CONSISTENT_TREEMAP.name(), AtomixConsistentTreeMapService::new) |
| 66 | .put(DistributedPrimitive.Type.CONSISTENT_MULTIMAP.name(), AtomixConsistentSetMultimapService::new) |
| 67 | .put(DistributedPrimitive.Type.COUNTER_MAP.name(), AtomixAtomicCounterMapService::new) |
| 68 | .put(DistributedPrimitive.Type.COUNTER.name(), AtomixCounterService::new) |
| 69 | .put(DistributedPrimitive.Type.LEADER_ELECTOR.name(), AtomixLeaderElectorService::new) |
| 70 | .put(DistributedPrimitive.Type.WORK_QUEUE.name(), AtomixWorkQueueService::new) |
| 71 | .put(DistributedPrimitive.Type.DOCUMENT_TREE.name(), AtomixDocumentTreeService::new) |
| 72 | .build(); |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 73 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 74 | public StoragePartition(Partition partition, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 75 | ClusterCommunicationService clusterCommunicator, |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 76 | ClusterService clusterService, |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 77 | File logFolder) { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 78 | this.partition = partition; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 79 | this.clusterCommunicator = clusterCommunicator; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 80 | this.localNodeId = clusterService.getLocalNode().id(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 81 | this.logFolder = logFolder; |
| 82 | } |
| 83 | |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Returns the partition client instance. |
| 86 | * @return client |
| 87 | */ |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 88 | public StoragePartitionClient client() { |
| 89 | return client; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public CompletableFuture<Void> open() { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 94 | if (partition.getMembers().contains(localNodeId)) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 95 | return openServer() |
| 96 | .thenCompose(v -> openClient()) |
| 97 | .thenAccept(v -> isOpened.set(true)) |
| 98 | .thenApply(v -> null); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 99 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 100 | return openClient() |
| 101 | .thenAccept(v -> isOpened.set(true)) |
| 102 | .thenApply(v -> null); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public CompletableFuture<Void> close() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 107 | // We do not explicitly close the server and instead let the cluster |
| 108 | // deal with this as an unclean exit. |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 109 | return closeClient(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Returns the identifier of the {@link Partition partition} associated with this instance. |
| 114 | * @return partition identifier |
| 115 | */ |
| 116 | public PartitionId getId() { |
| 117 | return partition.getId(); |
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 | /** |
| 121 | * Returns the identifiers of partition members. |
| 122 | * @return partition member instance ids |
| 123 | */ |
| 124 | public Collection<NodeId> getMembers() { |
| 125 | return partition.getMembers(); |
| 126 | } |
| 127 | |
| 128 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 129 | * Returns the {@link MemberId identifiers} of partition members. |
| 130 | * @return partition member identifiers |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 131 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 132 | public Collection<MemberId> getMemberIds() { |
| 133 | return Collections2.transform(partition.getMembers(), n -> MemberId.from(n.id())); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Attempts to rejoin the partition. |
| 138 | * @return future that is completed after the operation is complete |
| 139 | */ |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 140 | private CompletableFuture<Void> openServer() { |
| 141 | if (!partition.getMembers().contains(localNodeId) || server != null) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 142 | return CompletableFuture.completedFuture(null); |
| 143 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 144 | StoragePartitionServer server = new StoragePartitionServer(this, |
| 145 | MemberId.from(localNodeId.id()), |
| 146 | () -> new RaftServerCommunicator( |
| 147 | partition.getId(), |
| 148 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
| 149 | clusterCommunicator), |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 150 | logFolder); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 151 | return server.open().thenRun(() -> this.server = server); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Attempts to join the partition as a new member. |
| 156 | * @return future that is completed after the operation is complete |
| 157 | */ |
| 158 | private CompletableFuture<Void> joinCluster() { |
| 159 | Set<NodeId> otherMembers = partition.getMembers() |
| 160 | .stream() |
| 161 | .filter(nodeId -> !nodeId.equals(localNodeId)) |
| 162 | .collect(Collectors.toSet()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 163 | StoragePartitionServer server = new StoragePartitionServer(this, |
| 164 | MemberId.from(localNodeId.id()), |
| 165 | () -> new RaftServerCommunicator( |
| 166 | partition.getId(), |
| 167 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
| 168 | clusterCommunicator), |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 169 | logFolder); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 170 | return server.join(Collections2.transform(otherMembers, n -> MemberId.from(n.id()))) |
| 171 | .thenRun(() -> this.server = server); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 174 | private CompletableFuture<StoragePartitionClient> openClient() { |
| 175 | client = new StoragePartitionClient(this, |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 176 | MemberId.from(localNodeId.id()), |
| 177 | new RaftClientCommunicator( |
| 178 | partition.getId(), |
| 179 | Serializer.using(StorageNamespaces.RAFT_PROTOCOL), |
| 180 | clusterCommunicator)); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 181 | return client.open().thenApply(v -> client); |
| 182 | } |
| 183 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Closes the partition server if it was previously opened. |
| 186 | * @return future that is completed when the operation completes |
| 187 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 188 | public CompletableFuture<Void> leaveCluster() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 189 | return server != null ? server.closeAndExit() : CompletableFuture.completedFuture(null); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public boolean isOpen() { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 194 | return isOpened.get(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private CompletableFuture<Void> closeClient() { |
| 198 | if (client != null) { |
| 199 | return client.close(); |
| 200 | } |
| 201 | return CompletableFuture.completedFuture(null); |
| 202 | } |
| 203 | |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Returns the partition information if this partition is locally managed i.e. |
| 206 | * this node is a active member of the partition. |
| 207 | * @return partition info |
| 208 | */ |
| 209 | public Optional<PartitionInfo> info() { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 210 | return server != null && server.isOpen() ? Optional.of(server.info()) : Optional.empty(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Process updates to partitions and handles joining or leaving a partition. |
| 215 | * @param newValue new Partition |
| 216 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 217 | public void onUpdate(Partition newValue) { |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 218 | |
| 219 | boolean wasPresent = partition.getMembers().contains(localNodeId); |
| 220 | boolean isPresent = newValue.getMembers().contains(localNodeId); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 221 | this.partition = newValue; |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 222 | if ((wasPresent && isPresent) || (!wasPresent && !isPresent)) { |
| 223 | // no action needed |
| 224 | return; |
| 225 | } |
| 226 | //only need to do action if our membership changed |
| 227 | if (wasPresent) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 228 | leaveCluster(); |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 229 | } else if (isPresent) { |
| 230 | joinCluster(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 231 | } |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 232 | } |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 233 | } |