/
LabelSelector.java
288 lines (224 loc) · 10 KB
/
LabelSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.selector;
import com.alibaba.nacos.api.cmdb.pojo.PreservedEntityTypes;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.cmdb.service.CmdbReader;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.sys.utils.ApplicationUtils;
import com.alibaba.nacos.naming.core.Instance;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A selector to implement a so called same-label-prior rule for service discovery.
* <h2>Backgroup</h2>
* Consider service providers are deployed in two sites i.e. site A and site B, and consumers of this service provider
* are also deployed in site A and site B. So the consumers may want to visit the service provider in current site, thus
* consumers in site A visit service providers in site A and consumers in site B visit service providers in site B. This
* is quite useful to reduce the transfer delay of RPC. This is called same-site-prior strategy.
* <h2>Same Label Prior</h2>
* The same-site-prior strategy covers many circumstances in large companies and we can abstract it to a higher level
* strategy: same-label-prior.
*
* <p>So the idea is that presumed we have built a self-defined or integrated a third-party idc CMDB which stores all
* the labels of all IPs. Then we can filter provider IPs by the consumer IP and we only return the providers who have
* the same label values with consumer. We can define the labels we want to include in the comparison.
*
* <p>If no provider has the same label value with the consumer, we fall back to give all providers to the consumer.
* Note that this fallback strategy may also be abstracted in future to introduce more kinds of behaviors.
*
* @author nkorange
* @see CmdbReader
* @since 0.7.0
*/
@JsonTypeInfo(use = Id.NAME, property = "type")
public class LabelSelector extends ExpressionSelector implements Selector {
/**
* The labels relevant to this the selector.
*
* @see com.alibaba.nacos.api.cmdb.pojo.Label
*/
private Set<String> labels;
private static final Set<String> SUPPORTED_INNER_CONNCETORS = new HashSet<>();
private static final Set<String> SUPPORTED_OUTER_CONNCETORS = new HashSet<>();
private static final String CONSUMER_PREFIX = "CONSUMER.label.";
private static final String PROVIDER_PREFIX = "PROVIDER.label.";
private static final char CEQUAL = '=';
private static final char CAND = '&';
static {
SUPPORTED_INNER_CONNCETORS.add(String.valueOf(CEQUAL));
SUPPORTED_OUTER_CONNCETORS.add(String.valueOf(CAND));
JacksonUtils.registerSubtype(LabelSelector.class, SelectorType.label.name());
}
public Set<String> getLabels() {
return labels;
}
public void setLabels(Set<String> labels) {
this.labels = labels;
}
public LabelSelector() {
super();
}
private CmdbReader getCmdbReader() {
return ApplicationUtils.getBean(CmdbReader.class);
}
public static Set<String> parseExpression(String expression) throws NacosException {
return ExpressionInterpreter.parseExpression(expression);
}
@Override
public List<Instance> select(String consumer, List<Instance> providers) {
if (labels.isEmpty()) {
return providers;
}
List<Instance> instanceList = new ArrayList<>();
for (Instance instance : providers) {
boolean matched = true;
for (String labelName : getLabels()) {
String consumerLabelValue = getCmdbReader()
.queryLabel(consumer, PreservedEntityTypes.ip.name(), labelName);
if (StringUtils.isNotBlank(consumerLabelValue) && !StringUtils.equals(consumerLabelValue,
getCmdbReader().queryLabel(instance.getIp(), PreservedEntityTypes.ip.name(), labelName))) {
matched = false;
break;
}
}
if (matched) {
instanceList.add(instance);
}
}
if (instanceList.isEmpty()) {
return providers;
}
return instanceList;
}
/**
* Expression interpreter for label selector.
*
* <p>For now it supports very limited set of syntax rules.
*/
public static class ExpressionInterpreter {
/**
* Parse the label expression.
*
* <p>Currently we support the very single type of expression:
* <pre>
* consumer.labelA = provider.labelA & consumer.labelB = provider.labelB
* </pre>
* Later we will implement a interpreter to parse this expression in a standard LL parser way.
*
* @param expression the label expression to parse
* @return collection of labels
*/
public static Set<String> parseExpression(String expression) throws NacosException {
if (StringUtils.isBlank(expression)) {
return new HashSet<>();
}
expression = StringUtils.deleteWhitespace(expression);
List<String> elements = getTerms(expression);
Set<String> gotLabels = new HashSet<>();
int index = 0;
index = checkInnerSyntax(elements, index);
if (index == -1) {
throw new NacosException(NacosException.INVALID_PARAM, "parse expression failed!");
}
gotLabels.add(elements.get(index++).split(PROVIDER_PREFIX)[1]);
while (index < elements.size()) {
index = checkOuterSyntax(elements, index);
if (index >= elements.size()) {
return gotLabels;
}
if (index == -1) {
throw new NacosException(NacosException.INVALID_PARAM, "parse expression failed!");
}
gotLabels.add(elements.get(index++).split(PROVIDER_PREFIX)[1]);
}
return gotLabels;
}
public static List<String> getTerms(String expression) {
List<String> terms = new ArrayList<>();
Set<Character> characters = new HashSet<>();
characters.add(CEQUAL);
characters.add(CAND);
char[] chars = expression.toCharArray();
int lastIndex = 0;
for (int index = 0; index < chars.length; index++) {
char ch = chars[index];
if (characters.contains(ch)) {
terms.add(expression.substring(lastIndex, index));
terms.add(expression.substring(index, index + 1));
index++;
lastIndex = index;
}
}
terms.add(expression.substring(lastIndex, chars.length));
return terms;
}
private static int skipEmpty(List<String> elements, int start) {
while (start < elements.size() && StringUtils.isBlank(elements.get(start))) {
start++;
}
return start;
}
private static int checkOuterSyntax(List<String> elements, int start) {
int index = start;
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return index;
}
if (!SUPPORTED_OUTER_CONNCETORS.contains(elements.get(index++))) {
return -1;
}
return checkInnerSyntax(elements, index);
}
private static int checkInnerSyntax(List<String> elements, int start) {
int index = start;
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return -1;
}
if (!elements.get(index).startsWith(CONSUMER_PREFIX)) {
return -1;
}
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return -1;
}
if (!SUPPORTED_INNER_CONNCETORS.contains(elements.get(index++))) {
return -1;
}
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return -1;
}
if (!elements.get(index).startsWith(PROVIDER_PREFIX)) {
return -1;
}
String labelProvider = elements.get(index).split(PROVIDER_PREFIX)[1];
String labelConsumer = elements.get(index++).split(CONSUMER_PREFIX)[1];
if (!labelConsumer.equals(labelProvider)) {
return -1;
}
return index;
}
}
}