@@ -68,6 +68,8 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ
68
68
private Multimap <String , GrayReleaseRuleCache > grayReleaseRuleCache ;
69
69
//store clientAppId+clientNamespace+ip -> ruleId map
70
70
private Multimap <String , Long > reversedGrayReleaseRuleCache ;
71
+ //store clientAppId+clientNamespace+label -> ruleId map
72
+ private Multimap <String , Long > reversedGrayReleaseRuleLabelCache ;
71
73
//an auto increment version to indicate the age of rules
72
74
private AtomicLong loadVersion ;
73
75
@@ -80,6 +82,8 @@ public GrayReleaseRulesHolder(final GrayReleaseRuleRepository grayReleaseRuleRep
80
82
TreeMultimap .create (String .CASE_INSENSITIVE_ORDER , Ordering .natural ()));
81
83
reversedGrayReleaseRuleCache = Multimaps .synchronizedSetMultimap (
82
84
TreeMultimap .create (String .CASE_INSENSITIVE_ORDER , Ordering .natural ()));
85
+ reversedGrayReleaseRuleLabelCache = Multimaps .synchronizedSetMultimap (
86
+ TreeMultimap .create (String .CASE_INSENSITIVE_ORDER , Ordering .natural ()));
83
87
executorService = Executors .newScheduledThreadPool (1 , ApolloThreadFactory
84
88
.create ("GrayReleaseRulesHolder" , true ));
85
89
}
@@ -152,15 +156,29 @@ public Long findReleaseIdFromGrayReleaseRule(String clientAppId, String clientIp
152
156
}
153
157
154
158
/**
155
- * Check whether there are gray release rules for the clientAppId, clientIp, namespace
156
- * combination. Please note that even there are gray release rules, it doesn't mean it will always
157
- * load gray releases. Because gray release rules actually apply to one more dimension - cluster.
159
+ * Check whether there are gray release rules for the clientAppId, clientIp, clientLabel, namespace combination.
160
+ * Please note that even there are gray release rules, it doesn't mean it will always load gray
161
+ * releases. Because gray release rules actually apply to one more dimension - cluster.
158
162
*/
159
- public boolean hasGrayReleaseRule (String clientAppId , String clientIp , String namespaceName ) {
160
- return reversedGrayReleaseRuleCache .containsKey (assembleReversedGrayReleaseRuleKey (clientAppId ,
163
+ public boolean hasGrayReleaseRule (String clientAppId , String clientIp , String clientLabel ,
164
+ String namespaceName ) {
165
+ // check ip gray rule
166
+ if (reversedGrayReleaseRuleCache .containsKey (assembleReversedGrayReleaseRuleKey (clientAppId ,
161
167
namespaceName , clientIp )) || reversedGrayReleaseRuleCache .containsKey
162
168
(assembleReversedGrayReleaseRuleKey (clientAppId , namespaceName , GrayReleaseRuleItemDTO
163
- .ALL_IP ));
169
+ .ALL_IP ))) {
170
+ return true ;
171
+ }
172
+ // check label gray rule
173
+ if (!Strings .isNullOrEmpty (clientLabel ) &&
174
+ (reversedGrayReleaseRuleLabelCache .containsKey (
175
+ assembleReversedGrayReleaseRuleKey (clientAppId , namespaceName , clientLabel )) ||
176
+ reversedGrayReleaseRuleLabelCache .containsKey (
177
+ assembleReversedGrayReleaseRuleKey (clientAppId , namespaceName ,
178
+ GrayReleaseRuleItemDTO .ALL_Label )))) {
179
+ return true ;
180
+ }
181
+ return false ;
164
182
}
165
183
166
184
private void scanGrayReleaseRules () {
@@ -232,6 +250,10 @@ private void addCache(String key, GrayReleaseRuleCache ruleCache) {
232
250
reversedGrayReleaseRuleCache .put (assembleReversedGrayReleaseRuleKey (ruleItemDTO
233
251
.getClientAppId (), ruleCache .getNamespaceName (), clientIp ), ruleCache .getRuleId ());
234
252
}
253
+ for (String label : ruleItemDTO .getClientLabelList ()) {
254
+ reversedGrayReleaseRuleLabelCache .put (assembleReversedGrayReleaseRuleKey (ruleItemDTO
255
+ .getClientAppId (), ruleCache .getNamespaceName (), label ), ruleCache .getRuleId ());
256
+ }
235
257
}
236
258
}
237
259
grayReleaseRuleCache .put (key , ruleCache );
@@ -244,6 +266,10 @@ private void removeCache(String key, GrayReleaseRuleCache ruleCache) {
244
266
reversedGrayReleaseRuleCache .remove (assembleReversedGrayReleaseRuleKey (ruleItemDTO
245
267
.getClientAppId (), ruleCache .getNamespaceName (), clientIp ), ruleCache .getRuleId ());
246
268
}
269
+ for (String label : ruleItemDTO .getClientLabelList ()) {
270
+ reversedGrayReleaseRuleLabelCache .remove (assembleReversedGrayReleaseRuleKey (ruleItemDTO
271
+ .getClientAppId (), ruleCache .getNamespaceName (), label ), ruleCache .getRuleId ());
272
+ }
247
273
}
248
274
}
249
275
@@ -282,8 +308,8 @@ private String assembleGrayReleaseRuleKey(String configAppId, String configClust
282
308
}
283
309
284
310
private String assembleReversedGrayReleaseRuleKey (String clientAppId , String
285
- clientNamespaceName , String clientIp ) {
286
- return STRING_JOINER .join (clientAppId , clientNamespaceName , clientIp );
311
+ clientNamespaceName , String clientIpOrLabel ) {
312
+ return STRING_JOINER .join (clientAppId , clientNamespaceName , clientIpOrLabel );
287
313
}
288
314
289
315
}
0 commit comments