This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
155 lines (134 loc) · 4.28 KB
/
random.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
/*
* Copyright (C) 2017-2018 GIG Technology NV and Contributors
*
* 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 storage
import (
"errors"
"github.com/threefoldtech/0-stor/client/datastor"
"github.com/threefoldtech/0-stor/client/metastor/metatypes"
log "github.com/sirupsen/logrus"
)
// NewRandomChunkStorage creates a new RandomChunkStorage.
// See `RandomChunkStorage` for more information.
func NewRandomChunkStorage(cluster datastor.Cluster) (*RandomChunkStorage, error) {
if cluster == nil {
panic("no cluster given")
}
if cluster.ListedShardCount() < 1 {
return nil, errors.New("RandomChunkStorage: at least one listed shard is required")
}
return &RandomChunkStorage{cluster: cluster}, nil
}
// RandomChunkStorage is the most simplest Storage implementation.
// For writing it only writes to one shard, randomly chosen.
// For reading it expects just, and only, one shard, to read from.
// Repairing is not supported for this storage for obvious reasons.
type RandomChunkStorage struct {
cluster datastor.Cluster
}
// WriteChunk implements storage.ChunkStorage.WriteChunk
func (rs *RandomChunkStorage) WriteChunk(data []byte) (*ChunkConfig, error) {
var (
key []byte
err error
shard datastor.Shard
)
// go through all shards, in pseudo-random fashion,
// until the data could be written to one of them.
it := rs.cluster.GetShardIterator(nil)
for it.Next() {
shard = it.Shard()
key, err = shard.CreateObject(data)
if err == nil {
return &ChunkConfig{
Size: int64(len(data)),
Objects: []metatypes.Object{
{
Key: key,
ShardID: shard.Identifier(),
},
},
}, nil
}
// check if the error is because the namespace if full
// if it is, we don't log the error.
if err == datastor.ErrNamespaceFull {
log.WithField("shard", shard.Identifier()).Warningf("%v", err)
} else {
// if this is another error, we casually log the shard-write error,
// and continue trying with another shard...
log.WithField("shard", shard.Identifier()).WithError(err).Errorf("failed to write data to random shard")
}
}
return nil, ErrShardsUnavailable
}
// ReadChunk implements storage.ChunkStorage.ReadChunk
func (rs *RandomChunkStorage) ReadChunk(cfg ChunkConfig) ([]byte, error) {
if len(cfg.Objects) != 1 {
return nil, ErrUnexpectedObjectCount
}
obj := &cfg.Objects[0]
shard, err := rs.cluster.GetShard(obj.ShardID)
if err != nil {
return nil, err
}
object, err := shard.GetObject(obj.Key)
if err != nil {
return nil, err
}
if int64(len(object.Data)) != cfg.Size {
return object.Data, ErrInvalidDataSize
}
return object.Data, nil
}
// CheckChunk implements storage.ChunkStorage.CheckChunk
func (rs *RandomChunkStorage) CheckChunk(cfg ChunkConfig, fast bool) (CheckStatus, error) {
if len(cfg.Objects) != 1 {
return CheckStatusInvalid, ErrUnexpectedObjectCount
}
obj := &cfg.Objects[0]
shard, err := rs.cluster.GetShard(obj.ShardID)
if err != nil {
return CheckStatusInvalid, nil
}
status, err := shard.GetObjectStatus(obj.Key)
if err != nil || status != datastor.ObjectStatusOK {
return CheckStatusInvalid, nil
}
return CheckStatusOptimal, nil
}
// RepairChunk implements storage.ChunkStorage.RepairChunk
func (rs *RandomChunkStorage) RepairChunk(cfg ChunkConfig) (*ChunkConfig, error) {
return nil, ErrNotSupported
}
// DeleteChunk implements storage.ChunkStorage.DeleteChunk
func (rs *RandomChunkStorage) DeleteChunk(cfg ChunkConfig) error {
if len(cfg.Objects) != 1 {
return ErrUnexpectedObjectCount
}
obj := &cfg.Objects[0]
shard, err := rs.cluster.GetShard(obj.ShardID)
if err != nil {
return err
}
return shard.DeleteObject(obj.Key)
}
// Close implements ChunkStorage.Close
func (rs *RandomChunkStorage) Close() error {
return rs.cluster.Close()
}
var (
_ ChunkStorage = (*RandomChunkStorage)(nil)
)