2284a6fc0ff3054137871a298cb3b3329638f658
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
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  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor
18
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.util.Properties
29
30 /**
31  * To Start the cluster, minimum 2 Instances/ Replicas od CDS needed.
32  * All instance such as Blueprintprocessor, ResourceResolution, MessagePrioritization should be in
33  * same cluster and should have same cluster name.
34  *
35  * Data can be shared only between the clusters, outside the cluster data can't be shared.
36  * If cds-controller-x instance wants to share data with resource-resolution-x instance, then it should be in the
37  * same cluster.(cds-cluster) and same network (cds-network)
38  *
39  * Assumptions:
40  * 1. Container, Pod and Host names are same.
41  * 2. Container names should end with sequence number.
42  *      Blueprintprocessor example be : cds-controller-1, cds-controller-2, cds-controller-3
43  *      ResourceResolution example be : resource-resolution-1, resource-resolution-2,  resource-resolution-3
44  * 3. Each contained, should have environment properties CLUSTER_ID, CLUSTER_NODE_ID, CLUSTER_JOIN_AS_CLIENT,
45  * CLUSTER_CONFIG_FILE
46  *     Example values :
47  *      CLUSTER_ID: cds-cluster
48  *      CLUSTER_NODE_ID: cds-controller-2
49  *      CLUSTER_JOIN_AS_CLIENT: "true" or "false"
50  *      CLUSTER_CONFIG_FILE:  <Config location>
51  * 4. Cluster will be enabled only all the above properties present in the environments.
52  * if CLUSTER_ENABLED is present, then it will try to create cluster.
53  */
54 @Component
55 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
56
57     private val log = logger(BluePrintProcessorCluster::class)
58
59     @EventListener(ApplicationReadyEvent::class)
60     fun startAndJoinCluster() = runBlocking {
61
62         if (BluePrintConstants.CLUSTER_ENABLED) {
63
64             val clusterId = ClusterUtils.clusterId()
65             val nodeId = ClusterUtils.clusterNodeId()
66
67             val joinAsClient =
68                 (System.getenv(BluePrintConstants.PROPERTY_CLUSTER_JOIN_AS_CLIENT) ?: "false").toBoolean()
69
70             val clusterConfigFile = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
71
72             val properties = Properties()
73             properties["hazelcast.logging.type"] = "slf4j"
74
75             val clusterInfo = ClusterInfo(
76                 id = clusterId, nodeId = nodeId,
77                 joinAsClient = joinAsClient,
78                 configFile = clusterConfigFile,
79                 properties = properties
80             )
81             bluePrintClusterService.startCluster(clusterInfo)
82         } else {
83             log.info("Cluster is disabled, to enable cluster set the environment CLUSTER_* properties.")
84         }
85     }
86 }