19
19
package org .apache .pulsar .broker .loadbalance .extensions .models ;
20
20
21
21
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Label .Failure ;
22
- import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Label .Skip ;
23
22
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Label .Success ;
24
23
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Reason .Admin ;
25
- import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Reason .Balanced ;
26
24
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Reason .Bandwidth ;
27
25
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Reason .MsgRate ;
28
26
import static org .apache .pulsar .broker .loadbalance .extensions .models .SplitDecision .Reason .Sessions ;
32
30
import java .util .HashMap ;
33
31
import java .util .List ;
34
32
import java .util .Map ;
35
- import org . apache . commons . lang3 . mutable . MutableLong ;
33
+ import java . util . concurrent . atomic . AtomicLong ;
36
34
import org .apache .pulsar .common .stats .Metrics ;
37
35
38
36
/**
39
37
* Defines the information required for a service unit split(e.g. bundle split).
40
38
*/
41
39
public class SplitCounter {
42
40
43
- long splitCount = 0 ;
44
-
45
- final Map < SplitDecision . Label , Map < SplitDecision . Reason , MutableLong >> breakdownCounters ;
41
+ private long splitCount = 0 ;
42
+ private final Map < SplitDecision . Label , Map < SplitDecision . Reason , AtomicLong >> breakdownCounters ;
43
+ private volatile long updatedAt = 0 ;
46
44
47
45
public SplitCounter () {
48
46
breakdownCounters = Map .of (
49
47
Success , Map .of (
50
- Topics , new MutableLong (),
51
- Sessions , new MutableLong (),
52
- MsgRate , new MutableLong (),
53
- Bandwidth , new MutableLong (),
54
- Admin , new MutableLong ()),
55
- Skip , Map .of (
56
- Balanced , new MutableLong ()
57
- ),
48
+ Topics , new AtomicLong (),
49
+ Sessions , new AtomicLong (),
50
+ MsgRate , new AtomicLong (),
51
+ Bandwidth , new AtomicLong (),
52
+ Admin , new AtomicLong ()),
58
53
Failure , Map .of (
59
- Unknown , new MutableLong ())
54
+ Unknown , new AtomicLong ())
60
55
);
61
56
}
62
57
63
58
public void update (SplitDecision decision ) {
64
59
if (decision .label == Success ) {
65
60
splitCount ++;
66
61
}
67
- breakdownCounters .get (decision .getLabel ()).get (decision .getReason ()).increment ();
62
+ breakdownCounters .get (decision .getLabel ()).get (decision .getReason ()).incrementAndGet ();
63
+ updatedAt = System .currentTimeMillis ();
64
+ }
65
+
66
+ public void update (SplitDecision .Label label , SplitDecision .Reason reason ) {
67
+ if (label == Success ) {
68
+ splitCount ++;
69
+ }
70
+ breakdownCounters .get (label ).get (reason ).incrementAndGet ();
71
+ updatedAt = System .currentTimeMillis ();
68
72
}
69
73
70
74
public List <Metrics > toMetrics (String advertisedBrokerAddress ) {
@@ -77,22 +81,26 @@ public List<Metrics> toMetrics(String advertisedBrokerAddress) {
77
81
m .put ("brk_lb_bundles_split_total" , splitCount );
78
82
metrics .add (m );
79
83
80
- for (Map .Entry <SplitDecision .Label , Map <SplitDecision .Reason , MutableLong >> etr
84
+
85
+ for (Map .Entry <SplitDecision .Label , Map <SplitDecision .Reason , AtomicLong >> etr
81
86
: breakdownCounters .entrySet ()) {
82
87
var result = etr .getKey ();
83
- for (Map .Entry <SplitDecision .Reason , MutableLong > counter : etr .getValue ().entrySet ()) {
88
+ for (Map .Entry <SplitDecision .Reason , AtomicLong > counter : etr .getValue ().entrySet ()) {
84
89
var reason = counter .getKey ();
85
90
var count = counter .getValue ();
86
91
Map <String , String > breakdownDims = new HashMap <>(dimensions );
87
92
breakdownDims .put ("result" , result .toString ());
88
93
breakdownDims .put ("reason" , reason .toString ());
89
94
Metrics breakdownMetric = Metrics .create (breakdownDims );
90
- breakdownMetric .put ("brk_lb_bundles_split_breakdown_total" , count );
95
+ breakdownMetric .put ("brk_lb_bundles_split_breakdown_total" , count . get () );
91
96
metrics .add (breakdownMetric );
92
97
}
93
98
}
94
99
95
100
return metrics ;
96
101
}
97
102
103
+ public long updatedAt () {
104
+ return updatedAt ;
105
+ }
98
106
}
0 commit comments