From 8e7a8258e47a2b9d57dcc8b7d6a10972f2b96be5 Mon Sep 17 00:00:00 2001 From: Lucian Carata Date: Fri, 1 Nov 2024 14:50:25 +0000 Subject: [PATCH] move to UUID random suffix when getting the hostname fails --- .../src/main/kotlin/io/seldon/dataflow/Cli.kt | 17 +++++++++-------- .../test/kotlin/io/seldon/dataflow/CliTest.kt | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/scheduler/data-flow/src/main/kotlin/io/seldon/dataflow/Cli.kt b/scheduler/data-flow/src/main/kotlin/io/seldon/dataflow/Cli.kt index 015983e674..94a5e393a5 100644 --- a/scheduler/data-flow/src/main/kotlin/io/seldon/dataflow/Cli.kt +++ b/scheduler/data-flow/src/main/kotlin/io/seldon/dataflow/Cli.kt @@ -27,6 +27,7 @@ import io.klogging.noCoLogger import io.seldon.dataflow.kafka.security.KafkaSaslMechanisms import io.seldon.dataflow.kafka.security.KafkaSecurityProtocols import java.net.InetAddress +import java.util.UUID object Cli { private const val ENV_VAR_PREFIX = "SELDON_" @@ -117,18 +118,18 @@ object Cli { } private fun getSystemConfig(): Configuration { - val dataflowIdPair = this.dataflowReplicaId to getDataflowId() + val dataflowIdPair = this.dataflowReplicaId to getNewDataflowId() return ConfigurationMap(dataflowIdPair) } - private fun getDataflowId(): String { - return try { - InetAddress.getLocalHost().hostName - } catch (e: Exception) { - val hexCharPool: List = ('a'..'f') + ('0'..'9') - val randomIdLength = 50 - return "seldon-dataflow-engine-" + List(randomIdLength) { hexCharPool.random() }.joinToString("") + fun getNewDataflowId(assignRandomUuid: Boolean = false): String { + if (!assignRandomUuid) { + try { + return InetAddress.getLocalHost().hostName + } catch (_: Exception) { + } } + return "seldon-dataflow-engine-" + UUID.randomUUID().toString() } private fun parseArguments(rawArgs: Array): Configuration { diff --git a/scheduler/data-flow/src/test/kotlin/io/seldon/dataflow/CliTest.kt b/scheduler/data-flow/src/test/kotlin/io/seldon/dataflow/CliTest.kt index 52f6354690..52a97fa4b5 100644 --- a/scheduler/data-flow/src/test/kotlin/io/seldon/dataflow/CliTest.kt +++ b/scheduler/data-flow/src/test/kotlin/io/seldon/dataflow/CliTest.kt @@ -17,9 +17,12 @@ import org.junit.jupiter.params.provider.Arguments.arguments import org.junit.jupiter.params.provider.MethodSource import strikt.api.expectCatching import strikt.api.expectThat +import strikt.assertions.hasLength import strikt.assertions.isEqualTo import strikt.assertions.isNotEqualTo import strikt.assertions.isSuccess +import strikt.assertions.startsWith +import java.util.UUID import java.util.stream.Stream import kotlin.test.Test @@ -51,6 +54,17 @@ internal class CliTest { expectThat(cli[Cli.dataflowReplicaId]) { isEqualTo(testReplicaId) } + + // test random Uuid (v4) + val expectedReplicaIdPrefix = "seldon-dataflow-engine-" + val uuidStringLength = 36 + val randomReplicaUuid = Cli.getNewDataflowId(true) + expectThat(randomReplicaUuid) { + startsWith(expectedReplicaIdPrefix) + hasLength(expectedReplicaIdPrefix.length + uuidStringLength) + } + expectCatching { UUID.fromString(randomReplicaUuid.removePrefix(expectedReplicaIdPrefix)) } + .isSuccess() } companion object {