Cluster distributed lock service.
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / BluePrintProcessorCluster.kt
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.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.logger
25 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.ClusterUtils
27 import org.springframework.boot.context.event.ApplicationReadyEvent
28 import org.springframework.context.event.EventListener
29 import org.springframework.stereotype.Component
30 import java.time.Duration
31 import javax.annotation.PreDestroy
32
33 /**
34  * To Start the cluster, minimum 2 Instances/ Replicas od CDS needed.
35  * All instance such as Blueprintprocessor, ResourceResolution, MessagePrioritization should be in
36  * same cluster and should have same cluster name.
37  *
38  * Data can be shared only between the clusters, outside the cluster data can't be shared.
39  * If cds-controller-x instance wants to share data with resource-resolution-x instance, then it should be in the
40  * same cluster.(cds-cluster) and same network (cds-network)
41  *
42  * Assumptions:
43  * 1. Container, Pod and Host names are same.
44  * 2. Container names should end with sequence number.
45  *      Blueprintprocessor example be : cds-controller-1, cds-controller-2, cds-controller-3
46  *      ResourceResolution example be : resource-resolution-1, resource-resolution-2,  resource-resolution-3
47  * 3. Each contained, should have environment properties CLUSTER_ID, CLUSTER_NODE_ID, CLUSTER_NODE_ADDRESS,
48  * CLUSTER_MEMBERS, CLUSTER_STORAGE_PATH
49  *     Example values :
50  *      CLUSTER_ID: cds-cluster
51  *      CLUSTER_NODE_ID: cds-controller-2
52  *      CLUSTER_NODE_ADDRESS: cds-controller-2
53  *      CLUSTER_MEMBERS: cds-controller-1,cds-controller-2,cds-controller-3,resource-resolution-1,resource-resolution-2,resource-resolution-3
54  *      CLUSTER_STORAGE_PATH: /opt/app/onap/config/cluster
55  *      CLUSTER_CONFIG_FILE:  /opt/app/onap/config/atomix/atomix-multicast.conf
56  * 4. Cluster will be enabled only all the above properties present in the environments.
57  * if CLUSTER_ENABLED is present, then it will try to create cluster.
58  */
59 @Component
60 open class BluePrintProcessorCluster(private val bluePrintClusterService: BluePrintClusterService) {
61
62     private val log = logger(BluePrintProcessorCluster::class)
63
64     @EventListener(ApplicationReadyEvent::class)
65     fun startAndJoinCluster() = runBlocking {
66
67         if (BluePrintConstants.CLUSTER_ENABLED) {
68
69             val clusterId = ClusterUtils.clusterId()
70             val nodeId = ClusterUtils.clusterNodeId()
71             val nodeAddress = ClusterUtils.clusterNodeAddress()
72
73             val clusterMembers = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_MEMBERS)
74                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_MEMBERS}")
75
76             val clusterMemberList = clusterMembers.splitCommaAsList()
77
78             val clusterStorage = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_STORAGE_PATH)
79                 ?: throw BluePrintProcessorException("couldn't get environment variable ${BluePrintConstants.PROPERTY_CLUSTER_STORAGE_PATH}")
80
81             val clusterConfigFile = System.getenv(BluePrintConstants.PROPERTY_CLUSTER_CONFIG_FILE)
82
83             val clusterInfo = ClusterInfo(
84                 id = clusterId, nodeId = nodeId,
85                 clusterMembers = clusterMemberList, nodeAddress = nodeAddress,
86                 storagePath = clusterStorage,
87                 configFile = clusterConfigFile
88             )
89             bluePrintClusterService.startCluster(clusterInfo)
90         } else {
91             log.info("Cluster is disabled, to enable cluster set the environment CLUSTER_* properties.")
92         }
93     }
94
95     @PreDestroy
96     fun shutDown() = runBlocking {
97         bluePrintClusterService.shutDown(Duration.ofSeconds(1))
98     }
99 }