@@ -16,7 +16,7 @@ type graph struct {
16
16
// edge => who owns this edge; built in balancer's assignUnassigned
17
17
cxns []uint16
18
18
19
- // scores are all node scores from a seach node. The gscore field
19
+ // scores are all node scores from a seach node. The distance field
20
20
// is reset on findSteal to noScore.
21
21
scores pathScores
22
22
@@ -59,19 +59,18 @@ func (g *graph) changeOwnership(edge int32, newDst uint16) {
59
59
g .cxns [edge ] = newDst
60
60
}
61
61
62
- // findSteal uses A* search to find a path from the best node it can reach.
62
+ // findSteal uses Dijkstra search to find a path from the best node it can reach.
63
63
func (g * graph ) findSteal (from uint16 ) ([]stealSegment , bool ) {
64
64
// First, we must reset our scores from any prior run. This is O(M),
65
65
// but is fast and faster than making a map and extending it a lot.
66
66
for i := range g .scores {
67
- g .scores [i ].gscore = noScore
67
+ g .scores [i ].distance = noScore
68
68
g .scores [i ].done = false
69
69
}
70
70
71
71
first , _ := g .getScore (from )
72
72
73
- first .gscore = 0
74
- first .fscore = h (first , first )
73
+ first .distance = 0
75
74
first .done = true
76
75
77
76
g .heapBuf = append (g .heapBuf [:0 ], first )
@@ -104,22 +103,22 @@ func (g *graph) findSteal(from uint16) ([]stealSegment, bool) {
104
103
continue
105
104
}
106
105
107
- gscore := current .gscore + 1
108
- // If our neghbor gscore is less or equal, then we can
106
+ distance := current .distance + 1
107
+ // If our neghbor distance is less or equal, then we can
109
108
// reach the neighbor through a previous route we have
110
109
// tried and should not try again.
111
- if gscore < neighbor .gscore {
110
+ if distance < neighbor .distance {
112
111
neighbor .parent = current
113
112
neighbor .srcEdge = edge
114
- neighbor .gscore = gscore
115
- neighbor .fscore = gscore + h (first , neighbor )
113
+ neighbor .distance = distance
116
114
if isNew {
117
115
heap .Push (rem , neighbor )
118
116
}
117
+
119
118
// We never need to fix the heap position.
120
- // Our level and fscore is static, and once
121
- // we set gscore , it is the minumum it will be
122
- // and we never revisit this neighbor.
119
+ // Our level is static, and once we set
120
+ // distance , it is the minimum it will be
121
+ // and we never revisit the neighbor.
123
122
}
124
123
}
125
124
}
@@ -135,43 +134,27 @@ type stealSegment struct {
135
134
}
136
135
137
136
type pathScore struct {
138
- done bool
139
- node uint16 // member num
140
- parent * pathScore
141
- srcEdge int32 // partNum
142
- level int32 // partitions owned on this segment
143
- gscore int32 // how many steals it would take to get here
144
- fscore int32
137
+ done bool
138
+ node uint16 // member num
139
+ distance int32 // how many steals it would take to get here
140
+ srcEdge int32 // partNum
141
+ level int32 // partitions owned on this segment
142
+ parent * pathScore
145
143
}
146
144
147
145
type pathScores []pathScore
148
146
149
147
const infinityScore = 1 << 31 - 1
150
148
const noScore = - 1
151
149
152
- // For A*, if we never overestimate (with h), then the path we find is
153
- // optimal. A true estimation of our distance to any node is the node's
154
- // level minus ours.
155
- //
156
- // At worst, our target must be +2 levels from us. So, our estimation
157
- // any node can be our level, +2, minus theirs.
158
- func h (first , target * pathScore ) int32 {
159
- r := first .level + 2 - target .level
160
- if r < 0 {
161
- return 0
162
- }
163
- return r
164
- }
165
-
166
150
func (g * graph ) getScore (node uint16 ) (* pathScore , bool ) {
167
151
r := & g .scores [node ]
168
- exists := r .gscore != noScore
152
+ exists := r .distance != noScore
169
153
if ! exists {
170
154
* r = pathScore {
171
- node : node ,
172
- level : int32 (len (g .b .plan [node ])),
173
- gscore : infinityScore ,
174
- fscore : infinityScore ,
155
+ node : node ,
156
+ level : int32 (len (g .b .plan [node ])),
157
+ distance : infinityScore ,
175
158
}
176
159
}
177
160
return r , ! exists
@@ -188,9 +171,8 @@ func (p *pathHeap) Swap(i, j int) {
188
171
func (p * pathHeap ) Less (i , j int ) bool {
189
172
l , r := (* p )[i ], (* p )[j ]
190
173
return l .level > r .level || l .level == r .level &&
191
- (l .fscore < r .fscore || l .fscore == r .fscore &&
192
- (l .gscore < r .gscore || l .gscore == r .gscore &&
193
- l .node < r .node ))
174
+ (l .distance < r .distance || l .distance == r .distance &&
175
+ l .node < r .node )
194
176
}
195
177
196
178
func (p * pathHeap ) Push (x interface {}) { * p = append (* p , x .(* pathScore )) }
0 commit comments