9725553a52b0dcd904ee2ef2604513baf96810c4
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / service / BluePrintClusterService.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.core.service
18
19 import java.time.Duration
20 import java.util.Properties
21
22 interface BluePrintClusterService {
23
24     /** Start the cluster with [clusterInfo], By default clustering service is disabled.
25      * Application module has to start cluster */
26     suspend fun <T> startCluster(configuration: T)
27
28     fun clusterJoined(): Boolean
29
30     fun isClient(): Boolean
31
32     fun isLiteMember(): Boolean
33
34     /** Returns [partitionGroup] master member */
35     suspend fun masterMember(partitionGroup: String): ClusterMember
36
37     /** Returns all the data cluster members */
38     suspend fun allMembers(): Set<ClusterMember>
39
40     /**
41      * Returns application cluster members for [appName] joined as server or lite member,
42      * Node joined as client won't be visible. Here the assumption is node-id is combination of
43      * application id and replica number, for an example Application cds-cluster then the node ids will be
44      * cds-cluster-1, cds-cluster-2, cds-cluster-3
45      */
46     suspend fun applicationMembers(appName: String): Set<ClusterMember>
47
48     /** Create and get or get the distributed data map store with [name] */
49     suspend fun <T> clusterMapStore(name: String): MutableMap<String, T>
50
51     /** Create and get the distributed lock with [name] */
52     suspend fun clusterLock(name: String): ClusterLock
53
54     /** Shut down the cluster with [duration] */
55     suspend fun shutDown(duration: Duration)
56 }
57
58 data class ClusterInfo(
59     val id: String,
60     val nodeId: String,
61     var joinAsClient: Boolean = false,
62     var properties: Properties?,
63     var configFile: String
64 )
65
66 data class ClusterMember(
67     val id: String,
68     val name: String,
69     val memberAddress: String?,
70     val state: String? = null
71 )
72
73 interface ClusterLock {
74     fun name(): String
75     suspend fun lock()
76     suspend fun fenceLock(): String
77     suspend fun tryLock(timeout: Long): Boolean
78     suspend fun tryFenceLock(timeout: Long): String
79     suspend fun unLock()
80     fun isLocked(): Boolean
81     fun close()
82 }
83
84 const val CDS_LOCK_GROUP = "cds-lock"