/
codec.go
370 lines (322 loc) · 9.89 KB
/
codec.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
package codec
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strconv"
"go.temporal.io/api/common/v1"
"go.temporal.io/sdk/converter"
)
const (
remoteCodecName = "temporal.io/remote-codec"
)
type Codec struct {
// client is the HTTP client used for talking to the LPS server.
client *http.Client
// url is the base URL of the LPS server.
url *url.URL
// version is the LPS API version (v1 or v2).
version string
// minBytes is the minimum size of the payload in order to use remote codec.
minBytes int
// namespace is the Temporal namespace the client using this codec is connected to.
namespace string
}
type keyResponse struct {
Key string `json:"key"`
}
type remotePayload struct {
// Content of the original payload's Metadata.
Metadata map[string][]byte `json:"metadata"`
// Number of bytes in the payload Data.
Size uint `json:"size"`
// Digest of the payload Data, prefixed with the algorithm, e.g. sha256:deadbeef.
Digest string `json:"digest"`
// The key to retrieve the payload from remote storage.
Key string `json:"key"`
}
type Option interface {
apply(*Codec) error
}
type applier func(*Codec) error
func (a applier) apply(c *Codec) error {
return a(c)
}
// WithURL sets the endpoint for the remote payload storage service.
// This option is mandatory.
func WithURL(u string) Option {
return applier(func(c *Codec) error {
lpsURL, err := url.Parse(u)
if err != nil {
return errors.New("invalid remote codec URL")
}
c.url = lpsURL
return nil
})
}
// WithMinBytes configures the minimum size of an event payload needed to trigger
// encoding using the large payload codec. Any payload smaller than this value
// will be transparently persisted in workflow history.
//
// The default value is 128000, or 128KB.
//
// Setting this too low can lead to degraded performance, since decoding requires
// an additional network round trip per payload. This can add up quickly when
// replaying a workflow with a large number of events.
//
// According to https://docs.temporal.io/workflows, the hard limit for workflow
// history size is 50k events and 50MB. A workflow with exactly 50k events can
// therefore in theory have an average event payload size of 1048 bytes.
//
// In practice this worst case is very unlikely, since common workflow event
// types such as WorkflowTaskScheduled or WorkflowTaskCompleted do not include user
// defined payloads. If we estimate that one quarter of events have payloads just
// below the cutoff, then we can calculate how many events total would fit in
// one workflow's history (the point before which we must call ContinueAsNew):
//
// AverageNonUserTaskBytes = 1024 (generous estimate for events like WorkflowTaskScheduled)
// CodecMinBytes = 128_000
// AverageEventBytes = (AverageNonUserTaskBytes * 3 + CodecMinBytes) / 4 = 32_768
// MaxHistoryEventBytes = 50_000_000
// MaxHistoryEventCount = MaxHistoryEventBytes / AverageEventBytes = 1525
func WithMinBytes(bytes uint32) Option {
return applier(func(c *Codec) error {
c.minBytes = int(bytes)
return nil
})
}
// WithHTTPClient sets a custom http.Client.
//
// If unspecified, http.DefaultClient will be used.
func WithHTTPClient(client *http.Client) Option {
return applier(func(c *Codec) error {
c.client = client
return nil
})
}
// WithNamespace sets the Temporal namespace the client using this codec is connected to.
// This option is mandatory.
func WithNamespace(namespace string) Option {
return applier(func(c *Codec) error {
c.namespace = namespace
return nil
})
}
// WithVersion sets the version of the LPS API to use.
func WithVersion(version string) Option {
return applier(func(c *Codec) error {
c.version = version
return nil
})
}
// WithHTTPRoundTripper sets custom Transport on the http.Client.
//
// This may be used to implement use cases including authentication or tracing.
func WithHTTPRoundTripper(rt http.RoundTripper) Option {
return applier(func(c *Codec) error {
if c.client == nil {
return fmt.Errorf("no http client option set")
}
c.client.Transport = rt
return nil
})
}
// New instantiates a Codec. WithURL is a required option.
//
// An error may be returned if incompatible options are configured or if a
// connection to the remote payload storage service cannot be established.
func New(opts ...Option) (*Codec, error) {
c := Codec{
client: http.DefaultClient,
// 128KB happens to be the lower bound for blobs eligible for AWS S3
// Intelligent-Tiering:
// https://aws.amazon.com/s3/storage-classes/intelligent-tiering/
minBytes: 128_000,
}
for _, opt := range opts {
if err := opt.apply(&c); err != nil {
return nil, err
}
}
if c.namespace == "" {
return nil, fmt.Errorf("a namespace is required")
}
// Check for required attributes
if c.client == nil {
return nil, fmt.Errorf("an http client is required")
}
if c.url == nil {
return nil, fmt.Errorf("a remote codec URL is required")
}
if c.version == "" {
c.version = "v2"
}
if c.version != "v1" && c.version != "v2" {
return nil, fmt.Errorf("invalid codec version: %s", c.version)
}
// Check connectivity
headURL := c.url.JoinPath(c.version, "health", "head")
resp, err := c.client.Head(headURL.String())
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got status code %d from storage service at %s", resp.StatusCode, headURL)
}
return &c, nil
}
func (c *Codec) Encode(payloads []*common.Payload) ([]*common.Payload, error) {
var (
ctx = context.Background()
result = make([]*common.Payload, len(payloads))
)
for i, payload := range payloads {
if payload.Size() > c.minBytes {
encodePayload, err := c.encodePayload(ctx, payload)
if err != nil {
return nil, err
}
result[i] = encodePayload
} else {
result[i] = payload
}
}
return result, nil
}
func (c *Codec) encodePayload(ctx context.Context, payload *common.Payload) (*common.Payload, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodPut,
c.url.JoinPath(c.version).String(),
bytes.NewReader(payload.GetData()),
)
if err != nil {
return nil, err
}
req.URL.Path = path.Join(req.URL.Path, "blobs/put")
sha2 := sha256.New()
sha2.Write(payload.GetData())
digest := "sha256:" + hex.EncodeToString(sha2.Sum(nil))
q := req.URL.Query()
q.Set("digest", digest)
q.Set("namespace", c.namespace)
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/octet-stream")
req.ContentLength = int64(len(payload.GetData()))
// Set metadata header
md, err := json.Marshal(payload.GetMetadata())
if err != nil {
return nil, err
}
req.Header.Set("X-Temporal-Metadata", base64.StdEncoding.EncodeToString(md))
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status code %d: %s", resp.StatusCode, respBody)
}
var key keyResponse
if err := json.Unmarshal(respBody, &key); err != nil {
return nil, fmt.Errorf("unable to unmarshal put response: %w", err)
}
result, err := converter.GetDefaultDataConverter().ToPayload(remotePayload{
Metadata: payload.GetMetadata(),
Size: uint(len(payload.GetData())),
Digest: digest,
Key: key.Key,
})
if err != nil {
return nil, err
}
result.Metadata[remoteCodecName] = []byte(c.version)
return result, nil
}
func (c *Codec) Decode(payloads []*common.Payload) ([]*common.Payload, error) {
result := make([]*common.Payload, len(payloads))
for i, payload := range payloads {
if codecVersion, ok := payload.GetMetadata()[remoteCodecName]; ok {
switch string(codecVersion) {
case "v1", "v2":
decodedPayload, err := c.decodePayload(context.Background(), payload, string(codecVersion))
if err != nil {
return nil, err
}
result[i] = decodedPayload
default:
return nil, fmt.Errorf("unknown version for %s: %s", remoteCodecName, codecVersion)
}
} else {
result[i] = payload
}
}
return result, nil
}
func (c *Codec) decodePayload(ctx context.Context, payload *common.Payload, version string) (*common.Payload, error) {
var remoteP remotePayload
if err := converter.GetDefaultDataConverter().FromPayload(payload, &remoteP); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
c.url.JoinPath(version).String(),
nil,
)
if err != nil {
return nil, err
}
req.URL.Path = path.Join(req.URL.Path, "blobs/get")
q := req.URL.Query()
if version == "v1" {
q.Set("digest", remoteP.Digest)
}
if version == "v2" {
q.Set("key", remoteP.Key)
}
req.URL.RawQuery = q.Encode()
req.Header.Set("Content-Type", "application/octet-stream")
// TODO: we temporarily need this because we aren't checking object metadata on the server
req.Header.Set("X-Payload-Expected-Content-Length", strconv.FormatUint(uint64(remoteP.Size), 10))
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("server returned status code %d: %s", resp.StatusCode, respBody)
}
sha2 := sha256.New()
tee := io.TeeReader(resp.Body, sha2)
b, err := io.ReadAll(tee)
if err != nil {
return nil, err
}
if uint(len(b)) != remoteP.Size {
return nil, fmt.Errorf("wanted object of size %d, got %d", remoteP.Size, len(b))
}
checkSum := hex.EncodeToString(sha2.Sum(nil))
if fmt.Sprintf("sha256:%s", checkSum) != remoteP.Digest {
return nil, fmt.Errorf("wanted object sha %s, got %s", remoteP.Digest, checkSum)
}
return &common.Payload{
Metadata: remoteP.Metadata,
Data: b,
}, nil
}