/
storage_disk.go
152 lines (144 loc) · 3.46 KB
/
storage_disk.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
package storage
import (
"io/ioutil"
"os"
"strings"
"master-server/raft/proto"
)
type DiskRotateStorage struct {
dir string
shardId uint64
entryCache *LogEntryCache
curLogFile *LogFile
hardStateFile *HardStateFile
}
func NewDiskRotateStorage(dir string, shardId uint64) (*DiskRotateStorage, error) {
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
if fi, e := os.Stat(dir); e != nil {
return nil, e
} else if !fi.IsDir() {
return nil, Err_LogDirNotExist
}
// hard state
hsf, e := LoadHardStateFile(dir, shardId)
if e != nil && e != Err_NoHardStateFile {
return nil, e
}
logFile, e := LoadLastLogFile(dir, shardId)
if e != nil && !strings.Contains(e.Error(), "no such file or directory") {
return nil, e
}
// log file
ds := &DiskRotateStorage{
dir: dir,
shardId: shardId,
entryCache: NewLogEntryCache(Log_EntryCacheNum),
curLogFile: logFile,
hardStateFile: hsf,
}
return ds, nil
}
func (ds *DiskRotateStorage) StoreEntries(entries []*proto.Entry) error {
if len(entries) == 0 {
return nil
}
if nowLastIndex, e := ds.getLastIndex(); e != nil && ds.curLogFile != nil {
return e
} else if nowLastIndex+1 < entries[0].Index {
return Err_LastIndexNotMatch
}
e := ds.cutEnds(entries[0].Index - 1)
if e != nil {
return e
}
if e = ds.putEntries(entries); e != nil {
return e
}
ds.entryCache.write(entries)
return nil
}
func (ds *DiskRotateStorage) Truncate(index uint64) error {
pos := getLogFilePos(index)
for i := 0; i < pos; i++ {
os.RemoveAll(ds.dir + LogFileNameByPos(ds.shardId, i))
}
return nil
}
func (ds *DiskRotateStorage) Entries(lo, hi uint64, maxSize uint64) (entries []*proto.Entry, isCompact bool, e error) {
if lo >= hi {
return nil, false, Err_IndexOutOfRange
} else if lo == hi-1 {
return ds.getEntry(lo)
}
return ds.getEntries(lo, hi-1, maxSize)
}
func (ds *DiskRotateStorage) Term(index uint64) (term uint64, isCompact bool, e error) {
var em *EntryMeta
em, isCompact, e = ds.getEntryMeta(index)
if e != nil {
return
}
term = em.Term
return
}
func (ds *DiskRotateStorage) FirstIndex() (uint64, error) {
sm, e := ds.hardStateFile.getSnapshotMeta()
if e != nil {
return 0, e
}
return sm.Index, nil
}
func (ds *DiskRotateStorage) LastIndex() (uint64, error) {
return ds.getLastIndex()
}
func (ds *DiskRotateStorage) InitialState() (hs proto.HardState, e error) {
if ds.hardStateFile == nil {
return
}
hsp, e := ds.hardStateFile.getHardState()
if e != nil {
return hs, e
}
copyHardState(hsp, &hs)
return hs, nil
}
func (ds *DiskRotateStorage) StoreHardState(hs proto.HardState) (e error) {
if ds.hardStateFile == nil {
ds.hardStateFile, e = NewHardStateFile(ds.dir, ds.shardId, &hs, nil)
return e
}
return ds.hardStateFile.write(&hs, nil)
}
func (ds *DiskRotateStorage) ApplySnapshot(meta proto.SnapshotMeta) (e error) {
if ds.hardStateFile == nil {
ds.hardStateFile, e = NewHardStateFile(ds.dir, ds.shardId, nil, &meta)
return e
}
return ds.hardStateFile.write(nil, &meta)
}
func (ds *DiskRotateStorage) Clear() error { // 废
ds.entryCache.clear()
fis, e := ioutil.ReadDir(ds.dir)
if e != nil {
return e
}
if ds.curLogFile != nil {
ds.curLogFile.Close()
}
logPrefix := LogFilePrefix(ds.shardId)
for _, fi := range fis {
if !fi.IsDir() && strings.HasPrefix(fi.Name(), logPrefix) {
os.RemoveAll(ds.dir + fi.Name())
}
}
return nil
}
func (ds *DiskRotateStorage) Close() {
ds.entryCache.clear()
if ds.curLogFile != nil {
ds.curLogFile.Close()
}
return
}