blob: 89e1e82a79bcda077b71dcfacf8111856cb2ff86 [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.store.primitives.impl;
17
18import io.atomix.catalyst.serializer.Serializer;
19import io.atomix.catalyst.transport.Address;
20import io.atomix.resource.ResourceType;
21import io.atomix.variables.DistributedLong;
22
23import java.io.File;
24import java.util.Collection;
25import java.util.Optional;
Madan Jampanif172d402016-03-04 00:56:38 -080026import java.util.Set;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027import java.util.concurrent.CompletableFuture;
28import java.util.concurrent.atomic.AtomicBoolean;
Madan Jampanif172d402016-03-04 00:56:38 -080029import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080030
31import org.onosproject.cluster.ClusterService;
32import org.onosproject.cluster.ControllerNode;
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;
Madan Jampani15b8ef52016-02-02 17:35:05 -080036import org.onosproject.store.cluster.messaging.MessagingService;
37import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap;
Madan Jampani39fff102016-02-14 13:17:28 -080038import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector;
Madan Jampanie14a09c2016-02-11 10:43:21 -080039import org.onosproject.store.service.PartitionInfo;
Madan Jampani15b8ef52016-02-02 17:35:05 -080040
41import com.google.common.collect.Collections2;
42import com.google.common.collect.ImmutableSet;
43
44/**
45 * Storage partition.
46 */
Madan Jampani33547452016-02-29 16:45:04 -080047public class StoragePartition implements Managed<StoragePartition> {
Madan Jampani15b8ef52016-02-02 17:35:05 -080048
49 private final AtomicBoolean isOpened = new AtomicBoolean(false);
50 private final AtomicBoolean isClosed = new AtomicBoolean(false);
51 private final Serializer serializer;
52 private final MessagingService messagingService;
53 private final ClusterService clusterService;
54 private final File logFolder;
Madan Jampani33547452016-02-29 16:45:04 -080055 private Partition partition;
Madan Jampani15b8ef52016-02-02 17:35:05 -080056 private static final Collection<ResourceType> RESOURCE_TYPES = ImmutableSet.of(
57 new ResourceType(DistributedLong.class),
Madan Jampani39fff102016-02-14 13:17:28 -080058 new ResourceType(AtomixLeaderElector.class),
Madan Jampani15b8ef52016-02-02 17:35:05 -080059 new ResourceType(AtomixConsistentMap.class));
60
61 private NodeId localNodeId;
Madan Jampani33547452016-02-29 16:45:04 -080062 private StoragePartitionServer server;
Madan Jampani15b8ef52016-02-02 17:35:05 -080063 private StoragePartitionClient client;
64
65 public StoragePartition(Partition partition,
66 MessagingService messagingService,
67 ClusterService clusterService,
68 Serializer serializer,
69 File logFolder) {
Madan Jampani33547452016-02-29 16:45:04 -080070 this.partition = partition;
Madan Jampani15b8ef52016-02-02 17:35:05 -080071 this.messagingService = messagingService;
72 this.clusterService = clusterService;
73 this.localNodeId = clusterService.getLocalNode().id();
74 this.serializer = serializer;
75 this.logFolder = logFolder;
76 }
77
Madan Jampani3a9911c2016-02-21 11:25:45 -080078 /**
79 * Returns the partition client instance.
80 * @return client
81 */
Madan Jampani15b8ef52016-02-02 17:35:05 -080082 public StoragePartitionClient client() {
83 return client;
84 }
85
86 @Override
87 public CompletableFuture<Void> open() {
Madan Jampanif172d402016-03-04 00:56:38 -080088 if (partition.getMembers().contains(localNodeId)) {
89 openServer();
90 }
Madan Jampanic94b4852016-02-23 18:18:37 -080091 return openClient().thenAccept(v -> isOpened.set(true))
Madan Jampani15b8ef52016-02-02 17:35:05 -080092 .thenApply(v -> null);
93 }
94
95 @Override
96 public CompletableFuture<Void> close() {
Madan Jampani33547452016-02-29 16:45:04 -080097 // We do not explicitly close the server and instead let the cluster
98 // deal with this as an unclean exit.
99 return closeClient().thenAccept(v -> isClosed.set(true))
Madan Jampani15b8ef52016-02-02 17:35:05 -0800100 .thenApply(v -> null);
101 }
102
Madan Jampani33547452016-02-29 16:45:04 -0800103 /**
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 Jampani15b8ef52016-02-02 17:35:05 -0800109 }
110
Madan Jampani33547452016-02-29 16:45:04 -0800111 /**
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 Jampanif172d402016-03-04 00:56:38 -0800127 /**
128 * Attempts to rejoin the partition.
129 * @return future that is completed after the operation is complete
130 */
Madan Jampani33547452016-02-29 16:45:04 -0800131 private CompletableFuture<Void> openServer() {
132 if (!partition.getMembers().contains(localNodeId) || server != null) {
Madan Jampani15b8ef52016-02-02 17:35:05 -0800133 return CompletableFuture.completedFuture(null);
134 }
135 StoragePartitionServer server = new StoragePartitionServer(toAddress(localNodeId),
136 this,
137 serializer,
138 () -> new CopycatTransport(CopycatTransport.Mode.SERVER,
Madan Jampani33547452016-02-29 16:45:04 -0800139 partition.getId(),
Madan Jampani15b8ef52016-02-02 17:35:05 -0800140 messagingService),
141 RESOURCE_TYPES,
142 logFolder);
Madan Jampani33547452016-02-29 16:45:04 -0800143 return server.open().thenRun(() -> this.server = server);
Madan Jampani15b8ef52016-02-02 17:35:05 -0800144 }
145
Madan Jampanif172d402016-03-04 00:56:38 -0800146 /**
147 * Attempts to join the partition as a new member.
148 * @return future that is completed after the operation is complete
149 */
150 private CompletableFuture<Void> joinCluster() {
151 Set<NodeId> otherMembers = partition.getMembers()
152 .stream()
153 .filter(nodeId -> !nodeId.equals(localNodeId))
154 .collect(Collectors.toSet());
155 StoragePartitionServer server = new StoragePartitionServer(toAddress(localNodeId),
156 this,
157 serializer,
158 () -> new CopycatTransport(CopycatTransport.Mode.SERVER,
159 partition.getId(),
160 messagingService),
161 RESOURCE_TYPES,
162 logFolder);
163 return server.join(Collections2.transform(otherMembers, this::toAddress)).thenRun(() -> this.server = server);
164 }
165
Madan Jampani15b8ef52016-02-02 17:35:05 -0800166 private CompletableFuture<StoragePartitionClient> openClient() {
167 client = new StoragePartitionClient(this,
168 serializer,
169 new CopycatTransport(CopycatTransport.Mode.CLIENT,
Madan Jampani33547452016-02-29 16:45:04 -0800170 partition.getId(),
Madan Jampani15b8ef52016-02-02 17:35:05 -0800171 messagingService),
172 RESOURCE_TYPES);
173 return client.open().thenApply(v -> client);
174 }
175
Madan Jampani33547452016-02-29 16:45:04 -0800176 /**
177 * Closes the partition server if it was previously opened.
178 * @return future that is completed when the operation completes
179 */
Madan Jampanif172d402016-03-04 00:56:38 -0800180 public CompletableFuture<Void> leaveCluster() {
Madan Jampani33547452016-02-29 16:45:04 -0800181 return server != null ? server.closeAndExit() : CompletableFuture.completedFuture(null);
182 }
183
184 @Override
185 public boolean isOpen() {
186 return isOpened.get() && !isClosed.get();
187 }
188
189 @Override
190 public boolean isClosed() {
191 return isClosed.get();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800192 }
193
194 private CompletableFuture<Void> closeClient() {
195 if (client != null) {
196 return client.close();
197 }
198 return CompletableFuture.completedFuture(null);
199 }
200
201 private Address toAddress(NodeId nodeId) {
202 ControllerNode node = clusterService.getNode(nodeId);
203 return new Address(node.ip().toString(), node.tcpPort());
204 }
205
Madan Jampanie14a09c2016-02-11 10:43:21 -0800206 /**
207 * Returns the partition information if this partition is locally managed i.e.
208 * this node is a active member of the partition.
209 * @return partition info
210 */
211 public Optional<PartitionInfo> info() {
Madan Jampanif172d402016-03-04 00:56:38 -0800212 return server != null && !server.isClosed() ? Optional.of(server.info()) : Optional.empty();
Madan Jampani33547452016-02-29 16:45:04 -0800213 }
214
Madan Jampanif172d402016-03-04 00:56:38 -0800215 public void onUpdate(Partition newValue) {
216 if (partition.getMembers().contains(localNodeId) && newValue.getMembers().contains(localNodeId)) {
217 return;
218 }
219 if (!partition.getMembers().contains(localNodeId) && !newValue.getMembers().contains(localNodeId)) {
220 return;
221 }
222 this.partition = newValue;
Madan Jampani33547452016-02-29 16:45:04 -0800223 if (partition.getMembers().contains(localNodeId)) {
Madan Jampanif172d402016-03-04 00:56:38 -0800224 joinCluster();
Madan Jampani33547452016-02-29 16:45:04 -0800225 } else if (!partition.getMembers().contains(localNodeId)) {
Madan Jampanif172d402016-03-04 00:56:38 -0800226 leaveCluster();
Madan Jampani33547452016-02-29 16:45:04 -0800227 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800228 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800229}