Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 java.io.File; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.stream.Collectors; |
| 21 | |
| 22 | import io.atomix.protocols.raft.cluster.MemberId; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.Partition; |
| 25 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 26 | |
| 27 | /** |
| 28 | * Storage partition forked from a previous version partition. |
| 29 | */ |
| 30 | public class ForkedStoragePartition extends StoragePartition { |
| 31 | final Partition source; |
| 32 | |
| 33 | public ForkedStoragePartition( |
| 34 | Partition partition, |
| 35 | Partition source, |
| 36 | ClusterCommunicationService clusterCommunicator, |
| 37 | ClusterService clusterService) { |
| 38 | super(partition, clusterCommunicator, clusterService); |
| 39 | this.source = source; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public String getName() { |
| 44 | return partition.getId().toString(); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public File getDataFolder() { |
| 49 | return new File(PARTITIONS_DIR + partition.getId()); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected CompletableFuture<Void> openServer() { |
| 54 | StoragePartitionServer server = new StoragePartitionServer( |
| 55 | this, |
| 56 | MemberId.from(localNodeId.id()), |
| 57 | clusterCommunicator); |
| 58 | |
| 59 | CompletableFuture<Void> future; |
| 60 | if (partition.getMembers().size() == 1) { |
| 61 | future = server.fork(source); |
| 62 | } else { |
| 63 | future = server.join(partition.getMembers().stream() |
| 64 | .filter(nodeId -> !nodeId.equals(localNodeId)) |
| 65 | .map(nodeId -> MemberId.from(nodeId.id())) |
| 66 | .collect(Collectors.toList())); |
| 67 | } |
| 68 | return future.thenRun(() -> this.server = server); |
| 69 | } |
| 70 | } |