/
key_value.go
215 lines (186 loc) · 6.5 KB
/
key_value.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
package history
import (
"context"
"database/sql"
"strconv"
sq "github.com/Masterminds/squirrel"
"github.com/lantah/go/support/errors"
)
const (
ingestVersion = "exp_ingest_version"
// Distributed ingestion in OrbitR relies on this key and it is part
// of migration files. If you need to update the key name remember
// to upgrade it in migration files too!
lastLedgerKey = "exp_ingest_last_ledger"
stateInvalid = "exp_state_invalid"
offerCompactionSequence = "offer_compaction_sequence"
liquidityPoolCompactionSequence = "liquidity_pool_compaction_sequence"
)
// GetLastLedgerIngestNonBlocking works like GetLastLedgerIngest but
// it does not block the value and does not return error if the value
// has not been previously set.
// This is used in status reporting (ex. in root resource of OrbitR).
func (q *Q) GetLastLedgerIngestNonBlocking(ctx context.Context) (uint32, error) {
lastIngestedLedger, err := q.getValueFromStore(ctx, lastLedgerKey, false)
if err != nil {
return 0, err
}
if lastIngestedLedger == "" {
return 0, nil
} else {
ledgerSequence, err := strconv.ParseUint(lastIngestedLedger, 10, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting lastIngestedLedger value")
}
return uint32(ledgerSequence), nil
}
}
// GetLastLedgerIngest returns the last ledger ingested by ingest system
// in OrbitR. Returns ErrKeyNotFound error if no value has been previously set.
// This is using `SELECT ... FOR UPDATE` what means it's blocking the row for all other
// transactions.This behavior is critical in distributed ingestion so do not change
// it unless you know what you are doing.
// The value can be set using UpdateLastLedgerIngest.
func (q *Q) GetLastLedgerIngest(ctx context.Context) (uint32, error) {
lastIngestedLedger, err := q.getValueFromStore(ctx, lastLedgerKey, true)
if err != nil {
return 0, err
}
if lastIngestedLedger == "" {
// This key should always be in a DB (is added in migrations). Otherwise
// locking won't work.
return 0, errors.Errorf("`%s` key cannot be found in the key value store", ingestVersion)
} else {
ledgerSequence, err := strconv.ParseUint(lastIngestedLedger, 10, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting lastIngestedLedger value")
}
return uint32(ledgerSequence), nil
}
}
// UpdateLastLedgerIngest updates the last ledger ingested by ingest system.
// Can be read using GetLastLedgerExpIngest.
func (q *Q) UpdateLastLedgerIngest(ctx context.Context, ledgerSequence uint32) error {
return q.updateValueInStore(
ctx,
lastLedgerKey,
strconv.FormatUint(uint64(ledgerSequence), 10),
)
}
// GetIngestVersion returns the ingestion version. Returns zero
// if there is no value.
func (q *Q) GetIngestVersion(ctx context.Context) (int, error) {
parsed, err := q.getIntValueFromStore(ctx, ingestVersion, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting sequence value")
}
return int(parsed), nil
}
// UpdateIngestVersion updates the ingestion version.
func (q *Q) UpdateIngestVersion(ctx context.Context, version int) error {
return q.updateValueInStore(
ctx,
ingestVersion,
strconv.FormatUint(uint64(version), 10),
)
}
// GetExpStateInvalid returns true if the state was found to be invalid.
// Returns false otherwise.
func (q *Q) GetExpStateInvalid(ctx context.Context) (bool, error) {
invalid, err := q.getValueFromStore(ctx, stateInvalid, false)
if err != nil {
return false, err
}
if invalid == "" {
return false, nil
} else {
val, err := strconv.ParseBool(invalid)
if err != nil {
return false, errors.Wrap(err, "Error converting invalid value")
}
return val, nil
}
}
// UpdateExpStateInvalid updates the state invalid value.
func (q *Q) UpdateExpStateInvalid(ctx context.Context, val bool) error {
return q.updateValueInStore(
ctx,
stateInvalid,
strconv.FormatBool(val),
)
}
// GetOfferCompactionSequence returns the sequence number corresponding to the
// last time the offers table was compacted.
func (q *Q) GetOfferCompactionSequence(ctx context.Context) (uint32, error) {
parsed, err := q.getIntValueFromStore(ctx, offerCompactionSequence, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting sequence value")
}
return uint32(parsed), nil
}
// GetLiquidityPoolCompactionSequence returns the sequence number corresponding to the
// last time the liquidity pools table was compacted.
func (q *Q) GetLiquidityPoolCompactionSequence(ctx context.Context) (uint32, error) {
parsed, err := q.getIntValueFromStore(ctx, liquidityPoolCompactionSequence, 32)
if err != nil {
return 0, errors.Wrap(err, "Error converting sequence value")
}
return uint32(parsed), nil
}
func (q *Q) getIntValueFromStore(ctx context.Context, key string, bitSize int) (int64, error) {
sequence, err := q.getValueFromStore(ctx, key, false)
if err != nil {
return 0, err
}
if sequence == "" {
return 0, nil
}
parsed, err := strconv.ParseInt(sequence, 10, bitSize)
if err != nil {
return 0, errors.Wrap(err, "Error converting value")
}
return parsed, nil
}
// UpdateOfferCompactionSequence sets the sequence number corresponding to the
// last time the offers table was compacted.
func (q *Q) UpdateOfferCompactionSequence(ctx context.Context, sequence uint32) error {
return q.updateValueInStore(
ctx,
offerCompactionSequence,
strconv.FormatUint(uint64(sequence), 10),
)
}
func (q *Q) UpdateLiquidityPoolCompactionSequence(ctx context.Context, sequence uint32) error {
return q.updateValueInStore(
ctx,
liquidityPoolCompactionSequence,
strconv.FormatUint(uint64(sequence), 10),
)
}
// getValueFromStore returns a value for a given key from KV store. If value
// is not present in the key value store "" will be returned.
func (q *Q) getValueFromStore(ctx context.Context, key string, forUpdate bool) (string, error) {
query := sq.Select("key_value_store.value").
From("key_value_store").
Where("key_value_store.key = ?", key)
if forUpdate {
query = query.Suffix("FOR UPDATE")
}
var value string
if err := q.Get(ctx, &value, query); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return "", nil
}
return "", errors.Wrap(err, "could not get value")
}
return value, nil
}
// updateValueInStore updates a value for a given key in KV store
func (q *Q) updateValueInStore(ctx context.Context, key, value string) error {
query := sq.Insert("key_value_store").
Columns("key", "value").
Values(key, value).
Suffix("ON CONFLICT (key) DO UPDATE SET value=EXCLUDED.value")
_, err := q.Exec(ctx, query)
return err
}