/
replicate.go
237 lines (195 loc) · 5.26 KB
/
replicate.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
package server
import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/AudiusProject/audius-protocol/mediorum/server/signature"
"github.com/AudiusProject/audius-protocol/mediorum/cidutil"
"gocloud.dev/blob"
)
func (ss *MediorumServer) replicateFileParallel(cid string, filePath string, placementHosts []string) ([]string, error) {
replicaCount := ss.Config.ReplicationFactor
if len(placementHosts) > 0 {
// use all explicit placement hosts
replicaCount = len(placementHosts)
} else {
// use rendezvous
placementHosts, _ = ss.rendezvousHealthyHosts(cid)
}
queue := make(chan string, len(placementHosts))
for _, p := range placementHosts {
queue <- p
}
mu := sync.Mutex{}
results := []string{}
wg := sync.WaitGroup{}
wg.Add(replicaCount)
for i := 0; i < replicaCount; i++ {
go func() {
defer wg.Done()
file, err := os.Open(filePath)
if err != nil {
ss.logger.Error("failed to open file", "filePath", filePath, "err", err)
return
}
defer file.Close()
for peer := range queue {
file.Seek(0, 0)
err := ss.replicateFileToHost(peer, cid, file)
if err == nil {
mu.Lock()
results = append(results, peer)
mu.Unlock()
break
}
}
}()
}
wg.Wait()
return results, nil
}
func (ss *MediorumServer) replicateFile(fileName string, file io.ReadSeeker) ([]string, error) {
logger := ss.logger.With("task", "replicate", "cid", fileName)
success := []string{}
preferred, _ := ss.rendezvousHealthyHosts(fileName)
for _, peer := range preferred {
logger := logger.With("to", peer)
logger.Info("replicating")
file.Seek(0, 0)
err := ss.replicateFileToHost(peer, fileName, file)
if err != nil {
logger.Error("replication failed", "err", err)
} else {
logger.Info("replicated")
success = append(success, peer)
if len(success) == ss.Config.ReplicationFactor {
break
}
}
}
return success, nil
}
func (ss *MediorumServer) replicateToMyBucket(fileName string, file io.Reader) error {
ctx := context.Background()
logger := ss.logger.With("task", "replicateToMyBucket", "cid", fileName)
logger.Info("replicateToMyBucket")
key := cidutil.ShardCID(fileName)
w, err := ss.bucket.NewWriter(ctx, key, nil)
if err != nil {
return err
}
_, err = io.Copy(w, file)
if err != nil {
return err
}
return w.Close()
}
func (ss *MediorumServer) dropFromMyBucket(fileName string) error {
logger := ss.logger.With("task", "dropFromMyBucket", "cid", fileName)
logger.Info("deleting blob")
key := cidutil.ShardCID(fileName)
ctx := context.Background()
err := ss.bucket.Delete(ctx, key)
if err != nil {
logger.Error("failed to delete", "err", err)
}
return nil
}
func (ss *MediorumServer) replicateFileToHost(peer string, fileName string, file io.Reader) error {
// logger := ss.logger.With()
if peer == ss.Config.Self.Host {
return ss.replicateToMyBucket(fileName, file)
}
client := http.Client{
Timeout: time.Minute,
}
r, w := io.Pipe()
m := multipart.NewWriter(w)
errChan := make(chan error)
go func() {
defer w.Close()
defer m.Close()
part, err := m.CreateFormFile(filesFormFieldName, fileName)
if err != nil {
errChan <- err
return
}
if _, err = io.Copy(part, file); err != nil {
errChan <- err
return
}
close(errChan)
}()
req := signature.SignedPost(
peer+"/internal/blobs",
m.FormDataContentType(),
r,
ss.Config.privateKey,
ss.Config.Self.Host,
)
// send it
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return <-errChan
}
// hostHasBlob is a "quick check" that a host has a blob (used for checking host has blob before redirecting to it).
func (ss *MediorumServer) hostHasBlob(host, key string) bool {
attr, err := ss.hostGetBlobInfo(host, key)
return err == nil && attr != nil
}
func (ss *MediorumServer) hostGetBlobInfo(host, key string) (*blob.Attributes, error) {
var attr *blob.Attributes
u := apiPath(host, fmt.Sprintf("internal/blobs/info/%s", url.PathEscape(key)))
resp, err := ss.reqClient.R().SetSuccessResult(&attr).Get(u)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%s %s", u, resp.Status)
}
return attr, nil
}
func (ss *MediorumServer) pullFileFromHost(host, cid string) error {
if host == ss.Config.Self.Host {
return errors.New("should not pull blob from self")
}
client := http.Client{
Timeout: time.Minute,
}
u := apiPath(host, "internal/blobs", url.PathEscape(cid))
req, err := signature.SignedGet(u, ss.Config.privateKey, ss.Config.Self.Host)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("pull blob: bad status: %d cid: %s host: %s", resp.StatusCode, cid, host)
}
return ss.replicateToMyBucket(cid, resp.Body)
}
// if the node is using local (disk) storage, do not replicate if there is <200GB remaining (i.e. 10% of 2TB)
func (ss *MediorumServer) diskHasSpace() bool {
// don't worry about running out of space on dev or stage
if ss.Config.Env != "prod" {
return true
}
return !strings.HasPrefix(ss.Config.BlobStoreDSN, "file://") || ss.mediorumPathFree/uint64(1e9) > 200
}