-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgo_cgraph_lite.go
More file actions
457 lines (386 loc) · 10.7 KB
/
go_cgraph_lite.go
File metadata and controls
457 lines (386 loc) · 10.7 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Package gocgraph provides a lightweight DAG (Directed Acyclic Graph) execution framework
package gocgraph
import (
"context"
"fmt"
"reflect"
"runtime"
"sync"
"sync/atomic"
)
// CStatus represents the execution result status
type CStatus struct {
errorCode int
errorInfo string
}
// NewCStatus creates a new successful status
func NewCStatus() *CStatus {
return &CStatus{errorCode: 0, errorInfo: ""}
}
// NewCStatusWithError creates a new error status
func NewCStatusWithError(errorInfo string) *CStatus {
return &CStatus{errorCode: -1, errorInfo: errorInfo}
}
// Combine combines two status objects
func (s *CStatus) Combine(other *CStatus) *CStatus {
if s.IsOK() && !other.IsOK() {
s.errorCode = other.errorCode
s.errorInfo = other.errorInfo
}
return s
}
// IsOK checks if the status is successful
func (s *CStatus) IsOK() bool {
return s.errorCode == 0
}
// GetCode returns the error code
func (s *CStatus) GetCode() int {
return s.errorCode
}
// GetInfo returns the error information
func (s *CStatus) GetInfo() string {
return s.errorInfo
}
// GParam is the base interface for shared parameters between nodes
type GParam interface {
Setup() *CStatus
Reset(curStatus *CStatus)
}
// BaseGParam provides a default implementation of GParam
type BaseGParam struct {
paramSharedLock sync.RWMutex
}
// Setup provides default parameter initialization
func (p *BaseGParam) Setup() *CStatus {
return NewCStatus()
}
// Reset provides default parameter reset
func (p *BaseGParam) Reset(curStatus *CStatus) {
// Default implementation does nothing
}
// GetLock returns the shared lock for thread safety
func (p *BaseGParam) GetLock() *sync.RWMutex {
return &p.paramSharedLock
}
// GParamManager manages the lifecycle of all shared parameters
type GParamManager struct {
params map[string]GParam
mutex sync.RWMutex
}
// NewGParamManager creates a new parameter manager
func NewGParamManager() *GParamManager {
return &GParamManager{
params: make(map[string]GParam),
}
}
// Create creates a parameter with the specified key and type
func (pm *GParamManager) Create(key string, param GParam) *CStatus {
pm.mutex.Lock()
defer pm.mutex.Unlock()
if existingParam, exists := pm.params[key]; exists {
// 检查是否是同一类型,防止重复创建不同类型的同名参数
if reflect.TypeOf(existingParam) == reflect.TypeOf(param) {
return NewCStatus()
}
return NewCStatusWithError(fmt.Sprintf("create [%s] param duplicate", key))
}
pm.params[key] = param
return NewCStatus()
}
// Get retrieves a parameter by key
func (pm *GParamManager) Get(key string) GParam {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
param, exists := pm.params[key]
if !exists {
return nil
}
return param
}
// Setup initializes all parameters
func (pm *GParamManager) Setup() *CStatus {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
status := NewCStatus()
for _, param := range pm.params {
status.Combine(param.Setup())
}
return status
}
// Reset resets all parameters
func (pm *GParamManager) Reset(curStatus *CStatus) {
pm.mutex.RLock()
defer pm.mutex.RUnlock()
for _, param := range pm.params {
param.Reset(curStatus)
}
}
// Schedule implements a thread pool and task queue for concurrent execution
type Schedule struct {
workers int
taskQueue chan func()
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
// NewSchedule creates a new scheduler with specified number of workers
func NewSchedule(numWorkers int) *Schedule {
if numWorkers <= 0 {
numWorkers = runtime.NumCPU()
}
ctx, cancel := context.WithCancel(context.Background())
s := &Schedule{
workers: numWorkers,
taskQueue: make(chan func(), 1000),
ctx: ctx,
cancel: cancel,
}
for i := 0; i < numWorkers; i++ {
s.wg.Add(1)
go s.worker()
}
return s
}
// worker is the worker goroutine function
func (s *Schedule) worker() {
defer s.wg.Done()
for {
select {
case task := <-s.taskQueue:
if task != nil {
task()
}
case <-s.ctx.Done():
return
}
}
}
// Commit submits a task to the task queue
func (s *Schedule) Commit(task func()) {
select {
case s.taskQueue <- task:
case <-s.ctx.Done():
return
default:
go task()
}
}
// Stop stops all worker goroutines
func (s *Schedule) Stop() {
s.cancel()
close(s.taskQueue)
s.wg.Wait()
}
// GElement represents a node in the DAG
type GElement interface {
Init() *CStatus
Run() *CStatus
Destroy() *CStatus
GetName() string
// Parameter management methods
CreateGParam(key string, param GParam) *CStatus
GetGParam(key string) GParam
}
// GNode is an alias for GElement
type GNode = GElement
// elementInfo contains internal dependency management information
type elementInfo struct {
runBefore []*elementInfo // 当前节点完成后需要执行的节点集合
dependence []*elementInfo // 当前节点依赖的节点集合
leftDepend int64 // 剩余依赖数量(原子变量,线程安全)
name string // 节点名称
paramManager *GParamManager // 参数管理器指针
element GElement // 对应的元素接口
}
// BaseGElement provides a basic implementation that other nodes can embed
type BaseGElement struct {
name string
paramManager *GParamManager
}
// Init provides default initialization
func (e *BaseGElement) Init() *CStatus {
return NewCStatus()
}
// Destroy provides default destruction
func (e *BaseGElement) Destroy() *CStatus {
return NewCStatus()
}
// GetName returns the node name
func (e *BaseGElement) GetName() string {
return e.name
}
// SetName sets the node name
func (e *BaseGElement) SetName(name string) {
e.name = name
}
// SetParamManager sets the parameter manager
func (e *BaseGElement) SetParamManager(pm *GParamManager) {
e.paramManager = pm
}
// CreateGParam creates a parameter with the specified key and type
func (e *BaseGElement) CreateGParam(key string, param GParam) *CStatus {
if e.paramManager == nil {
return NewCStatusWithError("param manager is nil")
}
return e.paramManager.Create(key, param)
}
// GetGParam retrieves a parameter by key
func (e *BaseGElement) GetGParam(key string) GParam {
if e.paramManager == nil {
return nil
}
return e.paramManager.Get(key)
}
// GPipeline manages the entire DAG execution flow
type GPipeline struct {
elements []*elementInfo // 所有图元素的内部信息
elementMap map[GElement]*elementInfo // 元素到内部信息的映射
schedule *Schedule // 任务调度器
finishedSize int64 // 已完成节点数量
executeMutex sync.Mutex // 执行互斥锁
executeCond *sync.Cond // 执行条件变量
status *CStatus // 执行状态
paramManager *GParamManager // 参数管理器
}
// NewGPipeline creates a new pipeline
func NewGPipeline() *GPipeline {
p := &GPipeline{
elements: make([]*elementInfo, 0),
elementMap: make(map[GElement]*elementInfo),
paramManager: NewGParamManager(),
status: NewCStatus(),
}
p.executeCond = sync.NewCond(&p.executeMutex)
return p
}
// Process executes the pipeline for the specified number of times
func (p *GPipeline) Process(times int) *CStatus {
if times <= 0 {
times = 1
}
p.init()
for times > 0 && p.status.IsOK() {
p.run()
times--
}
p.destroy()
return p.status
}
// RegisterGElement registers a graph element with dependencies
func (p *GPipeline) RegisterGElement(element GElement, depends []GElement, name string) *CStatus {
// Set up the element with name and parameter manager
if setter, ok := element.(interface{ SetName(string) }); ok {
setter.SetName(name)
}
if setter, ok := element.(interface{ SetParamManager(*GParamManager) }); ok {
setter.SetParamManager(p.paramManager)
}
// Create element info
info := &elementInfo{
name: name,
paramManager: p.paramManager,
element: element,
runBefore: make([]*elementInfo, 0),
dependence: make([]*elementInfo, 0),
}
// Set up dependencies
for _, dep := range depends {
if depInfo, exists := p.elementMap[dep]; exists {
info.dependence = append(info.dependence, depInfo)
depInfo.runBefore = append(depInfo.runBefore, info)
} else {
return NewCStatusWithError("dependency element not found")
}
}
atomic.StoreInt64(&info.leftDepend, int64(len(info.dependence)))
// Add to pipeline
p.elements = append(p.elements, info)
p.elementMap[element] = info
return NewCStatus()
}
// init initializes all elements and scheduler
func (p *GPipeline) init() {
p.status = NewCStatus()
for _, info := range p.elements {
p.status.Combine(info.element.Init())
}
p.schedule = NewSchedule(8) // 创建调度器,默认8个工作线程
}
// run executes one complete DAG execution
func (p *GPipeline) run() {
p.setup() // 设置
p.executeAll() // 执行所有节点
p.reset() // 重置
}
// destroy cleans up all elements and scheduler
func (p *GPipeline) destroy() {
for _, info := range p.elements {
p.status.Combine(info.element.Destroy())
}
if p.schedule != nil {
p.schedule.Stop()
p.schedule = nil
}
}
// executeAll starts execution from nodes with no dependencies
func (p *GPipeline) executeAll() {
for _, info := range p.elements {
if len(info.dependence) == 0 {
p.schedule.Commit(func() {
p.execute(info)
})
}
}
}
// execute executes a single node
func (p *GPipeline) execute(info *elementInfo) {
if !p.status.IsOK() {
return
}
p.status.Combine(info.element.Run())
for _, cur := range info.runBefore {
if atomic.AddInt64(&cur.leftDepend, -1) <= 0 {
p.schedule.Commit(func() {
p.execute(cur)
})
}
}
p.executeMutex.Lock()
atomic.AddInt64(&p.finishedSize, 1)
if atomic.LoadInt64(&p.finishedSize) >= int64(len(p.elements)) || !p.status.IsOK() {
p.executeCond.Signal()
}
p.executeMutex.Unlock()
}
// setup prepares for execution
func (p *GPipeline) setup() {
atomic.StoreInt64(&p.finishedSize, 0)
for _, info := range p.elements {
atomic.StoreInt64(&info.leftDepend, int64(len(info.dependence)))
}
p.status.Combine(p.paramManager.Setup())
}
// reset cleans up after execution
func (p *GPipeline) reset() {
p.executeMutex.Lock()
for atomic.LoadInt64(&p.finishedSize) < int64(len(p.elements)) && p.status.IsOK() {
p.executeCond.Wait()
}
p.executeMutex.Unlock()
p.paramManager.Reset(p.status)
}
// GPipelineFactory provides factory methods for pipeline creation and destruction
type GPipelineFactory struct{}
// Create creates a new pipeline instance
func (f *GPipelineFactory) Create() *GPipeline {
return NewGPipeline()
}
// Remove destroys a pipeline instance
func (f *GPipelineFactory) Remove(pipeline *GPipeline) *CStatus {
if pipeline != nil {
pipeline.destroy()
}
return NewCStatus()
}
// Global factory instance for convenience
var Factory = &GPipelineFactory{}