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 22, 2024
1 parent b5646e6 commit 4785a11
Showing 1 changed file with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,34 +162,28 @@ class EnvoyEndpointsFactory(
zone: String,
locality: Locality
): LocalityLbEndpoints {
val priority = toEnvoyPriority(zone, locality, serviceInstances?.serviceName)
return LocalityLbEndpoints.newBuilder()
.setLocality(EnvoyProxyLocality.newBuilder().setZone(zone).build())
.addAllLbEndpoints(serviceInstances?.instances
?.map {
if (serviceInstances.serviceName == "service-mesh-service-first" ||
serviceInstances.serviceName == "service-mesh-service-second"
) {
logger.info(
"Creating endpoint of ${serviceInstances.serviceName} with locality $locality, " +
"zone $zone, priority props: ${properties.loadBalancing.priorities.zonePriorities}"
)
}
createLbEndpoint(it, serviceInstances.serviceName, locality)
createLbEndpoint(it, serviceInstances.serviceName, locality, priority)
} ?: emptyList())
.setPriority(toEnvoyPriority(zone, locality, serviceInstances?.serviceName))
.setPriority(priority)
.build()
}

private fun createLbEndpoint(
serviceInstance: ServiceInstance,
serviceName: String,
locality: Locality
locality: Locality,
priority: Int
): LbEndpoint {
return LbEndpoint.newBuilder()
.setEndpoint(
buildEndpoint(serviceInstance)
)
.setMetadata(serviceInstance, serviceName, locality)
.setMetadata(serviceInstance, serviceName, locality, priority)
.setLoadBalancingWeightFromInstance(serviceInstance)
.build()
}
Expand Down Expand Up @@ -218,7 +212,8 @@ class EnvoyEndpointsFactory(
private fun LbEndpoint.Builder.setMetadata(
instance: ServiceInstance,
serviceName: String,
locality: Locality
locality: Locality,
priority: Int
): LbEndpoint.Builder {
val lbMetadataKeys = Struct.newBuilder()
val socketMatchMetadataKeys = Struct.newBuilder()
Expand All @@ -244,9 +239,10 @@ 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(locality.name).build()
Value.newBuilder().setStringValue(calcLocality).build()
)
return setMetadata(
Metadata.newBuilder()
Expand Down Expand Up @@ -283,7 +279,7 @@ class EnvoyEndpointsFactory(
if (serviceName == "service-mesh-service-first" ||
serviceName == "service-mesh-service-second"
) {
logger.info(
logger.debug(
"Resolved lb priority to $it with zone=$zone, " +
"currentZone=$currentZone, locality: $locality priority " +
"props=${properties.loadBalancing.priorities.zonePriorities}, serviceName: $serviceName"
Expand Down

0 comments on commit 4785a11

Please sign in to comment.