2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor
19 import kotlinx.coroutines.runBlocking
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BluePrintClusterService
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterInfo
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils
25 import org.springframework.boot.context.event.ApplicationReadyEvent
26 import org.springframework.context.event.EventListener
27 import org.springframework.stereotype.Component
28 import java.time.Duration
29 import java.util.Properties
30 import javax.annotation.PreDestroy
33 * To Start the cluster, minimum 2 Instances/ Replicas od CDS needed.
34 * All instance such as Blueprintprocessor, ResourceResolution, MessagePrioritization should be in
35 * same cluster and should have same cluster name.
37 * Data can be shared only between the clusters, outside the cluster data can't be shared.
38 * If cds-controller-x instance wants to share data with resource-resolution-x instance, then it should be in the
39 * same cluster.(cds-cluster) and same network (cds-network)
42 * 1. Container, Pod and Host names are same.
43 * 2. Container names should end with sequence number.
44 * Blueprintprocessor example be : cds-controller-1, cds-controller-2, cds-controller-3
45 * ResourceResolution example be : resource-resolution-1, resource-resolution-2, resource-resolution-3
46 * 3. Each contained, should have environment properties CLUSTER_ID, CLUSTER_NODE_ID, CLUSTER_JOIN_AS_CLIENT,
49 * CLUSTER_ID: cds-cluster
50 * CLUSTER_NODE_ID: cds-controller-2
51 * CLUSTER_JOIN_AS_CLIENT: "true" or "false"
52 * CLUSTER_CONFIG_FILE: <Config location>
53 * 4. Cluster will be enabled only all the above properties present in the environments.
54 * if CLUSTER_ENABLED is present, then it will try to create cluster.
57 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
59 private val log = logger(BluePrintProcessorCluster::class)
61 @EventListener(ApplicationReadyEvent::class)
62 fun startAndJoinCluster() = runBlocking {
64 if (BluePrintConstants.CLUSTER_ENABLED) {
66 val clusterId = ClusterUtils.clusterId()
67 val nodeId = ClusterUtils.clusterNodeId()
70 (System.getenv(BluePrintConstants.PROPERTY_CLUSTER_JOIN_AS_CLIENT) ?: "false").toBoolean()
72 val clusterConfigFile = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
74 val properties = Properties()
75 properties["hazelcast.logging.type"] = "slf4j"
77 val clusterInfo = ClusterInfo(
78 id = clusterId, nodeId = nodeId,
79 joinAsClient = joinAsClient,
80 configFile = clusterConfigFile,
81 properties = properties
83 bluePrintClusterService.startCluster(clusterInfo)
85 log.info("Cluster is disabled, to enable cluster set the environment CLUSTER_* properties.")
90 fun shutDown() = runBlocking {
91 bluePrintClusterService.shutDown(Duration.ofMillis(1))