-
Notifications
You must be signed in to change notification settings - Fork 17
/
project.go
221 lines (180 loc) · 6.43 KB
/
project.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"github.com/zeebo/errs"
"storj.io/common/leak"
"storj.io/common/memory"
"storj.io/common/rpc"
"storj.io/common/storj"
"storj.io/uplink/private/ecclient"
"storj.io/uplink/private/metaclient"
"storj.io/uplink/private/storage/streams"
"storj.io/uplink/private/testuplink"
"storj.io/uplink/private/version"
)
// TODO we need find a way how to pass it from satellite to client.
const maxInlineSize = 4096 // 4KiB
// maxSegmentSize can be used to override max segment size with ldflags build parameter.
// Example: go build -ldflags "-X 'storj.io/uplink.maxSegmentSize=1MiB'" storj.io/storj/cmd/uplink.
var maxSegmentSize string
// Project provides access to managing buckets and objects.
type Project struct {
config Config
access *Access
satelliteDialer rpc.Dialer
storagenodeDialer rpc.Dialer
ec ecclient.Client
segmentSize int64
encryptionParameters storj.EncryptionParameters
concurrentSegmentUploadConfig *testuplink.ConcurrentSegmentUploadsConfig
tracker leak.Ref
}
// OpenProject opens a project with the specific access grant.
func OpenProject(ctx context.Context, access *Access) (*Project, error) {
return (Config{}).OpenProject(ctx, access)
}
// OpenProject opens a project with the specific access grant.
func (config Config) OpenProject(ctx context.Context, access *Access) (project *Project, err error) {
defer mon.Task()(&ctx)(&err)
if access == nil {
return nil, packageError.New("access grant is nil")
}
switch {
case config.DialTimeout < 0:
config.DialTimeout = 0 // no timeout
case config.DialTimeout == 0:
config.DialTimeout = defaultDialTimeout
}
if err := config.validateUserAgent(ctx); err != nil {
return nil, packageError.New("invalid user agent: %w", err)
}
config.UserAgent, err = version.AppendVersionToUserAgent(config.UserAgent)
if err != nil {
return nil, packageError.Wrap(err)
}
storagenodeDialer, err := config.getDialerForPool(ctx, config.pool)
if err != nil {
return nil, packageError.Wrap(err)
}
satelliteDialer, err := config.getDialerForPool(ctx, config.satellitePool)
if err != nil {
return nil, packageError.Wrap(err)
}
// TODO: This should come from the EncryptionAccess. For now it's hardcoded to twice the
// stripe size of the default redundancy scheme on the satellite.
encBlockSize := 29 * 256 * memory.B.Int32()
encryptionParameters := storj.EncryptionParameters{
// N.B.: This is the ciphersuite we use for encrypting content keys,
// which should absolutely be encrypted, even if the access grant
// says EncNull.
CipherSuite: storj.EncAESGCM,
BlockSize: encBlockSize,
}
// TODO: All these should be controlled by the satellite and not configured by the uplink.
// For now we need to have these hard coded values that match the satellite configuration
// to be able to create the underlying stream store.
var (
segmentsSize = 64 * memory.MiB.Int64()
)
if maxSegmentSize != "" {
segmentsSize, err = memory.ParseString(maxSegmentSize)
if err != nil {
return nil, packageError.Wrap(err)
}
} else {
s, ok := testuplink.GetMaxSegmentSize(ctx)
if ok {
segmentsSize = s.Int64()
}
}
ec := ecclient.New(storagenodeDialer, 0)
tracker := leak.FromContext(ctx)
if tracker == (leak.Ref{}) { // TODO: handle this check better
tracker = leak.Root(1)
}
return &Project{
config: config,
access: access,
satelliteDialer: satelliteDialer,
storagenodeDialer: storagenodeDialer,
ec: ec,
segmentSize: segmentsSize,
encryptionParameters: encryptionParameters,
concurrentSegmentUploadConfig: testuplink.GetConcurrentSegmentUploadsConfig(ctx),
tracker: tracker,
}, nil
}
// Close closes the project and all associated resources.
func (project *Project) Close() (err error) {
// only close the connection pools if it's created through OpenProject / getDialer()
if project.config.pool == nil {
err = errs.Combine(err, project.storagenodeDialer.Pool.Close())
if project.config.satellitePool == nil {
// if config.satellitePool is nil, but config.pool is not, it might be a second Close, but it's safe.
err = errs.Combine(err, project.satelliteDialer.Pool.Close())
}
}
return packageError.Wrap(errs.Combine(err, project.tracker.Close()))
}
func (project *Project) getStreamsStore(ctx context.Context) (_ *streams.Store, err error) {
defer mon.Task()(&ctx)(&err)
metainfoClient, err := project.dialMetainfoClient(ctx)
if err != nil {
return nil, packageError.Wrap(err)
}
defer func() {
if err != nil {
err = errs.Combine(err, metainfoClient.Close())
}
}()
var longTailMargin int
if project.concurrentSegmentUploadConfig != nil {
longTailMargin = project.concurrentSegmentUploadConfig.LongTailMargin
}
streamStore, err := streams.NewStreamStore(
metainfoClient,
project.ec,
project.segmentSize,
project.access.encAccess.Store,
project.encryptionParameters,
maxInlineSize,
longTailMargin)
if err != nil {
return nil, packageError.Wrap(err)
}
return streamStore, nil
}
func (project *Project) dialMetainfoDB(ctx context.Context) (_ *metaclient.DB, err error) {
defer mon.Task()(&ctx)(&err)
metainfoClient, err := project.dialMetainfoClient(ctx)
if err != nil {
return nil, packageError.Wrap(err)
}
return metaclient.New(metainfoClient, project.encryptionParameters, project.access.encAccess.Store), nil
}
func (project *Project) dialMetainfoClient(ctx context.Context) (_ *metaclient.Client, err error) {
defer mon.Task()(&ctx)(&err)
metainfoClient, err := metaclient.DialNodeURL(ctx,
project.satelliteDialer,
project.access.satelliteURL.String(),
project.access.apiKey,
project.config.UserAgent)
if err != nil {
return nil, packageError.Wrap(err)
}
return metainfoClient, nil
}
//nolint:deadcode
//lint:ignore U1000 its used in private/object package
func dialMetainfoDBWithProject(ctx context.Context, project *Project) (_ *metaclient.DB, err error) {
defer mon.Task()(&ctx)(&err)
return project.dialMetainfoDB(ctx)
}
//nolint:deadcode
//lint:ignore U1000 its used in private/object package
func getStreamsStoreWithProject(ctx context.Context, project *Project) (_ *streams.Store, err error) {
defer mon.Task()(&ctx)(&err)
return project.getStreamsStore(ctx)
}