/
tasks.go
168 lines (152 loc) · 4.13 KB
/
tasks.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
package server
import (
"fmt"
"log"
"time"
"github.com/getcfs/megacfs/formic/formicproto"
"github.com/gholt/store"
"go.uber.org/zap"
"golang.org/x/net/context"
)
type updateItem struct {
id []byte
block uint64
blocksize uint64
size uint64
mtime int64
}
type updatinator struct {
in chan *updateItem
fs *oortFS
}
// newUpdatinator ...
func newUpdatinator(in chan *updateItem, fs *oortFS) *updatinator {
return &updatinator{
in: in,
fs: fs,
}
}
func (u *updatinator) Run() {
// TODO: Add fan-out based on the id of the update
for {
toupdate := <-u.in
log.Println("Updating: ", toupdate)
// TODO: Need better context
ctx := context.Background()
err := u.fs.update(ctx, toupdate.id, toupdate.block, toupdate.blocksize, toupdate.size, toupdate.mtime)
if err != nil {
log.Println("Update failed, requeing: ", err)
u.in <- toupdate
}
}
}
type dirtyItem struct {
dirty *formicproto.Dirty
}
// TODO: Crawl the dirty folders to look for dirty objects to cleanup
type cleaninator struct {
in chan *dirtyItem
fs *oortFS
comms *storeComms
log *zap.Logger
}
func newCleaninator(in chan *dirtyItem, fs *oortFS, comms *storeComms, logger *zap.Logger) *cleaninator {
return &cleaninator{
in: in,
fs: fs,
log: logger,
comms: comms,
}
}
func (c *cleaninator) Run() {
// TODO: Parallelize?
for {
toclean := <-c.in
dirty := toclean.dirty
c.log.Debug("Cleaning", zap.Any("item", dirty))
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
fails := 0
for b := dirty.Blocks + 1; b > 0; b-- {
// Try to delete the old block
id := getID(dirty.FSID, dirty.INode, b)
err := c.fs.deleteChunk(ctx, id, dirty.DTime)
if err == errStoreHasNewerValue {
// Something has already been writte, so we are good
break
} else if store.IsNotFound(err) {
continue
} else if err != nil {
fails++
}
}
if fails > 0 {
// Not everything could be cleaned, so queue to try again later
c.in <- toclean
} else {
// All orphaned data is deleted so remove the tombstone
c.log.Debug("Done Cleaning", zap.Any("item", dirty))
err := c.comms.DeleteGroupItem(ctx, getDirtyID(dirty.FSID), []byte(fmt.Sprintf("%d", dirty.INode)))
if err != nil && !store.IsNotFound(err) {
// Failed to remove so queue again to retry later
c.in <- toclean
}
}
}
}
type deleteItem struct {
ts *formicproto.Tombstone
}
// TODO: Crawl the deleted folders to look for deletes to cleanup
// TODO: We should have sort of backoff in case of failures, so it isn't trying a delete over and over again if there are failures
type deletinator struct {
in chan *deleteItem
fs *oortFS
comms *storeComms
log *zap.Logger
}
func newDeletinator(in chan *deleteItem, fs *oortFS, comms *storeComms, logger *zap.Logger) *deletinator {
return &deletinator{
in: in,
fs: fs,
comms: comms,
log: logger,
}
}
func (d *deletinator) Run() {
// TODO: Parallelize this thing?
for {
todelete := <-d.in
ts := todelete.ts
d.log.Debug("Deleting", zap.Any("tombstone", ts))
deleted := uint64(0)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
for b := uint64(0); b < ts.Blocks; b++ {
// Delete each block
id := getID(ts.FSID, ts.INode, b+1)
err := d.fs.deleteChunk(ctx, id, ts.DTime)
if err != nil && !store.IsNotFound(err) && err != errStoreHasNewerValue {
continue
}
deleted++
}
if deleted == ts.Blocks {
// Everything is deleted so delete the entry
err := d.fs.deleteChunk(ctx, getID(ts.FSID, ts.INode, 0), ts.DTime)
if err != nil && !store.IsNotFound(err) && err != errStoreHasNewerValue {
// Couldn't delete the inode entry so try again later
d.in <- todelete
continue
}
} else {
// If all artifacts are not deleted requeue for later
d.in <- todelete
}
// All artifacts are deleted so remove the delete tombstone
d.log.Debug("Done Deleting", zap.Any("tombstone", ts))
err := d.comms.DeleteGroupItem(ctx, getDeletedID(ts.FSID), []byte(fmt.Sprintf("%d", ts.INode)))
if err != nil && !store.IsNotFound(err) {
// Failed to remove so queue again to retry later
d.in <- todelete
}
}
}