-
Notifications
You must be signed in to change notification settings - Fork 644
/
wal.go
185 lines (161 loc) · 3.39 KB
/
wal.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
// Copyright 2022 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package wal
import (
"path"
"runtime"
"sync"
"go.etcd.io/etcd/raft/v3"
pb "go.etcd.io/etcd/raft/v3/raftpb"
)
type Snapshot struct {
Index uint64
Term uint64
}
// Storage the storage
type Wal struct {
// Log Entry
sync bool
dir string
logfiles []logName // all logs name
last *logFile
nextFileSeq uint64
cache *logFileCache
hs pb.HardState
st Snapshot
mt *meta
once sync.Once
}
// OpenWal
func OpenWal(dir string, sync bool) (*Wal, error) {
dir = path.Clean(dir)
if err := InitPath(dir); err != nil {
return nil, err
}
mt, st, hs, err := NewMeta(dir)
if err != nil {
return nil, err
}
w := &Wal{
sync: sync,
dir: dir,
nextFileSeq: 1,
cache: newLogFileCache(logfileCacheNum,
func(name logName) (*logFile, error) {
lf, err := openLogFile(dir, name, false)
if err != nil {
return nil, err
}
runtime.SetFinalizer(lf, func(lf *logFile) {
lf.Close()
})
return lf, nil
}),
hs: hs,
st: st,
mt: mt,
}
err = w.reload(st.Index + 1)
if err != nil {
return nil, err
}
return w, nil
}
func (w *Wal) InitialState() pb.HardState {
return w.hs
}
func (w *Wal) Entries(lo, hi uint64, maxSize uint64) (entries []pb.Entry, err error) {
if lo >= hi {
return nil, raft.ErrUnavailable
} else if lo <= w.st.Index {
return nil, raft.ErrCompacted
}
return w.entries(lo, hi, maxSize)
}
func (w *Wal) Term(index uint64) (term uint64, err error) {
if index < w.st.Index {
return 0, raft.ErrCompacted
} else if index == w.st.Index {
return w.st.Term, nil
}
return w.term(index)
}
func (w *Wal) FirstIndex() uint64 {
return w.st.Index + 1
}
func (w *Wal) LastIndex() uint64 {
index := w.lastIndex()
if index < w.st.Index {
index = w.st.Index
}
return index
}
func (w *Wal) SaveEntries(entries []pb.Entry) error {
if err := w.saveEntries(entries); err != nil {
return err
}
if w.sync {
return w.Sync()
}
return nil
}
func (w *Wal) SaveHardState(hs pb.HardState) error {
w.mt.SaveHardState(hs)
w.hs = hs
return nil
}
func (w *Wal) Truncate(index uint64) error {
if index <= w.st.Index {
return raft.ErrCompacted
}
term, err := w.term(index)
if err != nil {
return err
}
// set st
w.st = Snapshot{
Index: index,
Term: term,
}
w.mt.SaveSnapshot(w.st)
if err = w.mt.Sync(); err != nil {
return err
}
if err = w.truncateFront(index); err != nil {
return err
}
return nil
}
func (w *Wal) ApplySnapshot(st Snapshot) error {
w.st = st
w.hs = pb.HardState{
Commit: st.Index,
Term: st.Term,
}
w.mt.SaveAll(st, w.hs)
if err := w.mt.Sync(); err != nil {
return err
}
if err := w.truncateAll(w.st.Index + 1); err != nil {
return err
}
return nil
}
func (w *Wal) Close() {
w.once.Do(func() {
w.cache.Close()
w.last.Close()
w.mt.Close()
})
}