Skip to content

Commit d8bbd91

Browse files
authored
Merge pull request alibaba#257 from alibaba/feature/cluster-core
Add basic interface and adaptations for Sentinel cluster flow control in `sentinel-core` (alibaba#257)
2 parents 34b66c7 + b59d6d1 commit d8bbd91

File tree

23 files changed

+1377
-319
lines changed

23 files changed

+1377
-319
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
/**
19+
* Token client interface for distributed flow control.
20+
*
21+
* @author Eric Zhao
22+
* @since 1.4.0
23+
*/
24+
public interface ClusterTokenClient extends TokenService {
25+
26+
/**
27+
* Get descriptor of current token server.
28+
*
29+
* @return current token server
30+
*/
31+
TokenServerDescriptor currentServer();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.ServiceLoader;
21+
22+
import com.alibaba.csp.sentinel.log.RecordLog;
23+
24+
/**
25+
* Provider for a universal {@link ClusterTokenClient} instance.
26+
*
27+
* @author Eric Zhao
28+
* @since 1.4.0
29+
*/
30+
public final class TokenClientProvider {
31+
32+
private static ClusterTokenClient client = null;
33+
34+
private static final ServiceLoader<ClusterTokenClient> LOADER = ServiceLoader.load(ClusterTokenClient.class);
35+
36+
static {
37+
// Not strictly thread-safe, but it's OK since it will be resolved only once.
38+
resolveTokenClientInstance();
39+
}
40+
41+
public static ClusterTokenClient getClient() {
42+
return client;
43+
}
44+
45+
private static void resolveTokenClientInstance() {
46+
List<ClusterTokenClient> clients = new ArrayList<ClusterTokenClient>();
47+
for (ClusterTokenClient client : LOADER) {
48+
clients.add(client);
49+
}
50+
51+
if (!clients.isEmpty()) {
52+
// Get first.
53+
client = clients.get(0);
54+
RecordLog.info("[TokenClientProvider] Token client resolved: " + client.getClass().getCanonicalName());
55+
} else {
56+
RecordLog.warn("[TokenClientProvider] No existing token client, resolve failed");
57+
}
58+
}
59+
60+
private TokenClientProvider() {}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
import java.util.Map;
19+
20+
/**
21+
* Result entity of acquiring cluster flow token.
22+
*
23+
* @author Eric Zhao
24+
* @since 1.4.0
25+
*/
26+
public class TokenResult {
27+
28+
private Integer status;
29+
30+
private int remaining;
31+
private int waitInMs;
32+
33+
private Map<String, String> attachments;
34+
35+
public TokenResult() {}
36+
37+
public TokenResult(Integer status) {
38+
this.status = status;
39+
}
40+
41+
public Integer getStatus() {
42+
return status;
43+
}
44+
45+
public TokenResult setStatus(Integer status) {
46+
this.status = status;
47+
return this;
48+
}
49+
50+
public int getRemaining() {
51+
return remaining;
52+
}
53+
54+
public TokenResult setRemaining(int remaining) {
55+
this.remaining = remaining;
56+
return this;
57+
}
58+
59+
public int getWaitInMs() {
60+
return waitInMs;
61+
}
62+
63+
public TokenResult setWaitInMs(int waitInMs) {
64+
this.waitInMs = waitInMs;
65+
return this;
66+
}
67+
68+
public Map<String, String> getAttachments() {
69+
return attachments;
70+
}
71+
72+
public TokenResult setAttachments(Map<String, String> attachments) {
73+
this.attachments = attachments;
74+
return this;
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return "TokenResult{" +
80+
"status=" + status +
81+
", remaining=" + remaining +
82+
", waitInMs=" + waitInMs +
83+
", attachments=" + attachments +
84+
'}';
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
/**
19+
* @author Eric Zhao
20+
* @since 1.4.0
21+
*/
22+
public final class TokenResultStatus {
23+
24+
/**
25+
* Bad client request.
26+
*/
27+
public static final int BAD_REQUEST = -4;
28+
/**
29+
* Server or client unexpected failure (due to transport or serialization failure).
30+
*/
31+
public static final int FAIL = -1;
32+
33+
/**
34+
* Token acquired.
35+
*/
36+
public static final int OK = 0;
37+
38+
/**
39+
* Token acquire failed (blocked).
40+
*/
41+
public static final int BLOCKED = 1;
42+
/**
43+
* Should wait for next buckets.
44+
*/
45+
public static final int SHOULD_WAIT = 2;
46+
/**
47+
* Token acquire failed (no rule exists).
48+
*/
49+
public static final int NO_RULE_EXISTS = 3;
50+
/**
51+
* Token acquire failed (reference resource is not available).
52+
*/
53+
public static final int NO_REF_RULE_EXISTS = 4;
54+
/**
55+
* Token acquire failed (strategy not available).
56+
*/
57+
public static final int NOT_AVAILABLE = 5;
58+
59+
private TokenResultStatus() {}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
/**
19+
* A simple descriptor for Sentinel token server.
20+
*
21+
* @author Eric Zhao
22+
* @since 1.4.0
23+
*/
24+
public class TokenServerDescriptor {
25+
26+
private String host;
27+
private int port;
28+
private String type;
29+
30+
public TokenServerDescriptor() {}
31+
32+
public TokenServerDescriptor(String host, int port) {
33+
this.host = host;
34+
this.port = port;
35+
}
36+
37+
public String getHost() {
38+
return host;
39+
}
40+
41+
public TokenServerDescriptor setHost(String host) {
42+
this.host = host;
43+
return this;
44+
}
45+
46+
public int getPort() {
47+
return port;
48+
}
49+
50+
public TokenServerDescriptor setPort(int port) {
51+
this.port = port;
52+
return this;
53+
}
54+
55+
public String getType() {
56+
return type;
57+
}
58+
59+
public TokenServerDescriptor setType(String type) {
60+
this.type = type;
61+
return this;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "TokenServerDescriptor{" +
67+
"host='" + host + '\'' +
68+
", port=" + port +
69+
", type='" + type + '\'' +
70+
'}';
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.cluster;
17+
18+
import java.util.Collection;
19+
20+
/**
21+
* Service interface of flow control.
22+
*
23+
* @author Eric Zhao
24+
* @since 1.4.0
25+
*/
26+
public interface TokenService {
27+
28+
/**
29+
* Request tokens from remote token server.
30+
*
31+
* @param ruleId the unique rule ID
32+
* @param acquireCount token count to acquire
33+
* @param prioritized whether the request is prioritized
34+
* @return result of the token request
35+
*/
36+
TokenResult requestToken(Long ruleId, int acquireCount, boolean prioritized);
37+
38+
/**
39+
* Request tokens for a specific parameter from remote token server.
40+
*
41+
* @param ruleId the unique rule ID
42+
* @param acquireCount token count to acquire
43+
* @param params parameter list
44+
* @return result of the token request
45+
*/
46+
TokenResult requestParamToken(Long ruleId, int acquireCount, Collection<Object> params);
47+
}

0 commit comments

Comments
 (0)