-
Notifications
You must be signed in to change notification settings - Fork 142
/
snapshot_manager_mocked.go
166 lines (140 loc) · 5.23 KB
/
snapshot_manager_mocked.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
package sm_snapshots
import (
"bytes"
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/wasp/packages/chain/statemanager/sm_gpa/sm_gpa_utils"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/util"
)
type MockedSnapshotManager struct {
*snapshotManagerRunner
t *testing.T
log *logger.Logger
createPeriod uint32
readySnapshots map[uint32]*util.SliceStruct[*state.L1Commitment]
readySnapshotsMutex sync.Mutex
snapshotCommitTime time.Duration
timeProvider sm_gpa_utils.TimeProvider
origStore state.Store
nodeStore state.Store
snapshotToLoad SnapshotInfo
snapshotCreateRequestCount atomic.Uint32
snapshotCreatedCount atomic.Uint32
snapshotCreateFinalisedCount atomic.Uint32
}
var (
_ snapshotManagerCore = &MockedSnapshotManager{}
_ SnapshotManager = &MockedSnapshotManager{}
)
func NewMockedSnapshotManager(
t *testing.T,
createPeriod uint32,
delayPeriod uint32,
origStore state.Store,
nodeStore state.Store,
snapshotToLoad SnapshotInfo,
snapshotCommitTime time.Duration,
timeProvider sm_gpa_utils.TimeProvider,
log *logger.Logger,
) *MockedSnapshotManager {
result := &MockedSnapshotManager{
t: t,
log: log.Named("MSnap"),
createPeriod: createPeriod,
readySnapshots: make(map[uint32]*util.SliceStruct[*state.L1Commitment]),
readySnapshotsMutex: sync.Mutex{},
snapshotCommitTime: snapshotCommitTime,
timeProvider: timeProvider,
origStore: origStore,
nodeStore: nodeStore,
snapshotToLoad: snapshotToLoad,
}
result.snapshotManagerRunner = newSnapshotManagerRunner(context.Background(), nodeStore, nil, createPeriod, delayPeriod, result, result.log)
return result
}
// -------------------------------------
// Implementations of SnapshotManager interface
// -------------------------------------
// NOTE: implementations are inherited from snapshotManagerRunner
// -------------------------------------
// Additional API functions of MockedSnapshotManager
// -------------------------------------
func (msmT *MockedSnapshotManager) IsSnapshotReady(snapshotInfo SnapshotInfo) bool {
msmT.readySnapshotsMutex.Lock()
defer msmT.readySnapshotsMutex.Unlock()
commitments, ok := msmT.readySnapshots[snapshotInfo.StateIndex()]
if !ok {
return false
}
return commitments.ContainsBy(func(elem *state.L1Commitment) bool { return elem.Equals(snapshotInfo.Commitment()) })
}
func (msmT *MockedSnapshotManager) WaitSnapshotCreateRequestCount(count uint32, sleepTime time.Duration, maxSleepCount int) bool {
return wait(func() bool { return msmT.snapshotCreateRequestCount.Load() == count }, sleepTime, maxSleepCount)
}
func (msmT *MockedSnapshotManager) WaitSnapshotCreatedCount(count uint32, sleepTime time.Duration, maxSleepCount int) bool {
return wait(func() bool { return msmT.snapshotCreatedCount.Load() == count }, sleepTime, maxSleepCount)
}
func (msmT *MockedSnapshotManager) WaitSnapshotCreateFinalisedCount(count uint32, sleepTime time.Duration, maxSleepCount int) bool {
return wait(func() bool { return msmT.snapshotCreateFinalisedCount.Load() == count }, sleepTime, maxSleepCount)
}
// -------------------------------------
// Implementations of snapshotManagerCore interface
// -------------------------------------
func (msmT *MockedSnapshotManager) createSnapshot(snapshotInfo SnapshotInfo) {
msmT.snapshotCreateRequestCount.Add(1)
msmT.log.Debugf("Creating snapshot %s...", snapshotInfo)
go func() {
<-msmT.timeProvider.After(msmT.snapshotCommitTime)
msmT.snapshotCreatedCount.Add(1)
msmT.snapshotReady(snapshotInfo)
msmT.log.Debugf("Creating snapshot %s: completed", snapshotInfo)
msmT.snapshotCreateFinalisedCount.Add(1)
msmT.snapshotManagerRunner.snapshotCreated(snapshotInfo)
}()
}
func (msmT *MockedSnapshotManager) loadSnapshot() SnapshotInfo {
if msmT.snapshotToLoad == nil {
return nil
}
msmT.log.Debugf("Loading snapshot %s...", msmT.snapshotToLoad)
snapshot := new(bytes.Buffer)
err := msmT.origStore.TakeSnapshot(msmT.snapshotToLoad.TrieRoot(), snapshot)
require.NoError(msmT.t, err)
err = msmT.nodeStore.RestoreSnapshot(msmT.snapshotToLoad.TrieRoot(), snapshot)
require.NoError(msmT.t, err)
msmT.log.Debugf("Loading snapshot %s: snapshot loaded", msmT.snapshotToLoad)
return msmT.snapshotToLoad
}
// -------------------------------------
// Internal functions
// -------------------------------------
func wait(predicateFun func() bool, sleepTime time.Duration, maxSleepCount int) bool {
if predicateFun() {
return true
}
for i := 0; i < maxSleepCount; i++ {
time.Sleep(sleepTime)
if predicateFun() {
return true
}
}
return false
}
func (msmT *MockedSnapshotManager) snapshotReady(snapshotInfo SnapshotInfo) {
msmT.readySnapshotsMutex.Lock()
defer msmT.readySnapshotsMutex.Unlock()
commitments, ok := msmT.readySnapshots[snapshotInfo.StateIndex()]
if ok {
if !commitments.ContainsBy(func(comm *state.L1Commitment) bool { return comm.Equals(snapshotInfo.Commitment()) }) {
commitments.Add(snapshotInfo.Commitment())
}
} else {
msmT.readySnapshots[snapshotInfo.StateIndex()] = util.NewSliceStruct(snapshotInfo.Commitment())
}
}