-
Notifications
You must be signed in to change notification settings - Fork 19
/
worker.go
133 lines (112 loc) · 4.03 KB
/
worker.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package snapshot
import (
"encoding/hex"
"sync/atomic"
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/logging"
)
type treeKeyToSnapshot struct {
treeKey []byte
namespace types.SnapshotNamespace
}
type snapshotResult struct {
input treeKeyToSnapshot
toRemove bool
err error
state []byte
updated bool
}
func gatherState(e *Engine, treeKeysToSnapshotChan chan treeKeyToSnapshot, snapshotResultsChan chan<- snapshotResult, treeKeysCounter *atomic.Int64, recordMetrics *snapMetricsState) {
for toSnapshot := range treeKeysToSnapshotChan {
// this is for metrics
startTime := time.Now()
currentNamespace := toSnapshot.namespace
treeKeyStr := string(toSnapshot.treeKey)
t0 := time.Now()
e.snapshotLock.RLock()
provider := e.treeKeysToProviders[treeKeyStr]
providerKey := e.treeKeysToProviderKeys[treeKeyStr]
e.snapshotLock.RUnlock()
if provider.Stopped() {
snapshotResultsChan <- snapshotResult{input: toSnapshot, updated: true, toRemove: true}
if treeKeysCounter.Add(-1) <= 0 {
close(treeKeysToSnapshotChan)
close(snapshotResultsChan)
}
continue
}
state, additionalProviders, err := provider.GetState(providerKey)
if err != nil {
snapshotResultsChan <- snapshotResult{input: toSnapshot, err: err, updated: true}
close(treeKeysToSnapshotChan)
close(snapshotResultsChan)
return
}
var treeKeys [][]byte
var ok bool
additionalTreeKeysToSnapshot := []treeKeyToSnapshot{}
// The provider has generated new providers, register them with the engine
// add them to the AVL tree
for _, additionalProvider := range additionalProviders {
knownTreeKeys := map[string]struct{}{}
// need to atomically check what's in there for the tree key and then add the provider
e.snapshotLock.Lock()
treeKeys, ok = e.namespacesToTreeKeys[additionalProvider.Namespace()]
if ok {
for _, treeKey := range treeKeys {
knownTreeKeys[string(treeKey)] = struct{}{}
}
}
e.addProviders(additionalProvider)
e.snapshotLock.Unlock()
e.log.Debug("Additional provider added by the worker",
logging.String("namespace", additionalProvider.Namespace().String()),
)
e.snapshotLock.RLock()
treeKeys, ok = e.namespacesToTreeKeys[additionalProvider.Namespace()]
e.snapshotLock.RUnlock()
if !ok || len(treeKeys) == 0 {
continue
}
for _, tk := range treeKeys {
// ignore tree keys we've already done
if _, ok := knownTreeKeys[string(tk)]; ok {
continue
}
additionalTreeKeysToSnapshot = append(additionalTreeKeysToSnapshot, treeKeyToSnapshot{treeKey: tk, namespace: additionalProvider.Namespace()})
}
}
e.log.Debug("State updated",
logging.String("tree-key", treeKeyStr),
logging.String("hash", hex.EncodeToString(crypto.Hash(state))),
logging.Float64("took", time.Since(t0).Seconds()),
)
treeKeysCounter.Add(int64(len(additionalTreeKeysToSnapshot)))
for _, treeKeyToSnapshot := range additionalTreeKeysToSnapshot {
treeKeysToSnapshotChan <- treeKeyToSnapshot
}
snapshotResultsChan <- snapshotResult{input: toSnapshot, state: state, updated: true}
if treeKeysCounter.Add(-1) <= 0 {
close(treeKeysToSnapshotChan)
close(snapshotResultsChan)
}
timeTaken := time.Since(startTime)
recordMetrics.Register(currentNamespace.String(), timeTaken, len(state))
}
}