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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import io.atomix.cluster.discovery.BootstrapDiscoveryProvider; |
| 19 | import io.atomix.core.Atomix; |
| 20 | import io.atomix.protocols.raft.partition.RaftPartitionGroup; |
| 21 | import org.onosproject.cluster.ClusterMetadata; |
| 22 | import org.onosproject.cluster.ClusterMetadataService; |
| 23 | import org.osgi.service.component.annotations.Activate; |
| 24 | import org.osgi.service.component.annotations.Component; |
| 25 | import org.osgi.service.component.annotations.Deactivate; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 31 | import java.io.File; |
| 32 | import java.util.Collections; |
| 33 | import java.util.List; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 34 | import java.util.stream.Collectors; |
| 35 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Atomix manager. |
| 38 | */ |
Ray Milkey | 584f54b | 2018-10-08 14:45:20 -0700 | [diff] [blame^] | 39 | @Component(immediate = true, enabled = false, service = AtomixManager.class) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 40 | public class AtomixManager { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 41 | 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] | 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 45 | 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 Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 60 | log.info("{}", metadataService.getClusterMetadata()); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 61 | 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 Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 73 | 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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 79 | .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 80 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 85 | .withAddress(node.host(), node.tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 86 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 104 | .withAddress(metadataService.getLocalNode().host(), metadataService.getLocalNode().tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 105 | .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 Halterman | e458f00 | 2018-09-18 17:42:05 -0700 | [diff] [blame] | 110 | .withAddress(node.host(), node.tcpPort()) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 111 | .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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 126 | } |
| 127 | } |