-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathclient_request.go
More file actions
431 lines (357 loc) · 15 KB
/
client_request.go
File metadata and controls
431 lines (357 loc) · 15 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package request
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"sort"
"sync"
"time"
"google.golang.org/protobuf/proto"
ragep2ptypes "github.com/smartcontractkit/libocr/ragep2p/types"
"github.com/smartcontractkit/chainlink-common/pkg/beholder"
commoncap "github.com/smartcontractkit/chainlink-common/pkg/capabilities"
caperrors "github.com/smartcontractkit/chainlink-common/pkg/capabilities/errors"
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-protos/workflows/go/events"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/remote"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/transmission"
"github.com/smartcontractkit/chainlink/v2/core/capabilities/validation"
p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types"
)
// errRemoteCapabilityExecuteError preserves the legacy "TRANSPORT : ErrorMsg" string from the
// remote executable client while wrapping a deserialized caperrors.Error so callers can
// errors.As into caperrors.Error after RPC (see capability_executor metrics).
type errRemoteCapabilityExecuteError struct {
s string
wrap caperrors.Error
}
func (e *errRemoteCapabilityExecuteError) Error() string { return e.s }
func (e *errRemoteCapabilityExecuteError) Unwrap() error { return e.wrap }
func newRemoteCapabilityExecuteError(transportErr types.Error, errMsg string) error {
return &errRemoteCapabilityExecuteError{
s: fmt.Sprintf("%s : %s", transportErr, errMsg),
wrap: caperrors.DeserializeErrorFromString(errMsg),
}
}
func newRemoteCapabilityExecuteErrorWithMessage(display string, errMsg string) error {
return &errRemoteCapabilityExecuteError{
s: display,
wrap: caperrors.DeserializeErrorFromString(errMsg),
}
}
type clientResponse struct {
Result []byte
Err error
}
type ClientRequest struct {
id string
cancelFn context.CancelFunc
responseCh chan clientResponse
createdAt time.Time
responseIDCount map[[32]byte]int
meteringResponses map[[32]byte][]commoncap.MeteringNodeDetail
errorCount map[string]int
totalErrorCount int
responseReceived map[p2ptypes.PeerID]bool
lggr logger.Logger
requiredIdenticalResponses int
remoteNodeCount int
requestTimeout time.Duration
respSent bool
mux sync.Mutex
wg *sync.WaitGroup
}
// TransmissionConfig has to be set only for V2 capabilities. V1 capabilities read transmission schedule from every request.
func NewClientExecuteRequest(ctx context.Context, lggr logger.Logger, req commoncap.CapabilityRequest,
remoteCapabilityInfo commoncap.CapabilityInfo, localDonInfo commoncap.DON, dispatcher types.Dispatcher,
requestTimeout time.Duration, transmissionConfig *transmission.TransmissionConfig, capMethodName string,
) (*ClientRequest, error) {
rawRequest, err := proto.MarshalOptions{Deterministic: true}.Marshal(pb.CapabilityRequestToProto(req))
if err != nil {
return nil, fmt.Errorf("failed to marshal capability request: %w", err)
}
workflowExecutionID := req.Metadata.WorkflowExecutionID
if err = validation.ValidateWorkflowOrExecutionID(workflowExecutionID); err != nil {
return nil, fmt.Errorf("workflow execution ID is invalid: %w", err)
}
// the requestID must be delineated by the workflow execution ID and the reference ID
// to ensure that it supports parallel step execution
requestID := types.MethodExecute + ":" + workflowExecutionID + ":" + req.Metadata.ReferenceID
var tc transmission.TransmissionConfig
if transmissionConfig != nil {
// all v2 capabilities should be all at once
tc = transmission.TransmissionConfig{
Schedule: transmission.Schedule_AllAtOnce,
}
} else { // per-workflow setting used by V1 Capabilities
tc, err = transmission.ExtractTransmissionConfig(req.Config)
if err != nil {
return nil, fmt.Errorf("failed to extract transmission config from request: %w", err)
}
}
lggr = logger.With(lggr, "requestId", requestID) // cap ID and method name included in the parent logger
return newClientRequest(ctx, lggr, requestID, remoteCapabilityInfo, localDonInfo, dispatcher, requestTimeout, tc, types.MethodExecute, rawRequest, workflowExecutionID, req.Metadata.ReferenceID, capMethodName)
}
var defaultDelayMargin = 10 * time.Second
func newClientRequest(ctx context.Context, lggr logger.Logger, requestID string, remoteCapabilityInfo commoncap.CapabilityInfo,
localDonInfo commoncap.DON, dispatcher types.Dispatcher, requestTimeout time.Duration,
tc transmission.TransmissionConfig, methodType string, rawRequest []byte, workflowExecutionID string, stepRef string, capMethodName string,
) (*ClientRequest, error) {
remoteCapabilityDonInfo := remoteCapabilityInfo.DON
if remoteCapabilityDonInfo == nil {
return nil, errors.New("remote capability info missing DON")
}
peerIDToTransmissionDelay, err := transmission.GetPeerIDToTransmissionDelaysForConfig(remoteCapabilityDonInfo.Members, requestID, tc)
if err != nil {
return nil, fmt.Errorf("failed to get peer ID to transmission delay: %w", err)
}
// send schedule through beholder for single execution performance tracking
err = emitTransmissionScheduleEvent(ctx,
tc.Schedule,
workflowExecutionID,
requestID,
remoteCapabilityInfo.ID,
stepRef,
peerIDToTransmissionDelay,
)
if err != nil {
lggr.Errorw("failed to emit transmission schedule event", "error", err)
}
responseReceived := make(map[p2ptypes.PeerID]bool)
maxDelayDuration := time.Duration(0)
for _, delay := range peerIDToTransmissionDelay {
if delay > maxDelayDuration {
maxDelayDuration = delay
}
}
// Add some margin to allow the last peer to respond
maxDelayDuration += defaultDelayMargin
// Instantiate a new context based on the parent, but without its deadline.
// We set a new deadline instead equal to the original timeout OR the full length
// of the execution schedule plus some margin, whichever is greater
// We do this to ensure that we will always execute the entire transmission schedule.
// This ensures that all capability DON nodes will receive a quorum of requests,
// and will execute all requests they receive from the workflow DON, preventing
// quorum errors from lagging members of the workflow DON.
dl, ok := ctx.Deadline()
originalTimeout := time.Duration(0)
if ok {
originalTimeout = time.Until(dl)
}
effectiveTimeout := max(originalTimeout, maxDelayDuration)
// Now let's create a new context based on the adjusted timeout value.
// By calling WithoutCancel, we ensure that this context can only be cancelled in
// one of two ways -- 1) by explicitly calling the cancelFn we create below, or 2)
// after the adjusted timeout expires.
ctxWithoutCancel := context.WithoutCancel(ctx)
ctxWithCancel, cancelFn := context.WithTimeout(ctxWithoutCancel, effectiveTimeout)
lggr.Debugw("sending request to peers", "schedule", peerIDToTransmissionDelay, "originalTimeout", originalTimeout, "effectiveTimeout", effectiveTimeout)
var wg sync.WaitGroup
for peerID, delay := range peerIDToTransmissionDelay {
responseReceived[peerID] = false
wg.Add(1)
go func(innerCtx context.Context, peerID ragep2ptypes.PeerID, delay time.Duration) {
defer wg.Done()
message := &types.MessageBody{
CapabilityId: remoteCapabilityInfo.ID,
CapabilityDonId: remoteCapabilityDonInfo.ID,
CallerDonId: localDonInfo.ID,
Method: methodType,
Payload: rawRequest,
MessageId: []byte(requestID),
CapabilityMethod: capMethodName,
}
select {
case <-innerCtx.Done():
lggr.Debugw("context done, not sending request to peer", "peerID", peerID)
return
case <-time.After(delay):
lggr.Debugw("sending request to peer", "peerID", peerID)
err := dispatcher.Send(peerID, message)
if err != nil {
lggr.Errorw("failed to send message", "peerID", peerID, "error", err)
}
}
}(ctxWithCancel, peerID, delay)
}
return &ClientRequest{
id: requestID,
cancelFn: cancelFn,
createdAt: time.Now(),
requestTimeout: requestTimeout,
requiredIdenticalResponses: int(remoteCapabilityDonInfo.F + 1),
remoteNodeCount: len(remoteCapabilityDonInfo.Members),
responseIDCount: make(map[[32]byte]int),
meteringResponses: make(map[[32]byte][]commoncap.MeteringNodeDetail),
errorCount: make(map[string]int),
responseReceived: responseReceived,
responseCh: make(chan clientResponse, 1),
wg: &wg,
lggr: lggr,
}, nil
}
func emitTransmissionScheduleEvent(ctx context.Context, scheduleType, workflowExecutionID, transmissionID, capabilityID, stepRef string, peerIDToTransmissionDelay map[p2ptypes.PeerID]time.Duration) error {
// Create a slice of peer IDs sorted by their delay values
type peerDelay struct {
peerID p2ptypes.PeerID
delay time.Duration
}
peerDelays := make([]peerDelay, 0, len(peerIDToTransmissionDelay))
for peerID, delay := range peerIDToTransmissionDelay {
peerDelays = append(peerDelays, peerDelay{peerID, delay})
}
// Sort by delay value
sort.Slice(peerDelays, func(i, j int) bool {
return peerDelays[i].delay < peerDelays[j].delay
})
// Create map with sorted peers and their delays in milliseconds
peerDelaysMap := make(map[string]int64, len(peerDelays))
for _, pd := range peerDelays {
peerDelaysMap[pd.peerID.String()] = pd.delay.Milliseconds()
}
msg := &events.TransmissionsScheduledEvent{
Timestamp: time.Now().Format(time.RFC3339),
ScheduleType: scheduleType,
WorkflowExecutionID: workflowExecutionID,
TransmissionID: transmissionID,
CapabilityID: capabilityID,
StepRef: stepRef,
PeerTransmissionDelays: peerDelaysMap,
}
b, err := proto.Marshal(msg)
if err != nil {
return fmt.Errorf("failed to marshal TransmissionScheduleEvent: %w", err)
}
// emit transmission schedule event to track which nodes are successful when called to emit
return beholder.GetEmitter().Emit(ctx, b,
"beholder_data_schema", TransmissionEventSchema, // required
"beholder_domain", "platform", // required
"beholder_entity", fmt.Sprintf("%s.%s", TransmissionEventProtoPkg, TransmissionEventEntity)) // required
}
func (c *ClientRequest) ID() string {
return c.id
}
func (c *ClientRequest) ResponseChan() <-chan clientResponse {
return c.responseCh
}
func (c *ClientRequest) Expired() bool {
return time.Since(c.createdAt) > c.requestTimeout
}
func (c *ClientRequest) Cancel(err error) {
c.cancelFn()
c.wg.Wait()
c.mux.Lock()
defer c.mux.Unlock()
if !c.respSent {
c.sendResponse(clientResponse{Err: err})
}
}
func (c *ClientRequest) OnMessage(_ context.Context, msg *types.MessageBody) error {
c.mux.Lock()
defer c.mux.Unlock()
if c.respSent {
return nil
}
if msg.Sender == nil {
return errors.New("sender missing from message")
}
c.lggr.Debugw("OnMessage called for client request")
sender, err := remote.ToPeerID(msg.Sender)
if err != nil {
return fmt.Errorf("failed to convert message sender to PeerID: %w", err)
}
received, expected := c.responseReceived[sender]
if !expected {
return fmt.Errorf("response from peer %s not expected", sender)
}
if received {
return fmt.Errorf("response from peer %s already received", sender)
}
c.responseReceived[sender] = true
if msg.Error == types.Error_OK {
// metering reports per node are aggregated into a single array of values. for any single node message, the
// metering values are extracted from the CapabilityResponse, added to an array, and the CapabilityResponse
// is marshalled without the metering value to get the hash. each node could have a different metering value
// which would result in different hashes. removing the metering detail allows for direct comparison of results.
responseID, metadata, err := c.getMessageHashAndMetadata(msg)
if err != nil {
return fmt.Errorf("failed to get message hash: %w", err)
}
lggr := logger.With(c.lggr, "responseID", hex.EncodeToString(responseID[:]), "requiredCount", c.requiredIdenticalResponses, "peer", sender)
nodeReports, exists := c.meteringResponses[responseID]
if !exists {
nodeReports = make([]commoncap.MeteringNodeDetail, 0)
}
if len(metadata.Metering) == 1 {
rpt := metadata.Metering[0]
rpt.Peer2PeerID = sender.String()
nodeReports = append(nodeReports, rpt)
} else {
lggr.Warnw("node metering detail did not contain exactly 1 record", "records", len(metadata.Metering))
}
c.responseIDCount[responseID]++
c.meteringResponses[responseID] = nodeReports
if len(c.responseIDCount) > 1 {
lggr.Warnw("received multiple unique responses for the same request", "count for responseID", len(c.responseIDCount))
}
if c.responseIDCount[responseID] == c.requiredIdenticalResponses {
payload, err := c.encodePayloadWithMetadata(msg, commoncap.ResponseMetadata{Metering: nodeReports})
if err != nil {
return fmt.Errorf("failed to encode payload with metadata: %w", err)
}
c.sendResponse(clientResponse{Result: payload})
}
} else {
c.lggr.Debugw("received error from peer", "error", msg.Error, "errorMsg", msg.ErrorMsg, "peer", sender)
c.errorCount[msg.ErrorMsg]++
c.totalErrorCount++
if len(c.errorCount) > 1 {
c.lggr.Warnw("received multiple different errors for the same request", "numDifferentErrors", len(c.errorCount))
}
if c.errorCount[msg.ErrorMsg] == c.requiredIdenticalResponses {
c.sendResponse(clientResponse{Err: newRemoteCapabilityExecuteError(msg.Error, msg.ErrorMsg)})
} else if c.totalErrorCount == c.remoteNodeCount-c.requiredIdenticalResponses+1 {
c.sendResponse(clientResponse{Err: newRemoteCapabilityExecuteErrorWithMessage(
fmt.Sprintf("received %d errors, last error %s : %s", c.totalErrorCount, msg.Error, msg.ErrorMsg),
msg.ErrorMsg,
)})
}
}
return nil
}
func (c *ClientRequest) sendResponse(response clientResponse) {
c.responseCh <- response
close(c.responseCh)
c.respSent = true
if response.Err != nil {
c.lggr.Warnw("received error response", "error", remote.SanitizeLogString(response.Err.Error()))
return
}
c.lggr.Debugw("received OK response")
}
func (c *ClientRequest) getMessageHashAndMetadata(msg *types.MessageBody) ([32]byte, commoncap.ResponseMetadata, error) {
var metadata commoncap.ResponseMetadata
resp, err := pb.UnmarshalCapabilityResponse(msg.Payload)
if err != nil {
return [32]byte{}, metadata, err
}
metadata = resp.Metadata
resp.Metadata = commoncap.ResponseMetadata{}
payload, err := pb.MarshalCapabilityResponse(resp)
if err != nil {
return [32]byte{}, metadata, err
}
return sha256.Sum256(payload), metadata, nil
}
func (c *ClientRequest) encodePayloadWithMetadata(msg *types.MessageBody, metadata commoncap.ResponseMetadata) ([]byte, error) {
resp, err := pb.UnmarshalCapabilityResponse(msg.Payload)
if err != nil {
return nil, err
}
resp.Metadata = metadata
return pb.MarshalCapabilityResponse(resp)
}