Skip to content

Commit cec1bc6

Browse files
author
Eren Boz
committed
simplify the alternative solution
1 parent 99b529f commit cec1bc6

2 files changed

Lines changed: 179 additions & 91 deletions

File tree

internal/xds/balancer/clusterresolver/configbuilder_childname.go

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ import (
3131
// struct keeps state between generate() calls, and a later generate() might
3232
// return names returned by the previous call.
3333
type nameGenerator struct {
34-
existingNames map[clients.Locality]string
35-
firstAssignedNames map[clients.Locality]string
36-
prefix uint64
37-
nextID uint64
34+
existingNames map[clients.Locality]string
35+
prefix uint64
36+
nextID uint64
3837
}
3938

4039
func newNameGenerator(prefix uint64) *nameGenerator {
4140
return &nameGenerator{
42-
prefix: prefix,
43-
existingNames: make(map[clients.Locality]string),
44-
firstAssignedNames: make(map[clients.Locality]string),
41+
prefix: prefix,
42+
existingNames: make(map[clients.Locality]string),
4543
}
4644
}
4745

@@ -50,7 +48,6 @@ func newNameGenerator(prefix uint64) *nameGenerator {
5048
// Each priority is a list of localities. The name for the priority is picked as
5149
// - for each locality in this priority, if it exists in the existing names,
5250
// this priority will reuse the name
53-
// - if no name is found in existing names, then prior names will be checked from first assigned names to localities
5451
// - if no reusable name is found for this priority, a new name is generated and associated with first priority
5552
// locality to be remembered through merges and splits
5653
//
@@ -62,7 +59,6 @@ func newNameGenerator(prefix uint64) *nameGenerator {
6259
func (ng *nameGenerator) generate(priorities [][]xdsresource.Locality) []string {
6360
var ret []string
6461
usedNames := make(map[string]bool)
65-
newNames := make(map[clients.Locality]string)
6662
for _, priority := range priorities {
6763
var nameFound string
6864
for _, locality := range priority {
@@ -73,34 +69,19 @@ func (ng *nameGenerator) generate(priorities [][]xdsresource.Locality) []string
7369
}
7470

7571
if nameFound == "" {
76-
// If no name found in current names, check first assigned names instead of generating completely new names.
77-
for _, locality := range priority {
78-
if name, ok := ng.firstAssignedNames[locality.ID]; ok && !usedNames[name] {
79-
nameFound = name
80-
break
81-
}
82-
}
83-
}
84-
85-
if nameFound == "" {
86-
// Cannot find a name to reuse. Generate a new one, and associate it the first priority.
72+
// Cannot find a name to reuse. Generate a new one, and associate it with the first priority.
8773
nameFound = fmt.Sprintf("priority-%d-%d", ng.prefix, ng.nextID)
8874
ng.nextID++
89-
if len(priority) > 0 {
90-
priorID := priority[0].ID
91-
if _, ok := ng.firstAssignedNames[priorID]; !ok {
92-
ng.firstAssignedNames[priorID] = nameFound
75+
for _, locality := range priority {
76+
priorID := locality.ID
77+
if _, ok := ng.existingNames[priorID]; !ok {
78+
ng.existingNames[priorID] = nameFound
79+
break
9380
}
9481
}
9582
}
9683
ret = append(ret, nameFound)
97-
// All localities in this priority share the same name. Add them all to
98-
// the new map.
99-
for _, l := range priority {
100-
newNames[l.ID] = nameFound
101-
}
10284
usedNames[nameFound] = true
10385
}
104-
ng.existingNames = newNames
10586
return ret
10687
}

0 commit comments

Comments
 (0)