forked from couchbaselabs/cbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsck.go
150 lines (132 loc) · 2.97 KB
/
fsck.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
package main
import (
"encoding/json"
"log"
"net/http"
)
// Take a stream of namedFiles and clump them into batches of at most
// the specified size
func keyClumper(ch chan *namedFile, size int) chan []*namedFile {
outch := make(chan []*namedFile)
go func() {
defer close(outch)
res := make([]*namedFile, 0, size)
for r := range ch {
res = append(res, r)
if len(res) == size {
outch <- res
res = make([]*namedFile, 0, size)
}
}
if len(res) > 0 {
outch <- res
}
}()
return outch
}
func dofsck(w http.ResponseWriter, req *http.Request,
path string) {
errsOnly := req.FormValue("errsonly") != ""
quit := make(chan bool)
defer close(quit)
ch := make(chan *namedFile)
cherr := make(chan error)
go pathGenerator(path, ch, cherr, quit)
go func() {
for e := range cherr {
log.Printf("View error: %v", e)
}
}()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
e := json.NewEncoder(w)
type status struct {
Path string `json:"path"`
OID string `json:"oid,omitempty"`
Reps int `json:"reps,omitempty"`
EType string `json:"etype,omitempty"`
Error string `json:"error,omitempty"`
}
for nfc := range keyClumper(ch, 1000) {
keys := []string{}
fnmap := map[string][]string{}
unprocessed := map[string]string{}
for _, nf := range nfc {
if nf.err != nil {
if err := e.Encode(status{
Path: nf.name,
OID: nf.meta.OID,
EType: "file",
Error: nf.err.Error(),
}); err != nil {
log.Printf("Error encoding: %v", err)
return
}
}
keys = append(keys, "/"+nf.meta.OID)
unprocessed[nf.name] = nf.meta.OID
a, ok := fnmap[nf.meta.OID]
if ok {
a = append(a, nf.name)
} else {
a = []string{nf.name}
}
fnmap[nf.meta.OID] = a
}
bres, err := couchbase.GetBulk(keys)
if err != nil {
log.Printf("Error getting bulk keys: %v", err)
return
}
for k, v := range bres {
names := fnmap[k[1:]]
for _, name := range names {
delete(unprocessed, name)
}
ownership := BlobOwnership{}
err := json.Unmarshal(v.Body, &ownership)
if err != nil {
for _, name := range names {
if err = e.Encode(status{
Path: name,
OID: k[1:],
EType: "blob",
Error: err.Error(),
}); err != nil {
log.Printf("Error encoding: %v", err)
return
}
}
}
if !errsOnly {
for _, name := range names {
if err := e.Encode(status{
Path: name,
OID: k[1:],
Reps: len(ownership.Nodes),
}); err != nil {
log.Printf("Error encoding: %v", err)
return
}
}
}
}
for k, v := range unprocessed {
// If we didn't get it in the first pass, try harder.
_, err := getBlobOwnership(v)
if err == nil {
log.Printf("Got %v on the second try", v)
continue
}
if err := e.Encode(status{
Path: k,
OID: v,
EType: "blob",
Error: "not found",
}); err != nil {
log.Printf("Error encoding: %v", err)
return
}
}
}
}