blob: 1135fda3340d43acf94fca02ad33e8bab47ee58c [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import io.atomix.cluster.discovery.BootstrapDiscoveryProvider;
19import io.atomix.core.Atomix;
20import io.atomix.protocols.raft.partition.RaftPartitionGroup;
21import org.onosproject.cluster.ClusterMetadata;
22import org.onosproject.cluster.ClusterMetadataService;
23import org.osgi.service.component.annotations.Activate;
24import org.osgi.service.component.annotations.Component;
25import org.osgi.service.component.annotations.Deactivate;
26import org.osgi.service.component.annotations.Reference;
27import org.osgi.service.component.annotations.ReferenceCardinality;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Jordan Halterman19c123a2018-07-30 13:57:19 -070031import java.io.File;
32import java.util.Collections;
33import java.util.List;
Jordan Halterman00e92da2018-05-22 23:05:52 -070034import java.util.stream.Collectors;
35
Jordan Halterman00e92da2018-05-22 23:05:52 -070036/**
37 * Atomix manager.
38 */
Ray Milkey584f54b2018-10-08 14:45:20 -070039@Component(immediate = true, enabled = false, service = AtomixManager.class)
Jordan Halterman00e92da2018-05-22 23:05:52 -070040public class AtomixManager {
Jordan Halterman19c123a2018-07-30 13:57:19 -070041 private static final String LOCAL_DATA_DIR = System.getProperty("karaf.data") + "/db/partitions/";
Jordan Halterman00e92da2018-05-22 23:05:52 -070042 private final Logger log = LoggerFactory.getLogger(getClass());
43
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman00e92da2018-05-22 23:05:52 -070045 protected ClusterMetadataService metadataService;
46
47 private Atomix atomix;
48
49 /**
50 * Returns the Atomix instance.
51 *
52 * @return the Atomix instance
53 */
54 public Atomix getAtomix() {
55 return atomix;
56 }
57
58 @Activate
59 public void activate() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070060 log.info("{}", metadataService.getClusterMetadata());
Jordan Halterman00e92da2018-05-22 23:05:52 -070061 atomix = createAtomix();
62 atomix.start().join();
63 log.info("Started");
64 }
65
66 @Deactivate
67 public void deactivate() {
68 atomix.stop().join();
69 log.info("Stopped");
70 }
71
72 private Atomix createAtomix() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070073 ClusterMetadata metadata = metadataService.getClusterMetadata();
74 if (!metadata.getStorageNodes().isEmpty()) {
75 // If storage nodes are defined, construct an instance that connects to them for service discovery.
76 return Atomix.builder(getClass().getClassLoader())
77 .withClusterId(metadata.getName())
78 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -070079 .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070080 .withProperty("type", "onos")
81 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
82 .withNodes(metadata.getStorageNodes().stream()
83 .map(node -> io.atomix.cluster.Node.builder()
84 .withId(node.id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -070085 .withAddress(node.host(), node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070086 .build())
87 .collect(Collectors.toList()))
88 .build())
89 .build();
90 } else {
91 log.warn("No storage nodes found in cluster metadata!");
92 log.warn("Bootstrapping ONOS cluster in test mode! For production use, configure external storage nodes.");
93
94 // If storage nodes are not defined, construct a local instance with a Raft partition group.
95 List<String> raftMembers = !metadata.getControllerNodes().isEmpty()
96 ? metadata.getControllerNodes()
97 .stream()
98 .map(node -> node.id().id())
99 .collect(Collectors.toList())
100 : Collections.singletonList(metadataService.getLocalNode().id().id());
101 return Atomix.builder(getClass().getClassLoader())
102 .withClusterId(metadata.getName())
103 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -0700104 .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700105 .withProperty("type", "onos")
106 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
107 .withNodes(metadata.getControllerNodes().stream()
108 .map(node -> io.atomix.cluster.Node.builder()
109 .withId(node.id().id())
Jordan Haltermane458f002018-09-18 17:42:05 -0700110 .withAddress(node.host(), node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700111 .build())
112 .collect(Collectors.toList()))
113 .build())
114 .withManagementGroup(RaftPartitionGroup.builder("system")
115 .withNumPartitions(1)
116 .withDataDirectory(new File(LOCAL_DATA_DIR, "system"))
117 .withMembers(raftMembers)
118 .build())
119 .addPartitionGroup(RaftPartitionGroup.builder("raft")
120 .withNumPartitions(raftMembers.size())
121 .withDataDirectory(new File(LOCAL_DATA_DIR, "data"))
122 .withMembers(raftMembers)
123 .build())
124 .build();
125 }
Jordan Halterman00e92da2018-05-22 23:05:52 -0700126 }
127}