blob: 70b12c3bb2d1feab36258e04960885b729d6b6cb [file] [log] [blame]
Jordan Halterman07f052b2017-10-08 14:22:41 -07001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.io.File;
19import java.util.concurrent.CompletableFuture;
20import java.util.stream.Collectors;
21
22import io.atomix.protocols.raft.cluster.MemberId;
23import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.Partition;
25import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
26
27/**
28 * Storage partition forked from a previous version partition.
29 */
30public 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}