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 | |
| 18 | import io.atomix.catalyst.serializer.Serializer; |
| 19 | import io.atomix.catalyst.transport.Address; |
| 20 | import io.atomix.resource.ResourceType; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 21 | |
| 22 | import java.io.File; |
| 23 | import java.util.Collection; |
| 24 | import java.util.Optional; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 25 | import java.util.Set; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 26 | import java.util.concurrent.CompletableFuture; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 27 | import java.util.concurrent.Executor; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 28 | import java.util.concurrent.atomic.AtomicBoolean; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 29 | import java.util.stream.Collectors; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 30 | |
| 31 | import org.onosproject.cluster.ClusterService; |
| 32 | import org.onosproject.cluster.ControllerNode; |
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; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 36 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 37 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 38 | import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 39 | import org.onosproject.store.service.PartitionInfo; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 40 | |
| 41 | import com.google.common.collect.Collections2; |
| 42 | import com.google.common.collect.ImmutableSet; |
| 43 | |
| 44 | /** |
| 45 | * Storage partition. |
| 46 | */ |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 47 | public class StoragePartition implements Managed<StoragePartition> { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 48 | |
| 49 | private final AtomicBoolean isOpened = new AtomicBoolean(false); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 50 | private final Serializer serializer; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 51 | private final Executor sharedExecutor; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 52 | private final MessagingService messagingService; |
| 53 | private final ClusterService clusterService; |
| 54 | private final File logFolder; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 55 | private Partition partition; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 56 | private NodeId localNodeId; |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 57 | private StoragePartitionServer server; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 58 | private StoragePartitionClient client; |
| 59 | |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 60 | public static final Collection<ResourceType> RESOURCE_TYPES = ImmutableSet.of( |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 61 | new ResourceType(AtomixLeaderElector.class), |
| 62 | new ResourceType(AtomixConsistentMap.class)); |
| 63 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 64 | public StoragePartition(Partition partition, |
| 65 | MessagingService messagingService, |
| 66 | ClusterService clusterService, |
| 67 | Serializer serializer, |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 68 | Executor sharedExecutor, |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 69 | File logFolder) { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 70 | this.partition = partition; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 71 | this.messagingService = messagingService; |
| 72 | this.clusterService = clusterService; |
| 73 | this.localNodeId = clusterService.getLocalNode().id(); |
| 74 | this.serializer = serializer; |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 75 | this.sharedExecutor = sharedExecutor; |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 76 | this.logFolder = logFolder; |
| 77 | } |
| 78 | |
Madan Jampani | 3a9911c | 2016-02-21 11:25:45 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Returns the partition client instance. |
| 81 | * @return client |
| 82 | */ |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 83 | public StoragePartitionClient client() { |
| 84 | return client; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public CompletableFuture<Void> open() { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 89 | if (partition.getMembers().contains(localNodeId)) { |
| 90 | openServer(); |
| 91 | } |
Madan Jampani | c94b485 | 2016-02-23 18:18:37 -0800 | [diff] [blame] | 92 | return openClient().thenAccept(v -> isOpened.set(true)) |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 93 | .thenApply(v -> null); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Void> close() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 98 | // We do not explicitly close the server and instead let the cluster |
| 99 | // deal with this as an unclean exit. |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 100 | return closeClient(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Returns the identifier of the {@link Partition partition} associated with this instance. |
| 105 | * @return partition identifier |
| 106 | */ |
| 107 | public PartitionId getId() { |
| 108 | return partition.getId(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Returns the identifiers of partition members. |
| 113 | * @return partition member instance ids |
| 114 | */ |
| 115 | public Collection<NodeId> getMembers() { |
| 116 | return partition.getMembers(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the {@link Address addresses} of partition members. |
| 121 | * @return partition member addresses |
| 122 | */ |
| 123 | public Collection<Address> getMemberAddresses() { |
| 124 | return Collections2.transform(partition.getMembers(), this::toAddress); |
| 125 | } |
| 126 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Attempts to rejoin the partition. |
| 129 | * @return future that is completed after the operation is complete |
| 130 | */ |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 131 | private CompletableFuture<Void> openServer() { |
| 132 | if (!partition.getMembers().contains(localNodeId) || server != null) { |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 133 | return CompletableFuture.completedFuture(null); |
| 134 | } |
| 135 | StoragePartitionServer server = new StoragePartitionServer(toAddress(localNodeId), |
| 136 | this, |
| 137 | serializer, |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 138 | () -> new CopycatTransport(partition.getId(), messagingService), |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 139 | logFolder); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 140 | return server.open().thenRun(() -> this.server = server); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Attempts to join the partition as a new member. |
| 145 | * @return future that is completed after the operation is complete |
| 146 | */ |
| 147 | private CompletableFuture<Void> joinCluster() { |
| 148 | Set<NodeId> otherMembers = partition.getMembers() |
| 149 | .stream() |
| 150 | .filter(nodeId -> !nodeId.equals(localNodeId)) |
| 151 | .collect(Collectors.toSet()); |
| 152 | StoragePartitionServer server = new StoragePartitionServer(toAddress(localNodeId), |
| 153 | this, |
| 154 | serializer, |
Jordan Halterman | e9c3709 | 2017-03-21 11:16:14 -0700 | [diff] [blame] | 155 | () -> new CopycatTransport(partition.getId(), messagingService), |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 156 | logFolder); |
| 157 | return server.join(Collections2.transform(otherMembers, this::toAddress)).thenRun(() -> this.server = server); |
| 158 | } |
| 159 | |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 160 | private CompletableFuture<StoragePartitionClient> openClient() { |
| 161 | client = new StoragePartitionClient(this, |
| 162 | serializer, |
Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame] | 163 | new CopycatTransport(partition.getId(), messagingService), |
| 164 | sharedExecutor); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 165 | return client.open().thenApply(v -> client); |
| 166 | } |
| 167 | |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Closes the partition server if it was previously opened. |
| 170 | * @return future that is completed when the operation completes |
| 171 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 172 | public CompletableFuture<Void> leaveCluster() { |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 173 | return server != null ? server.closeAndExit() : CompletableFuture.completedFuture(null); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean isOpen() { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 178 | return isOpened.get(); |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | private CompletableFuture<Void> closeClient() { |
| 182 | if (client != null) { |
| 183 | return client.close(); |
| 184 | } |
| 185 | return CompletableFuture.completedFuture(null); |
| 186 | } |
| 187 | |
| 188 | private Address toAddress(NodeId nodeId) { |
| 189 | ControllerNode node = clusterService.getNode(nodeId); |
| 190 | return new Address(node.ip().toString(), node.tcpPort()); |
| 191 | } |
| 192 | |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 193 | /** |
| 194 | * Returns the partition information if this partition is locally managed i.e. |
| 195 | * this node is a active member of the partition. |
| 196 | * @return partition info |
| 197 | */ |
| 198 | public Optional<PartitionInfo> info() { |
Madan Jampani | 65f24bb | 2016-03-15 15:16:18 -0700 | [diff] [blame] | 199 | return server != null && server.isOpen() ? Optional.of(server.info()) : Optional.empty(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Process updates to partitions and handles joining or leaving a partition. |
| 204 | * @param newValue new Partition |
| 205 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 206 | public void onUpdate(Partition newValue) { |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 207 | |
| 208 | boolean wasPresent = partition.getMembers().contains(localNodeId); |
| 209 | boolean isPresent = newValue.getMembers().contains(localNodeId); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 210 | this.partition = newValue; |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 211 | if ((wasPresent && isPresent) || (!wasPresent && !isPresent)) { |
| 212 | // no action needed |
| 213 | return; |
| 214 | } |
| 215 | //only need to do action if our membership changed |
| 216 | if (wasPresent) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 217 | leaveCluster(); |
Jon Hall | 1195afb | 2016-06-28 18:54:07 -0700 | [diff] [blame] | 218 | } else if (isPresent) { |
| 219 | joinCluster(); |
Madan Jampani | 3354745 | 2016-02-29 16:45:04 -0800 | [diff] [blame] | 220 | } |
Madan Jampani | e14a09c | 2016-02-11 10:43:21 -0800 | [diff] [blame] | 221 | } |
Madan Jampani | 15b8ef5 | 2016-02-02 17:35:05 -0800 | [diff] [blame] | 222 | } |