-
Notifications
You must be signed in to change notification settings - Fork 18
/
project.go
190 lines (154 loc) · 5.17 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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"github.com/zeebo/errs"
"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
dialer rpc.Dialer
ec ecclient.Client
segmentSize int64
encryptionParameters storj.EncryptionParameters
}
// 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)
}
dialer, err := config.getDialer(ctx)
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{
// TODO: the cipher should be provided by the Access, but we don't store it there yet.
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(dialer, 0)
return &Project{
config: config,
access: access,
dialer: dialer,
ec: ec,
segmentSize: segmentsSize,
encryptionParameters: encryptionParameters,
}, nil
}
// Close closes the project and all associated resources.
func (project *Project) Close() (err error) {
// only close the connection pool if it's created through OpenProject
if project.config.pool == nil {
err = errs.Combine(err, project.dialer.Pool.Close())
}
return packageError.Wrap(err)
}
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())
}
}()
streamStore, err := streams.NewStreamStore(
metainfoClient,
project.ec,
project.segmentSize,
project.access.encAccess.Store,
project.encryptionParameters,
maxInlineSize)
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.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.dialer,
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)
}