-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathoption.go
More file actions
352 lines (300 loc) · 11 KB
/
Copy pathoption.go
File metadata and controls
352 lines (300 loc) · 11 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2023 Google LLC
//
// 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 storage
import (
"os"
"strconv"
"time"
"cloud.google.com/go/storage/experimental"
storageinternal "cloud.google.com/go/storage/internal"
"go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
)
const (
dynamicReadReqIncreaseRateEnv = "DYNAMIC_READ_REQ_INCREASE_RATE"
dynamicReadReqInitialTimeoutEnv = "DYNAMIC_READ_REQ_INITIAL_TIMEOUT"
defaultDynamicReadReqIncreaseRate = 15.0
defaultDynamicReqdReqMaxTimeout = 1 * time.Hour
defaultDynamicReadReqMinTimeout = 500 * time.Millisecond
defaultTargetPercentile = 0.99
)
func init() {
// initialize experimental options
storageinternal.WithMetricExporter = withMetricExporter
storageinternal.WithMetricInterval = withMetricInterval
storageinternal.WithMeterProvider = withMeterProvider
storageinternal.WithReadStallTimeout = withReadStallTimeout
storageinternal.WithDirectConnectivityEnforced = withDirectConnectivityEnforced
storageinternal.WithOtelMetrics = withOtelMetrics
storageinternal.WithOtelDebugMetrics = withOtelDebugMetrics
storageinternal.WithBufferPool = withBufferPool
}
// getDynamicReadReqIncreaseRateFromEnv returns the value set in the env variable.
// It returns defaultDynamicReadReqIncreaseRate if env is not set or the set value is invalid.
func getDynamicReadReqIncreaseRateFromEnv() float64 {
increaseRate := os.Getenv(dynamicReadReqIncreaseRateEnv)
if increaseRate == "" {
return defaultDynamicReadReqIncreaseRate
}
val, err := strconv.ParseFloat(increaseRate, 64)
if err != nil {
return defaultDynamicReadReqIncreaseRate
}
return val
}
// getDynamicReadReqInitialTimeoutSecFromEnv returns the value set in the env variable.
// It returns the passed defaultVal if env is not set or the set value is invalid.
func getDynamicReadReqInitialTimeoutSecFromEnv(defaultVal time.Duration) time.Duration {
initialTimeout := os.Getenv(dynamicReadReqInitialTimeoutEnv)
if initialTimeout == "" {
return defaultVal
}
val, err := time.ParseDuration(initialTimeout)
if err != nil {
return defaultVal
}
return val
}
// set through storageClientOptions.
type storageConfig struct {
useJSONforReads bool
readAPIWasSet bool
disableClientMetrics bool
enableOtelMetrics bool
enableOtelDebugMetrics bool
metricExporter *metric.Exporter
metricInterval time.Duration
meterProvider *metric.MeterProvider
manualReader *metric.ManualReader
readStallTimeoutConfig *experimental.ReadStallTimeoutConfig
grpcBidiReads bool
grpcAppendableUploads bool
grpcDirectPathEnforced bool
bufferPool experimental.BufferPool
}
// newStorageConfig generates a new storageConfig with all the given
// storageClientOptions applied.
func newStorageConfig(opts ...option.ClientOption) storageConfig {
var conf storageConfig
for _, opt := range opts {
if storageOpt, ok := opt.(storageClientOption); ok {
storageOpt.ApplyStorageOpt(&conf)
}
}
return conf
}
// A storageClientOption is an option for a Google Storage client.
type storageClientOption interface {
option.ClientOption
ApplyStorageOpt(*storageConfig)
}
func withDirectConnectivityEnforced() option.ClientOption {
return &withDirectPathEnforced{}
}
type withDirectPathEnforced struct {
internaloption.EmbeddableAdapter
}
func (w *withDirectPathEnforced) ApplyStorageOpt(c *storageConfig) {
c.grpcDirectPathEnforced = true
}
// WithJSONReads is an option that may be passed to [NewClient].
// It sets the client to use the Cloud Storage JSON API for object
// reads. Currently, the default API used for reads is XML, but JSON will
// become the default in a future release.
//
// Setting this option is required to use the GenerationNotMatch condition. We
// also recommend using JSON reads to ensure consistency with other client
// operations (all of which use JSON by default).
//
// Note that when this option is set, reads will return a zero date for
// [ReaderObjectAttrs].LastModified and may return a different value for
// [ReaderObjectAttrs].CacheControl.
func WithJSONReads() option.ClientOption {
return &withReadAPI{useJSON: true}
}
// WithXMLReads is an option that may be passed to [NewClient].
// It sets the client to use the Cloud Storage XML API for object reads.
//
// This is the current default, but the default will switch to JSON in a future
// release.
func WithXMLReads() option.ClientOption {
return &withReadAPI{useJSON: false}
}
type withReadAPI struct {
internaloption.EmbeddableAdapter
useJSON bool
}
func (w *withReadAPI) ApplyStorageOpt(c *storageConfig) {
c.useJSONforReads = w.useJSON
c.readAPIWasSet = true
}
type withDisabledClientMetrics struct {
internaloption.EmbeddableAdapter
disabledClientMetrics bool
}
// WithDisabledClientMetrics is an option that may be passed to [NewClient].
// gRPC metrics are enabled by default in the GCS client and will export the
// gRPC telemetry discussed in [gRFC/66] and [gRFC/78] to
// [Google Cloud Monitoring]. The option is used to disable metrics.
// Google Cloud Support can use this information to more quickly diagnose
// problems related to GCS and gRPC.
// Sending this data does not incur any billing charges, and requires minimal
// CPU (a single RPC every few minutes) or memory (a few KiB to batch the
// telemetry).
//
// The default is to enable client metrics. To opt-out of metrics collected use
// this option.
//
// [gRFC/66]: https://github.com/grpc/proposal/blob/master/A66-otel-stats.md
// [gRFC/78]: https://github.com/grpc/proposal/blob/master/A78-grpc-metrics-wrr-pf-xds.md
// [Google Cloud Monitoring]: https://cloud.google.com/monitoring/docs
func WithDisabledClientMetrics() option.ClientOption {
return &withDisabledClientMetrics{disabledClientMetrics: true}
}
func (w *withDisabledClientMetrics) ApplyStorageOpt(c *storageConfig) {
c.disableClientMetrics = w.disabledClientMetrics
}
type withMeterOptions struct {
internaloption.EmbeddableAdapter
// set sampling interval
interval time.Duration
}
func withMetricInterval(interval time.Duration) option.ClientOption {
return &withMeterOptions{interval: interval}
}
func (w *withMeterOptions) ApplyStorageOpt(c *storageConfig) {
c.metricInterval = w.interval
}
type withMetricExporterConfig struct {
internaloption.EmbeddableAdapter
// exporter override
metricExporter *metric.Exporter
}
func withMetricExporter(ex *metric.Exporter) option.ClientOption {
return &withMetricExporterConfig{metricExporter: ex}
}
func (w *withMetricExporterConfig) ApplyStorageOpt(c *storageConfig) {
c.metricExporter = w.metricExporter
}
type withTestMetricReaderConfig struct {
internaloption.EmbeddableAdapter
// reader override
metricReader *metric.ManualReader
}
type withMeterProviderConfig struct {
internaloption.EmbeddableAdapter
// meter provider override
meterProvider *metric.MeterProvider
}
func withMeterProvider(provider *metric.MeterProvider) option.ClientOption {
return &withMeterProviderConfig{meterProvider: provider}
}
func (w *withMeterProviderConfig) ApplyStorageOpt(c *storageConfig) {
c.meterProvider = w.meterProvider
}
func withTestMetricReader(ex *metric.ManualReader) option.ClientOption {
return &withTestMetricReaderConfig{metricReader: ex}
}
func (w *withTestMetricReaderConfig) ApplyStorageOpt(c *storageConfig) {
c.manualReader = w.metricReader
}
// WithReadStallTimeout is an option that may be passed to [NewClient] or [NewGRPCClient].
// It enables the client to retry the stalled read request, happens as part of
// storage.Reader creation. As the name suggest, timeout is adjusted dynamically
// based on past observed read-req latencies.
//
// This is supported for read operations on HTTP (XML) and gRPC clients (not implemented for HTTP JSON client).
func withReadStallTimeout(rstc *experimental.ReadStallTimeoutConfig) option.ClientOption {
if rstc.Min == time.Duration(0) {
rstc.Min = defaultDynamicReadReqMinTimeout
}
if rstc.TargetPercentile == 0 {
rstc.TargetPercentile = defaultTargetPercentile
}
return &withReadStallTimeoutConfig{
readStallTimeoutConfig: rstc,
}
}
type withReadStallTimeoutConfig struct {
internaloption.EmbeddableAdapter
readStallTimeoutConfig *experimental.ReadStallTimeoutConfig
}
func (wrstc *withReadStallTimeoutConfig) ApplyStorageOpt(config *storageConfig) {
config.readStallTimeoutConfig = wrstc.readStallTimeoutConfig
}
// WithGRPCBidiReads provides an [option.ClientOption] that may be passed to
// [NewGRPCClient].
//
// It instructs the client to use the bi-directional gRPC API for all standard object
// downloads. Additionally, this option is strictly required to use the
// [MultiRangeDownloader] surface.
//
// Note: This option is exclusively supported for gRPC clients.
func WithGRPCBidiReads() option.ClientOption {
return &withGRPCBidiReadsConfig{}
}
type withGRPCBidiReadsConfig struct {
internaloption.EmbeddableAdapter
}
func (w *withGRPCBidiReadsConfig) ApplyStorageOpt(config *storageConfig) {
config.grpcBidiReads = true
}
// WithAppendableUploads provides an [option.ClientOption] that may be passed to
// [NewGRPCClient].
//
// It enables the client to use appendable object semantics for uploads by default.
//
// Note: This option is exclusively supported for gRPC clients.
func WithAppendableUploads() option.ClientOption {
return &withGRPCAppendableUploadsConfig{}
}
type withGRPCAppendableUploadsConfig struct {
internaloption.EmbeddableAdapter
}
func (w *withGRPCAppendableUploadsConfig) ApplyStorageOpt(config *storageConfig) {
config.grpcAppendableUploads = true
}
func withOtelMetrics() option.ClientOption {
return &withOtelMetricsConfig{}
}
type withOtelMetricsConfig struct {
internaloption.EmbeddableAdapter
}
func (w *withOtelMetricsConfig) ApplyStorageOpt(c *storageConfig) {
c.enableOtelMetrics = true
}
func withOtelDebugMetrics() option.ClientOption {
return &withOtelDebugMetricsConfig{}
}
type withOtelDebugMetricsConfig struct {
internaloption.EmbeddableAdapter
}
func (w *withOtelDebugMetricsConfig) ApplyStorageOpt(c *storageConfig) {
c.enableOtelDebugMetrics = true
}
// withBufferPool sets the buffer pool used to allocate memory for parallel
// uploads. It backs [cloud.google.com/go/storage/experimental.WithBufferPool].
//
// This option is not supported at the moment.
func withBufferPool(pool experimental.BufferPool) option.ClientOption {
return &withBufferPoolConfig{pool: pool}
}
type withBufferPoolConfig struct {
internaloption.EmbeddableAdapter
pool experimental.BufferPool
}
func (w *withBufferPoolConfig) ApplyStorageOpt(c *storageConfig) {
c.bufferPool = w.pool
}