-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhoarder.go
352 lines (276 loc) · 7.38 KB
/
hoarder.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright 2018 Sourabh Bollapragada
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 ifs
import (
"bazil.org/fuse"
"github.com/orcaman/concurrent-map"
"go.uber.org/zap"
"io/ioutil"
"os"
"path"
"strconv"
"sync"
"sync/atomic"
)
type CacheRequest interface {
}
// Use Packet
// FetchFile is RemotePath
// Read From Cache is ReadInfo
// Write To Cache is WriteInfo
// SetAttr To Cache is AttrInfo
// Delete is RemotePath
type hoarder struct {
Path string
Size uint64
cached cmap.ConcurrentMap
//fetching cmap.ConcurrentMap
fetching MutexMap
opened cmap.ConcurrentMap
fetchQueue chan interface{}
fileId uint64
}
var (
hoarderInstance *hoarder
hoarderOnce sync.Once
)
func Hoarder() *hoarder {
hoarderOnce.Do(func() {
hoarderInstance = &hoarder{
fileId: 1,
cached: cmap.New(),
fetching: *NewMutexMap(),
opened: cmap.New(),
fetchQueue: make(chan interface{}, ChannelLength),
}
})
return hoarderInstance
}
func (h *hoarder) Startup(path string, size uint64) {
h.Path = path
h.Size = size
h.DeleteCache()
//go h.processFetchRequests()
}
func (h *hoarder) DeleteCache() {
zap.L().Info("Deleting Cache")
os.RemoveAll(h.Path)
os.MkdirAll(h.Path, 0755)
}
func (h *hoarder) CacheRename(remotePath *RemotePath, destPath string) error {
if val, ok := h.cached.Get(remotePath.String()); ok {
fname := val.(string)
newRemotePath := &RemotePath{
Hostname: remotePath.Hostname,
Port: remotePath.Port,
Path: destPath,
}
h.cached.Set(newRemotePath.String(), fname)
h.cached.Remove(remotePath.String())
return nil
}
return os.ErrInvalid
}
func (h *hoarder) IsCached(rp *RemotePath) bool {
_, ok := h.cached.Get(rp.String())
return ok
}
func (h *hoarder) openCacheFile(fname string, fileDescriptor uint64, flags fuse.OpenFlags) error {
f, err := os.OpenFile(path.Join(h.Path, fname), int(flags), 0666)
if err != nil {
return err
}
h.opened.Set(strconv.FormatUint(fileDescriptor, 10), f)
return nil
}
func (h *hoarder) CacheOpen(remotePath *RemotePath, fileDescriptor uint64, flags fuse.OpenFlags) {
if val, ok := h.cached.Get(remotePath.String()); ok {
h.openCacheFile(val.(string), fileDescriptor, flags)
} else {
fetchInfo := &FetchInfo{
RemotePath: remotePath,
FileDescriptor: fileDescriptor,
Flags: flags,
}
h.cacheAndOpen(fetchInfo)
}
}
func (h *hoarder) cacheAndOpen(info *FetchInfo) error {
zap.L().Debug("Cache File",
zap.String("remotePath", info.RemotePath.String()),
)
h.fetching.Lock(info.RemotePath.String())
defer h.fetching.Unlock(info.RemotePath.String())
if !h.IsCached(info.RemotePath) {
err := h.cacheFile(info.RemotePath)
if err != nil {
return err
}
}
val, _ := h.cached.Get(info.RemotePath.String())
return h.openCacheFile(val.(string), info.FileDescriptor, info.Flags)
}
func (h *hoarder) CacheFetch(remotePath *RemotePath) {
zap.L().Debug("Fetch File",
zap.String("remotePath", remotePath.String()),
)
h.fetching.Lock(remotePath.String())
defer h.fetching.Unlock(remotePath.String())
if h.IsCached(remotePath) {
go h.cacheFile(remotePath)
}
}
//func (h *hoarder) processFetchRequests() {
// for info := range h.fetchQueue {
//
// switch val := info.(type) {
// case *FetchInfo:
//
// fetchInfo := val
//
// rp := fetchInfo.RemotePath
//
// _, cachedOk := h.cached.Get(rp.String())
// _, fetchingOk := h.fetching.Get(rp.String())
//
// if !cachedOk && !fetchingOk {
// go func() {
// err := h.cacheFile(rp)
//
// if err == nil {
// val, _ := h.cached.Get(rp.String())
// h.openCacheFile(val.(string), fetchInfo.FileDescriptor, fetchInfo.Flags)
// }
//
// }()
// }
//
// case *RemotePath:
//
// rp := val
//
// _, cachedOk := h.cached.Get(rp.String())
// _, fetchingOk := h.fetching.Get(rp.String())
//
// if cachedOk && !fetchingOk {
// go h.cacheFile(rp)
// }
// }
// }
//}
func (h *hoarder) cacheFile(remotePath *RemotePath) error {
// TODO Check Cache Space
// TODO Implement some form of cache management
resp := Talker().sendRequest(FetchFileRequest, remotePath.Hostname, remotePath)
// TODO Log Error
if err, ok := resp.Data.(Error); ok {
return err.Err
}
fname := h.GetCacheFileName()
fileChunk := resp.Data.(*FileChunk)
//fileChunk.Decompress()
err := ioutil.WriteFile(path.Join(h.Path, fname), fileChunk.Chunk,
0666)
if err == nil {
val, ok := h.cached.Get(remotePath.String())
h.cached.Set(remotePath.String(), fname)
if ok {
oldFname := val.(string)
os.Remove(path.Join(h.Path, oldFname))
}
}
return err
}
func (h *hoarder) SendWrite(hostname string, writeInfo *WriteInfo) error {
// TODO Log the error if any ?
Talker().sendRequest(WriteFileRequest, hostname, writeInfo)
return nil
}
func (h *hoarder) CacheTrunc(remotePath *RemotePath, truncInfo *AttrInfo) error {
if fname, ok := h.cached.Get(remotePath.String()); ok {
err := os.Truncate(path.Join(h.Path, fname.(string)), int64(truncInfo.Size))
return err
}
return os.ErrNotExist
}
func (h *hoarder) CacheCreate(remotePath *RemotePath, fd uint64) error {
if !h.IsCached(remotePath) {
fname := h.GetCacheFileName()
f, err := os.Create(path.Join(h.Path, fname))
// if error doesnt happens this will be nil right ?
if err == nil {
h.cached.Set(remotePath.String(), fname)
h.opened.Set(strconv.FormatUint(fd, 10), f)
}
return err
}
return os.ErrExist
}
func (h *hoarder) CacheDelete(remotePath *RemotePath) error {
if val, ok := h.cached.Get(remotePath.String()); ok {
fname := val.(string)
err := os.Remove(path.Join(h.Path, fname))
if err == nil {
h.cached.Remove(remotePath.String())
}
return err
}
return os.ErrInvalid
}
//func (h *Hoarder) ReadAllCache(remotePath *RemotePath) ([]byte, error) {
// if fname, ok := h.cached[remotePath.String()]; ok {
// data, err := ioutil.ReadFile(path.Join(h.Path, fname))
// return data, err
// }
//
// return nil, os.ErrNotExist
//}
func (h *hoarder) ReadCache(fd uint64, offset int64, size int) ([]byte, error) {
if val, ok := h.opened.Get(strconv.FormatUint(fd, 10)); ok {
f := val.(*os.File)
b := make([]byte, size)
_, err := f.ReadAt(b, offset)
if err == nil {
return b, nil
}
return nil, err
}
return nil, os.ErrInvalid
}
func (h *hoarder) GetCacheFileName() string {
fileId := atomic.AddUint64(&h.fileId, 1)
return strconv.FormatUint(fileId, 10)
}
func (h *hoarder) WriteCache(fd uint64, offset int64, data []byte) (int, error) {
if val, ok := h.opened.Get(strconv.FormatUint(fd, 10)); ok {
f := val.(*os.File)
n, err := f.WriteAt(data, offset)
return n, err
}
return 0, os.ErrInvalid
}
func (h *hoarder) CacheClose(fd uint64) error {
if val, ok := h.opened.Get(strconv.FormatUint(fd, 10)); ok {
f := val.(*os.File)
return f.Close()
}
return os.ErrInvalid
}
//func (h *Hoarder) CacheFlush(fd uint64) error {
// if val, ok := h.opened.Load(fd); ok {
// f := val.(*os.File)
// return f.Sync()
// }
//
// return os.ErrInvalid
//}