com.alibaba.csp.ahas.switchcenter.AhasSwitchSdkService Maven / Gradle / Ivy
package com.alibaba.csp.ahas.switchcenter;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import com.alibaba.csp.ahas.switchcenter.util.NamedThreadFactory;
import com.taobao.csp.ahas.service.api.client.ClientInfoService;
import com.taobao.csp.ahas.service.api.constant.AppConstants;
import com.taobao.csp.ahas.service.api.transport.TransportService;
import com.taobao.csp.ahas.service.component.AgwComponent;
import com.taobao.csp.ahas.service.component.AgwComponentManager;
import com.taobao.csp.ahas.service.component.AgwProductCode;
import com.taobao.csp.ahas.service.heartbeat.HeartbeatService;
import com.taobao.csp.ahas.service.init.AhasInitFunc;
import com.taobao.csp.ahas.transport.api.Response;
import com.taobao.csp.ahas.transport.api.ServiceConstants.Switch;
import com.taobao.csp.switchcenter.command.handler.GetCommandHandler;
import com.taobao.csp.switchcenter.command.handler.ListCommandHandler;
import com.taobao.csp.switchcenter.command.handler.SetCommandHandler;
import com.taobao.csp.switchcenter.command.handler.VersionCommandHandler;
import com.taobao.csp.switchcenter.command.handler.ViewCommandHandler;
import com.taobao.csp.switchcenter.exception.SwitchCenterError;
import com.taobao.csp.switchcenter.log.SwitchRecordLog;
import com.taobao.middleware.logger.support.LogLog;
public class AhasSwitchSdkService {
private static final String SWITCH_CLIENT_VERSION = "1.1.0";
private static final AgwComponent agwComponent = AgwComponentManager.getAgwComponent(AgwProductCode.SWITCH);
private static final ClientInfoService clientInfoService = agwComponent.getClientInfoService();
private static final TransportService transportService = agwComponent.getTransportService();
private static final HeartbeatService heartbeatService = agwComponent.getHeartbeatService();
private static final AtomicBoolean isAhasInit = new AtomicBoolean(false);
private static final AtomicBoolean isCommandHandlerRegistered = new AtomicBoolean(false);
private static final AtomicBoolean ahasInitSuccess = new AtomicBoolean(false);
private static final int INIT_MAX_RETRY_TIMES = 5;
private ScheduledExecutorService heartbeatPool = new ScheduledThreadPoolExecutor(2,
new NamedThreadFactory("ahas-switch-heartbeat-send-task", true), new DiscardOldestPolicy());
private static final int HEARTBEAT_INTERVAL_MS = 5 * 1000;
private static AhasSwitchSdkService switchSdkService;
public static synchronized AhasSwitchSdkService getInstance() {
if (switchSdkService == null) {
switchSdkService = new AhasSwitchSdkService();
}
return switchSdkService;
}
public AhasSwitchSdkService() {
try {
if (isCommandHandlerRegistered.compareAndSet(false, true)) {
registerSwitchCommandHandler();
if (AgwComponentManager.isInner()) {
AhasHttpRequestHandler.init();
}
}
} catch (Throwable e) {
SwitchRecordLog.info("[AhasSwitchDefaultSdkService] registerCommandHandler error", e);
}
initAhasSwitch();
ahasInitSuccess.set(true);
SwitchRecordLog.info("set initSuccess Flag to true");
// heartbeat
initHeartbeatTask();
}
public void init() {
if (!ahasInitSuccess.get()) {
return;
}
try {
// do not print stack trace when logger not found for ACM.
LogLog.setQuietMode(true);
SwitchAcmDatasourceService switchAcmDataSourceService = new SwitchAcmDatasourceService(clientInfoService);
switchAcmDataSourceService.init();
} catch (Throwable throwable) {
SwitchRecordLog.error(null, "[AhasSwitchDefaultSdkService] init acm error ", throwable);
}
}
private void initAhasSwitch() {
if (!isAhasInit.compareAndSet(false, true)) {
return;
}
// set sentinel sdk version by hand.
// TODO always remember keep the version same as in pom!!!
System.setProperty(AppConstants.AHAS_SWITCH_VERSION, SWITCH_CLIENT_VERSION);
//skip init ahas agw
if (System.getProperty(AppConstants.AHAS_INNER_AGW_DISABLE) != null){
SwitchRecordLog.info("[AhasSwitchDefaultSdkService] not need init ahas gateway");
return;
}
int times = 0;
boolean success = false;
while (true && times < INIT_MAX_RETRY_TIMES) {
try {
((AhasInitFunc) clientInfoService).init("JAVA_SDK", null);
transportService.init(clientInfoService);
heartbeatService.init(agwComponent);
success = true;
break;
} catch (Throwable throwable) {
SwitchRecordLog.info("[AhasSwitchDefaultSdkService] init ahas fail, will retry " + times, throwable);
try {
Thread.sleep(1000 * 5);
} catch (Exception ignore) {
}
} finally {
times++;
}
}
if (!success) {
throw new SwitchCenterError("AhasSwitchDefaultSdkService init failed");
}
SwitchRecordLog.info("[AhasSwitchDefaultSdkService] AHAS gateway host: " + clientInfoService.getGatewayHost()
+ "port:" + clientInfoService.getGatewayPort());
SwitchRecordLog.info("[AhasSwitchDefaultSdkService] Really init AHAS");
AhasGlobalContext.setClientInfoService(clientInfoService);
}
/**
* Register Switch handler to {@link TransportService}
*/
@SuppressWarnings("unchecked")
private void registerSwitchCommandHandler() {
transportService.registerHandler(Switch.GET_SWITCH.getHandlerName(),
new AhasRequestHandler(new GetCommandHandler()));
transportService.registerHandler(Switch.SET_SWITCH.getHandlerName(),
new AhasRequestHandler(new SetCommandHandler()));
transportService.registerHandler(Switch.LIST_SWITCH.getHandlerName(),
new AhasRequestHandler(new ListCommandHandler()));
transportService.registerHandler(Switch.VIEW_SWITCH.getHandlerName(),
new AhasRequestHandler(new ViewCommandHandler()));
transportService.registerHandler(Switch.VERSION_SWITCH.getHandlerName(),
new AhasRequestHandler(new VersionCommandHandler()));
}
private void initHeartbeatTask() {
heartbeatPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
sendHeartbeat();
} catch (Throwable e) {
SwitchRecordLog.info("[HeartbeatSender] Send heartbeat error", e);
}
}
}, 5000, clientInfoService.getHeartbeatRate(), TimeUnit.SECONDS);
SwitchRecordLog.info("[AhasSwitchInitFunc] HeartbeatSender started, heartbeatRate " + clientInfoService.getHeartbeatRate());
}
private boolean sendHeartbeat() throws Exception {
if (!ahasInitSuccess.get()) {
return false;
}
Response> mapResponse = heartbeatService.sendHeartbeat();
if (!mapResponse.isSuccess()) {
throw new RuntimeException(mapResponse.toString());
}
return mapResponse.isSuccess();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy