Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility for configuring priorities per service #436

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Lists all changes with user impact.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [0.22.3]
### Changed
- Added service-specific zone priorities

## [0.22.2]
### Changed
- Migrated metrics to prometheus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class LoadBalancingProperties {
var policy = Cluster.LbPolicy.LEAST_REQUEST
var useKeysSubsetFallbackPolicy = true
var priorities = LoadBalancingPriorityProperties()
var servicePriorities: Map<String, LoadBalancingPriorityProperties> = mapOf()
}

class CanaryProperties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class EnvoyEndpointsFactory(
?.map {
createLbEndpoint(it, serviceInstances.serviceName, locality)
} ?: emptyList())
.setPriority(toEnvoyPriority(zone, locality))
.setPriority(toEnvoyPriority(zone, locality, serviceInstances))
.build()
}

Expand Down Expand Up @@ -286,8 +286,14 @@ class EnvoyEndpointsFactory(
false -> this
}

private fun toEnvoyPriority(zone: String, locality: Locality): Int {
val zonePriorities = properties.loadBalancing.priorities.zonePriorities
private fun toEnvoyPriority(zone: String, locality: Locality, serviceInstances: ServiceInstances?): Int {
var zonePriorities = properties.loadBalancing.priorities.zonePriorities
serviceInstances?.let {
if (properties.loadBalancing.servicePriorities.containsKey(serviceInstances.serviceName)) {
zonePriorities =
properties.loadBalancing.servicePriorities[serviceInstances.serviceName]!!.zonePriorities
}
}
return when (zonePriorities.isNotEmpty()) {
true -> zonePriorities[currentZone]?.get(zone) ?: toEnvoyPriority(locality)
false -> toEnvoyPriority(locality)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,40 @@ internal class EnvoyEndpointsFactoryTest {
.anySatisfy { it.hasZoneWithPriority("DC3", 2) }
}

@Test
fun `should create load assignment with service zone priorities`() {
val envoyEndpointsFactory = EnvoyEndpointsFactory(
snapshotPropertiesWithPriorities
(
mapOf("DC2" to mapOf("DC1" to 1, "DC2" to 1, "DC3" to 1)),
mapOf("DC2" to mapOf("DC1" to 2, "DC2" to 2, "DC3" to 2)),
serviceName
),
currentZone = "DC2"
)
val loadAssignments = envoyEndpointsFactory.createLoadAssignment(setOf(serviceName), multiClusterStateDC2Local)
loadAssignments.assertHasLoadAssignment(
mapOf("DC1" to 2, "DC2" to 2, "DC3" to 2)
)
}

@Test
fun `should create load assignment with global zone priorities when no service config found`() {
val envoyEndpointsFactory = EnvoyEndpointsFactory(
snapshotPropertiesWithPriorities
(
mapOf("DC2" to mapOf("DC1" to 1, "DC2" to 1, "DC3" to 1)),
mapOf("DC2" to mapOf("DC1" to 2, "DC2" to 2, "DC3" to 2)),
"another-service"
),
currentZone = "DC2"
)
val loadAssignments = envoyEndpointsFactory.createLoadAssignment(setOf(serviceName), multiClusterStateDC2Local)
loadAssignments.assertHasLoadAssignment(
mapOf("DC1" to 1, "DC2" to 1, "DC3" to 1)
)
}

private fun List<ClusterLoadAssignment>.assertHasLoadAssignment(map: Map<String, Int>) {
assertThat(this)
.isNotEmpty()
Expand Down Expand Up @@ -453,13 +487,22 @@ internal class EnvoyEndpointsFactoryTest {
assertThat(this.locality.zone).isEqualTo(zone)
}

private fun snapshotPropertiesWithPriorities(priorities: Map<String, Map<String, Int>>) =
private fun snapshotPropertiesWithPriorities(
priorities: Map<String, Map<String, Int>>,
servicePriorities: Map<String, Map<String, Int>> = mapOf(),
serviceName: String? = null
) =
SnapshotProperties().apply {
loadBalancing = LoadBalancingProperties()
.apply {
this.priorities = LoadBalancingPriorityProperties().apply {
zonePriorities = priorities
}
serviceName?.let {
this.servicePriorities = mapOf(serviceName to LoadBalancingPriorityProperties().apply {
zonePriorities = servicePriorities
})
}
}
}

Expand Down
Loading