blob: c46ce87bf38db7236b2d3cf80e8a0b72a93eb334 [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 Halterman2b3ed3e2018-11-27 14:38:37 -080018import io.atomix.cluster.Node;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import io.atomix.cluster.discovery.BootstrapDiscoveryProvider;
20import io.atomix.core.Atomix;
21import io.atomix.protocols.raft.partition.RaftPartitionGroup;
22import org.onosproject.cluster.ClusterMetadata;
23import org.onosproject.cluster.ClusterMetadataService;
24import org.osgi.service.component.annotations.Activate;
25import org.osgi.service.component.annotations.Component;
26import org.osgi.service.component.annotations.Deactivate;
27import org.osgi.service.component.annotations.Reference;
28import org.osgi.service.component.annotations.ReferenceCardinality;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
Jordan Halterman19c123a2018-07-30 13:57:19 -070032import java.io.File;
33import java.util.Collections;
34import java.util.List;
Jordan Halterman00e92da2018-05-22 23:05:52 -070035import java.util.stream.Collectors;
36
Jordan Halterman00e92da2018-05-22 23:05:52 -070037/**
38 * Atomix manager.
39 */
Ray Milkey584f54b2018-10-08 14:45:20 -070040@Component(immediate = true, enabled = false, service = AtomixManager.class)
Jordan Halterman00e92da2018-05-22 23:05:52 -070041public class AtomixManager {
Jordan Halterman19c123a2018-07-30 13:57:19 -070042 private static final String LOCAL_DATA_DIR = System.getProperty("karaf.data") + "/db/partitions/";
Jordan Halterman00e92da2018-05-22 23:05:52 -070043 private final Logger log = LoggerFactory.getLogger(getClass());
44
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Halterman00e92da2018-05-22 23:05:52 -070046 protected ClusterMetadataService metadataService;
47
48 private Atomix atomix;
49
50 /**
51 * Returns the Atomix instance.
52 *
53 * @return the Atomix instance
54 */
55 public Atomix getAtomix() {
56 return atomix;
57 }
58
59 @Activate
60 public void activate() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070061 log.info("{}", metadataService.getClusterMetadata());
Jordan Halterman00e92da2018-05-22 23:05:52 -070062 atomix = createAtomix();
63 atomix.start().join();
64 log.info("Started");
65 }
66
67 @Deactivate
68 public void deactivate() {
69 atomix.stop().join();
70 log.info("Stopped");
71 }
72
73 private Atomix createAtomix() {
Jordan Halterman19c123a2018-07-30 13:57:19 -070074 ClusterMetadata metadata = metadataService.getClusterMetadata();
75 if (!metadata.getStorageNodes().isEmpty()) {
76 // If storage nodes are defined, construct an instance that connects to them for service discovery.
77 return Atomix.builder(getClass().getClassLoader())
78 .withClusterId(metadata.getName())
79 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Halterman2b3ed3e2018-11-27 14:38:37 -080080 .withHost(metadata.getLocalNode().host())
81 .withPort(metadata.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070082 .withProperty("type", "onos")
83 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
84 .withNodes(metadata.getStorageNodes().stream()
Jordan Halterman2b3ed3e2018-11-27 14:38:37 -080085 .map(node -> Node.builder()
Jordan Halterman19c123a2018-07-30 13:57:19 -070086 .withId(node.id().id())
Jordan Halterman2b3ed3e2018-11-27 14:38:37 -080087 .withHost(node.host())
88 .withPort(node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -070089 .build())
90 .collect(Collectors.toList()))
91 .build())
92 .build();
93 } else {
94 log.warn("No storage nodes found in cluster metadata!");
95 log.warn("Bootstrapping ONOS cluster in test mode! For production use, configure external storage nodes.");
96
97 // If storage nodes are not defined, construct a local instance with a Raft partition group.
98 List<String> raftMembers = !metadata.getControllerNodes().isEmpty()
99 ? metadata.getControllerNodes()
100 .stream()
101 .map(node -> node.id().id())
102 .collect(Collectors.toList())
103 : Collections.singletonList(metadataService.getLocalNode().id().id());
104 return Atomix.builder(getClass().getClassLoader())
105 .withClusterId(metadata.getName())
106 .withMemberId(metadataService.getLocalNode().id().id())
Jordan Halterman2b3ed3e2018-11-27 14:38:37 -0800107 .withHost(metadata.getLocalNode().host())
108 .withPort(metadata.getLocalNode().tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700109 .withProperty("type", "onos")
110 .withMembershipProvider(BootstrapDiscoveryProvider.builder()
111 .withNodes(metadata.getControllerNodes().stream()
112 .map(node -> io.atomix.cluster.Node.builder()
113 .withId(node.id().id())
Jordan Halterman2b3ed3e2018-11-27 14:38:37 -0800114 .withHost(node.host())
115 .withPort(node.tcpPort())
Jordan Halterman19c123a2018-07-30 13:57:19 -0700116 .build())
117 .collect(Collectors.toList()))
118 .build())
119 .withManagementGroup(RaftPartitionGroup.builder("system")
120 .withNumPartitions(1)
121 .withDataDirectory(new File(LOCAL_DATA_DIR, "system"))
122 .withMembers(raftMembers)
123 .build())
124 .addPartitionGroup(RaftPartitionGroup.builder("raft")
125 .withNumPartitions(raftMembers.size())
126 .withDataDirectory(new File(LOCAL_DATA_DIR, "data"))
127 .withMembers(raftMembers)
128 .build())
129 .build();
130 }
Jordan Halterman00e92da2018-05-22 23:05:52 -0700131 }
132}