-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint_receiver.go
184 lines (151 loc) · 4.7 KB
/
checkpoint_receiver.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
package gopaxos
import (
"fmt"
"github.com/buptmiao/gopaxos/paxospb"
"io/ioutil"
"os"
"strings"
)
type checkpointReceiver struct {
conf *config
logStorage LogStorage
senderNodeID uint64
uuid uint64
sequence uint64
hasInitDirMap map[string]bool
}
func newCheckpointReceiver(conf *config, ls LogStorage) *checkpointReceiver {
return &checkpointReceiver{
conf: conf,
logStorage: ls,
hasInitDirMap: make(map[string]bool),
}
}
func (c *checkpointReceiver) reset() {
c.hasInitDirMap = make(map[string]bool)
c.senderNodeID = nullNode
c.uuid = 0
c.sequence = 0
}
func (c *checkpointReceiver) newReceiver(senderNodeID uint64, uuid uint64) error {
if err := c.clearCheckpointTmp(); err != nil {
return err
}
if err := c.logStorage.ClearAllLog(c.conf.groupIdx); err != nil {
lPLGErr(c.conf.groupIdx, "ClearAllLog fail, groupidx %d, error: %v",
c.conf.getMyGroupIdx(), err)
return err
}
c.hasInitDirMap = make(map[string]bool)
c.senderNodeID = senderNodeID
c.uuid = uuid
c.sequence = 0
return nil
}
func (c *checkpointReceiver) clearCheckpointTmp() error {
logPath := c.logStorage.GetLogStorageDirPath(c.conf.groupIdx)
files, err := ioutil.ReadDir(logPath)
if err != nil {
lPLGErr(c.conf.groupIdx, "read dir failed, error: %v", err)
return err
}
for _, f := range files {
if strings.Contains(f.Name(), "cp_tmp_") {
childpath := fmt.Sprintf("%s/%s", logPath, f.Name())
err = deleteDir(childpath)
if err != nil {
break
}
lPLGHead(c.conf.groupIdx, "rm dir %s done!", childpath)
}
}
return err
}
func (c *checkpointReceiver) isReceiverFinish(senderNodeID uint64, uuid uint64, endSequence uint64) bool {
if senderNodeID == c.senderNodeID && uuid == c.uuid && endSequence == c.sequence+1 {
return true
}
return false
}
func (c *checkpointReceiver) getTmpDirPath(smID int64) string {
logPath := c.logStorage.GetLogStorageDirPath(c.conf.groupIdx)
return fmt.Sprintf("%s/cp_tmp_%d", logPath, smID)
}
func (c *checkpointReceiver) initFilePath(filePath string) (string, error) {
lPLGHead(c.conf.groupIdx, "START filepath %s", filePath)
newPath := "/" + filePath + "/"
tmpList := strings.Split(newPath, "/")
dirList := make([]string, 0, len(tmpList))
for _, v := range tmpList {
if v != "" {
dirList = append(dirList, v)
}
}
formatFilePath := "/"
for i, v := range dirList {
if i+1 == len(dirList) {
formatFilePath += v
} else {
formatFilePath += v + "/"
if _, ok := c.hasInitDirMap[formatFilePath]; !ok {
err := c.createDir(formatFilePath)
if err != nil {
return "", err
}
c.hasInitDirMap[formatFilePath] = true
}
}
}
lPLGImp(c.conf.groupIdx, "ok, format filepath %s", formatFilePath)
return formatFilePath, nil
}
func (c *checkpointReceiver) createDir(dirPath string) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
if err = os.Mkdir(dirPath, 0775); err != nil {
lPLGErr(c.conf.groupIdx, "Create dir fail, path %s", dirPath)
return err
}
}
return nil
}
func (c *checkpointReceiver) receiveCheckpoint(checkpointMsg *paxospb.CheckpointMsg) error {
if checkpointMsg.GetNodeID() != c.senderNodeID || checkpointMsg.GetUUID() != c.uuid {
lPLGErr(c.conf.groupIdx, "msg not valid, Msg.SenderNodeID %d Receiver.SenderNodeID %d Msg.UUID %lu Receiver.UUID %d",
checkpointMsg.GetNodeID(), c.senderNodeID, checkpointMsg.GetUUID(), c.uuid)
return errMsgNotValid
}
if checkpointMsg.GetSequence() == c.sequence {
lPLGErr(c.conf.groupIdx, "msg already receive, skip, Msg.Sequence %d Receiver.Sequence %d",
checkpointMsg.GetSequence(), c.sequence)
return nil
}
if checkpointMsg.GetSequence() != c.sequence+1 {
lPLGErr(c.conf.groupIdx, "msg sequence wrong, Msg.Sequence %d Receiver.Sequence %d",
checkpointMsg.GetSequence(), c.sequence)
return errMsgSequenceWrong
}
filePath := c.getTmpDirPath(checkpointMsg.GetSMID()) + "/" + checkpointMsg.GetFilePath()
formatFilePath, err := c.initFilePath(filePath)
if err != nil {
return err
}
fd, err := os.OpenFile(formatFilePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
if err != nil {
lPLGErr(c.conf.groupIdx, "open file fail, filepath %s", formatFilePath)
return err
}
defer fd.Close()
fileOffset, err := fd.Seek(0, os.SEEK_END)
if fileOffset != int64(checkpointMsg.GetOffset()) {
lPLGErr(c.conf.groupIdx, "file.offset %d not equal to msg.offset %d, error: %v", fileOffset, checkpointMsg.GetOffset(), err)
return errFileOffsetMismatch
}
writeLen, err := fd.Write(checkpointMsg.GetBuffer())
if err != nil {
lPLGImp(c.conf.groupIdx, "write fail, writelen %d buffer size %d", writeLen, len(checkpointMsg.GetBuffer()))
return err
}
c.sequence++
lPLGImp(c.conf.groupIdx, "END ok, writelen %d", writeLen)
return nil
}