-
Notifications
You must be signed in to change notification settings - Fork 270
/
upload_manager.go
277 lines (232 loc) · 7.41 KB
/
upload_manager.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
/*
* Copyright 2020 The Dragonfly Authors
*
* 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.
*/
//go:generate mockgen -destination mocks/upload_manager_mock.go -source upload_manager.go -package mocks
package upload
import (
"context"
"crypto/tls"
"fmt"
"io"
"math"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-http-utils/headers"
"github.com/johanbrandhorst/certify"
ginprometheus "github.com/mcuadros/go-gin-prometheus"
"github.com/soheilhy/cmux"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"golang.org/x/time/rate"
"d7y.io/dragonfly/v2/client/config"
"d7y.io/dragonfly/v2/client/daemon/storage"
"d7y.io/dragonfly/v2/client/util"
logger "d7y.io/dragonfly/v2/internal/dflog"
)
const (
PrometheusSubsystemName = "dragonfly_dfdaemon_upload"
OtelServiceName = "dragonfly-dfdaemon-upload"
)
const (
RouterGroupDownload = "/download"
)
var GinLogFileName = "gin-upload.log"
// Manager is the interface used for upload task.
type Manager interface {
// Started upload manager server.
Serve(lis net.Listener) error
// Stop upload manager server.
Stop() error
}
// uploadManager provides upload manager function.
type uploadManager struct {
*http.Server
*rate.Limiter
storageManager storage.Manager
certify *certify.Certify
}
// Option is a functional option for configuring the upload manager.
type Option func(um *uploadManager)
// WithLimiter sets upload rate limiter, the burst size must be bigger than piece size.
func WithLimiter(limiter *rate.Limiter) func(*uploadManager) {
return func(manager *uploadManager) {
manager.Limiter = limiter
}
}
func WithCertify(ct *certify.Certify) func(manager *uploadManager) {
return func(manager *uploadManager) {
manager.certify = ct
}
}
// New returns a new Manager instence.
func NewUploadManager(cfg *config.DaemonOption, storageManager storage.Manager, logDir string, opts ...Option) (Manager, error) {
um := &uploadManager{
storageManager: storageManager,
}
router := um.initRouter(cfg, logDir)
um.Server = &http.Server{
Handler: router,
}
for _, opt := range opts {
opt(um)
}
return um, nil
}
// Started upload manager server.
func (um *uploadManager) Serve(listener net.Listener) error {
if um.certify == nil {
return um.Server.Serve(listener)
}
logger.Debugf("use http and https uploader in same listener")
m := cmux.New(listener)
httpListener := m.Match(cmux.HTTP1Fast())
tlsListener := m.Match(cmux.Any())
go func() {
if err := um.Server.Serve(httpListener); err != nil {
logger.Debugf("upload server exit: %s", err)
}
}()
go func() {
tlsConfig := &tls.Config{
GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
// FIXME peers need pure ip cert, certify checks the ServerName, so workaround here
hello.ServerName = "peer"
return um.certify.GetCertificate(hello)
},
}
tlsListener = tls.NewListener(tlsListener, tlsConfig)
if err := um.Server.Serve(tlsListener); err != nil {
logger.Debugf("upload server exit: %s", err)
}
}()
return m.Serve()
}
// Stop upload manager server.
func (um *uploadManager) Stop() error {
return um.Server.Shutdown(context.Background())
}
// Initialize router of gin.
func (um *uploadManager) initRouter(cfg *config.DaemonOption, logDir string) *gin.Engine {
// Set mode
if !cfg.Verbose {
gin.SetMode(gin.ReleaseMode)
}
// Logging to a file
if !cfg.Console {
gin.DisableConsoleColor()
logDir := filepath.Join(logDir, "daemon")
f, _ := os.Create(filepath.Join(logDir, GinLogFileName))
gin.DefaultWriter = io.MultiWriter(f)
}
r := gin.New()
// Middleware
r.Use(gin.Logger())
r.Use(gin.Recovery())
// Prometheus metrics
p := ginprometheus.NewPrometheus(PrometheusSubsystemName)
// Prometheus metrics need to reduce label,
// refer to https://prometheus.io/docs/practices/instrumentation/#do-not-overuse-labels.
p.ReqCntURLLabelMappingFn = func(c *gin.Context) string {
if strings.HasPrefix(c.Request.URL.Path, RouterGroupDownload) {
return RouterGroupDownload
}
return c.Request.URL.Path
}
p.Use(r)
// Opentelemetry
if cfg.Options.Telemetry.Jaeger != "" {
r.Use(otelgin.Middleware(OtelServiceName))
}
// Health Check.
r.GET("/healthy", um.getHealth)
// Peer download task.
d := r.Group(RouterGroupDownload)
d.GET(":task_prefix/:task_id", um.getDownload)
return r
}
// getHealth uses to check server health.
func (um *uploadManager) getHealth(ctx *gin.Context) {
ctx.JSON(http.StatusOK, http.StatusText(http.StatusOK))
}
// getDownload uses to upload a task file when other peers download from it.
func (um *uploadManager) getDownload(ctx *gin.Context) {
var params DownloadParams
if err := ctx.ShouldBindUri(¶ms); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
var query DownalodQuery
if err := ctx.ShouldBindQuery(&query); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
taskID := params.TaskID
peerID := query.PeerID
log := logger.WithTaskAndPeerID(taskID, peerID).With("component", "uploadManager")
log.Debugf("upload piece for task %s/%s to %s, request header: %#v", taskID, peerID, ctx.Request.RemoteAddr, ctx.Request.Header)
rg, err := util.ParseRange(ctx.GetHeader(headers.Range), math.MaxInt64)
if err != nil {
log.Errorf("parse range with error: %s", err)
ctx.JSON(http.StatusBadRequest, gin.H{"errors": err.Error()})
return
}
if len(rg) != 1 {
log.Error("multi range parsed, not support")
ctx.JSON(http.StatusBadRequest, gin.H{"errors": "invalid range"})
return
}
reader, closer, err := um.storageManager.ReadPiece(ctx,
&storage.ReadPieceRequest{
PeerTaskMetadata: storage.PeerTaskMetadata{
TaskID: taskID,
PeerID: peerID,
},
PieceMetadata: storage.PieceMetadata{
Num: -1,
Range: rg[0],
},
})
if err != nil {
log.Errorf("get task data failed: %s", err)
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}
defer closer.Close()
// Add header "Content-Length" to avoid chunked body in http client.
ctx.Header(headers.ContentLength, fmt.Sprintf("%d", rg[0].Length))
// write header immediately, prevent client disconnecting after limiter.Wait() due to response header timeout
ctx.Writer.WriteHeaderNow()
ctx.Writer.Flush()
if um.Limiter != nil {
if err = um.Limiter.WaitN(ctx, int(rg[0].Length)); err != nil {
log.Errorf("get limit failed: %s", err)
ctx.JSON(http.StatusInternalServerError, gin.H{"errors": err.Error()})
return
}
}
// If w is a socket, golang will use sendfile or splice syscall for zero copy feature
// when start to transfer data, we could not call http.Error with header.
if n, err := io.Copy(ctx.Writer, reader); err != nil {
log.Errorf("transfer data failed: %s", err)
return
} else if n != rg[0].Length {
log.Errorf("transferred data length not match request, request: %d, transferred: %d",
rg[0].Length, n)
return
}
}