blob: f0c060934e53d90bc2f31092c39bdd1e1f04dfa9 [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 Jampanifc981772016-02-16 09:46:42 -080018import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080019import io.atomix.catalyst.serializer.Serializer;
20import io.atomix.catalyst.transport.Address;
21import io.atomix.catalyst.transport.Transport;
22import io.atomix.copycat.server.CopycatServer;
23import io.atomix.copycat.server.storage.Storage;
24import io.atomix.copycat.server.storage.StorageLevel;
Madan Jampani630e7ac2016-05-31 11:34:05 -070025import io.atomix.manager.internal.ResourceManagerState;
Madan Jampani65f24bb2016-03-15 15:16:18 -070026import io.atomix.manager.util.ResourceManagerTypeResolver;
Madan Jampani15b8ef52016-02-02 17:35:05 -080027
28import java.io.File;
29import java.util.Collection;
Madan Jampani15b8ef52016-02-02 17:35:05 -080030import java.util.concurrent.CompletableFuture;
31import java.util.function.Supplier;
32
Madan Jampanie14a09c2016-02-11 10:43:21 -080033import org.onosproject.store.service.PartitionInfo;
Madan Jampanifc981772016-02-16 09:46:42 -080034import org.slf4j.Logger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080035
Madan Jampani15b8ef52016-02-02 17:35:05 -080036/**
37 * {@link StoragePartition} server.
38 */
39public class StoragePartitionServer implements Managed<StoragePartitionServer> {
40
Madan Jampanifc981772016-02-16 09:46:42 -080041 private final Logger log = getLogger(getClass());
42
Madan Jampani15b8ef52016-02-02 17:35:05 -080043 private static final int MAX_ENTRIES_PER_LOG_SEGMENT = 32768;
44 private final StoragePartition partition;
45 private final Address localAddress;
46 private final Supplier<Transport> transport;
47 private final Serializer serializer;
48 private final File dataFolder;
Madan Jampani15b8ef52016-02-02 17:35:05 -080049 private CopycatServer server;
50
51 public StoragePartitionServer(Address localAddress,
52 StoragePartition partition,
53 Serializer serializer,
54 Supplier<Transport> transport,
Madan Jampani15b8ef52016-02-02 17:35:05 -080055 File dataFolder) {
56 this.partition = partition;
57 this.localAddress = localAddress;
58 this.serializer = serializer;
59 this.transport = transport;
Madan Jampani15b8ef52016-02-02 17:35:05 -080060 this.dataFolder = dataFolder;
61 }
62
63 @Override
64 public CompletableFuture<Void> open() {
65 CompletableFuture<CopycatServer> serverOpenFuture;
66 if (partition.getMemberAddresses().contains(localAddress)) {
Madan Jampani65f24bb2016-03-15 15:16:18 -070067 if (server != null && server.isRunning()) {
Madan Jampani15b8ef52016-02-02 17:35:05 -080068 return CompletableFuture.completedFuture(null);
69 }
70 synchronized (this) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070071 server = buildServer();
Madan Jampani15b8ef52016-02-02 17:35:05 -080072 }
Madan Jampani630e7ac2016-05-31 11:34:05 -070073 serverOpenFuture = server.bootstrap(partition.getMemberAddresses());
Madan Jampani15b8ef52016-02-02 17:35:05 -080074 } else {
75 serverOpenFuture = CompletableFuture.completedFuture(null);
76 }
Madan Jampanifc981772016-02-16 09:46:42 -080077 return serverOpenFuture.whenComplete((r, e) -> {
78 if (e == null) {
79 log.info("Successfully started server for partition {}", partition.getId());
80 } else {
81 log.info("Failed to start server for partition {}", partition.getId(), e);
82 }
83 }).thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -080084 }
85
86 @Override
87 public CompletableFuture<Void> close() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070088 return server.shutdown();
Madan Jampani15b8ef52016-02-02 17:35:05 -080089 }
90
Madan Jampani33547452016-02-29 16:45:04 -080091 /**
92 * Closes the server and exits the partition.
93 * @return future that is completed when the operation is complete
94 */
95 public CompletableFuture<Void> closeAndExit() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070096 return server.leave();
Madan Jampani33547452016-02-29 16:45:04 -080097 }
98
Madan Jampani630e7ac2016-05-31 11:34:05 -070099 private CopycatServer buildServer() {
100 CopycatServer server = CopycatServer.builder(localAddress)
Madan Jampani15b8ef52016-02-02 17:35:05 -0800101 .withName("partition-" + partition.getId())
102 .withSerializer(serializer.clone())
103 .withTransport(transport.get())
Madan Jampani65f24bb2016-03-15 15:16:18 -0700104 .withStateMachine(ResourceManagerState::new)
Madan Jampani15b8ef52016-02-02 17:35:05 -0800105 .withStorage(Storage.builder()
Madan Jampani3a9911c2016-02-21 11:25:45 -0800106 .withStorageLevel(StorageLevel.DISK)
Madan Jampani3ac20f92016-02-17 18:35:26 -0800107 .withCompactionThreads(1)
Madan Jampani15b8ef52016-02-02 17:35:05 -0800108 .withDirectory(dataFolder)
109 .withMaxEntriesPerSegment(MAX_ENTRIES_PER_LOG_SEGMENT)
110 .build())
111 .build();
Madan Jampani65f24bb2016-03-15 15:16:18 -0700112 server.serializer().resolve(new ResourceManagerTypeResolver());
Madan Jampani86cb2432016-02-17 11:07:56 -0800113 return server;
Madan Jampani15b8ef52016-02-02 17:35:05 -0800114 }
115
Madan Jampanif172d402016-03-04 00:56:38 -0800116 public CompletableFuture<Void> join(Collection<Address> otherMembers) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700117 server = buildServer();
118 return server.join(otherMembers).whenComplete((r, e) -> {
Madan Jampanif172d402016-03-04 00:56:38 -0800119 if (e == null) {
120 log.info("Successfully joined partition {}", partition.getId());
121 } else {
122 log.info("Failed to join partition {}", partition.getId(), e);
123 }
124 }).thenApply(v -> null);
125 }
126
Madan Jampani15b8ef52016-02-02 17:35:05 -0800127 @Override
128 public boolean isOpen() {
Madan Jampani65f24bb2016-03-15 15:16:18 -0700129 return server.isRunning();
Madan Jampani15b8ef52016-02-02 17:35:05 -0800130 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800131
132 /**
133 * Returns the partition information.
134 * @return partition info
135 */
136 public PartitionInfo info() {
137 return new StoragePartitionDetails(partition.getId(),
138 server.cluster().members(),
139 server.cluster().members(),
140 server.cluster().leader(),
141 server.cluster().term()).toPartitionInfo();
142 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800143}