Skip to content

Commit 65fdc1b

Browse files
committed
update polletracker for type breakdown
1 parent e3802b7 commit 65fdc1b

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

internal/common/debug/example_test.go

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ import (
2525
"fmt"
2626
"sort"
2727
"sync"
28-
29-
"go.uber.org/atomic"
3028
)
3129

3230
type (
3331
// pollerTrackerImpl implements the PollerTracker interface
3432
pollerTrackerImpl struct {
35-
pollerCount atomic.Int32
33+
sync.RWMutex
34+
count map[string]int64
3635
}
3736

3837
// stopperImpl implements the Stopper interface
3938
stopperImpl struct {
39+
sync.Once
40+
workerType string
4041
pollerTracker *pollerTrackerImpl
4142
}
4243

@@ -93,40 +94,67 @@ func (asi *activityStopperImpl) Stop() {
9394
})
9495
}
9596

96-
func (p *pollerTrackerImpl) Start() Stopper {
97-
p.pollerCount.Inc()
98-
return &stopperImpl{
99-
pollerTracker: p,
100-
}
97+
func (p *pollerTrackerImpl) Start(workerType string) Stopper {
98+
p.Lock()
99+
defer p.Unlock()
100+
p.count[workerType]++
101+
return &stopperImpl{pollerTracker: p, workerType: workerType}
101102
}
102103

103-
func (p *pollerTrackerImpl) Stats() int32 {
104-
return p.pollerCount.Load()
104+
func (p *pollerTrackerImpl) Stats() Pollers {
105+
var pollers Pollers
106+
p.RLock()
107+
defer p.RUnlock()
108+
for pollerType, count := range p.count {
109+
if count > 0 {
110+
pollers = append(pollers, struct {
111+
Type string
112+
Count int64
113+
}{Type: pollerType, Count: count})
114+
}
115+
}
116+
sort.Slice(pollers, func(i, j int) bool {
117+
return pollers[i].Type < pollers[j].Type
118+
})
119+
return pollers
105120
}
106121

107122
func (s *stopperImpl) Stop() {
108-
s.pollerTracker.pollerCount.Dec()
123+
s.Do(func() {
124+
s.pollerTracker.Lock()
125+
defer s.pollerTracker.Unlock()
126+
s.pollerTracker.count[s.workerType]--
127+
if s.pollerTracker.count[s.workerType] == 0 {
128+
delete(s.pollerTracker.count, s.workerType)
129+
}
130+
})
109131
}
110132

111133
func Example() {
112134
var pollerTracker PollerTracker
113-
pollerTracker = &pollerTrackerImpl{}
135+
pollerTracker = &pollerTrackerImpl{count: make(map[string]int64)}
114136

115137
// Initially, poller count should be 0
116-
fmt.Println(fmt.Sprintf("poller stats: %d", pollerTracker.Stats()))
117-
118-
// Start a poller and verify that the count increments
119-
stopper1 := pollerTracker.Start()
120-
fmt.Println(fmt.Sprintf("poller stats: %d", pollerTracker.Stats()))
121-
122-
// Start another poller and verify that the count increments again
123-
stopper2 := pollerTracker.Start()
124-
fmt.Println(fmt.Sprintf("poller stats: %d", pollerTracker.Stats()))
125-
138+
jsonPollers, _ := json.MarshalIndent(pollerTracker.Stats(), "", " ")
139+
fmt.Println(string(jsonPollers))
140+
141+
// Start pollers and verify that the count increments
142+
stopper1 := pollerTracker.Start("ActivityWorker")
143+
jsonPollers, _ = json.MarshalIndent(pollerTracker.Stats(), "", " ")
144+
fmt.Println(string(jsonPollers))
145+
stopper2 := pollerTracker.Start("ActivityWorker")
146+
jsonPollers, _ = json.MarshalIndent(pollerTracker.Stats(), "", " ")
147+
fmt.Println(string(jsonPollers))
148+
// Start another poller type and verify that the count increments
149+
stopper3 := pollerTracker.Start("WorkflowWorker")
150+
jsonPollers, _ = json.MarshalIndent(pollerTracker.Stats(), "", " ")
151+
fmt.Println(string(jsonPollers))
126152
// Stop the pollers and verify the counter
127153
stopper1.Stop()
128154
stopper2.Stop()
129-
fmt.Println(fmt.Sprintf("poller stats: %d", pollerTracker.Stats()))
155+
stopper3.Stop()
156+
jsonPollers, _ = json.MarshalIndent(pollerTracker.Stats(), "", " ")
157+
fmt.Println(string(jsonPollers))
130158

131159
var activityTracker ActivityTracker
132160
activityTracker = &activityTrackerImpl{activityCount: make(map[ActivityInfo]int64)}
@@ -157,10 +185,30 @@ func Example() {
157185
fmt.Println(string(jsonActivities))
158186

159187
// Output:
160-
// poller stats: 0
161-
// poller stats: 1
162-
// poller stats: 2
163-
// poller stats: 0
188+
// null
189+
// [
190+
// {
191+
// "Type": "ActivityWorker",
192+
// "Count": 1
193+
// }
194+
// ]
195+
// [
196+
// {
197+
// "Type": "ActivityWorker",
198+
// "Count": 2
199+
// }
200+
// ]
201+
// [
202+
// {
203+
// "Type": "ActivityWorker",
204+
// "Count": 2
205+
// },
206+
// {
207+
// "Type": "WorkflowWorker",
208+
// "Count": 1
209+
// }
210+
// ]
211+
// null
164212
// [
165213
// {
166214
// "Info": {

internal/common/debug/types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ type (
3434
PollerTracker interface {
3535
// Start collects information on poller start up.
3636
// consumers should provide a concurrency-safe implementation.
37-
Start() Stopper
37+
Start(workerType string) Stopper
3838
// Stats return the number or running pollers
39-
Stats() int32
39+
Stats() Pollers
4040
}
4141

4242
// WorkerStats provides a set of methods that can be used to collect
@@ -70,6 +70,11 @@ type (
7070
Count int64
7171
}
7272

73+
Pollers []struct {
74+
Type string
75+
Count int64
76+
}
77+
7378
// Debugger exposes stats collected on a running Worker
7479
// Deprecated: in development and very likely to change
7580
Debugger interface {

internal/common/debug/workerstats_noop.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ type (
2929
activityTrackerNoopImpl struct{}
3030
)
3131

32-
func (lc *pollerTrackerNoopImpl) Start() Stopper { return &stopperNoopImpl{} }
33-
func (lc *pollerTrackerNoopImpl) Stats() int32 { return 0 }
34-
func (r *stopperNoopImpl) Stop() {}
32+
func (lc *pollerTrackerNoopImpl) Start(workerType string) Stopper { return &stopperNoopImpl{} }
33+
func (lc *pollerTrackerNoopImpl) Stats() Pollers { return nil }
34+
func (r *stopperNoopImpl) Stop() {}
3535

3636
// NewNoopPollerTracker creates a new PollerTracker instance
3737
func NewNoopPollerTracker() PollerTracker { return &pollerTrackerNoopImpl{} }

internal/common/debug/workerstats_noop_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ func TestWorkerStats(t *testing.T) {
3030
pollerTracker := NewNoopPollerTracker()
3131
activityTracker := NewNoopActivityTracker()
3232
assert.NotNil(t, pollerTracker)
33-
assert.NotNil(t, pollerTracker.Start())
34-
assert.Equal(t, int32(0), pollerTracker.Stats())
35-
assert.NotPanics(t, pollerTracker.Start().Stop)
33+
assert.NotNil(t, pollerTracker.Start(""))
34+
assert.Nil(t, pollerTracker.Stats())
35+
assert.NotPanics(t, pollerTracker.Start("").Stop)
3636
assert.NotNil(t, activityTracker.Start(ActivityInfo{}))
3737
assert.Nil(t, activityTracker.Stats())
3838
}

internal/internal_worker_base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (bw *baseWorker) isShutdown() bool {
236236

237237
func (bw *baseWorker) runPoller() {
238238
defer bw.shutdownWG.Done()
239-
defer bw.options.pollerTracker.Start().Stop()
239+
defer bw.options.pollerTracker.Start(bw.options.workerType).Stop()
240240

241241
bw.metricsScope.Counter(metrics.PollerStartCounter).Inc(1)
242242

0 commit comments

Comments
 (0)