-
Notifications
You must be signed in to change notification settings - Fork 37
/
manager.go
387 lines (339 loc) · 10.9 KB
/
manager.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you 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 task
import (
"context"
"fmt"
"sync"
"time"
"github.com/apache/skywalking-rover/pkg/core"
"github.com/apache/skywalking-rover/pkg/logger"
"github.com/apache/skywalking-rover/pkg/module"
"github.com/apache/skywalking-rover/pkg/process"
"github.com/apache/skywalking-rover/pkg/process/api"
"github.com/apache/skywalking-rover/pkg/profiling/task/base"
common_v3 "skywalking.apache.org/repo/goapi/collect/common/v3"
profiling_v3 "skywalking.apache.org/repo/goapi/collect/ebpf/profiling/v3"
)
var log = logger.GetLogger("profiling", "task")
type Manager struct {
moduleMgr *module.Manager
processOperator process.Operator
profilingClient profiling_v3.EBPFProfilingServiceClient
ctx context.Context
cancel context.CancelFunc
taskConfig *base.TaskConfig
tasks map[string]*Context
instanceID string
lastUpdateTime int64
}
func NewManager(ctx context.Context, moduleMgr *module.Manager, taskConfig *base.TaskConfig) (*Manager, error) {
coreOperator := moduleMgr.FindModule(core.ModuleName).(core.Operator)
connection := coreOperator.BackendOperator().GetConnection()
profilingClient := profiling_v3.NewEBPFProfilingServiceClient(connection)
processOperator := moduleMgr.FindModule(process.ModuleName).(process.Operator)
if err := CheckProfilingTaskConfig(taskConfig, moduleMgr); err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(ctx)
manager := &Manager{
moduleMgr: moduleMgr,
processOperator: processOperator,
profilingClient: profilingClient,
taskConfig: taskConfig,
tasks: make(map[string]*Context),
instanceID: coreOperator.InstanceID(),
ctx: ctx,
cancel: cancel,
}
return manager, nil
}
func (m *Manager) Start() {
}
func (m *Manager) BuildContextFromCommand(command *common_v3.Command) (*Context, error) {
// analyze command
t, err := base.ProfilingTaskFromCommand(command)
if err != nil || t == nil {
return nil, fmt.Errorf("parsing profiling task failure, command: %v, reason: %v", command.GetArgs(), err)
}
// find processes
processes := make([]api.ProcessInterface, 0)
for _, processID := range t.ProcessIDList {
taskProcess := m.processOperator.FindProcessByID(processID)
if taskProcess == nil {
return nil, fmt.Errorf("could not found %s processes %s", t.TaskID, t.ProcessIDList)
}
processes = append(processes, taskProcess)
}
taskContext := &Context{task: t, processes: processes, status: NotRunning, recalcDuration: make(chan bool, 1)}
// check existing task, extended the running time
existTask := m.tasks[taskContext.BuildTaskIdentity()]
// if task are same, then just rewrite the task information and return
if existTask != nil && existTask.IsSameTask(taskContext) {
existTask.task = t
return existTask, nil
}
// init runner
if err := m.initTaskContext(taskContext); err != nil {
return nil, err
}
return taskContext, nil
}
func (m *Manager) BuildContextFromContinuous(processes []api.ProcessInterface,
taskSetter func(task *base.ProfilingTask), taskIDGenerator func() (string, error)) (*Context, error) {
task := base.ProfilingTaskFromContinuous(processes, taskSetter)
taskContext := &Context{task: task, processes: processes, status: NotRunning, recalcDuration: make(chan bool, 1)}
// if the task already exist, then return error
existTask := m.tasks[taskContext.BuildTaskIdentity()]
// if task are same, then just rewrite the task information and return
if existTask != nil && existTask.IsSameTask(taskContext) {
return nil, fmt.Errorf("already exist profiling task, so ignore")
}
taskID, err := taskIDGenerator()
if err != nil {
return nil, err
}
task.TaskID = taskID
// init runner
if err := m.initTaskContext(taskContext); err != nil {
return nil, err
}
return taskContext, nil
}
func (m *Manager) initTaskContext(taskContext *Context) error {
// init runner
var r base.ProfileTaskRunner
task := taskContext.task
if runner, err := NewProfilingRunner(task.TargetType, m.taskConfig, m.moduleMgr); err != nil {
return err
} else if err := runner.Init(task, taskContext.processes); err != nil {
return fmt.Errorf("could not init %s runner for task: %s: %v", task.TriggerType, task.TaskID, err)
} else {
r = runner
}
taskContext.runner = r
taskContext.ctx, taskContext.cancel = context.WithCancel(m.ctx)
return nil
}
func (m *Manager) StartTask(c *Context) {
// shutdown task if exists
taskIdentity := c.BuildTaskIdentity()
existTask := m.tasks[taskIdentity]
if existTask != nil {
// just extend the task time if the task are same
if c.IsSameTask(existTask) {
// notify to re-calculate the task duration(task stop timer)
c.recalcDuration <- true
return
}
// close task if not same
id := m.tasks[taskIdentity].TaskID()
log.Infof("existing profiling task: %s, so need to stop it", id)
if err := m.ShutdownAndRemoveTask(m.tasks[taskIdentity]); err != nil {
log.Warnf("shutdown existing profiling task failure, so cannot to start new profiling task: %v. reason: %v", c.task.TaskID, err)
return
}
}
currentMilli := time.Now().UnixNano() / int64(time.Millisecond)
m.tasks[taskIdentity] = c
// already reach time
if currentMilli >= c.task.StartTime {
m.runTask(c)
return
}
// schedule to execute
afterRun := time.Since(time.UnixMilli(c.task.StartTime))
go func() {
select {
case <-time.After(afterRun):
log.Infof("the profiling task need to wait %fmin to run: %s", afterRun.Minutes(), c.TaskID())
m.runTask(c)
case <-c.ctx.Done():
return
}
}()
}
func (m *Manager) runTask(c *Context) {
log.Infof("ready to starting profiling task: %s", c.TaskID())
var wg sync.WaitGroup
wg.Add(1)
c.runningWg = &wg
go func() {
defer func() {
wg.Done()
c.status = Stopped
}()
notify := func() {
c.status = Running
c.startRunningTime = time.Now()
m.afterProfilingStartSuccess(c)
}
// start running
if err := c.runner.Run(m.ctx, notify); err != nil {
log.Warnf("executing profiling task failure, taskId: %s, reason: %v", c.task.TaskID, err)
}
}()
}
func (m *Manager) afterProfilingStartSuccess(c *Context) {
pidList := make([]int32, 0)
for _, p := range c.processes {
pidList = append(pidList, p.Pid())
}
log.Infof("profiling task has been started. taskId: %s, pid: %d", c.task.TaskID, pidList)
go func() {
for {
endTime := c.startRunningTime.Add(c.task.MaxRunningDuration)
select {
// shutdown task when arrived task running task
case <-time.After(time.Until(endTime)):
log.Infof("arrived task running time, shutting down task: %s", c.task.TaskID)
if err := m.shutdownTask(c); err != nil {
log.Warnf("shutting down task failure: %s, reason: %v", c.task.TaskID, err)
}
return
case <-c.recalcDuration:
// re-calculate the task end-time
log.Infof("received the extend duration task, task id: %s", c.task.TaskID)
continue
// shutdown when context finished
case <-c.ctx.Done():
if err := m.shutdownTask(c); err != nil {
log.Warnf("shutting down task failure: %s, reason: %v", c.task.TaskID, err)
}
return
}
}
}()
}
func (m *Manager) shutdownTask(c *Context) error {
// return if not running
if c.runningWg == nil {
return nil
}
defer func() {
if r := recover(); r != nil {
log.Warnf("recover from shutdown task, id: %s, error: %v", c.TaskID(), r)
}
}()
err := c.runner.Stop()
c.runningWg.Wait()
c.cancel()
return err
}
func (m *Manager) ShutdownAndRemoveTask(c *Context) error {
err := m.shutdownTask(c)
delete(m.tasks, c.BuildTaskIdentity())
return err
}
func (m *Manager) Shutdown() error {
m.cancel()
return nil
}
func (m *Manager) checkStoppedTaskAndRemoved() {
for identity, t := range m.tasks {
if t.status == Stopped {
delete(m.tasks, identity)
}
}
}
func (m *Manager) StartingWatchTask() error {
// query task
tasks, err := m.profilingClient.QueryTasks(m.ctx, &profiling_v3.EBPFProfilingTaskQuery{
RoverInstanceId: m.instanceID,
LatestUpdateTime: m.lastUpdateTime,
})
if err != nil {
return err
}
if len(tasks.Commands) == 0 {
return nil
}
// analyze profiling tasks
taskContexts := make([]*Context, 0)
lastUpdateTime := m.lastUpdateTime
for _, cmd := range tasks.Commands {
taskContext, err := m.BuildContextFromCommand(cmd)
if err != nil {
log.Warnf("could not execute task, ignored. %v", err)
continue
}
if taskContext.UpdateTime() > lastUpdateTime {
lastUpdateTime = taskContext.UpdateTime()
}
if !taskContext.CheckTaskRunnable() {
continue
}
taskContexts = append(taskContexts, taskContext)
}
// update last task time
m.lastUpdateTime = lastUpdateTime
if len(taskContexts) == 0 {
return nil
}
taskIDList := make([]string, len(taskContexts))
for inx, c := range taskContexts {
taskIDList[inx] = c.TaskID()
}
log.Infof("received %d profiling task: %v", len(taskContexts), taskIDList)
// start tasks
for _, t := range taskContexts {
m.StartTask(t)
}
return nil
}
func (m *Manager) FlushProfilingData() error {
// cleanup the stopped after flush profiling data to make sure all the profiling data been sent
defer m.checkStoppedTaskAndRemoved()
if len(m.tasks) == 0 {
return nil
}
stream, err := m.profilingClient.CollectProfilingData(m.ctx)
if err != nil {
return err
}
currentMilli := time.Now().UnixMilli()
totalSendCount := make(map[string]int)
for _, t := range m.tasks {
data, err1 := t.runner.FlushData()
if err1 != nil {
log.Warnf("reading profiling task data failure. taskId: %s, error: %v", t.task.TaskID, err1)
continue
}
if len(data) == 0 {
continue
}
totalSendCount[t.TaskID()] += len(data)
// only the first data have task metadata
data[0].Task = &profiling_v3.EBPFProfilingTaskMetadata{
TaskId: t.task.TaskID,
ProcessId: t.task.ProcessIDList[0], // the profiling(draw flame-graph) task usually have the one process only
ProfilingStartTime: t.startRunningTime.UnixMilli(),
CurrentTime: currentMilli,
}
for _, d := range data {
// send each data, stop flush data if the stream have found error
if err1 := stream.Send(d); err1 != nil {
return err1
}
}
}
if len(totalSendCount) > 0 {
log.Infof("send profiling data summary: %v", totalSendCount)
}
_, err = stream.CloseAndRecv()
return err
}