use of com.actiontech.dble.config.loader.zkprocess.zookeeper.process.BinlogPause in project dble by actiontech.
the class ShowBinlogStatus method showBinlogWithUcore.
private static void showBinlogWithUcore(ManagerConnection c, long timeout) {
// step 1 get the distributeLock of the ucore
UDistributeLock distributeLock = new UDistributeLock(UcorePathUtil.getBinlogPauseLockPath(), UcoreConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID));
try {
if (!distributeLock.acquire()) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "There is another command is showing BinlogStatus");
return;
}
try {
// step 2 try to lock all the commit flag in server
if (!DbleServer.getInstance().getBackupLocked().compareAndSet(false, true)) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "There is another command is showing BinlogStatus");
} else {
// step 3 notify other dble to stop the commit & set self status
BinlogPause pauseOnInfo = new BinlogPause(UcoreConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID), BinlogPauseStatus.ON);
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatus(), pauseOnInfo.toString());
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatusSelf(), "true");
// step 4 wait til other dbles to feedback the ucore flag
long beginTime = TimeUtil.currentTimeMillis();
boolean isAllSuccess = true;
List<UKvBean> responseList = ClusterUcoreSender.getKeyTree(UcorePathUtil.getBinlogPauseStatus());
List<UKvBean> onlineList = ClusterUcoreSender.getKeyTree(UcorePathUtil.getOnlinePath());
while (responseList.size() < onlineList.size()) {
// if it is time out
if (TimeUtil.currentTimeMillis() > beginTime + 2 * timeout) {
isAllSuccess = false;
errMsg = "timeout while waiting for unfinished distributed transactions.";
logger.info(errMsg);
break;
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000));
responseList = ClusterUcoreSender.getKeyTree(UcorePathUtil.getBinlogPauseStatus());
onlineList = ClusterUcoreSender.getKeyTree(UcorePathUtil.getOnlinePath());
}
// step 5 check the result if the all the dbles return sucess
if (isAllSuccess) {
for (UKvBean reponseBean : responseList) {
if (!Boolean.parseBoolean(reponseBean.getValue())) {
isAllSuccess = false;
}
}
}
// step 6 query for the GTID and write back to frontend connections
if (isAllSuccess) {
getQueryResult(c.getCharset().getResults());
}
writeResponse(c);
// step 7 delete the KVtree and notify the cluster
ClusterUcoreSender.deleteKVTree(UcorePathUtil.getBinlogPauseStatus() + SEPARATOR);
BinlogPause pauseOffInfo = new BinlogPause(UcoreConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID), BinlogPauseStatus.OFF);
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatus(), pauseOffInfo.toString());
}
} catch (Exception e) {
logger.info("catch Exception", e);
} finally {
DbleServer.getInstance().getBackupLocked().compareAndSet(true, false);
distributeLock.release();
}
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, e.getMessage());
}
}
use of com.actiontech.dble.config.loader.zkprocess.zookeeper.process.BinlogPause in project dble by actiontech.
the class ShowBinlogStatus method showBinlogWithZK.
private static void showBinlogWithZK(ManagerConnection c, long timeout) {
CuratorFramework zkConn = ZKUtils.getConnection();
String lockPath = KVPathUtil.getBinlogPauseLockPath();
InterProcessMutex distributeLock = new InterProcessMutex(zkConn, lockPath);
try {
// zkLock, the other instance cant't get lock before finished
if (!distributeLock.acquire(100, TimeUnit.MILLISECONDS)) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "There is another command is showing BinlogStatus");
return;
}
try {
if (!DbleServer.getInstance().getBackupLocked().compareAndSet(false, true)) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, "There is another command is showing BinlogStatus");
} else {
errMsg = null;
// notify zk to wait all session
String binlogStatusPath = KVPathUtil.getBinlogPauseStatus();
String binlogPause = KVPathUtil.getBinlogPauseInstance();
BinlogPause pauseOnInfo = new BinlogPause(ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID), BinlogPauseStatus.ON);
zkConn.setData().forPath(binlogStatusPath, pauseOnInfo.toString().getBytes(StandardCharsets.UTF_8));
long beginTime = TimeUtil.currentTimeMillis();
boolean isPaused = waitAllSession(c, timeout, beginTime);
// tell zk this instance has prepared
ZKUtils.createTempNode(binlogPause, ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID), String.valueOf(isPaused).getBytes(StandardCharsets.UTF_8));
// check all session waiting status
List<String> preparedList = zkConn.getChildren().forPath(binlogPause);
List<String> onlineList = zkConn.getChildren().forPath(KVPathUtil.getOnlinePath());
// TODO: While waiting, a new instance of dble is upping and working.
boolean isAllSuccess = true;
while (preparedList.size() < onlineList.size()) {
if (TimeUtil.currentTimeMillis() > beginTime + 2 * timeout) {
isAllSuccess = false;
errMsg = "timeout while waiting for unfinished distributed transactions.";
logger.info(errMsg);
break;
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000));
onlineList = zkConn.getChildren().forPath(KVPathUtil.getOnlinePath());
preparedList = zkConn.getChildren().forPath(binlogPause);
}
if (isAllSuccess) {
for (String preparedNode : preparedList) {
String preparePath = ZKPaths.makePath(binlogPause, preparedNode);
byte[] resultStatus = zkConn.getData().forPath(preparePath);
String data = new String(resultStatus, StandardCharsets.UTF_8);
if (!Boolean.parseBoolean(data)) {
isAllSuccess = false;
}
}
}
if (isAllSuccess) {
getQueryResult(c.getCharset().getResults());
}
writeResponse(c);
BinlogPause pauseOffInfo = new BinlogPause(ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID), BinlogPauseStatus.OFF);
zkConn.setData().forPath(binlogStatusPath, pauseOffInfo.toString().getBytes(StandardCharsets.UTF_8));
zkConn.delete().forPath(ZKPaths.makePath(binlogPause, ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID)));
List<String> releaseList = zkConn.getChildren().forPath(binlogPause);
while (releaseList.size() != 0) {
releaseList = zkConn.getChildren().forPath(binlogPause);
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000));
}
}
} catch (Exception e) {
logger.info("catch Exception", e);
} finally {
DbleServer.getInstance().getBackupLocked().compareAndSet(true, false);
distributeLock.release();
}
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_UNKNOWN_ERROR, e.getMessage());
}
}
use of com.actiontech.dble.config.loader.zkprocess.zookeeper.process.BinlogPause in project dble by actiontech.
the class OfflineStatusListener method releaseForBinlog.
private void releaseForBinlog(String crashNode) {
String binlogStatusPath = KVPathUtil.getBinlogPauseStatus();
CuratorFramework zkConn = ZKUtils.getConnection();
try {
byte[] binlogStatusData = zkConn.getData().forPath(binlogStatusPath);
if (binlogStatusData == null) {
return;
}
String data = new String(binlogStatusData, StandardCharsets.UTF_8);
BinlogPause binlogPauseInfo = new BinlogPause(data);
if (!binlogPauseInfo.getFrom().equals(crashNode)) {
return;
}
if (BinlogPause.BinlogPauseStatus.ON == binlogPauseInfo.getStatus()) {
// ClusterParamCfg.CLUSTER_CFG_MYID
String instancePath = ZKPaths.makePath(KVPathUtil.getBinlogPauseInstance(), ZkConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID));
boolean needDelete = true;
long beginTime = TimeUtil.currentTimeMillis();
long timeout = DbleServer.getInstance().getConfig().getSystem().getShowBinlogStatusTimeout();
while (zkConn.checkExists().forPath(instancePath) == null) {
// wait 2* timeout to release itself
if (TimeUtil.currentTimeMillis() > beginTime + 2 * timeout) {
LOGGER.warn(AlarmCode.CORE_ZK_WARN + "checkExists of " + instancePath + " time out");
needDelete = false;
break;
}
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1000));
}
if (needDelete) {
try {
zkConn.delete().forPath(instancePath);
} catch (Exception e) {
LOGGER.warn(AlarmCode.CORE_ZK_WARN + " delete binlogPause instance failed", e);
}
}
DbleServer.getInstance().getBackupLocked().compareAndSet(true, false);
}
LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + " service instance[" + crashNode + "] has crashed." + "Please manually make sure node [" + binlogStatusPath + "] status in zookeeper " + "after every instance received this message");
} catch (Exception e) {
LOGGER.warn(AlarmCode.CORE_ZK_WARN + " releaseForBinlog error", e);
}
}
use of com.actiontech.dble.config.loader.zkprocess.zookeeper.process.BinlogPause in project dble by actiontech.
the class UOffLineListener method checkBinlogStatusRelease.
private void checkBinlogStatusRelease(String serverId) {
try {
// check the lastest binglog status
UKvBean lock = ClusterUcoreSender.getKey(UcorePathUtil.getBinlogPauseLockPath());
if ("".equals(lock.getValue()) || serverId.equals(lock.getValue())) {
DbleServer.getInstance().getBackupLocked().compareAndSet(true, false);
}
UKvBean status = ClusterUcoreSender.getKey(UcorePathUtil.getBinlogPauseStatus());
if (!"".equals(status.getValue())) {
BinlogPause pauseInfo = new BinlogPause(status.getValue());
if (pauseInfo.getStatus() == BinlogPause.BinlogPauseStatus.ON && serverId.equals(pauseInfo.getFrom())) {
ClusterUcoreSender.deleteKVTree(UcorePathUtil.getBinlogPauseStatus() + "/");
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatus(), (new BinlogPause("", BinlogPause.BinlogPauseStatus.OFF)).toString());
ClusterUcoreSender.deleteKV(UcorePathUtil.getBinlogPauseLockPath());
}
}
} catch (Exception e) {
LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + " server offline binlog status check error");
}
}
use of com.actiontech.dble.config.loader.zkprocess.zookeeper.process.BinlogPause in project dble by actiontech.
the class UBinlogPauseStatusResponse method notifyProcess.
@Override
public void notifyProcess(UKvBean configValue) throws Exception {
// step 1 check if the block is from the server itself
BinlogPause pauseInfo = new BinlogPause(configValue.getValue());
if (pauseInfo.getFrom().equals(UcoreConfig.getInstance().getValue(ClusterParamCfg.CLUSTER_CFG_MYID))) {
return;
}
// step 2 if the flag is on than try to lock all the commit
if (pauseInfo.getStatus() == BinlogPause.BinlogPauseStatus.ON && configValue.getChangeType() != UKvBean.DELETE) {
DbleServer.getInstance().getBackupLocked().compareAndSet(false, true);
boolean isPaused = ShowBinlogStatus.waitAllSession();
if (!isPaused) {
cleanResource();
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatusSelf(), "Error can't wait all session finished ");
return;
}
try {
ClusterUcoreSender.sendDataToUcore(UcorePathUtil.getBinlogPauseStatusSelf(), "true");
} catch (Exception e) {
cleanResource();
LOGGER.warn(AlarmCode.CORE_ZK_WARN + "create binlogPause instance failed", e);
}
} else if (pauseInfo.getStatus() == BinlogPause.BinlogPauseStatus.OFF) {
// step 3 if the flag is off than try to unlock the commit
cleanResource();
}
}
Aggregations