blob: 66013c029ba1e88f34c8dc7cc0bfd40a781df849 [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;
26import java.util.concurrent.CompletableFuture;
27import java.util.concurrent.atomic.AtomicBoolean;
28
29import org.onosproject.cluster.ClusterService;
30import org.onosproject.cluster.ControllerNode;
31import org.onosproject.cluster.DefaultPartition;
32import org.onosproject.cluster.NodeId;
33import org.onosproject.cluster.Partition;
34import org.onosproject.store.cluster.messaging.MessagingService;
35import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap;
Madan Jampani39fff102016-02-14 13:17:28 -080036import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector;
Madan Jampanie14a09c2016-02-11 10:43:21 -080037import org.onosproject.store.service.PartitionInfo;
Madan Jampani15b8ef52016-02-02 17:35:05 -080038
39import com.google.common.collect.Collections2;
40import com.google.common.collect.ImmutableSet;
41
42/**
43 * Storage partition.
44 */
45public class StoragePartition extends DefaultPartition implements Managed<StoragePartition> {
46
47 private final AtomicBoolean isOpened = new AtomicBoolean(false);
48 private final AtomicBoolean isClosed = new AtomicBoolean(false);
49 private final Serializer serializer;
50 private final MessagingService messagingService;
51 private final ClusterService clusterService;
52 private final File logFolder;
Madan Jampanic94b4852016-02-23 18:18:37 -080053 private CompletableFuture<StoragePartitionServer> serverOpenFuture;
Madan Jampani15b8ef52016-02-02 17:35:05 -080054 private static final Collection<ResourceType> RESOURCE_TYPES = ImmutableSet.of(
55 new ResourceType(DistributedLong.class),
Madan Jampani39fff102016-02-14 13:17:28 -080056 new ResourceType(AtomixLeaderElector.class),
Madan Jampani15b8ef52016-02-02 17:35:05 -080057 new ResourceType(AtomixConsistentMap.class));
58
59 private NodeId localNodeId;
60 private Optional<StoragePartitionServer> server = Optional.empty();
61 private StoragePartitionClient client;
62
63 public StoragePartition(Partition partition,
64 MessagingService messagingService,
65 ClusterService clusterService,
66 Serializer serializer,
67 File logFolder) {
68 super(partition);
69 this.messagingService = messagingService;
70 this.clusterService = clusterService;
71 this.localNodeId = clusterService.getLocalNode().id();
72 this.serializer = serializer;
73 this.logFolder = logFolder;
74 }
75
Madan Jampani3a9911c2016-02-21 11:25:45 -080076 /**
77 * Returns the partition client instance.
78 * @return client
79 */
Madan Jampani15b8ef52016-02-02 17:35:05 -080080 public StoragePartitionClient client() {
81 return client;
82 }
83
Madan Jampani3a9911c2016-02-21 11:25:45 -080084 /**
85 * Returns the optional server instance.
86 * @return server
87 */
88 public Optional<StoragePartitionServer> server() {
89 return server;
90 }
91
Madan Jampani15b8ef52016-02-02 17:35:05 -080092 @Override
93 public CompletableFuture<Void> open() {
Madan Jampanic94b4852016-02-23 18:18:37 -080094 serverOpenFuture = openServer();
95 serverOpenFuture.thenAccept(s -> server = Optional.ofNullable(s));
96 return openClient().thenAccept(v -> isOpened.set(true))
Madan Jampani15b8ef52016-02-02 17:35:05 -080097 .thenApply(v -> null);
98 }
99
100 @Override
101 public CompletableFuture<Void> close() {
102 return closeClient().thenCompose(v -> closeServer())
103 .thenAccept(v -> isClosed.set(true))
104 .thenApply(v -> null);
105 }
106
107 public Collection<Address> getMemberAddresses() {
108 return Collections2.transform(getMembers(), this::toAddress);
109 }
110
111 private CompletableFuture<StoragePartitionServer> openServer() {
112 if (!getMembers().contains(localNodeId)) {
113 return CompletableFuture.completedFuture(null);
114 }
115 StoragePartitionServer server = new StoragePartitionServer(toAddress(localNodeId),
116 this,
117 serializer,
118 () -> new CopycatTransport(CopycatTransport.Mode.SERVER,
119 getId(),
120 messagingService),
121 RESOURCE_TYPES,
122 logFolder);
123 return server.open().thenApply(v -> server);
124 }
125
126 private CompletableFuture<StoragePartitionClient> openClient() {
127 client = new StoragePartitionClient(this,
128 serializer,
129 new CopycatTransport(CopycatTransport.Mode.CLIENT,
130 getId(),
131 messagingService),
132 RESOURCE_TYPES);
133 return client.open().thenApply(v -> client);
134 }
135
136 private CompletableFuture<Void> closeServer() {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800137 return server.map(StoragePartitionServer::close)
138 .orElse(CompletableFuture.completedFuture(null));
Madan Jampani15b8ef52016-02-02 17:35:05 -0800139 }
140
141 private CompletableFuture<Void> closeClient() {
142 if (client != null) {
143 return client.close();
144 }
145 return CompletableFuture.completedFuture(null);
146 }
147
148 private Address toAddress(NodeId nodeId) {
149 ControllerNode node = clusterService.getNode(nodeId);
150 return new Address(node.ip().toString(), node.tcpPort());
151 }
152
153 @Override
154 public boolean isOpen() {
155 return !isClosed.get() && isOpened.get();
156 }
157
158 @Override
159 public boolean isClosed() {
160 return isOpened.get() && isClosed.get();
161 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800162
163 /**
164 * Returns the partition information if this partition is locally managed i.e.
165 * this node is a active member of the partition.
166 * @return partition info
167 */
168 public Optional<PartitionInfo> info() {
169 return server.map(StoragePartitionServer::info);
170 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800171}