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