-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.go
157 lines (128 loc) · 3.37 KB
/
datastore.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
package main
import (
"errors"
)
// Data store
var datastore *Datastore
type Datastore struct {
fileLocator *FileLocator
nodeRouter *NodeRouter
fileSplitter *BinaryTransportFileSplitter
}
// Prepare data store for interactions
func (this *Datastore) prepare() {
// Load volumes
for _, volume := range this.Volumes() {
volume.prepare()
}
}
// Get volumes
func (this *Datastore) Volumes() []*Volume {
return conf.Datastore.Volumes
}
// Locate file
func (this *Datastore) LocateFile(fullName string) ([]*ShardIndex, uint32, error) {
return this.fileLocator._locate(this, fullName)
}
// Add file
func (this *Datastore) AddFile(fullName string, data []byte) (bool, error) {
// Validate max file size
if len(data) > conf.MaxFileSize {
return false, errors.New("Exceeds maximum file size")
}
// Select node on where to execute this (it will be written there locally to a shard with space)
node, nodeSelectionErr := this.nodeRouter.PickNode(nil)
if nodeSelectionErr != nil {
return false, nodeSelectionErr
}
log.Infof("Routing add file request to %s", node)
// Create meta
fileMeta := newFileMeta(fullName)
fileMeta.UpdateFromData(data)
// Split file into message chunks (no target shard)
msgs := this.fileSplitter.Split(fileMeta, data, nil)
// Send data to node
for _, msg := range msgs {
// log.Infof("send chunk %d %v", chunkNumber, msg.Data[12:])
binaryTransport._sendFileChunk(node, msg)
}
// @todo Write to other replicate nodes
// @todo Write shard index change (effectively add file to bloom filter) to all nodes (UDP)
return true, nil
}
// Find block
func (this *Datastore) BlockByIdStr(id string) *Block {
for _, volume := range this.Volumes() {
for _, block := range volume.Blocks() {
if id == block.IdStr() {
return block
}
}
}
return nil
}
// Find shard
func (this *Datastore) LocalShardByIdStr(id string) *Shard {
for _, volume := range this.Volumes() {
for _, shard := range volume.Shards() {
if shard.IdStr() == id {
return shard
}
}
}
return nil
}
// Find writable shard
func (this *Datastore) AllocateShardCapacity(fileMeta *FileMeta) *Shard {
for _, volume := range this.Volumes() {
for _, shard := range volume.Shards() {
// Ignore parity shards
if shard.Parity {
continue
}
// Allocate capacity
if shard.AllocateCapacity(fileMeta.Size) {
return shard
}
}
}
// No capacity found, let's create some
this.NewBlock()
return this.AllocateShardCapacity(fileMeta)
}
// Get volume
func (this *Datastore) GetVolume() *Volume {
volumes := this.Volumes()
// @todo Improve volume allocator (e.g. least full / fastest)
volume := volumes[len(volumes)-1]
return volume
}
// New block
func (this *Datastore) NewBlock() *Block {
log.Info("Allocating new block")
// Volume
volume := this.GetVolume()
// Create new block
b := newBlock(volume)
// Init shards
b.initShards()
// Register block (takes care of shard registration internally)
volume.RegisterBlock(b)
// Replicate shard to other host
_, remoteErr := b.initRemoteShards()
if remoteErr != nil {
log.Errorf("Failed to initialize remote shards: %s", remoteErr)
}
// Persist to disk
b.Persist()
return b
}
func newDatastore() *Datastore {
d := &Datastore{
fileLocator: newFileLocator(),
nodeRouter: newNodeRouter(),
fileSplitter: newBinaryTransportFileSplitter(uint32(conf.BinaryTransportWriteBuffer)),
}
d.prepare()
return d
}