-
Notifications
You must be signed in to change notification settings - Fork 11
/
pollarchive.go
343 lines (319 loc) · 9.79 KB
/
pollarchive.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
// Copyright (c) 2019 IoTeX
// This program is free software: you can redistribute it and/or modify it under the terms of the
// GNU 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 General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If
// not, see <http://www.gnu.org/licenses/>.
package committee
import (
"context"
"database/sql"
"os"
"reflect"
"sync"
"time"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
// require sqlite3 driver
_ "github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-election/db"
"github.com/iotexproject/iotex-election/types"
"github.com/iotexproject/iotex-election/util"
)
// PollArchive stores registrations, buckets, and other data
type PollArchive interface {
HeightBefore(time.Time) (uint64, error)
// Buckets returns a list of Bucket of a given height
Buckets(uint64) ([]*types.Bucket, error)
// NativeBuckets returns a list of Bucket of a given epoch number
NativeBuckets(uint64) ([]*types.Bucket, error)
// Registrations returns a list of Registration of a given height
Registrations(uint64) ([]*types.Registration, error)
// MintTime returns the mint time of a given height
MintTime(uint64) (time.Time, error)
// NativeMintTime returns the mint time of a given epoch number
NativeMintTime(uint64) (time.Time, error)
// PutPoll puts one poll record
PutPoll(uint64, time.Time, []*types.Registration, []*types.Bucket) error
// PutNativePoll puts one native poll record on IoTeX chain
PutNativePoll(uint64, time.Time, []*types.Bucket) error
// TipHeight returns the tip height stored in archive
TipHeight() (uint64, error)
// Start starts the archive
Start(context.Context) error
// Stop stops the archive
Stop(context.Context) error
}
type archive struct {
startHeight uint64
interval uint64
db *sql.DB
bucketTableOperator Operator
nativeBucketTableOperator Operator
registrationTableOperator Operator
timeTableOperator *TimeTableOperator
nativeTimeTableOperator *TimeTableOperator
oldDB db.KVStoreWithNamespace
// Put (native) polls are synchronized to get rid of the risk of reading uncommitted changes from other tx on the
// same connection.
mutex sync.Mutex
}
// NewArchive creates a new archive of poll
func NewArchive(dbPath string, numOfRetries uint8, startHeight uint64, interval uint64) (PollArchive, error) {
fileExists := func(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
if err != nil {
zap.L().Panic("unexpected error", zap.Error(err))
}
return true
}
isOldCommitteeDB := func(oldDbPath string) bool {
if !fileExists(oldDbPath) {
return false
}
db, err := bolt.Open(oldDbPath, 0666, nil)
if err != nil {
if err == bolt.ErrInvalid {
return false
}
zap.L().Panic("unexpected error", zap.Error(err))
}
if err = db.Close(); err != nil {
zap.L().Panic("unexpected error", zap.Error(err))
}
return true
}
oldDbPath := dbPath + ".bolt"
var kvstore db.KVStoreWithNamespace
if isOldCommitteeDB(dbPath) {
if err := os.Rename(dbPath, oldDbPath); err != nil {
return nil, err
}
}
if fileExists(oldDbPath) {
kvstore = db.NewBoltDB(oldDbPath, numOfRetries)
}
sqlDB, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
bucketTableOperator, err := NewBucketTableOperator("buckets", SQLITE)
if err != nil {
return nil, err
}
nativeBucketTableOperator, err := NewBucketTableOperator("native_buckets", SQLITE)
if err != nil {
return nil, err
}
registrationTableOperator, err := NewRegistrationTableOperator("registrations", SQLITE)
if err != nil {
return nil, err
}
return &archive{
db: sqlDB,
startHeight: startHeight,
interval: interval,
bucketTableOperator: bucketTableOperator,
nativeBucketTableOperator: nativeBucketTableOperator,
registrationTableOperator: registrationTableOperator,
timeTableOperator: NewTimeTableOperator("mint_time", SQLITE),
nativeTimeTableOperator: NewTimeTableOperator("native_mint_time", SQLITE),
oldDB: kvstore,
}, nil
}
func (arch *archive) Registrations(height uint64) ([]*types.Registration, error) {
value, err := arch.registrationTableOperator.Get(height, arch.db, nil)
if err != nil {
return nil, err
}
records, ok := value.([]*types.Registration)
if !ok {
return nil, errors.Errorf("Unexpected type %s", reflect.TypeOf(value))
}
return records, nil
}
func (arch *archive) Buckets(height uint64) ([]*types.Bucket, error) {
value, err := arch.bucketTableOperator.Get(height, arch.db, nil)
if err != nil {
return nil, err
}
records, ok := value.([]*types.Bucket)
if !ok {
return nil, errors.Errorf("Unexpected type %s", reflect.TypeOf(value))
}
return records, nil
}
func (arch *archive) NativeBuckets(epochNum uint64) ([]*types.Bucket, error) {
value, err := arch.nativeBucketTableOperator.Get(epochNum, arch.db, nil)
if err != nil {
return nil, err
}
records, ok := value.([]*types.Bucket)
if !ok {
return nil, errors.Errorf("Unexpected type %s", reflect.TypeOf(value))
}
return records, nil
}
func (arch *archive) PutPoll(height uint64, mintTime time.Time, regs []*types.Registration, buckets []*types.Bucket) (err error) {
arch.mutex.Lock()
defer arch.mutex.Unlock()
tx, err := arch.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if err := arch.registrationTableOperator.Put(height, regs, tx); err != nil {
return err
}
if err := arch.bucketTableOperator.Put(height, buckets, tx); err != nil {
return err
}
if err := arch.timeTableOperator.Put(height, mintTime, tx); err != nil {
return err
}
return tx.Commit()
}
func (arch *archive) PutNativePoll(epochNum uint64, mintTime time.Time, buckets []*types.Bucket) (err error) {
arch.mutex.Lock()
defer arch.mutex.Unlock()
tx, err := arch.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if err := arch.nativeBucketTableOperator.Put(epochNum, buckets, tx); err != nil {
return err
}
if err := arch.nativeTimeTableOperator.Put(epochNum, mintTime, tx); err != nil {
return err
}
return tx.Commit()
}
func (arch *archive) TipHeight() (uint64, error) {
return arch.timeTableOperator.TipHeight(arch.db, nil)
}
func (arch *archive) HeightBefore(ts time.Time) (uint64, error) {
return arch.timeTableOperator.HeightBefore(ts, arch.db, nil)
}
func (arch *archive) MintTime(height uint64) (time.Time, error) {
value, err := arch.timeTableOperator.Get(height, arch.db, nil)
if err != nil {
return time.Time{}, err
}
mintTime, ok := value.(time.Time)
if !ok {
return time.Time{}, errors.Errorf("Unexpected type %s", reflect.TypeOf(value))
}
return mintTime, nil
}
func (arch *archive) NativeMintTime(epochNum uint64) (time.Time, error) {
value, err := arch.nativeTimeTableOperator.Get(epochNum, arch.db, nil)
if err != nil {
return time.Time{}, err
}
mintTime, ok := value.(time.Time)
if !ok {
return time.Time{}, errors.Errorf("Unexpected type %s", reflect.TypeOf(value))
}
return mintTime, nil
}
func (arch *archive) Start(ctx context.Context) (err error) {
var tx *sql.Tx
tx, err = arch.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if err = arch.bucketTableOperator.CreateTables(tx); err != nil {
return err
}
if err = arch.nativeBucketTableOperator.CreateTables(tx); err != nil {
return err
}
if err = arch.registrationTableOperator.CreateTables(tx); err != nil {
return err
}
if err = arch.timeTableOperator.CreateTables(tx); err != nil {
return err
}
if err = arch.nativeTimeTableOperator.CreateTables(tx); err != nil {
return err
}
if err = tx.Commit(); err != nil {
return err
}
return arch.migrate(ctx)
}
func (arch *archive) Stop(_ context.Context) (err error) {
return arch.db.Close()
}
func (arch *archive) migrateResult(height uint64, r *types.ElectionResult) error {
candidates := r.Delegates()
regs := make([]*types.Registration, 0, len(candidates))
for _, candidate := range candidates {
regs = append(regs, &candidate.Registration)
}
votes := r.Votes()
buckets := make([]*types.Bucket, 0, len(votes))
for _, vote := range votes {
buckets = append(buckets, &vote.Bucket)
}
return arch.PutPoll(height, r.MintTime(), regs, buckets)
}
func (arch *archive) migrate(ctx context.Context) (err error) {
if arch.oldDB == nil {
return nil
}
kvstore := db.NewKVStoreWithNamespaceWrapper("electionNS", arch.oldDB)
if err = kvstore.Start(ctx); err != nil {
return err
}
defer func() {
stopErr := kvstore.Stop(ctx)
if err == nil {
err = stopErr
}
}()
nextHeightHash, err := kvstore.Get(db.NextHeightKey)
if err != nil {
return err
}
nextHeight := util.BytesToUint64(nextHeightHash)
tipHeight, err := arch.TipHeight()
var heightToFetch uint64
if err != nil {
heightToFetch = arch.startHeight
} else {
heightToFetch = tipHeight + arch.interval
if heightToFetch >= nextHeight {
return nil
}
}
lastPrintTime := time.Time{}
for heightToFetch < nextHeight {
if time.Now().Sub(lastPrintTime) > 5*time.Second {
zap.L().Info("migrating", zap.Uint64("height", heightToFetch))
lastPrintTime = time.Now()
}
data, err := kvstore.Get(util.Uint64ToBytes(heightToFetch))
if err != nil {
return err
}
r := &types.ElectionResult{}
if err = r.Deserialize(data); err != nil {
return err
}
if err = arch.migrateResult(heightToFetch, r); err != nil {
return errors.Wrapf(err, "failed to migrate %d", heightToFetch)
}
heightToFetch += arch.interval
}
return nil
}