forked from juicedata/juicefs
/
object_storage.go
279 lines (238 loc) · 6.46 KB
/
object_storage.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* JuiceFS, Copyright 2018 leonatone, Inc.
*
* 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 object
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/leonatone/juicefs/pkg/utils"
)
var ctx = context.Background()
var logger = utils.GetLogger("juicefs")
var UserAgent = "JuiceFS"
type MtimeChanger interface {
Chtimes(path string, mtime time.Time) error
}
type SupportSymlink interface {
// Symlink create a symbolic link
Symlink(oldName, newName string) error
// Readlink read a symbolic link
Readlink(name string) (string, error)
}
type File interface {
Object
Owner() string
Group() string
Mode() os.FileMode
}
type onlyWriter struct {
io.Writer
}
type file struct {
obj
owner string
group string
mode os.FileMode
isSymlink bool
}
func (f *file) Owner() string { return f.owner }
func (f *file) Group() string { return f.group }
func (f *file) Mode() os.FileMode { return f.mode }
func (f *file) IsSymlink() bool { return f.isSymlink }
func MarshalObject(o Object) map[string]interface{} {
m := make(map[string]interface{})
m["key"] = o.Key()
m["size"] = o.Size()
m["mtime"] = o.Mtime().UnixNano()
m["isdir"] = o.IsDir()
if f, ok := o.(File); ok {
m["mode"] = f.Mode()
m["owner"] = f.Owner()
m["group"] = f.Group()
m["isSymlink"] = f.IsSymlink()
}
return m
}
func UnmarshalObject(m map[string]interface{}) Object {
mtime := time.Unix(0, int64(m["mtime"].(float64)))
o := obj{
key: m["key"].(string),
size: int64(m["size"].(float64)),
mtime: mtime,
isDir: m["isdir"].(bool)}
if _, ok := m["mode"]; ok {
f := file{o, m["owner"].(string), m["group"].(string), os.FileMode(m["mode"].(float64)), m["isSymlink"].(bool)}
return &f
}
return &o
}
type FileSystem interface {
MtimeChanger
Chmod(path string, mode os.FileMode) error
Chown(path string, owner, group string) error
}
var notSupported = utils.ENOTSUP
type DefaultObjectStorage struct{}
func (s DefaultObjectStorage) Create() error {
return nil
}
func (s DefaultObjectStorage) Limits() Limits {
return Limits{IsSupportMultipartUpload: false, IsSupportUploadPartCopy: false}
}
func (s DefaultObjectStorage) Head(key string) (Object, error) {
return nil, notSupported
}
func (s DefaultObjectStorage) Copy(dst, src string) error {
return notSupported
}
func (s DefaultObjectStorage) CreateMultipartUpload(key string) (*MultipartUpload, error) {
return nil, notSupported
}
func (s DefaultObjectStorage) UploadPart(key string, uploadID string, num int, body []byte) (*Part, error) {
return nil, notSupported
}
func (s DefaultObjectStorage) UploadPartCopy(key string, uploadID string, num int, srcKey string, off, size int64) (*Part, error) {
return nil, notSupported
}
func (s DefaultObjectStorage) AbortUpload(key string, uploadID string) {}
func (s DefaultObjectStorage) CompleteUpload(key string, uploadID string, parts []*Part) error {
return notSupported
}
func (s DefaultObjectStorage) ListUploads(marker string) ([]*PendingPart, string, error) {
return nil, "", nil
}
func (s DefaultObjectStorage) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return nil, notSupported
}
func (s DefaultObjectStorage) ListAll(prefix, marker string, followLink bool) (<-chan Object, error) {
return nil, notSupported
}
type Creator func(bucket, accessKey, secretKey, token string) (ObjectStorage, error)
var storages = make(map[string]Creator)
func Register(name string, register Creator) {
storages[name] = register
}
func CreateStorage(name, endpoint, accessKey, secretKey, token string) (ObjectStorage, error) {
f, ok := storages[name]
if ok {
logger.Debugf("Creating %s storage at endpoint %s", name, endpoint)
return f(endpoint, accessKey, secretKey, token)
}
return nil, fmt.Errorf("invalid storage: %s", name)
}
var bufPool = sync.Pool{
New: func() interface{} {
// Default io.Copy uses 32KB buffer, here we choose a larger one (1MiB io-size increases throughput by ~20%)
buf := make([]byte, 1<<20)
return &buf
},
}
type listThread struct {
sync.Mutex
cond *utils.Cond
ready bool
err error
entries []Object
}
func ListAllWithDelimiter(store ObjectStorage, prefix, start, end string, followLink bool) (<-chan Object, error) {
entries, err := store.List(prefix, "", "/", 1e9, followLink)
if err != nil {
logger.Errorf("list %s: %s", prefix, err)
return nil, err
}
listed := make(chan Object, 10240)
var walk func(string, []Object) error
walk = func(prefix string, entries []Object) error {
var concurrent = 10
var err error
threads := make([]listThread, concurrent)
for c := 0; c < concurrent; c++ {
t := &threads[c]
t.cond = utils.NewCond(t)
go func(c int) {
for i := c; i < len(entries); i += concurrent {
key := entries[i].Key()
if end != "" && key >= end {
break
}
if key < start && !strings.HasPrefix(start, key) {
continue
}
if !entries[i].IsDir() || key == prefix {
continue
}
t.entries, t.err = store.List(key, "\x00", "/", 1e9, followLink) // exclude itself
t.Lock()
t.ready = true
t.cond.Signal()
for t.ready {
t.cond.WaitWithTimeout(time.Second)
if err != nil {
t.Unlock()
return
}
}
t.Unlock()
}
}(c)
}
for i, e := range entries {
key := e.Key()
if end != "" && key >= end {
return nil
}
if key >= start {
listed <- e
} else if !strings.HasPrefix(start, key) {
continue
}
if !e.IsDir() || key == prefix {
continue
}
t := &threads[i%concurrent]
t.Lock()
for !t.ready {
t.cond.WaitWithTimeout(time.Millisecond * 10)
}
if t.err != nil {
err = t.err
t.Unlock()
return err
}
t.ready = false
t.cond.Signal()
children := t.entries
t.Unlock()
err = walk(key, children)
if err != nil {
return err
}
}
return nil
}
go func() {
defer close(listed)
err := walk(prefix, entries)
if err != nil {
listed <- nil
}
}()
return listed, nil
}