forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
114 lines (103 loc) · 3.48 KB
/
feed.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
// Copyright 2015 The Cockroach Authors.
//
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Matt Tracy (matt@cockroachlabs.com)
package status
import (
"github.com/cockroachdb/cockroach/proto"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/tracer"
)
// StartNodeEvent is published when a node is started.
type StartNodeEvent struct {
Desc proto.NodeDescriptor
StartedAt int64
}
// CallSuccessEvent is published when a call to a node completes without error.
type CallSuccessEvent struct {
NodeID proto.NodeID
Method proto.Method
}
// CallErrorEvent is published when a call to a node returns an error.
type CallErrorEvent struct {
NodeID proto.NodeID
Method proto.Method
}
// NodeEventFeed is a helper structure which publishes node-specific events to a
// util.Feed. If the target feed is nil, event methods become no-ops.
type NodeEventFeed struct {
id proto.NodeID
f *util.Feed
}
// NewNodeEventFeed creates a new NodeEventFeed which publishes events for a
// specific node to the supplied feed.
func NewNodeEventFeed(id proto.NodeID, feed *util.Feed) NodeEventFeed {
return NodeEventFeed{
id: id,
f: feed,
}
}
// StartNode is called by a node when it has started.
func (nef NodeEventFeed) StartNode(desc proto.NodeDescriptor, startedAt int64) {
nef.f.Publish(&StartNodeEvent{
Desc: desc,
StartedAt: startedAt,
})
}
// CallComplete is called by a node whenever it completes a request. This will
// publish an appropriate event to the feed based on the results of the call.
// TODO(tschottdorf): move to batch, account for multiple methods per batch.
// In particular, on error want an error position to identify the failed
// request.
func (nef NodeEventFeed) CallComplete(args proto.Request, reply proto.Response) {
method := args.Method()
if ba, ok := args.(*proto.BatchRequest); ok && len(ba.Requests) > 0 {
method = ba.Requests[0].GetValue().(proto.Request).Method()
}
if err := reply.Header().Error; err != nil &&
err.CanRestartTransaction() == proto.TransactionRestart_ABORT {
nef.f.Publish(&CallErrorEvent{
NodeID: nef.id,
Method: method,
})
} else {
nef.f.Publish(&CallSuccessEvent{
NodeID: nef.id,
Method: method,
})
}
}
// NodeEventListener is an interface that can be implemented by objects which
// listen for events published by nodes.
type NodeEventListener interface {
OnStartNode(event *StartNodeEvent)
OnCallSuccess(event *CallSuccessEvent)
OnCallError(event *CallErrorEvent)
// TODO(tschottdorf): break this out into a TraceEventListener.
OnTrace(event *tracer.Trace)
}
// ProcessNodeEvent dispatches an event on the NodeEventListener.
func ProcessNodeEvent(l NodeEventListener, event interface{}) {
switch specificEvent := event.(type) {
case *StartNodeEvent:
l.OnStartNode(specificEvent)
case *tracer.Trace:
l.OnTrace(specificEvent)
case *CallSuccessEvent:
l.OnCallSuccess(specificEvent)
case *CallErrorEvent:
l.OnCallError(specificEvent)
}
}