blob: c9cfe53c7ee2bed2989a078ecd414ef39585ac89 [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani15b8ef52016-02-02 17:35:05 -08003 *
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
Madan Jampani15b8ef52016-02-02 17:35:05 -080018import java.io.File;
Jordan Halterman980a8c12017-09-22 18:01:19 -070019import java.io.IOException;
Jordan Halterman07f052b2017-10-08 14:22:41 -070020import java.nio.file.FileVisitResult;
Jordan Halterman980a8c12017-09-22 18:01:19 -070021import java.nio.file.Files;
Jordan Halterman07f052b2017-10-08 14:22:41 -070022import java.nio.file.Path;
23import java.nio.file.SimpleFileVisitor;
24import java.nio.file.attribute.BasicFileAttributes;
Jordan Halterman19201232017-09-12 17:20:26 -070025import java.time.Duration;
Madan Jampani15b8ef52016-02-02 17:35:05 -080026import java.util.Collection;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027import java.util.concurrent.CompletableFuture;
Jordan Halterman07f052b2017-10-08 14:22:41 -070028import java.util.stream.Collectors;
Madan Jampani15b8ef52016-02-02 17:35:05 -080029
Jordan Halterman2bf177c2017-06-29 01:49:08 -070030import io.atomix.protocols.raft.RaftServer;
31import io.atomix.protocols.raft.cluster.MemberId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070032import io.atomix.protocols.raft.storage.RaftStorage;
33import io.atomix.storage.StorageLevel;
Jordan Halterman07f052b2017-10-08 14:22:41 -070034import org.onosproject.cluster.Partition;
Jordan Halterman28183ee2017-10-17 17:29:10 -070035import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070036import org.onosproject.store.primitives.resources.impl.AtomixSerializerAdapter;
Madan Jampanie14a09c2016-02-11 10:43:21 -080037import org.onosproject.store.service.PartitionInfo;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070038import org.onosproject.store.service.Serializer;
Madan Jampanifc981772016-02-16 09:46:42 -080039import org.slf4j.Logger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080040
Jordan Halterman2bf177c2017-06-29 01:49:08 -070041import static org.slf4j.LoggerFactory.getLogger;
42
Madan Jampani15b8ef52016-02-02 17:35:05 -080043/**
44 * {@link StoragePartition} server.
45 */
46public class StoragePartitionServer implements Managed<StoragePartitionServer> {
47
Madan Jampanifc981772016-02-16 09:46:42 -080048 private final Logger log = getLogger(getClass());
49
Jordan Halterman035231e2017-07-18 08:39:07 -070050 private static final int MAX_SEGMENT_SIZE = 1024 * 1024 * 64;
Jordan Halterman19201232017-09-12 17:20:26 -070051 private static final long ELECTION_TIMEOUT_MILLIS = 2500;
Jordan Halterman19486e32017-11-02 15:00:06 -070052 private static final long HEARTBEAT_INTERVAL_MILLIS = 250;
Jordan Halterman19201232017-09-12 17:20:26 -070053
Jordan Halterman2bf177c2017-06-29 01:49:08 -070054 private final MemberId localMemberId;
Madan Jampani15b8ef52016-02-02 17:35:05 -080055 private final StoragePartition partition;
Jordan Halterman28183ee2017-10-17 17:29:10 -070056 private final ClusterCommunicationService clusterCommunicator;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070057 private RaftServer server;
Madan Jampani15b8ef52016-02-02 17:35:05 -080058
Jordan Halterman2bf177c2017-06-29 01:49:08 -070059 public StoragePartitionServer(
Madan Jampani15b8ef52016-02-02 17:35:05 -080060 StoragePartition partition,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070061 MemberId localMemberId,
Jordan Halterman28183ee2017-10-17 17:29:10 -070062 ClusterCommunicationService clusterCommunicator) {
Madan Jampani15b8ef52016-02-02 17:35:05 -080063 this.partition = partition;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070064 this.localMemberId = localMemberId;
Jordan Halterman980a8c12017-09-22 18:01:19 -070065 this.clusterCommunicator = clusterCommunicator;
Madan Jampani15b8ef52016-02-02 17:35:05 -080066 }
67
68 @Override
69 public CompletableFuture<Void> open() {
Jordan Halterman980a8c12017-09-22 18:01:19 -070070 log.info("Starting server for partition {} ({})", partition.getId(), partition.getVersion());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070071 CompletableFuture<RaftServer> serverOpenFuture;
72 if (partition.getMemberIds().contains(localMemberId)) {
Madan Jampani65f24bb2016-03-15 15:16:18 -070073 if (server != null && server.isRunning()) {
Madan Jampani15b8ef52016-02-02 17:35:05 -080074 return CompletableFuture.completedFuture(null);
75 }
76 synchronized (this) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070077 server = buildServer();
Madan Jampani15b8ef52016-02-02 17:35:05 -080078 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -070079 serverOpenFuture = server.bootstrap(partition.getMemberIds());
Madan Jampani15b8ef52016-02-02 17:35:05 -080080 } else {
81 serverOpenFuture = CompletableFuture.completedFuture(null);
82 }
Madan Jampanifc981772016-02-16 09:46:42 -080083 return serverOpenFuture.whenComplete((r, e) -> {
84 if (e == null) {
Jordan Halterman980a8c12017-09-22 18:01:19 -070085 log.info("Successfully started server for partition {} ({})",
86 partition.getId(), partition.getVersion());
Madan Jampanifc981772016-02-16 09:46:42 -080087 } else {
Jordan Halterman980a8c12017-09-22 18:01:19 -070088 log.info("Failed to start server for partition {} ({})",
89 partition.getId(), partition.getVersion(), e);
Madan Jampanifc981772016-02-16 09:46:42 -080090 }
91 }).thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -080092 }
93
94 @Override
95 public CompletableFuture<Void> close() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070096 return server.shutdown();
Madan Jampani15b8ef52016-02-02 17:35:05 -080097 }
98
Madan Jampani33547452016-02-29 16:45:04 -080099 /**
100 * Closes the server and exits the partition.
101 * @return future that is completed when the operation is complete
102 */
103 public CompletableFuture<Void> closeAndExit() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700104 return server.leave();
Madan Jampani33547452016-02-29 16:45:04 -0800105 }
106
Jordan Halterman980a8c12017-09-22 18:01:19 -0700107 /**
Jordan Halterman07f052b2017-10-08 14:22:41 -0700108 * Deletes the server.
109 */
110 public void delete() {
111 try {
112 Files.walkFileTree(partition.getDataFolder().toPath(), new SimpleFileVisitor<Path>() {
113 @Override
114 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
115 Files.delete(file);
116 return FileVisitResult.CONTINUE;
117 }
118 @Override
119 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
120 Files.delete(dir);
121 return FileVisitResult.CONTINUE;
122 }
123 });
124 } catch (IOException e) {
125 log.error("Failed to delete partition: {}", e);
126 }
127 }
128
129 /**
Jordan Halterman980a8c12017-09-22 18:01:19 -0700130 * Forks the existing partition into a new partition.
131 *
Jordan Halterman07f052b2017-10-08 14:22:41 -0700132 * @param fromPartition the partition from which to fork the server
Jordan Halterman980a8c12017-09-22 18:01:19 -0700133 * @return future to be completed once the fork operation is complete
134 */
Jordan Halterman07f052b2017-10-08 14:22:41 -0700135 public CompletableFuture<Void> fork(Partition fromPartition) {
136 log.info("Forking server for partition {} ({}->{})",
137 partition.getId(), fromPartition.getVersion(), partition.getVersion());
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700138 RaftServer.Builder builder = RaftServer.newBuilder(localMemberId)
Jordan Halterman07f052b2017-10-08 14:22:41 -0700139 .withName(String.format("partition-%s", fromPartition.getId()))
Jordan Halterman980a8c12017-09-22 18:01:19 -0700140 .withProtocol(new RaftServerCommunicator(
Jordan Halterman07f052b2017-10-08 14:22:41 -0700141 String.format("partition-%s-%s", fromPartition.getId(), fromPartition.getVersion()),
Jordan Halterman980a8c12017-09-22 18:01:19 -0700142 Serializer.using(StorageNamespaces.RAFT_PROTOCOL),
143 clusterCommunicator))
Jordan Halterman19201232017-09-12 17:20:26 -0700144 .withElectionTimeout(Duration.ofMillis(ELECTION_TIMEOUT_MILLIS))
145 .withHeartbeatInterval(Duration.ofMillis(HEARTBEAT_INTERVAL_MILLIS))
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700146 .withStorage(RaftStorage.newBuilder()
Jordan Halterman07f052b2017-10-08 14:22:41 -0700147 .withPrefix(String.format("partition-%s", partition.getId()))
Jordan Halterman19201232017-09-12 17:20:26 -0700148 .withStorageLevel(StorageLevel.MAPPED)
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700149 .withSerializer(new AtomixSerializerAdapter(Serializer.using(StorageNamespaces.RAFT_STORAGE)))
Jordan Halterman980a8c12017-09-22 18:01:19 -0700150 .withDirectory(partition.getDataFolder())
151 .withMaxSegmentSize(MAX_SEGMENT_SIZE)
152 .build());
153 StoragePartition.RAFT_SERVICES.forEach(builder::addService);
154 RaftServer server = builder.build();
Jordan Halterman07f052b2017-10-08 14:22:41 -0700155
156 // Create a collection of members currently in the source partition.
157 Collection<MemberId> members = fromPartition.getMembers()
158 .stream()
159 .map(id -> MemberId.from(id.id()))
160 .collect(Collectors.toList());
161
162 // If this node is a member of the partition, join the partition. Otherwise, listen to the partition.
163 CompletableFuture<RaftServer> future = members.contains(localMemberId)
164 ? server.bootstrap(members) : server.listen(members);
165
166 // TODO: We should leave the cluster for nodes that aren't normally members to ensure the source
167 // cluster's configuration is kept consistent for rolling back upgrades, but Atomix deletes configuration
168 // files when a node leaves the cluster so we can't do that here.
169 return future.thenCompose(v -> server.shutdown())
Jordan Halterman980a8c12017-09-22 18:01:19 -0700170 .thenCompose(v -> {
171 // Delete the cluster configuration file from the forked partition.
172 try {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700173 Files.delete(new File(
174 partition.getDataFolder(),
175 String.format("partition-%s.conf", partition.getId())).toPath());
Jordan Halterman980a8c12017-09-22 18:01:19 -0700176 } catch (IOException e) {
177 log.error("Failed to delete partition configuration: {}", e);
178 }
179
180 // Build and bootstrap a new server.
181 this.server = buildServer();
182 return this.server.bootstrap();
183 }).whenComplete((r, e) -> {
184 if (e == null) {
185 log.info("Successfully forked server for partition {} ({}->{})",
Jordan Halterman07f052b2017-10-08 14:22:41 -0700186 partition.getId(), fromPartition.getVersion(), partition.getVersion());
Jordan Halterman980a8c12017-09-22 18:01:19 -0700187 } else {
188 log.info("Failed to fork server for partition {} ({}->{})",
Jordan Halterman07f052b2017-10-08 14:22:41 -0700189 partition.getId(), fromPartition.getVersion(), partition.getVersion(), e);
Jordan Halterman980a8c12017-09-22 18:01:19 -0700190 }
191 }).thenApply(v -> null);
192 }
193
194 private RaftServer buildServer() {
195 RaftServer.Builder builder = RaftServer.newBuilder(localMemberId)
Jordan Halterman07f052b2017-10-08 14:22:41 -0700196 .withName(String.format("partition-%s", partition.getId()))
Jordan Halterman980a8c12017-09-22 18:01:19 -0700197 .withProtocol(new RaftServerCommunicator(
Jordan Halterman07f052b2017-10-08 14:22:41 -0700198 String.format("partition-%s-%s", partition.getId(), partition.getVersion()),
Jordan Halterman980a8c12017-09-22 18:01:19 -0700199 Serializer.using(StorageNamespaces.RAFT_PROTOCOL),
200 clusterCommunicator))
201 .withElectionTimeout(Duration.ofMillis(ELECTION_TIMEOUT_MILLIS))
202 .withHeartbeatInterval(Duration.ofMillis(HEARTBEAT_INTERVAL_MILLIS))
203 .withStorage(RaftStorage.newBuilder()
Jordan Halterman07f052b2017-10-08 14:22:41 -0700204 .withPrefix(String.format("partition-%s", partition.getId()))
Jordan Halterman980a8c12017-09-22 18:01:19 -0700205 .withStorageLevel(StorageLevel.MAPPED)
206 .withSerializer(new AtomixSerializerAdapter(Serializer.using(StorageNamespaces.RAFT_STORAGE)))
207 .withDirectory(partition.getDataFolder())
Jordan Halterman035231e2017-07-18 08:39:07 -0700208 .withMaxSegmentSize(MAX_SEGMENT_SIZE)
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700209 .build());
210 StoragePartition.RAFT_SERVICES.forEach(builder::addService);
211 return builder.build();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800212 }
213
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700214 public CompletableFuture<Void> join(Collection<MemberId> otherMembers) {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700215 log.info("Joining partition {} ({})", partition.getId(), partition.getName());
Madan Jampani630e7ac2016-05-31 11:34:05 -0700216 server = buildServer();
217 return server.join(otherMembers).whenComplete((r, e) -> {
Madan Jampanif172d402016-03-04 00:56:38 -0800218 if (e == null) {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700219 log.info("Successfully joined partition {} ({})", partition.getId(), partition.getName());
Madan Jampanif172d402016-03-04 00:56:38 -0800220 } else {
Jordan Halterman07f052b2017-10-08 14:22:41 -0700221 log.info("Failed to join partition {} ({})", partition.getId(), partition.getName(), e);
Madan Jampanif172d402016-03-04 00:56:38 -0800222 }
223 }).thenApply(v -> null);
224 }
225
Madan Jampani15b8ef52016-02-02 17:35:05 -0800226 @Override
227 public boolean isOpen() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700228 return server.isRunning();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800229 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800230
231 /**
232 * Returns the partition information.
233 * @return partition info
234 */
235 public PartitionInfo info() {
236 return new StoragePartitionDetails(partition.getId(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700237 server.cluster().getMembers(),
238 server.cluster().getMembers(),
239 server.cluster().getLeader(),
240 server.cluster().getTerm()).toPartitionInfo();
Madan Jampanie14a09c2016-02-11 10:43:21 -0800241 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800242}