forked from cubefs/cubefs
/
partition_op_by_raft.go
271 lines (243 loc) · 7.56 KB
/
partition_op_by_raft.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2018 The Chubao 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 datanode
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/repl"
"github.com/chubaofs/chubaofs/storage"
"github.com/chubaofs/chubaofs/util/exporter"
"github.com/chubaofs/chubaofs/util/log"
"github.com/tiglabs/raft"
"net"
"strings"
"syscall"
)
type RaftCmdItem struct {
Op uint32 `json:"op"`
K []byte `json:"k"`
V []byte `json:"v"`
}
type rndWrtOpItem struct {
opcode uint8
extentID uint64
offset int64
size int64
data []byte
crc uint32
}
// Marshal random write value to binary data.
// Binary frame structure:
// +------+----+------+------+------+------+------+
// | Item | extentID | offset | size | crc | data |
// +------+----+------+------+------+------+------+
// | byte | 8 | 8 | 8 | 4 | size |
// +------+----+------+------+------+------+------+
const (
BinaryMarshalMagicVersion = 0xFF
)
func MarshalRandWriteRaftLog(opcode uint8, extentID uint64, offset, size int64, data []byte, crc uint32) (result []byte, err error) {
buff := bytes.NewBuffer(make([]byte, 0))
buff.Grow(8 + 8*2 + 4 + int(size) + 4 + 4)
if err = binary.Write(buff, binary.BigEndian, uint32(BinaryMarshalMagicVersion)); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, opcode); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, extentID); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, offset); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, size); err != nil {
return
}
if err = binary.Write(buff, binary.BigEndian, crc); err != nil {
return
}
if _, err = buff.Write(data); err != nil {
return
}
result = buff.Bytes()
return
}
// RandomWriteSubmit submits the proposal to raft.
func UnmarshalRandWriteRaftLog(raw []byte) (opItem *rndWrtOpItem, err error) {
opItem = new(rndWrtOpItem)
buff := bytes.NewBuffer(raw)
var version uint32
if err = binary.Read(buff, binary.BigEndian, &version); err != nil {
return
}
if version != BinaryMarshalMagicVersion {
opItem, err = UnmarshalOldVersionRaftLog(raw)
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.opcode); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.extentID); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.offset); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.size); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.crc); err != nil {
return
}
opItem.data = make([]byte, opItem.size)
if _, err = buff.Read(opItem.data); err != nil {
return
}
return
}
func UnmarshalOldVersionRaftLog(raw []byte) (opItem *rndWrtOpItem, err error) {
raftOpItem := new(RaftCmdItem)
defer func() {
log.LogDebugf("Unmarsh use oldVersion,result %v", err)
}()
if err = json.Unmarshal(raw, raftOpItem); err != nil {
return
}
opItem, err = UnmarshalOldVersionRandWriteOpItem(raftOpItem.V)
if err != nil {
return
}
opItem.opcode = uint8(raftOpItem.Op)
return
}
func UnmarshalOldVersionRandWriteOpItem(raw []byte) (result *rndWrtOpItem, err error) {
var opItem rndWrtOpItem
buff := bytes.NewBuffer(raw)
if err = binary.Read(buff, binary.BigEndian, &opItem.extentID); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.offset); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.size); err != nil {
return
}
if err = binary.Read(buff, binary.BigEndian, &opItem.crc); err != nil {
return
}
opItem.data = make([]byte, opItem.size)
if _, err = buff.Read(opItem.data); err != nil {
return
}
result = &opItem
return
}
func (dp *DataPartition) checkWriteErrs(errMsg string) (ignore bool) {
// file has been deleted when applying the raft log
if strings.Contains(errMsg, storage.ExtentHasBeenDeletedError.Error()) || strings.Contains(errMsg, storage.ExtentNotFoundError.Error()) {
return true
}
return false
}
// CheckLeader checks if itself is the leader during read
func (dp *DataPartition) CheckLeader(request *repl.Packet, connect net.Conn) (err error) {
// and use another getRaftLeaderAddr() to return the actual address
_, ok := dp.IsRaftLeader()
if !ok {
err = raft.ErrNotLeader
logContent := fmt.Sprintf("action[ReadCheck] %v.", request.LogMessage(request.GetOpMsg(), connect.RemoteAddr().String(), request.StartT, err))
log.LogWarnf(logContent)
return
}
return
}
type ItemIterator struct {
applyID uint64
}
// NewItemIterator creates a new item iterator.
func NewItemIterator(applyID uint64) *ItemIterator {
si := new(ItemIterator)
si.applyID = applyID
return si
}
// ApplyIndex returns the appliedID
func (si *ItemIterator) ApplyIndex() uint64 {
return si.applyID
}
// Close Closes the iterator.
func (si *ItemIterator) Close() {
return
}
// Next returns the next item in the iterator.
func (si *ItemIterator) Next() (data []byte, err error) {
appIDBuf := make([]byte, 8)
binary.BigEndian.PutUint64(appIDBuf, si.applyID)
data = appIDBuf[:]
return
}
// ApplyRandomWrite random write apply
func (dp *DataPartition) ApplyRandomWrite(command []byte, raftApplyID uint64) (resp interface{}, err error) {
opItem := &rndWrtOpItem{}
defer func() {
if err == nil {
resp = proto.OpOk
dp.uploadApplyID(raftApplyID)
} else {
err = fmt.Errorf("[ApplyRandomWrite] ApplyID(%v) Partition(%v)_Extent(%v)_ExtentOffset(%v)_Size(%v) apply err(%v) retry[20]", raftApplyID, dp.partitionID, opItem.extentID, opItem.offset, opItem.size, err)
exporter.Warning(err.Error())
resp = proto.OpDiskErr
}
}()
if dp.IsRejectWrite() {
err = fmt.Errorf("partition(%v) disk(%v) err(%v)", dp.partitionID, dp.Disk().Path, syscall.ENOSPC)
return
}
if opItem, err = UnmarshalRandWriteRaftLog(command); err != nil {
log.LogErrorf("[ApplyRandomWrite] ApplyID(%v) Partition(%v) unmarshal failed(%v)", raftApplyID, dp.partitionID, err)
return
}
log.LogDebugf("[ApplyRandomWrite] ApplyID(%v) Partition(%v)_Extent(%v)_ExtentOffset(%v)_Size(%v)",
raftApplyID, dp.partitionID, opItem.extentID, opItem.offset, opItem.size)
for i := 0; i < 20; i++ {
err = dp.ExtentStore().Write(opItem.extentID, opItem.offset, opItem.size, opItem.data, opItem.crc, storage.RandomWriteType, opItem.opcode == proto.OpSyncRandomWrite)
if dp.checkIsDiskError(err) {
return
}
if err == nil {
break
}
log.LogErrorf("[ApplyRandomWrite] ApplyID(%v) Partition(%v)_Extent(%v)_ExtentOffset(%v)_Size(%v) apply err(%v) retry(%v)", raftApplyID, dp.partitionID, opItem.extentID, opItem.offset, opItem.size, err, i)
}
return
}
// RandomWriteSubmit submits the proposal to raft.
func (dp *DataPartition) RandomWriteSubmit(pkg *repl.Packet) (err error) {
val, err := MarshalRandWriteRaftLog(pkg.Opcode, pkg.ExtentID, pkg.ExtentOffset, int64(pkg.Size), pkg.Data, pkg.CRC)
if err != nil {
return
}
var (
resp interface{}
)
if resp, err = dp.Put(nil, val); err != nil {
return
}
pkg.ResultCode = resp.(uint8)
log.LogDebugf("[RandomWrite] SubmitRaft: %v", pkg.GetUniqueLogId())
return
}