-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_pending_finished.go
92 lines (77 loc) · 2.67 KB
/
handle_pending_finished.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// 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 logicrunner
import (
"context"
"github.com/pkg/errors"
"go.opencensus.io/stats"
"github.com/insolar/insolar/insolar/bus"
"github.com/insolar/insolar/insolar/flow"
"github.com/insolar/insolar/insolar/payload"
"github.com/insolar/insolar/insolar/reply"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/logicrunner/metrics"
"github.com/insolar/insolar/logicrunner/writecontroller"
)
func checkPayloadPendingFinished(finished payload.PendingFinished) error {
if finished.ObjectRef.IsEmpty() {
return errors.New("Got PendingFinished message, but field ObjectRef is empty")
}
if !finished.ObjectRef.IsObjectReference() {
return errors.Errorf("PendingFinished.ObjectRef should be RecordReference; ref=%s", finished.ObjectRef.String())
}
return nil
}
type HandlePendingFinished struct {
dep *Dependencies
Message payload.Meta
}
func (h *HandlePendingFinished) Present(ctx context.Context, _ flow.Flow) error {
message := payload.PendingFinished{}
err := message.Unmarshal(h.Message.Payload)
if err != nil {
return errors.Wrap(err, "failed to unmarshal message")
}
ctx, logger := inslogger.WithFields(ctx, map[string]interface{}{
"object": message.ObjectRef.String(),
"sender": h.Message.Sender.String(),
})
logger.Debug("handle PendingFinished message")
if err := checkPayloadPendingFinished(message); err != nil {
return err
}
done, err := h.dep.WriteAccessor.Begin(ctx, flow.Pulse(ctx))
if err != nil {
if err == writecontroller.ErrWriteClosed {
return flow.ErrCancelled
}
return errors.Wrap(err, "failed to acquire write access")
}
defer done()
broker := h.dep.StateStorage.UpsertExecutionState(message.ObjectRef)
err = broker.PrevExecutorSentPendingFinished(ctx)
if err != nil {
if err == ErrAlreadyExecuting {
stats.Record(ctx, metrics.PendingFinishedAlreadyExecuting.M(1))
}
err = errors.Wrap(err, "handle PendingFinished failed")
inslogger.FromContext(ctx).Error(err.Error())
return err
}
replyOk := bus.ReplyAsMessage(ctx, &reply.OK{})
h.dep.Sender.Reply(ctx, h.Message, replyOk)
return nil
}