-
Notifications
You must be signed in to change notification settings - Fork 681
/
neighbor_connections.go
467 lines (404 loc) · 12.9 KB
/
neighbor_connections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package hnsw
import (
"context"
"math"
"time"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/adapters/repos/db/helpers"
"github.com/weaviate/weaviate/adapters/repos/db/priorityqueue"
"github.com/weaviate/weaviate/adapters/repos/db/vector/compressionhelpers"
"github.com/weaviate/weaviate/adapters/repos/db/vector/hnsw/visited"
)
func (h *hnsw) findAndConnectNeighbors(node *vertex,
entryPointID uint64, nodeVec []float32, distancer compressionhelpers.CompressorDistancer, targetLevel, currentMaxLevel int,
denyList helpers.AllowList,
) error {
nfc := newNeighborFinderConnector(h, node, entryPointID, nodeVec, distancer, targetLevel,
currentMaxLevel, denyList, false)
return nfc.Do()
}
func (h *hnsw) reconnectNeighboursOf(node *vertex,
entryPointID uint64, nodeVec []float32, distancer compressionhelpers.CompressorDistancer, targetLevel, currentMaxLevel int,
denyList helpers.AllowList,
) error {
nfc := newNeighborFinderConnector(h, node, entryPointID, nodeVec, distancer, targetLevel,
currentMaxLevel, denyList, true)
return nfc.Do()
}
type neighborFinderConnector struct {
ctx context.Context
graph *hnsw
node *vertex
entryPointID uint64
entryPointDist float32
nodeVec []float32
distancer compressionhelpers.CompressorDistancer
targetLevel int
currentMaxLevel int
denyList helpers.AllowList
// bufLinksLog BufferedLinksLogger
tombstoneCleanupNodes bool
}
func newNeighborFinderConnector(graph *hnsw, node *vertex, entryPointID uint64,
nodeVec []float32, distancer compressionhelpers.CompressorDistancer, targetLevel, currentMaxLevel int,
denyList helpers.AllowList, tombstoneCleanupNodes bool,
) *neighborFinderConnector {
return &neighborFinderConnector{
ctx: graph.shutdownCtx,
graph: graph,
node: node,
entryPointID: entryPointID,
nodeVec: nodeVec,
distancer: distancer,
targetLevel: targetLevel,
currentMaxLevel: currentMaxLevel,
denyList: denyList,
tombstoneCleanupNodes: tombstoneCleanupNodes,
}
}
func (n *neighborFinderConnector) Do() error {
for level := min(n.targetLevel, n.currentMaxLevel); level >= 0; level-- {
err := n.doAtLevel(level)
if err != nil {
return errors.Wrapf(err, "at level %d", level)
}
}
return nil
}
func (n *neighborFinderConnector) processNode(id uint64) (float32, error) {
var dist float32
var ok bool
var err error
if n.distancer == nil {
dist, ok, err = n.graph.distBetweenNodeAndVec(id, n.nodeVec)
} else {
dist, ok, err = n.distancer.DistanceToNode(id)
}
if err != nil {
// not an error we could recover from - fail!
return math.MaxFloat32, errors.Wrapf(err,
"calculate distance between insert node and entrypoint")
}
if !ok {
return math.MaxFloat32, nil
}
return dist, nil
}
func (n *neighborFinderConnector) processRecursively(from uint64, results *priorityqueue.Queue[any], visited visited.ListSet, level, top int) error {
if err := n.ctx.Err(); err != nil {
return err
}
var pending []uint64
if uint64(len(n.graph.nodes)) < from || n.graph.nodes[from] == nil {
n.graph.handleDeletedNode(from)
return nil
}
// lock the nodes slice
n.graph.shardedNodeLocks.Lock(from)
// lock the node itself
n.graph.nodes[from].Lock()
if level >= len(n.graph.nodes[from].connections) {
n.graph.nodes[from].Unlock()
n.graph.shardedNodeLocks.Unlock(from)
return nil
}
connections := make([]uint64, len(n.graph.nodes[from].connections[level]))
copy(connections, n.graph.nodes[from].connections[level])
n.graph.nodes[from].Unlock()
n.graph.shardedNodeLocks.Unlock(from)
for _, id := range connections {
if visited.Visited(id) {
continue
}
visited.Visit(id)
if n.denyList.Contains(id) {
pending = append(pending, id)
continue
}
dist, err := n.processNode(id)
if err != nil {
return err
}
if results.Len() >= top && dist < results.Top().Dist {
results.Pop()
results.Insert(id, dist)
} else if results.Len() < top {
results.Insert(id, dist)
}
}
for _, id := range pending {
if results.Len() >= top {
dist, err := n.processNode(id)
if err != nil {
return err
}
if dist > results.Top().Dist {
continue
}
}
err := n.processRecursively(id, results, visited, level, top)
if err != nil {
return err
}
}
return nil
}
func (n *neighborFinderConnector) doAtLevel(level int) error {
before := time.Now()
var results *priorityqueue.Queue[any]
var extraIDs []uint64 = nil
var total int = 0
var maxConnections int = n.graph.maximumConnections
if n.tombstoneCleanupNodes {
results = n.graph.pools.pqResults.GetMax(n.graph.efConstruction)
n.graph.pools.visitedListsLock.RLock()
visited := n.graph.pools.visitedLists.Borrow()
n.graph.pools.visitedListsLock.RUnlock()
n.node.Lock()
connections := make([]uint64, len(n.node.connections[level]))
copy(connections, n.node.connections[level])
n.node.Unlock()
visited.Visit(n.node.id)
top := n.graph.efConstruction
var pending []uint64 = nil
for _, id := range connections {
visited.Visit(id)
if n.denyList.Contains(id) {
pending = append(pending, id)
continue
}
extraIDs = append(extraIDs, id)
top--
total++
}
for _, id := range pending {
visited.Visit(id)
err := n.processRecursively(id, results, visited, level, top)
if err != nil {
return err
}
}
n.graph.pools.visitedListsLock.RLock()
n.graph.pools.visitedLists.Return(visited)
n.graph.pools.visitedListsLock.RUnlock()
if err := n.pickEntrypoint(); err != nil {
return errors.Wrap(err, "pick entrypoint at level beginning")
}
// use dynamic max connections only during tombstone cleanup
maxConnections = n.maximumConnections(level)
} else {
if err := n.pickEntrypoint(); err != nil {
return errors.Wrap(err, "pick entrypoint at level beginning")
}
eps := priorityqueue.NewMin[any](1)
eps.Insert(n.entryPointID, n.entryPointDist)
var err error
results, err = n.graph.searchLayerByVectorWithDistancer(n.nodeVec, eps, n.graph.efConstruction,
level, nil, n.distancer)
if err != nil {
return errors.Wrapf(err, "search layer at level %d", level)
}
n.graph.insertMetrics.findAndConnectSearch(before)
before = time.Now()
}
if err := n.graph.selectNeighborsHeuristic(results, maxConnections-total, n.denyList); err != nil {
return errors.Wrap(err, "heuristic")
}
n.graph.insertMetrics.findAndConnectHeuristic(before)
before = time.Now()
// // for distributed spike
// neighborsAtLevel[level] = neighbors
neighbors := make([]uint64, total, total+results.Len())
copy(neighbors, extraIDs)
for results.Len() > 0 {
id := results.Pop().ID
neighbors = append(neighbors, id)
}
n.graph.pools.pqResults.Put(results)
// set all outgoing in one go
n.node.setConnectionsAtLevel(level, neighbors)
if err := n.graph.commitLog.ReplaceLinksAtLevel(n.node.id, level, neighbors); err != nil {
return errors.Wrapf(err, "ReplaceLinksAtLevel node %d at level %d", n.node.id, level)
}
for _, neighborID := range neighbors {
if err := n.connectNeighborAtLevel(neighborID, level); err != nil {
return errors.Wrapf(err, "connect neighbor %d", neighborID)
}
}
if len(neighbors) > 0 {
// there could be no neighbors left, if all are marked deleted, in this
// case, don't change the entrypoint
nextEntryPointID := neighbors[len(neighbors)-1]
if nextEntryPointID == n.node.id {
return nil
}
n.entryPointID = nextEntryPointID
}
n.graph.insertMetrics.findAndConnectUpdateConnections(before)
return nil
}
func (n *neighborFinderConnector) connectNeighborAtLevel(neighborID uint64,
level int,
) error {
neighbor := n.graph.nodeByID(neighborID)
if skip := n.skipNeighbor(neighbor); skip {
return nil
}
neighbor.Lock()
defer neighbor.Unlock()
if level > neighbor.level {
// upgrade neighbor level if the level is out of sync due to a delete re-assign
neighbor.upgradeToLevelNoLock(level)
}
currentConnections := neighbor.connectionsAtLevelNoLock(level)
maximumConnections := n.maximumConnections(level)
if len(currentConnections) < maximumConnections {
// we can simply append
// updatedConnections = append(currentConnections, n.node.id)
neighbor.appendConnectionAtLevelNoLock(level, n.node.id, maximumConnections)
if err := n.graph.commitLog.AddLinkAtLevel(neighbor.id, level, n.node.id); err != nil {
return err
}
} else {
// we need to run the heuristic
dist, ok, err := n.graph.distBetweenNodes(n.node.id, neighborID)
if err != nil {
return errors.Wrapf(err, "dist between %d and %d", n.node.id, neighborID)
}
if !ok {
// it seems either the node or the neighbor were deleted in the meantime,
// there is nothing we can do now
return nil
}
candidates := priorityqueue.NewMax[any](len(currentConnections) + 1)
candidates.Insert(n.node.id, dist)
for _, existingConnection := range currentConnections {
dist, ok, err := n.graph.distBetweenNodes(existingConnection, neighborID)
if err != nil {
return errors.Wrapf(err, "dist between %d and %d", existingConnection, neighborID)
}
if !ok {
// was deleted in the meantime
continue
}
candidates.Insert(existingConnection, dist)
}
err = n.graph.selectNeighborsHeuristic(candidates, maximumConnections, n.denyList)
if err != nil {
return errors.Wrap(err, "connect neighbors")
}
neighbor.resetConnectionsAtLevelNoLock(level)
if err := n.graph.commitLog.ClearLinksAtLevel(neighbor.id, uint16(level)); err != nil {
return err
}
for candidates.Len() > 0 {
id := candidates.Pop().ID
neighbor.appendConnectionAtLevelNoLock(level, id, maximumConnections)
if err := n.graph.commitLog.AddLinkAtLevel(neighbor.id, level, id); err != nil {
return err
}
}
}
return nil
}
func (n *neighborFinderConnector) skipNeighbor(neighbor *vertex) bool {
if neighbor == n.node {
// don't connect to self
return true
}
if neighbor == nil || n.graph.hasTombstone(neighbor.id) {
// don't connect to tombstoned nodes. This would only increase the
// cleanup that needs to be done. Even worse: A tombstoned node can be
// cleaned up at any time, also while we are connecting to it. So,
// while the node still exists right now, it might already be nil in
// the next line, which would lead to a nil-pointer panic.
return true
}
return false
}
func (n *neighborFinderConnector) maximumConnections(level int) int {
if level == 0 {
return n.graph.maximumConnectionsLayerZero
}
return n.graph.maximumConnections
}
func (n *neighborFinderConnector) pickEntrypoint() error {
// the neighborFinderConnector always has a suggestion for an entrypoint that
// it got from the outside, most of the times we can use this, but in some
// cases we can't. To see if we can use it, three conditions need to be met:
//
// 1. it needs to exist in the graph, i.e. be not nil
//
// 2. it can't be under maintenance
//
// 3. we need to be able to obtain a vector for it
localDeny := n.denyList.DeepCopy()
candidate := n.entryPointID
// make sure the loop cannot block forever. In most cases, results should be
// found within micro to milliseconds, this is just a last resort to handle
// the unknown somewhat gracefully, for example if there is a bug in the
// underlying object store and we cannot retrieve the vector in time, etc.
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
for {
if err := ctx.Err(); err != nil {
return err
}
success, err := n.tryEpCandidate(candidate)
if err != nil {
return err
}
if success {
return nil
}
// no success so far, we need to keep going and find a better candidate
// make sure we never visit this candidate again
localDeny.Insert(candidate)
// now find a new one
alternative, _, err := n.graph.findNewLocalEntrypoint(localDeny,
n.graph.currentMaximumLayer, candidate)
if err != nil {
return err
}
candidate = alternative
}
}
func (n *neighborFinderConnector) tryEpCandidate(candidate uint64) (bool, error) {
node := n.graph.nodeByID(candidate)
if node == nil {
return false, nil
}
if node.isUnderMaintenance() {
return false, nil
}
var dist float32
var ok bool
var err error
if n.distancer == nil {
dist, ok, err = n.graph.distBetweenNodeAndVec(candidate, n.nodeVec)
} else {
dist, ok, err = n.distancer.DistanceToNode(candidate)
}
if err != nil {
// not an error we could recover from - fail!
return false, errors.Wrapf(err,
"calculate distance between insert node and entrypoint")
}
if !ok {
return false, nil
}
// we were able to calculate a distance, we're good
n.entryPointDist = dist
n.entryPointID = candidate
return true, nil
}