17
17
18
18
package org .apache .hertzbeat .alert .calculate ;
19
19
20
- import java .util .Arrays ;
21
20
import java .util .HashMap ;
22
- import java .util .Objects ;
23
21
import lombok .extern .slf4j .Slf4j ;
24
22
import org .apache .hertzbeat .alert .reduce .AlarmCommonReduce ;
25
23
import org .apache .hertzbeat .alert .service .DataSourceService ;
26
24
import org .apache .hertzbeat .alert .util .AlertTemplateUtil ;
25
+ import org .apache .hertzbeat .alert .util .AlertUtil ;
27
26
import org .apache .hertzbeat .common .constants .CommonConstants ;
28
27
import org .apache .hertzbeat .common .entity .alerter .AlertDefine ;
29
28
import org .apache .hertzbeat .common .entity .alerter .SingleAlert ;
30
29
import java .util .List ;
31
30
import java .util .Map ;
32
- import java .util .concurrent .ConcurrentHashMap ;
33
31
import org .apache .commons .lang3 .StringUtils ;
34
32
import org .apache .commons .collections4 .CollectionUtils ;
35
33
import org .springframework .stereotype .Component ;
@@ -47,22 +45,13 @@ public class PeriodicAlertCalculator {
47
45
48
46
private final DataSourceService dataSourceService ;
49
47
private final AlarmCommonReduce alarmCommonReduce ;
50
- /**
51
- * The alarm in the process is triggered
52
- * key - labels fingerprint
53
- */
54
- private final Map <String , SingleAlert > pendingAlertMap ;
55
- /**
56
- * The not recover alert
57
- * key - labels fingerprint
58
- */
59
- private final Map <String , SingleAlert > firingAlertMap ;
60
-
61
- public PeriodicAlertCalculator (DataSourceService dataSourceService , AlarmCommonReduce alarmCommonReduce ) {
48
+ private final AlarmCacheManager alarmCacheManager ;
49
+
50
+ public PeriodicAlertCalculator (DataSourceService dataSourceService , AlarmCommonReduce alarmCommonReduce ,
51
+ AlarmCacheManager alarmCacheManager ) {
62
52
this .dataSourceService = dataSourceService ;
63
53
this .alarmCommonReduce = alarmCommonReduce ;
64
- this .pendingAlertMap = new ConcurrentHashMap <>(8 );
65
- this .firingAlertMap = new ConcurrentHashMap <>(8 );
54
+ this .alarmCacheManager = alarmCacheManager ;
66
55
}
67
56
68
57
public void calculate (AlertDefine rule ) {
@@ -122,8 +111,8 @@ public void calculate(AlertDefine rule) {
122
111
123
112
private void afterThresholdRuleMatch (long currentTimeMilli , Map <String , String > fingerPrints ,
124
113
Map <String , Object > fieldValueMap , AlertDefine define ) {
125
- String fingerprint = calculateFingerprint (fingerPrints );
126
- SingleAlert existingAlert = pendingAlertMap . get (fingerprint );
114
+ String fingerprint = AlertUtil . calculateFingerprint (fingerPrints );
115
+ SingleAlert existingAlert = alarmCacheManager . getPending (fingerprint );
127
116
Map <String , String > labels = new HashMap <>(8 );
128
117
fieldValueMap .putAll (define .getLabels ());
129
118
labels .putAll (fingerPrints );
@@ -144,11 +133,11 @@ private void afterThresholdRuleMatch(long currentTimeMilli, Map<String, String>
144
133
// If required trigger times is 1, set to firing status directly
145
134
if (requiredTimes <= 1 ) {
146
135
newAlert .setStatus (CommonConstants .ALERT_STATUS_FIRING );
147
- firingAlertMap . put (fingerprint , newAlert );
136
+ alarmCacheManager . putFiring (fingerprint , newAlert );
148
137
alarmCommonReduce .reduceAndSendAlarm (newAlert .clone ());
149
138
} else {
150
139
// Otherwise put into pending queue first
151
- pendingAlertMap . put (fingerprint , newAlert );
140
+ alarmCacheManager . putPending (fingerprint , newAlert );
152
141
}
153
142
} else {
154
143
// Update existing alert
@@ -158,31 +147,25 @@ private void afterThresholdRuleMatch(long currentTimeMilli, Map<String, String>
158
147
// Check if required trigger times reached
159
148
if (existingAlert .getStatus ().equals (CommonConstants .ALERT_STATUS_PENDING ) && existingAlert .getTriggerTimes () >= requiredTimes ) {
160
149
// Reached trigger times threshold, change to firing status
161
- pendingAlertMap . remove (fingerprint );
150
+ alarmCacheManager . removePending (fingerprint );
162
151
existingAlert .setStatus (CommonConstants .ALERT_STATUS_FIRING );
163
- firingAlertMap . put (fingerprint , existingAlert );
152
+ alarmCacheManager . putFiring (fingerprint , existingAlert );
164
153
alarmCommonReduce .reduceAndSendAlarm (existingAlert .clone ());
165
154
}
166
155
}
167
156
}
168
157
169
158
private void handleRecoveredAlert (Map <String , String > fingerprints ) {
170
- String fingerprint = calculateFingerprint (fingerprints );
171
- SingleAlert firingAlert = firingAlertMap . remove (fingerprint );
159
+ String fingerprint = AlertUtil . calculateFingerprint (fingerprints );
160
+ SingleAlert firingAlert = alarmCacheManager . removeFiring (fingerprint );
172
161
if (firingAlert != null ) {
173
162
// todo consider multi times to tig for resolved alert
174
163
firingAlert .setTriggerTimes (1 );
175
164
firingAlert .setEndAt (System .currentTimeMillis ());
176
165
firingAlert .setStatus (CommonConstants .ALERT_STATUS_RESOLVED );
177
166
alarmCommonReduce .reduceAndSendAlarm (firingAlert .clone ());
178
167
}
179
- pendingAlertMap . remove (fingerprint );
168
+ alarmCacheManager . removePending (fingerprint );
180
169
}
181
170
182
- private String calculateFingerprint (Map <String , String > fingerPrints ) {
183
- List <String > keyList = fingerPrints .keySet ().stream ().filter (Objects ::nonNull ).sorted ().toList ();
184
- List <String > valueList = fingerPrints .values ().stream ().filter (Objects ::nonNull ).sorted ().toList ();
185
- return Arrays .hashCode (keyList .toArray (new String [0 ])) + "-"
186
- + Arrays .hashCode (valueList .toArray (new String [0 ]));
187
- }
188
171
}
0 commit comments