Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 18 | import java.io.File; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 21 | import java.util.stream.Collectors; |
| 22 | |
| 23 | import io.atomix.cluster.discovery.BootstrapDiscoveryProvider; |
| 24 | import io.atomix.core.Atomix; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 25 | import io.atomix.protocols.raft.partition.RaftPartitionGroup; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 32 | import org.onosproject.cluster.ClusterMetadata; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 33 | import org.onosproject.cluster.ClusterMetadataService; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | /** |
| 38 | * Atomix manager. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service(value = AtomixManager.class) |
| 42 | public class AtomixManager { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 43 | private static final String LOCAL_DATA_DIR = System.getProperty("karaf.data") + "/db/partitions/"; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 44 | 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 Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 62 | log.info("{}", metadataService.getClusterMetadata()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 63 | 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 Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 75 | 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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 81 | .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 82 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 87 | .withAddress(node.host(), node.tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 88 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 106 | .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 107 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 112 | .withAddress(node.host(), node.tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 113 | .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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 128 | } |
| 129 | } |