cubefs

Форк
0
/
partition_op_uniq.go 
134 строки · 3.0 Кб
1
// Copyright 2018 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package metanode
16

17
import (
18
	"encoding/binary"
19
	"encoding/json"
20
	"fmt"
21
	"sync/atomic"
22

23
	"github.com/cubefs/cubefs/proto"
24
)
25

26
func (mp *metaPartition) GetUniqID(p *Packet, num uint32) (err error) {
27
	idBuf := make([]byte, 4)
28
	binary.BigEndian.PutUint32(idBuf, num)
29
	resp, err := mp.submit(opFSMUniqID, idBuf)
30
	if err != nil {
31
		p.PacketErrorWithBody(proto.OpAgain, []byte(err.Error()))
32
		return
33
	}
34

35
	var (
36
		status = proto.OpErr
37
		reply  []byte
38
	)
39

40
	idResp := resp.(*UniqIdResp)
41
	if idResp.Status == proto.OpOk {
42
		resp := &GetUniqIDResp{
43
			Start: idResp.Start,
44
		}
45
		status = proto.OpOk
46
		reply, err = json.Marshal(resp)
47
		if err != nil {
48
			status = proto.OpErr
49
			reply = []byte(err.Error())
50
		}
51
	}
52
	p.PacketErrorWithBody(status, reply)
53
	return
54
}
55

56
func (mp *metaPartition) allocateUniqID(num uint32) (start, end uint64) {
57
	for {
58
		// cur is the last allocated id
59
		cur := mp.GetUniqId()
60
		start = cur + 1
61
		end := cur + uint64(num)
62
		if atomic.CompareAndSwapUint64(&mp.config.UniqId, cur, end) {
63
			return start, end
64
		}
65
	}
66
}
67

68
func (mp *metaPartition) uniqCheckerEvict() (left int, evict int, err error) {
69
	checker := mp.uniqChecker
70
	left, idx, op := checker.evictIndex()
71
	if op == nil {
72
		return left, 0, nil
73
	}
74

75
	fsmReq := &fsmEvictUniqCheckerRequest{
76
		Idx:    idx,
77
		UniqID: op.uniqid,
78
	}
79
	reqBytes, err := json.Marshal(fsmReq)
80
	if err != nil {
81
		panic(err)
82
	}
83
	_, err = mp.submit(opFSMUniqCheckerEvict, reqBytes)
84
	return left, idx + 1, err
85
}
86

87
var (
88
	inodeOnceSize    = 16
89
	newInodeOnceSize = 24
90
)
91

92
type InodeOnce struct {
93
	UniqID uint64
94
	Inode  uint64 // Inode ID
95
	VerSeq uint64
96
}
97

98
func (i *InodeOnce) Marshal() (val []byte) {
99
	val = make([]byte, newInodeOnceSize)
100
	binary.BigEndian.PutUint64(val[0:8], i.UniqID)
101
	binary.BigEndian.PutUint64(val[8:16], i.Inode)
102
	binary.BigEndian.PutUint64(val[16:24], i.VerSeq)
103
	return val
104
}
105

106
func InodeOnceUnlinkMarshal(req *UnlinkInoReq) []byte {
107
	inoOnce := &InodeOnce{
108
		UniqID: req.UniqID,
109
		Inode:  req.Inode,
110
		VerSeq: req.VerSeq,
111
	}
112
	return inoOnce.Marshal()
113
}
114

115
func InodeOnceLinkMarshal(req *LinkInodeReq) []byte {
116
	inoOnce := &InodeOnce{
117
		UniqID: req.UniqID,
118
		Inode:  req.Inode,
119
	}
120
	return inoOnce.Marshal()
121
}
122

123
func InodeOnceUnmarshal(val []byte) (i *InodeOnce, err error) {
124
	i = &InodeOnce{}
125
	if len(val) < inodeOnceSize {
126
		return i, fmt.Errorf("size incorrect")
127
	}
128
	i.UniqID = binary.BigEndian.Uint64(val[0:8])
129
	i.Inode = binary.BigEndian.Uint64(val[8:16])
130
	if len(val) == 24 {
131
		i.VerSeq = binary.BigEndian.Uint64(val[16:24])
132
	}
133
	return
134
}
135

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.