blob: c1ea37cb29b6a51ad8fbd783b4b62fa08bc00b46 [file] [log] [blame]
Madan Jampani15b8ef52016-02-02 17:35:05 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
19import java.util.Collection;
Madan Jampani15b8ef52016-02-02 17:35:05 -080020import java.util.concurrent.CompletableFuture;
21import java.util.function.Supplier;
22
Jordan Halterman2bf177c2017-06-29 01:49:08 -070023import io.atomix.protocols.raft.RaftServer;
24import io.atomix.protocols.raft.cluster.MemberId;
25import io.atomix.protocols.raft.protocol.RaftServerProtocol;
26import io.atomix.protocols.raft.storage.RaftStorage;
27import io.atomix.storage.StorageLevel;
28import org.onosproject.store.primitives.resources.impl.AtomixSerializerAdapter;
Madan Jampanie14a09c2016-02-11 10:43:21 -080029import org.onosproject.store.service.PartitionInfo;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070030import org.onosproject.store.service.Serializer;
Madan Jampanifc981772016-02-16 09:46:42 -080031import org.slf4j.Logger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080032
Jordan Halterman2bf177c2017-06-29 01:49:08 -070033import static org.slf4j.LoggerFactory.getLogger;
34
Madan Jampani15b8ef52016-02-02 17:35:05 -080035/**
36 * {@link StoragePartition} server.
37 */
38public class StoragePartitionServer implements Managed<StoragePartitionServer> {
39
Madan Jampanifc981772016-02-16 09:46:42 -080040 private final Logger log = getLogger(getClass());
41
Madan Jampani15b8ef52016-02-02 17:35:05 -080042 private static final int MAX_ENTRIES_PER_LOG_SEGMENT = 32768;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070043 private final MemberId localMemberId;
Madan Jampani15b8ef52016-02-02 17:35:05 -080044 private final StoragePartition partition;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070045 private final Supplier<RaftServerProtocol> protocol;
Madan Jampani15b8ef52016-02-02 17:35:05 -080046 private final File dataFolder;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070047 private RaftServer server;
Madan Jampani15b8ef52016-02-02 17:35:05 -080048
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049 public StoragePartitionServer(
Madan Jampani15b8ef52016-02-02 17:35:05 -080050 StoragePartition partition,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070051 MemberId localMemberId,
52 Supplier<RaftServerProtocol> protocol,
Madan Jampani15b8ef52016-02-02 17:35:05 -080053 File dataFolder) {
54 this.partition = partition;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070055 this.localMemberId = localMemberId;
56 this.protocol = protocol;
Madan Jampani15b8ef52016-02-02 17:35:05 -080057 this.dataFolder = dataFolder;
58 }
59
60 @Override
61 public CompletableFuture<Void> open() {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070062 CompletableFuture<RaftServer> serverOpenFuture;
63 if (partition.getMemberIds().contains(localMemberId)) {
Madan Jampani65f24bb2016-03-15 15:16:18 -070064 if (server != null && server.isRunning()) {
Madan Jampani15b8ef52016-02-02 17:35:05 -080065 return CompletableFuture.completedFuture(null);
66 }
67 synchronized (this) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070068 server = buildServer();
Madan Jampani15b8ef52016-02-02 17:35:05 -080069 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -070070 serverOpenFuture = server.bootstrap(partition.getMemberIds());
Madan Jampani15b8ef52016-02-02 17:35:05 -080071 } else {
72 serverOpenFuture = CompletableFuture.completedFuture(null);
73 }
Madan Jampanifc981772016-02-16 09:46:42 -080074 return serverOpenFuture.whenComplete((r, e) -> {
75 if (e == null) {
76 log.info("Successfully started server for partition {}", partition.getId());
77 } else {
78 log.info("Failed to start server for partition {}", partition.getId(), e);
79 }
80 }).thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -080081 }
82
83 @Override
84 public CompletableFuture<Void> close() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070085 return server.shutdown();
Madan Jampani15b8ef52016-02-02 17:35:05 -080086 }
87
Madan Jampani33547452016-02-29 16:45:04 -080088 /**
89 * Closes the server and exits the partition.
90 * @return future that is completed when the operation is complete
91 */
92 public CompletableFuture<Void> closeAndExit() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070093 return server.leave();
Madan Jampani33547452016-02-29 16:45:04 -080094 }
95
Jordan Halterman2bf177c2017-06-29 01:49:08 -070096 private RaftServer buildServer() {
97 RaftServer.Builder builder = RaftServer.newBuilder(localMemberId)
Madan Jampani15b8ef52016-02-02 17:35:05 -080098 .withName("partition-" + partition.getId())
Jordan Halterman2bf177c2017-06-29 01:49:08 -070099 .withProtocol(protocol.get())
100 .withStorage(RaftStorage.newBuilder()
Madan Jampani3a9911c2016-02-21 11:25:45 -0800101 .withStorageLevel(StorageLevel.DISK)
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700102 .withSerializer(new AtomixSerializerAdapter(Serializer.using(StorageNamespaces.RAFT_STORAGE)))
Madan Jampani15b8ef52016-02-02 17:35:05 -0800103 .withDirectory(dataFolder)
104 .withMaxEntriesPerSegment(MAX_ENTRIES_PER_LOG_SEGMENT)
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700105 .build());
106 StoragePartition.RAFT_SERVICES.forEach(builder::addService);
107 return builder.build();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800108 }
109
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700110 public CompletableFuture<Void> join(Collection<MemberId> otherMembers) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700111 server = buildServer();
112 return server.join(otherMembers).whenComplete((r, e) -> {
Madan Jampanif172d402016-03-04 00:56:38 -0800113 if (e == null) {
114 log.info("Successfully joined partition {}", partition.getId());
115 } else {
116 log.info("Failed to join partition {}", partition.getId(), e);
117 }
118 }).thenApply(v -> null);
119 }
120
Madan Jampani15b8ef52016-02-02 17:35:05 -0800121 @Override
122 public boolean isOpen() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700123 return server.isRunning();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800124 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800125
126 /**
127 * Returns the partition information.
128 * @return partition info
129 */
130 public PartitionInfo info() {
131 return new StoragePartitionDetails(partition.getId(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700132 server.cluster().getMembers(),
133 server.cluster().getMembers(),
134 server.cluster().getLeader(),
135 server.cluster().getTerm()).toPartitionInfo();
Madan Jampanie14a09c2016-02-11 10:43:21 -0800136 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800137}