-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
307 lines (270 loc) · 8.14 KB
/
handler.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
//
// 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 handler
import (
"context"
"fmt"
watermillMsg "github.com/ThreeDotsLabs/watermill/message"
"github.com/insolar/insolar/configuration"
"github.com/insolar/insolar/insolar/bus"
"github.com/insolar/insolar/insolar/bus/meta"
"github.com/insolar/insolar/insolar/jet"
"github.com/insolar/insolar/insolar/payload"
"github.com/insolar/insolar/insolar/pulse"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/instracer"
"github.com/insolar/insolar/ledger/drop"
"github.com/insolar/insolar/ledger/heavy/executor"
"github.com/pkg/errors"
"github.com/insolar/insolar/ledger/heavy/proc"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/ledger/object"
)
// Handler is a base struct for heavy's methods
type Handler struct {
cfg configuration.Ledger
JetCoordinator jet.Coordinator
PCS insolar.PlatformCryptographyScheme
RecordAccessor object.RecordAccessor
RecordModifier object.RecordModifier
IndexAccessor object.IndexAccessor
IndexModifier object.IndexModifier
DropModifier drop.Modifier
PulseAccessor pulse.Accessor
JetModifier jet.Modifier
JetAccessor jet.Accessor
JetKeeper executor.JetKeeper
BackupMaker executor.BackupMaker
InitialStateReader executor.InitialStateAccessor
Sender bus.Sender
StartPulse pulse.StartPulse
PulseCalculator pulse.Calculator
JetTree jet.Storage
DropDB *drop.DB
Replicator executor.HeavyReplicator
dep *proc.Dependencies
}
// New creates a new handler.
func New(cfg configuration.Ledger) *Handler {
h := &Handler{
cfg: cfg,
}
dep := proc.Dependencies{
PassState: func(p *proc.PassState) {
p.Dep.Records = h.RecordAccessor
p.Dep.Sender = h.Sender
p.Dep.Pulses = h.PulseAccessor
},
SendCode: func(p *proc.SendCode) {
p.Dep.Sender = h.Sender
p.Dep.RecordAccessor = h.RecordAccessor
},
SendRequests: func(p *proc.SendRequests) {
p.Dep(h.Sender, h.RecordAccessor, h.IndexAccessor)
},
SendRequest: func(p *proc.SendRequest) {
p.Dep(h.RecordAccessor, h.Sender)
},
Replication: func(p *proc.Replication) {
p.Dep(
h.Replicator,
)
},
SendJet: func(p *proc.SendJet) {
p.Dep(
h.JetAccessor,
h.Sender)
},
SendIndex: func(p *proc.SendIndex) {
p.Dep(
h.IndexAccessor,
h.Sender,
)
},
SearchIndex: func(p *proc.SearchIndex) {
p.Dep(
h.IndexAccessor,
h.PulseCalculator,
h.PulseAccessor,
h.Sender,
)
},
SendInitialState: func(p *proc.SendInitialState) {
p.Dep(
h.StartPulse,
h.JetKeeper,
h.InitialStateReader,
h.PulseAccessor,
h.Sender,
)
},
}
h.dep = &dep
return h
}
func (h *Handler) Process(msg *watermillMsg.Message) error {
ctx := inslogger.ContextWithTrace(context.Background(), msg.Metadata.Get(meta.TraceID))
parentSpan, err := instracer.Deserialize([]byte(msg.Metadata.Get(meta.SpanData)))
if err == nil {
ctx = instracer.WithParentSpan(ctx, parentSpan)
} else {
inslogger.FromContext(ctx).Error(err)
}
for k, v := range msg.Metadata {
if k == meta.SpanData || k == meta.TraceID {
continue
}
ctx, _ = inslogger.WithField(ctx, k, v)
}
logger := inslogger.FromContext(ctx)
metaPayload := payload.Meta{}
err = metaPayload.Unmarshal(msg.Payload)
if err != nil {
logger.Error(errors.Wrap(err, "failed to unmarshal payload"))
return nil
}
err = h.handle(ctx, metaPayload)
if err != nil {
logger.Error(errors.Wrap(err, "handle error"))
}
return nil
}
func (h *Handler) handle(ctx context.Context, meta payload.Meta) error {
var err error
logger := inslogger.FromContext(ctx)
payloadType, err := payload.UnmarshalType(meta.Payload)
if err != nil {
logger.Error(err)
return errors.Wrap(err, "failed to unmarshal payload type")
}
ctx, _ = inslogger.WithField(ctx, "msg_type", payloadType.String())
ctx, span := instracer.StartSpan(ctx, payloadType.String())
defer span.End()
switch payloadType {
case payload.TypeGetRequest:
p := proc.NewSendRequest(meta)
h.dep.SendRequest(p)
err = p.Proceed(ctx)
case payload.TypeGetFilament:
p := proc.NewSendRequests(meta)
h.dep.SendRequests(p)
err = p.Proceed(ctx)
case payload.TypePassState:
p := proc.NewPassState(meta)
h.dep.PassState(p)
err = p.Proceed(ctx)
case payload.TypeGetCode:
p := proc.NewSendCode(meta)
h.dep.SendCode(p)
err = p.Proceed(ctx)
case payload.TypeReplication:
p := proc.NewReplication(meta, h.cfg)
h.dep.Replication(p)
err = p.Proceed(ctx)
case payload.TypeGetJet:
p := proc.NewSendJet(meta)
h.dep.SendJet(p)
err = p.Proceed(ctx)
case payload.TypeGetIndex:
p := proc.NewSendIndex(meta)
h.dep.SendIndex(p)
err = p.Proceed(ctx)
case payload.TypeSearchIndex:
p := proc.NewSearchIndex(meta)
h.dep.SearchIndex(p)
err = p.Proceed(ctx)
case payload.TypePass:
err = h.handlePass(ctx, meta)
case payload.TypeError:
h.handleError(ctx, meta)
case payload.TypeGotHotConfirmation:
h.handleGotHotConfirmation(ctx, meta)
case payload.TypeGetLightInitialState:
p := proc.NewSendInitialState(meta)
h.dep.SendInitialState(p)
err = p.Proceed(ctx)
default:
err = fmt.Errorf("no handler for message type %s", payloadType.String())
}
if err != nil {
instracer.AddError(span, err)
bus.ReplyError(ctx, h.Sender, meta, err)
}
return err
}
func (h *Handler) handleError(ctx context.Context, msg payload.Meta) {
logger := inslogger.FromContext(ctx)
pl := payload.Error{}
err := pl.Unmarshal(msg.Payload)
if err != nil {
logger.Error(errors.Wrap(err, "failed to unmarshal error"))
return
}
logger.Error("received error: ", pl.Text)
}
func (h *Handler) handlePass(ctx context.Context, meta payload.Meta) error {
pass := payload.Pass{}
err := pass.Unmarshal(meta.Payload)
if err != nil {
return errors.Wrap(err, "failed to unmarshal pass payload")
}
originMeta := payload.Meta{}
err = originMeta.Unmarshal(pass.Origin)
if err != nil {
return errors.Wrap(err, "failed to unmarshal origin message")
}
payloadType, err := payload.UnmarshalType(originMeta.Payload)
if err != nil {
return errors.Wrap(err, "failed to unmarshal payload type")
}
ctx, _ = inslogger.WithField(ctx, "msg_type_original", payloadType.String())
switch payloadType { // nolint
case payload.TypeGetCode:
p := proc.NewSendCode(originMeta)
h.dep.SendCode(p)
err = p.Proceed(ctx)
case payload.TypeGetRequest:
p := proc.NewSendRequest(originMeta)
h.dep.SendRequest(p)
err = p.Proceed(ctx)
default:
err = fmt.Errorf("no pass handler for message type %s", payloadType.String())
}
if err != nil {
bus.ReplyError(ctx, h.Sender, originMeta, err)
}
return err
}
func (h *Handler) Init(ctx context.Context) error {
return nil
}
func (h *Handler) handleGotHotConfirmation(ctx context.Context, meta payload.Meta) {
logger := inslogger.FromContext(ctx)
logger.Info("handleGotHotConfirmation got new message")
confirm := payload.GotHotConfirmation{}
err := confirm.Unmarshal(meta.Payload)
if err != nil {
logger.Error(errors.Wrap(err, "failed to unmarshal to GotHotConfirmation"))
return
}
logger.Info("handleGotHotConfirmation. pulse: ", confirm.Pulse, ". jet: ", confirm.JetID.DebugString(), ". Split: ", confirm.Split)
err = h.JetKeeper.AddHotConfirmation(ctx, confirm.Pulse, confirm.JetID, confirm.Split)
if err != nil {
logger.Fatalf("failed to add hot confirmation jet=%v: %v", confirm.String(), err.Error())
}
executor.FinalizePulse(ctx, h.PulseCalculator, h.BackupMaker, h.JetKeeper, h.IndexModifier, confirm.Pulse)
logger.Info("handleGotHotConfirmation finish. pulse: ", confirm.Pulse, ". jet: ", confirm.JetID.DebugString(), ". Split: ", confirm.Split)
}