forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrap_prepare.go
149 lines (131 loc) · 3.97 KB
/
wrap_prepare.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
// 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 (
"encoding/json"
"fmt"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/repl"
"github.com/chubaofs/chubaofs/storage"
"github.com/juju/errors"
"hash/crc32"
)
func (s *DataNode) Prepare(p *repl.Packet) (err error) {
defer func() {
if err != nil {
p.PackErrorBody(repl.ActionPreparePkt, err.Error())
}
}()
if p.IsMasterCommand() {
return
}
p.BeforeTp(s.clusterID)
err = s.checkStoreMode(p)
if err != nil {
return
}
if err = s.checkCrc(p); err != nil {
return
}
if err = s.checkPartition(p); err != nil {
return
}
// For certain packet, we meed to add some additional extent information.
if err = s.addExtentInfo(p); err != nil {
return
}
return
}
func (s *DataNode) checkStoreMode(p *repl.Packet) (err error) {
if p.ExtentType == proto.TinyExtentType || p.ExtentType == proto.NormalExtentType {
return nil
}
return ErrIncorrectStoreType
}
func (s *DataNode) checkCrc(p *repl.Packet) (err error) {
if !isWriteOperation(p) {
return
}
crc := crc32.ChecksumIEEE(p.Data[:p.Size])
if crc != p.CRC {
return storage.CrcMismatchError
}
return
}
func (s *DataNode) checkPartition(p *repl.Packet) (err error) {
dp := s.space.Partition(p.PartitionID)
if dp == nil {
err = errors.Errorf("partition %v is not exist", p.PartitionID)
return
}
p.Object = dp
if isWriteOperation(p) || isCreateExtentOperation(p) {
if dp.Available() <= 0 {
err = storage.NoSpaceError
return
}
}
return
}
func (s *DataNode) addExtentInfo(p *repl.Packet) error {
partition := p.Object.(*DataPartition)
store := p.Object.(*DataPartition).ExtentStore()
if isLeaderPacket(p) && p.ExtentType == proto.TinyExtentType && isWriteOperation(p) {
extentID, err := store.GetAvailableTinyExtent()
if err != nil {
return err
}
p.ExtentID = extentID
p.ExtentOffset, err = store.GetTinyExtentOffset(extentID)
if err != nil {
return err
}
} else if isLeaderPacket(p) && isCreateExtentOperation(p) {
if partition.GetExtentCount() >= storage.MaxExtentCount*3 {
return fmt.Errorf("partition %v has reached maxExtentId", p.PartitionID)
}
p.ExtentID = store.NextExtentID()
} else if isLeaderPacket(p) && isMarkDeleteExtentOperation(p) && isTinyExtentType(p) {
record := new(proto.TinyExtentDeleteRecord)
if err := json.Unmarshal(p.Data[:p.Size], record); err != nil {
return fmt.Errorf("addExtentInfo failed %v", err.Error())
}
record.TinyDeleteFileOffset = store.NextTinyDeleteFileOffset()
p.Data, _ = json.Marshal(record)
p.Size = uint32(len(p.Data))
}
return nil
}
// A leader packet is the packet send to the leader and does not require packet forwarding.
func isLeaderPacket(p *repl.Packet) (ok bool) {
if p.IsForwardPkt() && (isWriteOperation(p) || isCreateExtentOperation(p) || isMarkDeleteExtentOperation(p)) {
ok = true
}
return
}
func isWriteOperation(p *repl.Packet) bool {
return p.Opcode == proto.OpWrite || p.Opcode == proto.OpSyncWrite
}
func isCreateExtentOperation(p *repl.Packet) bool {
return p.Opcode == proto.OpCreateExtent
}
func isMarkDeleteExtentOperation(p *repl.Packet) bool {
return p.Opcode == proto.OpMarkDelete
}
func isTinyExtentType(p *repl.Packet) bool {
return p.ExtentType == proto.TinyExtentType
}
func isReadExtentOperation(p *repl.Packet) bool {
return p.Opcode == proto.OpStreamRead || p.Opcode == proto.OpExtentRepairRead || p.Opcode == proto.OpRead || p.Opcode == proto.OpReadTinyDelete
}