blob: 8df1a403fb5c8b88384c3c4c96fb5248304a9e75 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
2 * Copyright 2018-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 */
Thomas Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
Jordan Halterman19c123a2018-07-30 13:57:19 -070018import java.io.File;
19import java.util.Collections;
20import java.util.List;
Jordan Halterman00e92da2018-05-22 23:05:52 -070021import java.util.stream.Collectors;
22
23import io.atomix.cluster.discovery.BootstrapDiscoveryProvider;
24import io.atomix.core.Atomix;
Jordan Halterman19c123a2018-07-30 13:57:19 -070025import io.atomix.protocols.raft.partition.RaftPartitionGroup;
Jordan Halterman00e92da2018-05-22 23:05:52 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
Jordan Halterman19c123a2018-07-30 13:57:19 -070032import org.onosproject.cluster.ClusterMetadata;
Jordan Halterman00e92da2018-05-22 23:05:52 -070033import org.onosproject.cluster.ClusterMetadataService;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37/**
38 * Atomix manager.
39 */
40@Component(immediate = true)
41@Service(value = AtomixManager.class)
42public class AtomixManager {
Jordan Halterman19c123a2018-07-30 13:57:19 -070043 private static final String LOCAL_DATA_DIR = System.getProperty("karaf.data") + "/db/partitions/";
Jordan Halterman00e92da2018-05-22 23:05:52 -070044 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected ClusterMetadataService metadataService;
48
49 private Atomix atomix;
50
51 /**
52 * Returns the Atomix instance.
53 *
54 * @return the Atomix instance
55 */
56 public Atomix getAtomix() {
57 return atomix;
58 }
59
60 @Activate
61 public void activate() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070062 log.info("{}", metadataService.getClusterMetadata());
Jordan Halterman00e92da2018-05-22 23:05:52 -070063 atomix = createAtomix();
64 atomix.start().join();
65 log.info("Started");
66 }
67
68 @Deactivate
69 public void deactivate() {
70 atomix.stop().join();
71 log.info("Stopped");
72 }
73
74 private Atomix createAtomix() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070075 ClusterMetadata metadata = metadataService.getClusterMetadata();
76 if (!metadata.getStorageNodes().isEmpty()) {
77 // If storage nodes are defined, construct an instance that connects to them for service discovery.
78 return Atomix.builder(getClass().getClassLoader())
79 .withClusterId(metadata.getName())
80 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -070081 .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070082 .withProperty("type", "onos")
83 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
84 .withNodes(metadata.getStorageNodes().stream()
85 .map(node -> io.atomix.cluster.Node.builder()
86 .withId(node.id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -070087 .withAddress(node.host(), node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070088 .build())
89 .collect(Collectors.toList()))
90 .build())
91 .build();
92 } else {
93 log.warn("No storage nodes found in cluster metadata!");
94 log.warn("Bootstrapping ONOS cluster in test mode! For production use, configure external storage nodes.");
95
96 // If storage nodes are not defined, construct a local instance with a Raft partition group.
97 List<String> raftMembers = !metadata.getControllerNodes().isEmpty()
98 ? metadata.getControllerNodes()
99 .stream()
100 .map(node -> node.id().id())
101 .collect(Collectors.toList())
102 : Collections.singletonList(metadataService.getLocalNode().id().id());
103 return Atomix.builder(getClass().getClassLoader())
104 .withClusterId(metadata.getName())
105 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -0700106 .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700107 .withProperty("type", "onos")
108 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
109 .withNodes(metadata.getControllerNodes().stream()
110 .map(node -> io.atomix.cluster.Node.builder()
111 .withId(node.id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -0700112 .withAddress(node.host(), node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700113 .build())
114 .collect(Collectors.toList()))
115 .build())
116 .withManagementGroup(RaftPartitionGroup.builder("system")
117 .withNumPartitions(1)
118 .withDataDirectory(new File(LOCAL_DATA_DIR, "system"))
119 .withMembers(raftMembers)
120 .build())
121 .addPartitionGroup(RaftPartitionGroup.builder("raft")
122 .withNumPartitions(raftMembers.size())
123 .withDataDirectory(new File(LOCAL_DATA_DIR, "data"))
124 .withMembers(raftMembers)
125 .build())
126 .build();
127 }
Jordan Halterman00e92da2018-05-22 23:05:52 -0700128 }
129}