Skip to content

Commit

Permalink
move to UUID random suffix when getting the hostname fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lc525 committed Nov 1, 2024
1 parent 4ecf712 commit 8e7a825
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
17 changes: 9 additions & 8 deletions scheduler/data-flow/src/main/kotlin/io/seldon/dataflow/Cli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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_"
Expand Down Expand Up @@ -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<Char> = ('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<String>): Configuration {
Expand Down
14 changes: 14 additions & 0 deletions scheduler/data-flow/src/test/kotlin/io/seldon/dataflow/CliTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8e7a825

Please sign in to comment.