21
21
package io .temporal .internal .worker ;
22
22
23
23
import io .temporal .api .enums .v1 .TaskQueueKind ;
24
- import java .util .concurrent .atomic .AtomicBoolean ;
25
- import java .util .concurrent .atomic .AtomicInteger ;
26
24
import javax .annotation .concurrent .ThreadSafe ;
27
25
28
26
@ ThreadSafe
29
27
public class StickyQueueBalancer {
30
28
private final int pollersCount ;
31
29
private final boolean stickyQueueEnabled ;
32
- private final AtomicInteger stickyPollers = new AtomicInteger (0 );
33
- private final AtomicInteger normalPollers = new AtomicInteger (0 );
34
- private final AtomicBoolean disableNormalPoll = new AtomicBoolean (false );
35
-
36
- private volatile long stickyBacklogSize = 0 ;
30
+ private int stickyPollers = 0 ;
31
+ private int normalPollers = 0 ;
32
+ private boolean disableNormalPoll = false ;
33
+ private long stickyBacklogSize = 0 ;
37
34
38
35
public StickyQueueBalancer (int pollersCount , boolean stickyQueueEnabled ) {
39
36
this .pollersCount = pollersCount ;
@@ -43,35 +40,35 @@ public StickyQueueBalancer(int pollersCount, boolean stickyQueueEnabled) {
43
40
/**
44
41
* @return task queue kind that should be used for the next poll
45
42
*/
46
- public TaskQueueKind makePoll () {
43
+ public synchronized TaskQueueKind makePoll () {
47
44
if (stickyQueueEnabled ) {
48
- if (disableNormalPoll . get () ) {
49
- stickyPollers . incrementAndGet () ;
45
+ if (disableNormalPoll ) {
46
+ stickyPollers ++ ;
50
47
return TaskQueueKind .TASK_QUEUE_KIND_STICKY ;
51
48
}
52
49
// If pollersCount >= stickyBacklogSize > 0 we want to go back to a normal ratio to avoid a
53
50
// situation that too many pollers (all of them in the worst case) will open only sticky queue
54
51
// polls observing a stickyBacklogSize == 1 for example (which actually can be 0 already at
55
52
// that moment) and get stuck causing dip in worker load.
56
- if (stickyBacklogSize > pollersCount || stickyPollers . get () <= normalPollers . get () ) {
57
- stickyPollers . incrementAndGet () ;
53
+ if (stickyBacklogSize > pollersCount || stickyPollers <= normalPollers ) {
54
+ stickyPollers ++ ;
58
55
return TaskQueueKind .TASK_QUEUE_KIND_STICKY ;
59
56
}
60
57
}
61
- normalPollers . incrementAndGet () ;
58
+ normalPollers ++ ;
62
59
return TaskQueueKind .TASK_QUEUE_KIND_NORMAL ;
63
60
}
64
61
65
62
/**
66
63
* @param taskQueueKind what kind of task queue poll was just finished
67
64
*/
68
- public void finishPoll (TaskQueueKind taskQueueKind ) {
65
+ public synchronized void finishPoll (TaskQueueKind taskQueueKind ) {
69
66
switch (taskQueueKind ) {
70
67
case TASK_QUEUE_KIND_NORMAL :
71
- normalPollers . decrementAndGet () ;
68
+ normalPollers -- ;
72
69
break ;
73
70
case TASK_QUEUE_KIND_STICKY :
74
- stickyPollers . decrementAndGet () ;
71
+ stickyPollers -- ;
75
72
break ;
76
73
default :
77
74
throw new IllegalArgumentException ("Invalid task queue kind: " + taskQueueKind );
@@ -83,18 +80,14 @@ public void finishPoll(TaskQueueKind taskQueueKind) {
83
80
* @param backlogSize backlog size from the poll response, helps to determine if the sticky queue
84
81
* is backlogged
85
82
*/
86
- public void finishPoll (TaskQueueKind taskQueueKind , long backlogSize ) {
83
+ public synchronized void finishPoll (TaskQueueKind taskQueueKind , long backlogSize ) {
87
84
finishPoll (taskQueueKind );
88
85
if (TaskQueueKind .TASK_QUEUE_KIND_STICKY .equals (taskQueueKind )) {
89
86
stickyBacklogSize = backlogSize ;
90
87
}
91
88
}
92
89
93
- public void disableNormalPoll () {
94
- disableNormalPoll .set (true );
95
- }
96
-
97
- public int getNormalPollerCount () {
98
- return normalPollers .get ();
90
+ public synchronized void disableNormalPoll () {
91
+ disableNormalPoll = true ;
99
92
}
100
93
}
0 commit comments