-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
186 lines (161 loc) · 4.13 KB
/
block.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
)
// Block of data in a volume
type Block struct {
Id []byte // Unique ID
volume *Volume
shardsMux sync.RWMutex
// these must be an array to preserve the order
DataShards []*Shard
ParityShards []*Shard
}
// Init local shards
func (this *Block) initShards() {
for i := 0; i < conf.DataShardsPerBlock; i++ {
shard := newShard(this)
shard.BlockIndex = uint(i)
this.RegisterDataShard(shard)
}
for i := 0; i < conf.ParityShardsPerBlock; i++ {
shard := newShard(this)
shard.Parity = true
shard.BlockIndex = uint(i + conf.DataShardsPerBlock)
this.RegisterParityShard(shard)
}
}
// Init remote shards
func (this *Block) initRemoteShards() (bool, error) {
// Router criteria
criteria := newNodeRouterCriteria()
criteria.ExcludeLocalNodes = true
// For each data shard
for _, dataShard := range this.DataShards {
// Pick random remote node
node, nodeSelectionErr := datastore.nodeRouter.PickNode(criteria)
if nodeSelectionErr != nil {
return false, nodeSelectionErr
}
log.Infof("Routing add remote shard (%s) request to %s", dataShard.IdStr(), node)
// Send
binaryTransport._sendCreateShard(node, this.Id, dataShard.Id)
}
// Done
return true, nil
}
// To string
func (this *Block) IdStr() string {
return uuidToString(this.Id)
}
// Volume
func (this *Block) Volume() *Volume {
return this.volume
}
// Persist block to disk
func (this *Block) Persist() bool {
log.Infof("Persisting block %s to disk", this.IdStr())
// Prepare folder
this.PrepareFolder()
// Shards to disk
for _, shard := range this.DataShards {
shard.Persist()
}
for _, shard := range this.ParityShards {
shard.Persist()
}
return true
}
// Prepare folder
func (this *Block) PrepareFolder() {
// @todo cache only once
// Prepare folder
if _, err := os.Stat(this.FullPath()); os.IsNotExist(err) {
// Create
log.Infof("Creating folder for block %s in %s", this.IdStr(), this.FullPath())
e := os.MkdirAll(this.FullPath(), conf.UnixFolderPermissions)
if e != nil {
log.Errorf("Failed to create %s: %s", this.FullPath(), e)
}
}
}
// Recover shards
func (this *Block) recoverShards() {
// Read files
list, e := ioutil.ReadDir(this.FullPath())
if e != nil {
log.Errorf("Failed to list shards in %s: %s", this.FullPath(), e)
return
}
// Iterate
log.Infof("Found %d entries in block directory", len(list))
for _, elm := range list {
split := strings.Split(elm.Name(), "_")
// Must be in format s_UUID
if len(split) != 2 || split[0] != "s" {
log.Warnf("Ignoring invalid shard %s", elm)
continue
}
// Shard
nameSplit := strings.Split(split[1], ".")
shard := newShardFromId(this, uuidStringToBytes(nameSplit[0]))
// Add to list
if strings.Contains(elm.Name(), ".parity") {
// Parity
shard.Parity = true
this.RegisterParityShard(shard)
} else {
// Data
this.RegisterDataShard(shard)
}
}
}
// Register shards
func (this *Block) RegisterDataShard(s *Shard) {
if s.Parity == true {
panic("Not a data shard")
}
this.shardsMux.Lock()
if len(this.DataShards) > conf.DataShardsPerBlock {
panic("Block full, can not register data shard")
}
log.Infof("Registered data shard %s with block %s", s.IdStr(), this.IdStr())
this.DataShards = append(this.DataShards, s)
this.shardsMux.Unlock()
}
// Register shards
func (this *Block) RegisterParityShard(s *Shard) {
if s.Parity == false {
panic("Not a parity shard")
}
this.shardsMux.Lock()
if len(this.ParityShards) > conf.ParityShardsPerBlock {
panic("Block full, can not register data shard")
}
log.Infof("Registered parity shard %s with block %s", s.IdStr(), this.IdStr())
this.ParityShards = append(this.ParityShards, s)
this.shardsMux.Unlock()
}
// Full path
func (this *Block) FullPath() string {
return fmt.Sprintf("%s/b_%s", this.Volume().FullPath(), this.IdStr())
}
// New block
func newBlock(v *Volume) *Block {
id := randomUuid()
return newBlockFromId(v, id)
}
// New block from ID
func newBlockFromId(v *Volume, id []byte) *Block {
b := &Block{
volume: v,
Id: id,
DataShards: make([]*Shard, 0),
ParityShards: make([]*Shard, 0),
}
return b
}