cubefs

Форк
0
/
datapartition.go 
82 строки · 2.3 Кб
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
	"strings"
19
	"sync"
20

21
	"github.com/cubefs/cubefs/proto"
22
	"github.com/cubefs/cubefs/util/log"
23
)
24

25
// DataPartition defines the struct of data partition that will be used on the meta node.
26
type DataPartition struct {
27
	PartitionID   uint64
28
	Status        int8
29
	ReplicaNum    uint8
30
	PartitionType string
31
	Hosts         []string
32
	IsDiscard     bool
33
}
34

35
// GetAllAddrs returns all addresses of the data partition.
36
func (dp *DataPartition) GetAllAddrs() (m string) {
37
	return strings.Join(dp.Hosts[1:], proto.AddrSplit) + proto.AddrSplit
38
}
39

40
// DataPartitionsView defines the view of the data node.
41
type DataPartitionsView struct {
42
	DataPartitions []*DataPartition
43
}
44

45
func NewDataPartitionsView() *DataPartitionsView {
46
	return &DataPartitionsView{}
47
}
48

49
// Vol defines the view of the data partition with the read/write lock.
50
type Vol struct {
51
	sync.RWMutex
52
	dataPartitionView map[uint64]*DataPartition
53
	volDeleteLockTime int64
54
}
55

56
// NewVol returns a new volume instance.
57
func NewVol() *Vol {
58
	return &Vol{
59
		dataPartitionView: make(map[uint64]*DataPartition),
60
	}
61
}
62

63
// GetPartition returns the data partition based on the given partition ID.
64
func (v *Vol) GetPartition(partitionID uint64) *DataPartition {
65
	v.RLock()
66
	defer v.RUnlock()
67
	return v.dataPartitionView[partitionID]
68
}
69

70
// UpdatePartitions updates the data partition.
71
func (v *Vol) UpdatePartitions(partitions *DataPartitionsView) {
72
	for _, dp := range partitions.DataPartitions {
73
		log.LogDebugf("action[UpdatePartitions] dp (id:%v,status:%v)", dp.PartitionID, dp.Status)
74
		v.replaceOrInsert(dp)
75
	}
76
}
77

78
func (v *Vol) replaceOrInsert(partition *DataPartition) {
79
	v.Lock()
80
	defer v.Unlock()
81
	v.dataPartitionView[partition.PartitionID] = partition
82
}
83

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

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

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

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