blob: 834d8e99bb6cf21f72b9747c798fdeb7a2165b8f [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
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;
25import io.atomix.manager.state.ResourceManagerState;
26import io.atomix.resource.ResourceRegistry;
27import io.atomix.resource.ResourceType;
28import io.atomix.resource.ResourceTypeResolver;
29import io.atomix.resource.ServiceLoaderResourceResolver;
30
31import java.io.File;
32import java.util.Collection;
33import java.util.Set;
34import java.util.concurrent.CompletableFuture;
35import java.util.function.Supplier;
36
37import org.onosproject.cluster.NodeId;
Madan Jampanie14a09c2016-02-11 10:43:21 -080038import org.onosproject.store.service.PartitionInfo;
Madan Jampanifc981772016-02-16 09:46:42 -080039import org.slf4j.Logger;
Madan Jampani15b8ef52016-02-02 17:35:05 -080040
41import com.google.common.collect.ImmutableSet;
42import com.google.common.collect.Sets;
43
44/**
45 * {@link StoragePartition} server.
46 */
47public class StoragePartitionServer implements Managed<StoragePartitionServer> {
48
Madan Jampanifc981772016-02-16 09:46:42 -080049 private final Logger log = getLogger(getClass());
50
Madan Jampani15b8ef52016-02-02 17:35:05 -080051 private static final int MAX_ENTRIES_PER_LOG_SEGMENT = 32768;
52 private final StoragePartition partition;
53 private final Address localAddress;
54 private final Supplier<Transport> transport;
55 private final Serializer serializer;
56 private final File dataFolder;
57 private final Collection<ResourceType> resourceTypes;
58 private CopycatServer server;
59
60 public StoragePartitionServer(Address localAddress,
61 StoragePartition partition,
62 Serializer serializer,
63 Supplier<Transport> transport,
64 Collection<ResourceType> resourceTypes,
65 File dataFolder) {
66 this.partition = partition;
67 this.localAddress = localAddress;
68 this.serializer = serializer;
69 this.transport = transport;
70 this.resourceTypes = ImmutableSet.copyOf(resourceTypes);
71 this.dataFolder = dataFolder;
72 }
73
74 @Override
75 public CompletableFuture<Void> open() {
76 CompletableFuture<CopycatServer> serverOpenFuture;
77 if (partition.getMemberAddresses().contains(localAddress)) {
78 if (server != null && server.isOpen()) {
79 return CompletableFuture.completedFuture(null);
80 }
81 synchronized (this) {
82 server = server();
83 }
84 serverOpenFuture = server.open();
85 } else {
86 serverOpenFuture = CompletableFuture.completedFuture(null);
87 }
Madan Jampanifc981772016-02-16 09:46:42 -080088 return serverOpenFuture.whenComplete((r, e) -> {
89 if (e == null) {
90 log.info("Successfully started server for partition {}", partition.getId());
91 } else {
92 log.info("Failed to start server for partition {}", partition.getId(), e);
93 }
94 }).thenApply(v -> null);
Madan Jampani15b8ef52016-02-02 17:35:05 -080095 }
96
97 @Override
98 public CompletableFuture<Void> close() {
99 // We do not close the server because doing so is equivalent to this node
100 // leaving the cluster and we don't want that here.
101 // The Raft protocol should take care of servers leaving unannounced.
102 return CompletableFuture.completedFuture(null);
103 }
104
105 private CopycatServer server() {
106 ResourceTypeResolver resourceResolver = new ServiceLoaderResourceResolver();
107 ResourceRegistry registry = new ResourceRegistry();
108 resourceTypes.forEach(registry::register);
109 resourceResolver.resolve(registry);
110 return CopycatServer.builder(localAddress, partition.getMemberAddresses())
111 .withName("partition-" + partition.getId())
112 .withSerializer(serializer.clone())
113 .withTransport(transport.get())
114 .withStateMachine(() -> new ResourceManagerState(registry))
115 .withStorage(Storage.builder()
116 // FIXME: StorageLevel should be DISK
117 .withStorageLevel(StorageLevel.MEMORY)
Madan Jampani15b8ef52016-02-02 17:35:05 -0800118 .withDirectory(dataFolder)
119 .withMaxEntriesPerSegment(MAX_ENTRIES_PER_LOG_SEGMENT)
120 .build())
121 .build();
122 }
123
124 public Set<NodeId> configuredMembers() {
125 return Sets.newHashSet(partition.getMembers());
126 }
127
128 @Override
129 public boolean isOpen() {
130 return server.isOpen();
131 }
132
133 @Override
134 public boolean isClosed() {
135 return server.isClosed();
136 }
Madan Jampanie14a09c2016-02-11 10:43:21 -0800137
138 /**
139 * Returns the partition information.
140 * @return partition info
141 */
142 public PartitionInfo info() {
143 return new StoragePartitionDetails(partition.getId(),
144 server.cluster().members(),
145 server.cluster().members(),
146 server.cluster().leader(),
147 server.cluster().term()).toPartitionInfo();
148 }
Madan Jampani15b8ef52016-02-02 17:35:05 -0800149}