Skip to content

Commit

Permalink
Implemented locality weighted load balancing (removed logs)
Browse files Browse the repository at this point in the history
  • Loading branch information
nastassia-dailidava committed Jan 22, 2024
1 parent c5a14d1 commit 1312d1b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ControlPlane private constructor(
private var servicesDisposable: Disposable? = null

companion object {
private val logger by logger()

fun builder(
properties: EnvoyControlProperties,
meterRegistry: MeterRegistry
Expand Down Expand Up @@ -361,6 +363,7 @@ class ControlPlane private constructor(
}

fun withCurrentZone(zone: String): ControlPlaneBuilder {
logger.info("Setting zone to $zone")
this.currentZone = zone
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ class EnvoyEndpointsFactory(
zone: String,
locality: Locality
): LocalityLbEndpoints {
val priority = toEnvoyPriority(zone, locality, serviceInstances?.serviceName)
val priority = toEnvoyPriority(zone, locality)
return LocalityLbEndpoints.newBuilder()
.setLocality(EnvoyProxyLocality.newBuilder().setZone(zone).build())
.addAllLbEndpoints(serviceInstances?.instances
?.map {
createLbEndpoint(it, serviceInstances.serviceName, locality, priority, zone)
createLbEndpoint(it, serviceInstances.serviceName, locality, priority)
} ?: emptyList())
.setPriority(priority)
.build()
Expand All @@ -177,14 +177,13 @@ class EnvoyEndpointsFactory(
serviceInstance: ServiceInstance,
serviceName: String,
locality: Locality,
priority: Int,
zone: String
priority: Int
): LbEndpoint {
return LbEndpoint.newBuilder()
.setEndpoint(
buildEndpoint(serviceInstance)
)
.setMetadata(serviceInstance, serviceName, locality, priority, zone)
.setMetadata(serviceInstance, serviceName, locality, priority)
.setLoadBalancingWeightFromInstance(serviceInstance)
.build()
}
Expand Down Expand Up @@ -214,8 +213,7 @@ class EnvoyEndpointsFactory(
instance: ServiceInstance,
serviceName: String,
locality: Locality,
priority: Int,
zone: String
priority: Int
): LbEndpoint.Builder {
val lbMetadataKeys = Struct.newBuilder()
val socketMatchMetadataKeys = Struct.newBuilder()
Expand All @@ -242,16 +240,7 @@ class EnvoyEndpointsFactory(
)
}
val calcLocality = if (priority == 0) Locality.LOCAL.name else locality.name
if (serviceName == "service-mesh-service-first" ||
serviceName == "service-mesh-service-second"
) {
logger.info(
"Resolved metadata locality to $calcLocality with " +
"currentZone=$currentZone, locality: $locality priority $priority " +
", serviceName: $serviceName, zone: $zone" +
"props=${properties.loadBalancing.priorities.zonePriorities}"
)
}

lbMetadataKeys.putFields(
properties.loadBalancing.localityMetadataKey,
Value.newBuilder().setStringValue(calcLocality).build()
Expand Down Expand Up @@ -282,21 +271,11 @@ class EnvoyEndpointsFactory(
false -> this
}

private fun toEnvoyPriority(zone: String, locality: Locality, serviceName: String?): Int {
private fun toEnvoyPriority(zone: String, locality: Locality): Int {
val zonePriorities = properties.loadBalancing.priorities.zonePriorities
return when (zonePriorities.isNotEmpty()) {
true -> zonePriorities[currentZone]?.get(zone) ?: toEnvoyPriority(locality)
false -> toEnvoyPriority(locality)
}.also {
if (serviceName == "service-mesh-service-first" ||
serviceName == "service-mesh-service-second"
) {
logger.debug(
"Resolved lb priority to $it with zone=$zone, " +
"currentZone=$currentZone, locality: $locality priority " +
"props=${properties.loadBalancing.priorities.zonePriorities}, serviceName: $serviceName"
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ControlPlaneConfig {
): ControlPlane =
ControlPlane.builder(properties, meterRegistry)
.withMetrics(metrics)
.withCurrentZone(localDatacenter(consulProperties, properties))
.withCurrentZone(localDatacenter(consulProperties))
.withEnvoyHttpFilters(envoyHttpFilters)
.build(globalStateChanges.combined())

Expand Down Expand Up @@ -115,12 +115,11 @@ class ControlPlaneConfig {
fun localClusterStateChanges(
consulServiceChanges: ConsulServiceChanges,
consulProperties: ConsulProperties,
transformers: List<ServiceInstancesTransformer>,
envoyControlProperties: EnvoyControlProperties
transformers: List<ServiceInstancesTransformer>
): LocalClusterStateChanges = ConsulLocalClusterStateChanges(
consulServiceChanges,
Locality.LOCAL,
localDatacenter(consulProperties, envoyControlProperties),
localDatacenter(consulProperties),
transformers
)

Expand Down Expand Up @@ -174,9 +173,7 @@ class ControlPlaneConfig {
return EnvoyHttpFilters.defaultFilters(properties.envoy.snapshot)
}

fun localDatacenter(properties: ConsulProperties, envoyControlProperties: EnvoyControlProperties): String {
logger.info("LoadBalancing priorities: ${envoyControlProperties.envoy
.snapshot.loadBalancing.priorities.zonePriorities}")
fun localDatacenter(properties: ConsulProperties): String {
return (ConsulClient(properties.host, properties.port).agentSelf.value?.config?.datacenter ?: "local")
.also { logger.info("Resolved local datacenter name: $it") }
}
Expand Down

0 comments on commit 1312d1b

Please sign in to comment.