Skip to content

Commit 45c175c

Browse files
author
Brendan Doyle
committed
add instance id check for overwrite
1 parent 1a111ff commit 45c175c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

core/invoker/src/main/scala/org/apache/openwhisk/core/invoker/InstanceIdAssigner.scala

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import org.apache.curator.framework.recipes.shared.SharedCount
2222
import org.apache.curator.retry.RetryUntilElapsed
2323
import org.apache.openwhisk.common.Logging
2424

25+
import scala.collection.JavaConverters._
26+
2527
/**
2628
* Computes the instanceId for invoker
2729
*
@@ -37,7 +39,8 @@ private[invoker] class InstanceIdAssigner(connectionString: String)(implicit log
3739
zkClient.blockUntilConnected()
3840
logger.info(this, "invokerReg: connected to zookeeper")
3941

40-
val myIdPath = "/invokers/idAssignment/mapping/" + name
42+
val rootPath = "/invokers/idAssignment/mapping"
43+
val myIdPath = rootPath + s"/$name"
4144
val assignedId = if (overwriteId.isEmpty) {
4245
Option(zkClient.checkExists().forPath(myIdPath)) match {
4346
case None =>
@@ -70,6 +73,22 @@ private[invoker] class InstanceIdAssigner(connectionString: String)(implicit log
7073
}
7174
} else {
7275
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+
7392
zkClient.create().orSetData().forPath(myIdPath, BigInt(newId).toByteArray)
7493
logger.info(this, s"invokerReg: invoker $name was assigned invokerId $newId")
7594
newId

tests/src/test/scala/org/apache/openwhisk/core/invoker/test/InstanceIdAssignerTests.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,11 @@ class InstanceIdAssignerTests extends FlatSpec with Matchers with StreamLogging
5555
assigner.setAndGetId("foo") shouldBe 0
5656
assigner.setAndGetId("foo", Some(2)) shouldBe 2
5757
}
58+
59+
it should "fail to overwrite an id for unique name that already exists" in {
60+
val assigner = new InstanceIdAssigner(zkServer.getConnectString)
61+
assigner.setAndGetId("foo") shouldBe 0
62+
assigner.setAndGetId("bar") shouldBe 1
63+
assertThrows[IllegalArgumentException](assigner.setAndGetId("bar", Some(1)))
64+
}
5865
}

0 commit comments

Comments
 (0)