-
Notifications
You must be signed in to change notification settings - Fork 455
/
agent.go
354 lines (309 loc) · 9.41 KB
/
agent.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
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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package agent
import (
"fmt"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/client"
"github.com/m3db/m3/src/m3nsch"
"github.com/m3db/m3/src/m3nsch/datums"
"github.com/m3db/m3x/ident"
"github.com/m3db/m3x/instrument"
xlog "github.com/m3db/m3x/log"
xtime "github.com/m3db/m3x/time"
)
var (
errCannotStartNotInitialized = fmt.Errorf("unable to start, agent is not initialized")
errCannotStopNotInitialized = fmt.Errorf("unable to stop, agent is not initialized")
errAlreadyInitialized = fmt.Errorf("unable to initialize, agent already initialized")
)
type m3nschAgent struct {
sync.RWMutex
token string // workload token
workload m3nsch.Workload // workload to operate upon
registry datums.Registry // workload fake metric registry
session client.Session // m3db session to operate upon
agentStatus m3nsch.Status // agent status
opts m3nsch.AgentOptions // agent options
logger xlog.Logger // logger
metrics agentMetrics // agent performance metrics
workerChans workerChannels // worker-idx -> channel for worker notification
workerWg sync.WaitGroup // used to track when workers are finished
params workerParams // worker params
}
type workerParams struct {
sync.RWMutex
fn workerFn // workerFn (read|write)
workingSet []generatedMetric // metrics corresponding to workload
ranges []workerRange // worker-idx -> workingSet idx range
}
// New returns a new Agent.
func New(
registry datums.Registry,
opts m3nsch.AgentOptions,
) m3nsch.Agent {
ms := &m3nschAgent{
registry: registry,
opts: opts,
logger: opts.InstrumentOptions().Logger(),
params: workerParams{
fn: workerWriteFn,
},
}
ms.metrics = agentMetrics{
writeMethodMetrics: ms.newMethodMetrics("write"),
}
return ms
}
func newWorkerChannels(numWorkers int) workerChannels {
var chans workerChannels
for i := 0; i < numWorkers; i++ {
chans = append(chans, make(chan workerNotification))
}
return chans
}
func (ms *m3nschAgent) closeWorkerChannelsWithLock() {
ms.workerChans.close()
ms.workerChans = nil
}
func (ms *m3nschAgent) notifyWorkersWithLock(msg workerNotification) {
ms.workerChans.notify(msg)
}
func (ms *m3nschAgent) resetWithLock() {
if ms.workerChans != nil {
ms.closeWorkerChannelsWithLock()
}
if ms.session != nil {
ms.session.Close()
ms.session = nil
}
ms.workload = m3nsch.Workload{}
ms.agentStatus = m3nsch.StatusUninitialized
ms.params.workingSet = nil
ms.params.ranges = nil
}
func (ms *m3nschAgent) setWorkerParams(workload m3nsch.Workload) {
ms.params.Lock()
defer ms.params.Unlock()
var (
current = ms.params.workingSet
cardinality = workload.Cardinality
)
if len(current) > cardinality {
current = current[:cardinality]
}
for i := len(current); i < cardinality; i++ {
idx := workload.MetricStartIdx + i
current = append(current, generatedMetric{
name: fmt.Sprintf("%v.m%d", workload.MetricPrefix, idx),
timeseries: ms.registry.Get(i),
})
}
ms.params.workingSet = current
concurrency := ms.opts.Concurrency()
numMetricsPerWorker := len(current) / concurrency
workerRanges := make([]workerRange, concurrency)
for i := 0; i < concurrency; i++ {
workerRanges[i] = workerRange{
startIdx: i * numMetricsPerWorker,
endIdx: (i+1)*numMetricsPerWorker - 1,
lastIdx: -1,
}
}
ms.params.ranges = workerRanges
}
func (ms *m3nschAgent) Status() m3nsch.AgentStatus {
ms.RLock()
defer ms.RUnlock()
return m3nsch.AgentStatus{
Status: ms.agentStatus,
Token: ms.token,
}
}
func (ms *m3nschAgent) Workload() m3nsch.Workload {
ms.RLock()
defer ms.RUnlock()
return ms.workload
}
func (ms *m3nschAgent) SetWorkload(w m3nsch.Workload) {
ms.Lock()
defer ms.Unlock()
ms.workload = w
ms.setWorkerParams(w)
if ms.agentStatus == m3nsch.StatusRunning {
ms.notifyWorkersWithLock(workerNotification{update: true})
}
}
func (ms *m3nschAgent) Init(
token string,
w m3nsch.Workload,
force bool,
targetZone string,
targetEnv string,
) error {
ms.Lock()
defer ms.Unlock()
status := ms.agentStatus
if status != m3nsch.StatusUninitialized && !force {
return errAlreadyInitialized
}
if status == m3nsch.StatusRunning {
if err := ms.stopWithLock(); err != nil {
return err
}
}
session, err := ms.opts.NewSessionFn()(targetZone, targetEnv)
if err != nil {
return err
}
ms.session = session
ms.token = token
ms.workload = w
ms.setWorkerParams(w)
ms.agentStatus = m3nsch.StatusInitialized
return nil
}
func (ms *m3nschAgent) Start() error {
ms.Lock()
defer ms.Unlock()
if ms.agentStatus != m3nsch.StatusInitialized {
return errCannotStartNotInitialized
}
concurrency := ms.opts.Concurrency()
ms.workerChans = newWorkerChannels(concurrency)
ms.agentStatus = m3nsch.StatusRunning
ms.workerWg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go ms.runWorker(i, ms.workerChans[i])
}
return nil
}
func (ms *m3nschAgent) stopWithLock() error {
status := ms.agentStatus
if status == m3nsch.StatusUninitialized {
return errCannotStopNotInitialized
}
if status == m3nsch.StatusRunning {
ms.notifyWorkersWithLock(workerNotification{stop: true})
ms.workerWg.Wait()
}
ms.resetWithLock()
return nil
}
func (ms *m3nschAgent) Stop() error {
ms.Lock()
defer ms.Unlock()
return ms.stopWithLock()
}
func (ms *m3nschAgent) MaxQPS() int64 {
return int64(ms.opts.Concurrency()) * ms.opts.MaxWorkerQPS()
}
// nolint: unparam
func (ms *m3nschAgent) newMethodMetrics(method string) instrument.MethodMetrics {
subScope := ms.opts.InstrumentOptions().MetricsScope().SubScope("agent")
return instrument.NewMethodMetrics(subScope, method, ms.opts.InstrumentOptions().MetricsSamplingRate())
}
func (ms *m3nschAgent) tickPeriodWithLock() time.Duration {
var (
qps = ms.workload.IngressQPS
numWorkers = ms.opts.Concurrency()
qpsPerWorker = float64(qps) / float64(numWorkers)
tickPeriod = time.Duration(1000*1000*1000/qpsPerWorker) * time.Nanosecond
)
return tickPeriod
}
func (ms *m3nschAgent) workerParams() (xtime.Unit, string, time.Time, time.Duration) {
ms.params.RLock()
defer ms.params.RUnlock()
return ms.opts.TimeUnit(), ms.workload.Namespace, ms.workload.BaseTime, ms.tickPeriodWithLock()
}
func (ms *m3nschAgent) nextWorkerMetric(workerIdx int) generatedMetric {
ms.params.RLock()
defer ms.params.RUnlock()
metricIdx := ms.params.ranges[workerIdx].next()
return ms.params.workingSet[metricIdx]
}
func (ms *m3nschAgent) runWorker(workerIdx int, workerCh chan workerNotification) {
defer ms.workerWg.Done()
var (
methodMetrics = ms.metrics.writeMethodMetrics
timeUnit, namespace, fakeNow, tickPeriod = ms.workerParams()
tickLoop = time.NewTicker(tickPeriod)
)
defer tickLoop.Stop()
for {
select {
case msg := <-workerCh:
if msg.stop {
return
}
if msg.update {
tickLoop.Stop()
timeUnit, namespace, fakeNow, tickPeriod = ms.workerParams()
tickLoop = time.NewTicker(tickPeriod)
}
case <-tickLoop.C:
fakeNow = fakeNow.Add(tickPeriod)
metric := ms.nextWorkerMetric(workerIdx)
start := time.Now()
err := ms.params.fn(workerIdx, ms.session, namespace, metric, fakeNow, timeUnit)
elapsed := time.Since(start)
methodMetrics.ReportSuccessOrError(err, elapsed)
}
}
}
type generatedMetric struct {
name string
timeseries datums.SyntheticTimeSeries
}
type workerRange struct {
startIdx int // inclusive
endIdx int // exclusive
lastIdx int // last idx returned
}
func (wr *workerRange) next() int {
i := wr.lastIdx
next := wr.startIdx + ((i + 1) % (wr.endIdx - wr.startIdx + 1))
wr.lastIdx = next
return next
}
type workerNotification struct {
stop bool
update bool
}
type workerChannels []chan workerNotification
func (c workerChannels) close() {
for _, ch := range c {
close(ch)
}
}
func (c workerChannels) notify(msg workerNotification) {
for _, ch := range c {
ch <- msg
}
}
type agentMetrics struct {
writeMethodMetrics instrument.MethodMetrics
}
type workerFn func(workerIdx int, session client.Session, namespace string, metric generatedMetric, t time.Time, u xtime.Unit) error
func workerWriteFn(_ int, session client.Session, namespace string, metric generatedMetric, t time.Time, u xtime.Unit) error {
return session.Write(ident.StringID(namespace), ident.StringID(metric.name), t, metric.timeseries.Next(), u, nil)
}