This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
/
worker_commands.go
241 lines (208 loc) · 5.4 KB
/
worker_commands.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
// Copyright 2017, OpenCensus 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.
//
package stats
import (
"fmt"
"time"
"go.opencensus.io/tag"
)
type command interface {
handleCommand(w *worker)
}
// getMeasureByNameReq is the command to get a measure given its name.
type getMeasureByNameReq struct {
name string
c chan *getMeasureByNameResp
}
type getMeasureByNameResp struct {
m Measure
}
func (cmd *getMeasureByNameReq) handleCommand(w *worker) {
ref, ok := w.measures[cmd.name]
if ok {
cmd.c <- &getMeasureByNameResp{ref.measure}
return
}
cmd.c <- &getMeasureByNameResp{nil}
}
// registerMeasureReq is the command to register a measure with the library.
type registerMeasureReq struct {
m Measure
err chan error
}
func (cmd *registerMeasureReq) handleCommand(w *worker) {
cmd.err <- w.tryRegisterMeasure(cmd.m)
}
// deleteMeasureReq is the command to delete a measure from the library.
type deleteMeasureReq struct {
m Measure
err chan error
}
func (cmd *deleteMeasureReq) handleCommand(w *worker) {
ref, ok := w.measures[cmd.m.Name()]
if !ok {
cmd.err <- nil
return
}
if ref.measure != cmd.m {
cmd.err <- nil
return
}
if c := len(ref.views); c > 0 {
cmd.err <- fmt.Errorf("cannot delete; measure %q used by %v registered views", cmd.m.Name(), c)
return
}
delete(w.measures, cmd.m.Name())
cmd.err <- nil
}
// getViewByNameReq is the command to get a view given its name.
type getViewByNameReq struct {
name string
c chan *getViewByNameResp
}
type getViewByNameResp struct {
v *View
}
func (cmd *getViewByNameReq) handleCommand(w *worker) {
cmd.c <- &getViewByNameResp{w.views[cmd.name]}
}
// registerViewReq is the command to register a view with the library.
type registerViewReq struct {
v *View
err chan error
}
func (cmd *registerViewReq) handleCommand(w *worker) {
cmd.err <- w.tryRegisterView(cmd.v)
}
// unregisterViewReq is the command to unregister a view from the library.
type unregisterViewReq struct {
v *View
err chan error
}
func (cmd *unregisterViewReq) handleCommand(w *worker) {
v, ok := w.views[cmd.v.Name()]
if !ok {
cmd.err <- nil
return
}
if v != cmd.v {
cmd.err <- nil
return
}
if v.isSubscribed() {
cmd.err <- fmt.Errorf("cannot unregister view %q; all subscriptions must be unsubscribed first", cmd.v.Name())
return
}
delete(w.views, cmd.v.Name())
ref := w.measures[v.Measure().Name()]
delete(ref.views, v)
cmd.err <- nil
}
// subscribeToViewReq is the command to subscribe to a view.
type subscribeToViewReq struct {
v *View
err chan error
}
func (cmd *subscribeToViewReq) handleCommand(w *worker) {
if cmd.v.isSubscribed() {
cmd.err <- nil
return
}
if err := w.tryRegisterView(cmd.v); err != nil {
cmd.err <- fmt.Errorf("cannot subscribe to view: %v", err)
return
}
cmd.v.subscribe()
cmd.err <- nil
}
// unsubscribeFromViewReq is the command to unsubscribe to a view. Has no
// impact on the data collection for client that are pulling data from the
// library.
type unsubscribeFromViewReq struct {
v *View
err chan error
}
func (cmd *unsubscribeFromViewReq) handleCommand(w *worker) {
cmd.v.unsubscribe()
if !cmd.v.isSubscribed() {
// this was the last subscription and view is not collecting anymore.
// The collected data can be cleared.
cmd.v.clearRows()
}
// we always return nil because this operation never fails. However we
// still need to return something on the channel to signal to the waiting
// go routine that the operation completed.
cmd.err <- nil
}
// retrieveDataReq is the command to retrieve data for a view.
type retrieveDataReq struct {
now time.Time
v *View
c chan *retrieveDataResp
}
type retrieveDataResp struct {
rows []*Row
err error
}
func (cmd *retrieveDataReq) handleCommand(w *worker) {
if _, ok := w.views[cmd.v.Name()]; !ok {
cmd.c <- &retrieveDataResp{
nil,
fmt.Errorf("cannot retrieve data; view %q is not registered", cmd.v.Name()),
}
return
}
if !cmd.v.isSubscribed() {
cmd.c <- &retrieveDataResp{
nil,
fmt.Errorf("cannot retrieve data; view %q has no subscriptions or collection is not forcibly started", cmd.v.Name()),
}
return
}
cmd.c <- &retrieveDataResp{
cmd.v.collectedRows(cmd.now),
nil,
}
}
// recordReq is the command to record data related to multiple measures
// at once.
type recordReq struct {
now time.Time
tm *tag.Map
ms []Measurement
}
func (cmd *recordReq) handleCommand(w *worker) {
for _, m := range cmd.ms {
ref := w.measures[m.m.Name()]
for v := range ref.views {
v.addSample(cmd.tm, m.v, cmd.now)
}
}
}
// setReportingPeriodReq is the command to modify the duration between
// reporting the collected data to the subscribed clients.
type setReportingPeriodReq struct {
d time.Duration
c chan bool
}
func (cmd *setReportingPeriodReq) handleCommand(w *worker) {
w.timer.Stop()
if cmd.d <= 0 {
w.timer = time.NewTicker(defaultReportingDuration)
} else {
w.timer = time.NewTicker(cmd.d)
}
cmd.c <- true
}