Skip to content

Commit

Permalink
Implemented locality weighted load balancing (fixed locality)
Browse files Browse the repository at this point in the history
  • Loading branch information
nastassia-dailidava committed Jan 25, 2024
1 parent 5a7a250 commit 5f9fbc8
Showing 1 changed file with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,26 @@ class EnvoyEndpointsFactory(
zone: String,
locality: Locality
): LocalityLbEndpoints {
val priority = toEnvoyPriority(zone, locality)
return LocalityLbEndpoints.newBuilder()
.setLocality(EnvoyProxyLocality.newBuilder().setZone(zone).build())
.addAllLbEndpoints(serviceInstances?.instances
?.map {
createLbEndpoint(it, serviceInstances.serviceName, locality, priority)
createLbEndpoint(it, serviceInstances.serviceName, locality)
} ?: emptyList())
.setPriority(priority)
.setPriority(toEnvoyPriority(zone, locality))
.build()
}

private fun createLbEndpoint(
serviceInstance: ServiceInstance,
serviceName: String,
locality: Locality,
priority: Int
locality: Locality
): LbEndpoint {
return LbEndpoint.newBuilder()
.setEndpoint(
buildEndpoint(serviceInstance)
)
.setMetadata(serviceInstance, serviceName, locality, priority)
.setMetadata(serviceInstance, serviceName, locality)
.setLoadBalancingWeightFromInstance(serviceInstance)
.build()
}
Expand Down Expand Up @@ -204,8 +202,7 @@ class EnvoyEndpointsFactory(
private fun LbEndpoint.Builder.setMetadata(
instance: ServiceInstance,
serviceName: String,
locality: Locality,
priority: Int
locality: Locality
): LbEndpoint.Builder {
val lbMetadataKeys = Struct.newBuilder()
val socketMatchMetadataKeys = Struct.newBuilder()
Expand All @@ -231,11 +228,9 @@ class EnvoyEndpointsFactory(
Value.newBuilder().setBoolValue(true).build()
)
}
val calcLocality = if (priority == 0) Locality.LOCAL.name else locality.name

lbMetadataKeys.putFields(
properties.loadBalancing.localityMetadataKey,
Value.newBuilder().setStringValue(calcLocality).build()
Value.newBuilder().setStringValue(locality.name).build()
)
return setMetadata(
Metadata.newBuilder()
Expand Down

0 comments on commit 5f9fbc8

Please sign in to comment.