forked from cubefs/cubefs
/
wrap_prepare.go
128 lines (117 loc) · 3.48 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
// 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"
"hash/crc32"
)
func (s *DataNode) Prepare(p *repl.Packet) (err error) {
defer func() {
p.SetPacketHasPrepare()
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 !p.IsWriteOperation() {
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 = proto.ErrDataPartitionNotExists
return
}
p.Object = dp
if p.IsWriteOperation() || p.IsCreateExtentOperation() {
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()
var (
extentID uint64
err error
)
if p.IsLeaderPacket() && p.IsTinyExtentType() && p.IsWriteOperation() {
extentID, err = store.GetAvailableTinyExtent()
if err != nil {
return fmt.Errorf("addExtentInfo partition %v GetAvailableTinyExtent error %v", p.PartitionID, err.Error())
}
p.ExtentID = extentID
p.ExtentOffset, err = store.GetTinyExtentOffset(extentID)
if err != nil {
return fmt.Errorf("addExtentInfo partition %v %v GetTinyExtentOffset error %v", p.PartitionID, extentID, err.Error())
}
} else if p.IsLeaderPacket() && p.IsCreateExtentOperation() {
if partition.GetExtentCount() >= storage.MaxExtentCount*3 {
return fmt.Errorf("addExtentInfo partition %v has reached maxExtentId", p.PartitionID)
}
p.ExtentID, err = store.NextExtentID()
if err != nil {
return fmt.Errorf("addExtentInfo partition %v alloc NextExtentId error %v", p.PartitionID, err)
}
} else if p.IsLeaderPacket() && p.IsMarkDeleteExtentOperation() && p.IsTinyExtentType() {
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))
}
p.OrgBuffer = p.Data
return nil
}