-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stats): Persist the unique id to redis (#947)
* feat(stats): Persist the unique id to redis Currently the only place that the unique id for a Spinnaker instance is persisted is in the hal config. This works for users using halyard as it gets generated the first time it is needed then gets re-used on subsequent deployments. (It does have the failure mode of copying the hal config as then the new instance would have the same id as the old one.) This doesn't work for users either deploying without Halyard or deploying via kleat. In this case there is nowhere to store this uniqueid (as kleat explicitly treats the config only as input). To solve this problem, we'll store the unique id in redis. On startup, we'll check to see if there's a value in redis; if there is we'll use that, if not we'll use the configured value (and persist it). * fix(stats): Address code review comments * fix(stats): Remove duplicated code
- Loading branch information
Showing
5 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
echo-telemetry/src/main/java/com/netflix/spinnaker/echo/telemetry/InstanceIdSupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2020 Google, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.echo.telemetry | ||
|
||
import com.netflix.spinnaker.echo.config.TelemetryConfig | ||
import com.netflix.spinnaker.kork.jedis.RedisClientSelector | ||
import com.netflix.spinnaker.kork.jedis.exception.RedisClientNotFound | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.stereotype.Component | ||
import redis.clients.jedis.commands.JedisCommands | ||
|
||
val UNIQUE_ID_KEY = "spinnaker:stats:instanceId" | ||
|
||
@Component | ||
@ConditionalOnProperty("stats.enabled") | ||
class InstanceIdSupplier( | ||
private val config: TelemetryConfig.TelemetryConfigProps, | ||
redisSelector: RedisClientSelector? | ||
) { | ||
private val log: Logger = LoggerFactory.getLogger(this.javaClass) | ||
private val redis = try { redisSelector?.primary("default") } catch (e: RedisClientNotFound) { null } | ||
val uniqueId by lazy { getOrSetId(config.instanceId) } | ||
|
||
/** | ||
* Returns the current instance id for this Spinnaker instance. If no id is currently set, | ||
* sets it to [newId] and returns [newId]. | ||
*/ | ||
private fun getOrSetId(newId: String): String { | ||
return try { | ||
redis?.withCommandsClient<String> { c: JedisCommands -> | ||
c.setnx(UNIQUE_ID_KEY, newId) | ||
c.get(UNIQUE_ID_KEY) | ||
} | ||
} catch (e: Exception) { | ||
log.warn("Error synchronizing unique instance id with redis", e) | ||
null | ||
} ?: newId | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...-telemetry/src/test/kotlin/com/netflix/spinnaker/echo/telemetry/InstanceIdSupplierTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2020 Google, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.echo.telemetry | ||
|
||
import com.netflix.spinnaker.echo.config.TelemetryConfig.TelemetryConfigProps | ||
import com.netflix.spinnaker.kork.jedis.EmbeddedRedis | ||
import com.netflix.spinnaker.kork.jedis.JedisClientDelegate | ||
import com.netflix.spinnaker.kork.jedis.RedisClientSelector | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.jupiter.api.Test | ||
import redis.clients.jedis.Jedis | ||
import redis.clients.jedis.exceptions.JedisException | ||
import redis.clients.jedis.util.Pool | ||
import strikt.api.expectThat | ||
import strikt.assertions.isEqualTo | ||
|
||
class InstanceIdSupplierTest { | ||
@Test | ||
fun `returns the configured value with no redis backend`() { | ||
val instanceIdSupplier = InstanceIdSupplier(withInstanceId("my-id"), null) | ||
|
||
expectThat(instanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
} | ||
|
||
@Test | ||
fun `returns the configured value when no current value in redis`() { | ||
val instanceIdSupplier = InstanceIdSupplier(withInstanceId("my-id"), getRedis()) | ||
|
||
expectThat(instanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
} | ||
|
||
@Test | ||
fun `returns the value in redis if it exists`() { | ||
val redis = getRedis() | ||
val instanceIdSupplier = InstanceIdSupplier(withInstanceId("my-id"), redis) | ||
expectThat(instanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
|
||
val otherInstanceIdSupplier = InstanceIdSupplier(withInstanceId("my-new-id"), redis) | ||
expectThat(otherInstanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
} | ||
|
||
@Test | ||
fun `returns the configured value if the selector is unable to find the configured redis`() { | ||
val instanceIdSupplier = InstanceIdSupplier(withInstanceId("my-id"), getMissingRedis()) | ||
expectThat(instanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
} | ||
|
||
@Test | ||
fun `returns the configured value if a redis exception occurs`() { | ||
val instanceIdSupplier = InstanceIdSupplier(withInstanceId("my-id"), getBadRedis()) | ||
expectThat(instanceIdSupplier.uniqueId).isEqualTo("my-id") | ||
} | ||
|
||
private fun withInstanceId(id: String): TelemetryConfigProps { | ||
val result = TelemetryConfigProps() | ||
result.instanceId = id | ||
return result | ||
} | ||
|
||
private fun getRedis(): RedisClientSelector { | ||
val embeddedRedis = EmbeddedRedis.embed() | ||
val redisClientDelegate = JedisClientDelegate("primaryDefault", embeddedRedis.pool) | ||
return RedisClientSelector(listOf(redisClientDelegate)) | ||
} | ||
|
||
private fun getMissingRedis(): RedisClientSelector { | ||
return RedisClientSelector(listOf()) | ||
} | ||
|
||
private fun getBadRedis(): RedisClientSelector { | ||
val jedis = mockk<Jedis>() | ||
every { jedis.setnx(any<String>(), any<String>()) } throws JedisException("An error occurred") | ||
|
||
val pool = mockk<Pool<Jedis>>() | ||
every { pool.resource } returns jedis | ||
|
||
val delegate = JedisClientDelegate("primaryDefault", pool) | ||
|
||
return RedisClientSelector(listOf(delegate)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters