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