@@ -22,6 +22,8 @@ import org.apache.curator.framework.recipes.shared.SharedCount
22
22
import org .apache .curator .retry .RetryUntilElapsed
23
23
import org .apache .openwhisk .common .Logging
24
24
25
+ import scala .collection .JavaConverters ._
26
+
25
27
/**
26
28
* Computes the instanceId for invoker
27
29
*
@@ -37,7 +39,8 @@ private[invoker] class InstanceIdAssigner(connectionString: String)(implicit log
37
39
zkClient.blockUntilConnected()
38
40
logger.info(this , " invokerReg: connected to zookeeper" )
39
41
40
- val myIdPath = " /invokers/idAssignment/mapping/" + name
42
+ val rootPath = " /invokers/idAssignment/mapping"
43
+ val myIdPath = rootPath + s " / $name"
41
44
val assignedId = if (overwriteId.isEmpty) {
42
45
Option (zkClient.checkExists().forPath(myIdPath)) match {
43
46
case None =>
@@ -70,6 +73,22 @@ private[invoker] class InstanceIdAssigner(connectionString: String)(implicit log
70
73
}
71
74
} else {
72
75
val newId = overwriteId.get
76
+
77
+ // check if the invokerId already exists for another unique name
78
+ val instanceIdExists = zkClient
79
+ .getChildren()
80
+ .forPath(rootPath)
81
+ .asScala
82
+ .map(uniqueName => {
83
+ val idPath = rootPath + s " / $uniqueName"
84
+ BigInt (zkClient.getData().forPath(idPath)).intValue
85
+ })
86
+ .find(_ == newId)
87
+
88
+ if (instanceIdExists.nonEmpty) {
89
+ throw new IllegalArgumentException (s " invokerReg: an invoker with id $newId already exists in zookeeper " )
90
+ }
91
+
73
92
zkClient.create().orSetData().forPath(myIdPath, BigInt (newId).toByteArray)
74
93
logger.info(this , s " invokerReg: invoker $name was assigned invokerId $newId" )
75
94
newId
0 commit comments