-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpanicstorage.go
142 lines (113 loc) · 3.58 KB
/
panicstorage.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
package raft
import (
"github.com/Sirupsen/logrus"
"github.com/relab/raft/commonpb"
)
// PanicStorage wraps a Storage with methods that panic instead of returning a
// error.
type PanicStorage struct {
s Storage
logger logrus.FieldLogger
}
// NewPanicStorage returns a new initialized PanicStorage.
func NewPanicStorage(s Storage, logger logrus.FieldLogger) *PanicStorage {
return &PanicStorage{
s: s,
logger: logger,
}
}
// Set calls underlying Set method and panics if there is any error.
func (ps *PanicStorage) Set(key uint64, value uint64) {
err := ps.s.Set(key, value)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"key": key,
"value": value,
}).Panicln("Could not set key-value")
}
}
// Get calls underlying Get method and panics if there is any error.
func (ps *PanicStorage) Get(key uint64) uint64 {
value, err := ps.s.Get(key)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"key": key,
}).Panicln("Could not get value")
}
return value
}
// StoreEntries calls underlying StoreEntries method and panics if there is any error.
func (ps *PanicStorage) StoreEntries(entries []*commonpb.Entry) {
err := ps.s.StoreEntries(entries)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"lenentries": len(entries),
}).Panicln("Could not store entries")
}
}
// GetEntry calls underlying GetEntry method and panics if there is any error.
func (ps *PanicStorage) GetEntry(index uint64) *commonpb.Entry {
entry, err := ps.s.GetEntry(index)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"index": index,
}).Panicln("Could not get entry")
}
return entry
}
// GetEntries calls underlying GetEntries method and panics if there is any error.
func (ps *PanicStorage) GetEntries(first, last uint64) []*commonpb.Entry {
entries, err := ps.s.GetEntries(first, last)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"first": first,
"last": last,
}).Panicln("Could not get entries")
}
return entries
}
// RemoveEntries calls underlying RemoveEntries method and panics if there is any error.
func (ps *PanicStorage) RemoveEntries(first, last uint64) {
err := ps.s.RemoveEntries(first, last)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"first": first,
"last": last,
}).Panicln("Could not remove entries")
}
}
// FirstIndex calls underlying FirstIndex method and panics if there is any error.
func (ps *PanicStorage) FirstIndex() uint64 {
idx, err := ps.s.FirstIndex()
if err != nil {
ps.logger.WithError(err).Panicln("Could not get first index")
}
return idx
}
// NextIndex calls underlying NextIndex method and panics if there is any error.
func (ps *PanicStorage) NextIndex() uint64 {
idx, err := ps.s.NextIndex()
if err != nil {
ps.logger.WithError(err).Panicln("Could not get next index")
}
return idx
}
// SetSnapshot calls underlying SetSnapshot method and panics if there is any error.
func (ps *PanicStorage) SetSnapshot(snapshot *commonpb.Snapshot) {
err := ps.s.SetSnapshot(snapshot)
if err != nil {
ps.logger.WithError(err).WithFields(logrus.Fields{
"snapshotterm": snapshot.Term,
"lastincludedindex": snapshot.LastIncludedIndex,
"lastincludedterm": snapshot.LastIncludedTerm,
}).Panicln("Could not set snapshot")
}
}
// GetSnapshot calls underlying GetSnapshot method and panics if there is any error.
func (ps *PanicStorage) GetSnapshot() *commonpb.Snapshot {
snapshot, err := ps.s.GetSnapshot()
if err != nil {
ps.logger.WithError(err).Panicln("Could not get snapshot")
}
return snapshot
}