-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_s3.go
More file actions
207 lines (196 loc) · 5.8 KB
/
Copy pathmain_s3.go
File metadata and controls
207 lines (196 loc) · 5.8 KB
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
package main
import (
"context"
"net"
"strings"
"github.com/bootjp/elastickv/adapter"
"github.com/bootjp/elastickv/kv"
"github.com/cockroachdb/errors"
"golang.org/x/sync/errgroup"
)
// startS3Server stands up the S3-compatible HTTP listener on s3Addr
// and returns the constructed *adapter.S3Server. Callers that need a
// reference to the running server (e.g. the admin HTTP listener,
// which calls SigV4-bypass admin entrypoints on it) hold the
// returned value; callers that don't can ignore it.
//
// Returns (nil, nil) when s3Addr is empty — that is the well-known
// "S3 disabled" state, not a configuration error. Other failures
// surface as a non-nil error and a nil server.
func startS3Server(
ctx context.Context,
lc *net.ListenConfig,
eg *errgroup.Group,
s3Addr string,
shardStore *kv.ShardStore,
coordinate kv.Coordinator,
leaderS3 map[string]string,
region string,
credentialsFile string,
pathStyleOnly bool,
readTracker *kv.ActiveTimestampTracker,
putAdmissionObserver adapter.S3PutAdmissionObserver,
blobOffloadObserver adapter.S3BlobOffloadObserver,
) (*adapter.S3Server, error) {
s3Server, _, err := prepareS3Server(
ctx, lc, s3Addr, shardStore, coordinate, leaderS3, region,
credentialsFile, pathStyleOnly, readTracker, putAdmissionObserver,
blobOffloadObserver, nil,
)
if err != nil {
return nil, err
}
runS3Server(ctx, eg, s3Server)
return s3Server, nil
}
func prepareS3Server(
ctx context.Context,
lc *net.ListenConfig,
s3Addr string,
shardStore *kv.ShardStore,
coordinate kv.Coordinator,
leaderS3 map[string]string,
region string,
credentialsFile string,
pathStyleOnly bool,
readTracker *kv.ActiveTimestampTracker,
putAdmissionObserver adapter.S3PutAdmissionObserver,
blobOffloadObserver adapter.S3BlobOffloadObserver,
blobBackfiller *adapter.S3BlobBackfiller,
) (*adapter.S3Server, net.Listener, error) {
s3Server, err := newS3Server(
s3Addr, shardStore, coordinate, leaderS3, region, credentialsFile,
pathStyleOnly, readTracker, putAdmissionObserver, blobOffloadObserver, nil, nil, blobBackfiller,
)
if err != nil {
return nil, nil, err
}
s3L, err := bindS3Server(ctx, lc, s3Addr, s3Server)
if err != nil {
return nil, nil, err
}
return s3Server, s3L, nil
}
func newS3Server(
s3Addr string,
shardStore *kv.ShardStore,
coordinate kv.Coordinator,
leaderS3 map[string]string,
region string,
credentialsFile string,
pathStyleOnly bool,
readTracker *kv.ActiveTimestampTracker,
putAdmissionObserver adapter.S3PutAdmissionObserver,
blobOffloadObserver adapter.S3BlobOffloadObserver,
blobCluster adapter.S3BlobCluster,
blobPushBlocked func() bool,
blobBackfiller *adapter.S3BlobBackfiller,
) (*adapter.S3Server, error) {
s3Addr = strings.TrimSpace(s3Addr)
peerOnly := s3Addr == "" && blobCluster != nil && blobBackfiller != nil
if s3Addr == "" && !peerOnly {
// (nil, nil) is the explicit "S3 disabled" signal — the empty
// flag value is a valid configuration, not an error. The
// nilnil linter is not enabled in .golangci.yaml so no
// suppression directive is needed.
return nil, nil
}
if s3Addr != "" && !pathStyleOnly {
return nil, errors.New("virtual-hosted style S3 requests are not implemented")
}
staticCreds, err := loadS3StaticCredentialsForAddress(s3Addr, credentialsFile)
if err != nil {
return nil, err
}
minReplicas, err := adapter.S3BlobMinReplicasFromEnv()
if err != nil {
return nil, errors.WithStack(err)
}
options := []adapter.S3ServerOption{
adapter.WithS3Region(region),
adapter.WithS3StaticCredentials(staticCreds),
adapter.WithS3ActiveTimestampTracker(readTracker),
adapter.WithS3PutAdmissionObserver(putAdmissionObserver),
adapter.WithS3BlobOffloadObserver(blobOffloadObserver),
adapter.WithS3BlobMinReplicas(minReplicas),
adapter.WithS3BlobPushBlocked(blobPushBlocked),
adapter.WithS3BlobBackfiller(blobBackfiller),
}
if blobCluster != nil {
options = append(options, adapter.WithS3BlobCluster(blobCluster))
}
s3Server := adapter.NewS3Server(
nil,
s3Addr,
shardStore,
coordinate,
leaderS3,
options...,
)
return s3Server, nil
}
func bindS3Server(ctx context.Context, lc *net.ListenConfig, s3Addr string, s3Server *adapter.S3Server) (net.Listener, error) {
s3Addr = strings.TrimSpace(s3Addr)
if s3Server == nil || s3Addr == "" {
return nil, nil
}
s3L, err := lc.Listen(ctx, "tcp", s3Addr)
if err != nil {
return nil, errors.Wrapf(err, "failed to listen on %s", s3Addr)
}
s3Server.SetListener(s3L)
return s3L, nil
}
func runS3Server(ctx context.Context, eg *errgroup.Group, s3Server *adapter.S3Server) {
if s3Server == nil {
return
}
runDoneCtx, runDoneCancel := context.WithCancel(context.Background())
eg.Go(func() error {
select {
case <-ctx.Done():
s3Server.Stop()
case <-runDoneCtx.Done():
}
return nil
})
eg.Go(func() error {
if err := s3Server.StartBlobBackfill(ctx); err != nil {
s3Server.Stop()
runDoneCancel()
return errors.WithStack(err)
}
err := s3Server.Run()
runDoneCancel()
if err == nil || errors.Is(err, net.ErrClosed) {
return nil
}
return errors.WithStack(err)
})
}
func runS3BlobBackfillOnly(ctx context.Context, eg *errgroup.Group, s3Server *adapter.S3Server) {
if s3Server == nil {
return
}
eg.Go(func() error {
if err := s3Server.StartBlobBackfill(ctx); err != nil {
s3Server.Stop()
return errors.WithStack(err)
}
<-ctx.Done()
s3Server.Stop()
return nil
})
}
func s3BlobNodeEnabled(s3Address, peerTokenFile string) bool {
return strings.TrimSpace(s3Address) != "" || strings.TrimSpace(peerTokenFile) != ""
}
func loadS3StaticCredentialsForAddress(s3Address, credentialsFile string) (map[string]string, error) {
if s3Address == "" {
return nil, nil
}
return loadS3StaticCredentials(credentialsFile)
}
func loadS3StaticCredentials(path string) (map[string]string, error) {
return loadSigV4StaticCredentialsFile(path, "s3")
}