Skip to content

Commit

Permalink
Config and log enhancement for cluster token client
Browse files Browse the repository at this point in the history
- Refactor token client common config and assign config
- Log enhancement when transport to token server failed
- Add `getState` method to `ClusterTokenClient` interface

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Jan 4, 2019
1 parent 77df7d2 commit 40368bd
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import java.util.concurrent.atomic.AtomicBoolean;

import com.alibaba.csp.sentinel.cluster.ClusterConstants;
import com.alibaba.csp.sentinel.cluster.ClusterErrorMessages;
import com.alibaba.csp.sentinel.cluster.ClusterTransportClient;
import com.alibaba.csp.sentinel.cluster.TokenResult;
import com.alibaba.csp.sentinel.cluster.TokenResultStatus;
import com.alibaba.csp.sentinel.cluster.TokenServerDescriptor;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientAssignConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager;
import com.alibaba.csp.sentinel.cluster.client.config.ServerChangeObserver;
import com.alibaba.csp.sentinel.cluster.log.ClusterClientStatLogUtil;
Expand Down Expand Up @@ -51,14 +52,14 @@ public class DefaultClusterTokenClient implements ClusterTokenClient {
public DefaultClusterTokenClient() {
ClusterClientConfigManager.addServerChangeObserver(new ServerChangeObserver() {
@Override
public void onRemoteServerChange(ClusterClientConfig clusterClientConfig) {
changeServer(clusterClientConfig);
public void onRemoteServerChange(ClusterClientAssignConfig assignConfig) {
changeServer(assignConfig);
}
});
initNewConnection();
}

private boolean serverEqual(TokenServerDescriptor descriptor, ClusterClientConfig config) {
private boolean serverEqual(TokenServerDescriptor descriptor, ClusterClientAssignConfig config) {
if (descriptor == null || config == null) {
return false;
}
Expand All @@ -84,7 +85,7 @@ private void initNewConnection() {
}
}

private void changeServer(/*@Valid*/ ClusterClientConfig config) {
private void changeServer(/*@Valid*/ ClusterClientAssignConfig config) {
if (serverEqual(serverDescriptor, config)) {
return;
}
Expand All @@ -93,7 +94,7 @@ private void changeServer(/*@Valid*/ ClusterClientConfig config) {
transportClient.stop();
}
// Replace with new, even if the new client is not ready.
this.transportClient = new NettyTransportClient(config);
this.transportClient = new NettyTransportClient(config.getServerHost(), config.getServerPort());
this.serverDescriptor = new TokenServerDescriptor(config.getServerHost(), config.getServerPort());
startClientIfScheduled();
RecordLog.info("[DefaultClusterTokenClient] New client created: " + serverDescriptor);
Expand Down Expand Up @@ -132,6 +133,14 @@ public void stop() throws Exception {
stopClientIfStarted();
}

@Override
public int getState() {
if (transportClient == null) {
return ClientConstants.CLIENT_STATUS_OFF;
}
return transportClient.isReady() ? ClientConstants.CLIENT_STATUS_STARTED : ClientConstants.CLIENT_STATUS_OFF;
}

@Override
public TokenServerDescriptor currentServer() {
return serverDescriptor;
Expand All @@ -146,7 +155,9 @@ public TokenResult requestToken(Long flowId, int acquireCount, boolean prioritiz
.setFlowId(flowId).setPriority(prioritized);
ClusterRequest<FlowRequestData> request = new ClusterRequest<>(ClusterConstants.MSG_TYPE_FLOW, data);
try {
return sendTokenRequest(request);
TokenResult result = sendTokenRequest(request);
logForResult(result);
return result;
} catch (Exception ex) {
ClusterClientStatLogUtil.log(ex.getMessage());
return new TokenResult(TokenResultStatus.FAIL);
Expand All @@ -162,16 +173,31 @@ public TokenResult requestParamToken(Long flowId, int acquireCount, Collection<O
.setFlowId(flowId).setParams(params);
ClusterRequest<ParamFlowRequestData> request = new ClusterRequest<>(ClusterConstants.MSG_TYPE_PARAM_FLOW, data);
try {
return sendTokenRequest(request);
TokenResult result = sendTokenRequest(request);
logForResult(result);
return result;
} catch (Exception ex) {
ClusterClientStatLogUtil.log(ex.getMessage());
return new TokenResult(TokenResultStatus.FAIL);
}
}

private void logForResult(TokenResult result) {
switch (result.getStatus()) {
case TokenResultStatus.NO_RULE_EXISTS:
ClusterClientStatLogUtil.log(ClusterErrorMessages.NO_RULES_IN_SERVER);
break;
case TokenResultStatus.TOO_MANY_REQUEST:
ClusterClientStatLogUtil.log(ClusterErrorMessages.TOO_MANY_REQUESTS);
break;
default:
}
}

private TokenResult sendTokenRequest(ClusterRequest request) throws Exception {
if (transportClient == null) {
RecordLog.warn("[DefaultClusterTokenClient] Client not created, please check your config for cluster client");
RecordLog.warn(
"[DefaultClusterTokenClient] Client not created, please check your config for cluster client");
return clientFail();
}
ClusterResponse response = transportClient.sendRequest(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.csp.sentinel.cluster.client.config;

/**
* @author Eric Zhao
* @since 1.4.1
*/
public class ClusterClientAssignConfig {

private String serverHost;
private Integer serverPort;

public ClusterClientAssignConfig() {}

public ClusterClientAssignConfig(String serverHost, Integer serverPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
}

public String getServerHost() {
return serverHost;
}

public ClusterClientAssignConfig setServerHost(String serverHost) {
this.serverHost = serverHost;
return this;
}

public Integer getServerPort() {
return serverPort;
}

public ClusterClientAssignConfig setServerPort(Integer serverPort) {
this.serverPort = serverPort;
return this;
}

@Override
public String toString() {
return "ClusterClientAssignConfig{" +
"serverHost='" + serverHost + '\'' +
", serverPort=" + serverPort +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,21 @@
*/
public class ClusterClientConfig {

private String serverHost;
private int serverPort;
private Integer requestTimeout;

private int requestTimeout;
private int connectTimeout;

public String getServerHost() {
return serverHost;
}

public ClusterClientConfig setServerHost(String serverHost) {
this.serverHost = serverHost;
return this;
}

public int getServerPort() {
return serverPort;
}

public ClusterClientConfig setServerPort(int serverPort) {
this.serverPort = serverPort;
return this;
}

public int getRequestTimeout() {
public Integer getRequestTimeout() {
return requestTimeout;
}

public ClusterClientConfig setRequestTimeout(int requestTimeout) {
public ClusterClientConfig setRequestTimeout(Integer requestTimeout) {
this.requestTimeout = requestTimeout;
return this;
}

public int getConnectTimeout() {
return connectTimeout;
}

public ClusterClientConfig setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

@Override
public String toString() {
return "ClusterClientConfig{" +
"serverHost='" + serverHost + '\'' +
", serverPort=" + serverPort +
", requestTimeout=" + requestTimeout +
", connectTimeout=" + connectTimeout +
"requestTimeout=" + requestTimeout +
'}';
}
}
Loading

0 comments on commit 40368bd

Please sign in to comment.