/
router_multipart.go
242 lines (221 loc) · 6.52 KB
/
router_multipart.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
/*
* Copyright © 2024 Clyso GmbH
*
* 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 router
import (
"errors"
"fmt"
xctx "github.com/clyso/chorus/pkg/ctx"
"github.com/clyso/chorus/pkg/dom"
"github.com/clyso/chorus/pkg/s3client"
"github.com/clyso/chorus/pkg/tasks"
mclient "github.com/minio/minio-go/v7"
"github.com/rs/zerolog"
"net/http"
)
func (r *router) createMultipartUpload(req *http.Request) (resp *http.Response, storage string, isApiErr bool, err error) {
ctx := req.Context()
user, bucket, object := xctx.GetUser(ctx), xctx.GetBucket(ctx), xctx.GetObject(ctx)
resp, storage, isApiErr, err = r.commonWrite(req)
if err != nil || isApiErr {
return
}
replSwitch, switchErr := r.policySvc.GetReplicationSwitch(ctx, user, bucket)
if switchErr != nil {
if errors.Is(switchErr, dom.ErrNotFound) {
return
}
//return error
err = switchErr
return
}
if replSwitch.IsDone {
return
}
// switch in progress
respBody := initiateMultipartUploadResult{}
err = s3client.ExtractRespBody(resp, &respBody)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("unable to unmarshal initiateMultipartUploadResult response body")
return
}
err = r.storageSvc.StoreUploadID(ctx, user, bucket, object, respBody.UploadID, replSwitch.MultipartTTL)
return
}
func (r *router) completeMultipartUpload(req *http.Request) (resp *http.Response, taskList []tasks.SyncTask, storage string, isApiErr bool, err error) {
ctx := req.Context()
user, bucket, object := xctx.GetUser(ctx), xctx.GetBucket(ctx), xctx.GetObject(ctx)
var switchInProgress bool
storage, switchInProgress, err = r.routeMultipart(req)
if err != nil {
return
}
client, err := r.clients.GetByName(ctx, storage)
if err != nil {
return nil, nil, "", false, err
}
resp, isApiErr, err = client.Do(req)
if err != nil || isApiErr {
return
}
if switchInProgress {
_ = r.storageSvc.DeleteUploadID(ctx, user, bucket, object, req.URL.Query().Get("uploadId"))
}
var res completeMultipartUploadResult
xmlErr := s3client.ExtractRespBody(resp, &res)
if xmlErr != nil {
zerolog.Ctx(ctx).Err(xmlErr).Msg("unable to unmarshal response body")
} else {
zerolog.Ctx(ctx).Debug().Str("etag", res.ETag).Msg("multipart uploaded object with etag")
}
var objSize int64
objInfo, err := client.S3().StatObject(ctx, bucket, object, mclient.StatObjectOptions{})
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("unable to get uploaded object size")
err = nil
} else {
objSize = objInfo.Size
}
obj := dom.Object{Bucket: bucket, Name: object}
taskList = []tasks.SyncTask{
&tasks.ObjectSyncPayload{
Object: obj,
Sync: tasks.Sync{FromStorage: storage},
ObjSize: objSize,
},
&tasks.ObjSyncACLPayload{
Object: obj,
Sync: tasks.Sync{FromStorage: storage},
},
&tasks.ObjSyncTagsPayload{
Object: obj,
Sync: tasks.Sync{FromStorage: storage},
},
}
return
}
func (r *router) abortMultipartUpload(req *http.Request) (resp *http.Response, storage string, isApiErr bool, err error) {
ctx := req.Context()
user, bucket, object := xctx.GetUser(ctx), xctx.GetBucket(ctx), xctx.GetObject(ctx)
var switchInProgress bool
storage, switchInProgress, err = r.routeMultipart(req)
if err != nil {
return
}
client, err := r.clients.GetByName(ctx, storage)
if err != nil {
return nil, "", false, err
}
resp, isApiErr, err = client.Do(req)
if err != nil || isApiErr {
return
}
if switchInProgress {
_ = r.storageSvc.DeleteUploadID(ctx, user, bucket, object, req.URL.Query().Get("uploadId"))
}
return
}
func (r *router) listMultipartUploads(req *http.Request) (resp *http.Response, storage string, isApiErr bool, err error) {
ctx := req.Context()
storage, err = r.routeListMultipart(req)
if err != nil {
return
}
client, err := r.clients.GetByName(ctx, storage)
if err != nil {
return nil, "", false, err
}
resp, isApiErr, err = client.Do(req)
return
}
func (r *router) uploadPart(req *http.Request) (resp *http.Response, storage string, isApiErr bool, err error) {
ctx := req.Context()
storage, _, err = r.routeMultipart(req)
if err != nil {
return
}
client, err := r.clients.GetByName(ctx, storage)
if err != nil {
return nil, "", false, err
}
resp, isApiErr, err = client.Do(req)
return
}
func (r *router) routeMultipart(req *http.Request) (storage string, switchInProgress bool, err error) {
ctx := req.Context()
user, bucket, object := xctx.GetUser(ctx), xctx.GetBucket(ctx), xctx.GetObject(ctx)
storage, err = r.policySvc.GetRoutingPolicy(ctx, user, bucket)
if err != nil {
if errors.Is(err, dom.ErrNotFound) {
return "", false, fmt.Errorf("%w: routing policy not configured: %v", dom.ErrPolicy, err)
}
return "", false, err
}
replSwitch, getSwitchErr := r.policySvc.GetReplicationSwitch(ctx, user, bucket)
if getSwitchErr != nil {
if errors.Is(getSwitchErr, dom.ErrNotFound) {
return
}
//return error
err = getSwitchErr
return
}
switchInProgress = replSwitch.IsDone
if replSwitch.IsDone {
return
}
uploadID := req.URL.Query().Get("uploadId")
var exists bool
exists, err = r.storageSvc.ExistsUploadID(ctx, user, bucket, object, uploadID)
if err != nil {
return
}
if exists {
return storage, switchInProgress, nil
}
return replSwitch.OldMain, switchInProgress, nil
}
func (r *router) routeListMultipart(req *http.Request) (storage string, err error) {
ctx := req.Context()
user, bucket := xctx.GetUser(ctx), xctx.GetBucket(ctx)
storage, err = r.policySvc.GetRoutingPolicy(ctx, user, bucket)
if err != nil {
if errors.Is(err, dom.ErrNotFound) {
return "", fmt.Errorf("%w: routing policy not configured: %v", dom.ErrPolicy, err)
}
return "", err
}
replSwitch, getSwitchErr := r.policySvc.GetReplicationSwitch(ctx, user, bucket)
if getSwitchErr != nil {
if errors.Is(getSwitchErr, dom.ErrNotFound) {
return
}
//return error
err = getSwitchErr
return
}
if replSwitch.IsDone {
return
}
// todo: maybe better always return old?
exists, err := r.storageSvc.ExistsUploads(ctx, user, bucket)
if err != nil {
return "", err
}
if exists {
return storage, nil
}
return replSwitch.OldMain, nil
}