use of com.bonree.brfs.common.rebalance.route.NormalRoute in project BRFS by zhangnianli.
the class RouteRoleCache method loadRouteRole.
private void loadRouteRole() throws Exception {
// load virtual id
try {
String virtualPath = ZKPaths.makePath(baseRoutePath, Constants.VIRTUAL_ROUTE, String.valueOf(storageIndex));
if (zkClient.checkExists().forPath(virtualPath) != null) {
List<String> virtualNodes = zkClient.getChildren().forPath(virtualPath);
if (virtualNodes != null && !virtualNodes.isEmpty()) {
for (String virtualNode : virtualNodes) {
try {
byte[] data = zkClient.getData().forPath(ZKPaths.makePath(virtualPath, virtualNode));
VirtualRoute virtual = JSON.parseObject(BrStringUtils.fromUtf8Bytes(data), VirtualRoute.class);
virtualRouteDetail.put(virtual.getVirtualID(), virtual);
} catch (Exception e) {
LOG.error("load virtual route[{}] error", virtualNode, e);
}
}
}
}
} catch (Exception e) {
LOG.error("get virtual nodes error", e);
}
// load normal id
try {
String normalPath = ZKPaths.makePath(baseRoutePath, Constants.NORMAL_ROUTE, String.valueOf(storageIndex));
if (zkClient.checkExists().forPath(normalPath) != null) {
List<String> normalNodes = zkClient.getChildren().forPath(normalPath);
if (normalNodes != null && !normalNodes.isEmpty()) {
for (String normalNode : normalNodes) {
try {
byte[] data = zkClient.getData().forPath(ZKPaths.makePath(normalPath, normalNode));
NormalRoute normal = JSON.parseObject(BrStringUtils.fromUtf8Bytes(data), NormalRoute.class);
normalRouteDetail.put(normal.getSecondID(), normal);
} catch (Exception e) {
LOG.error("load normal route[{}] error", normalNode, e);
}
}
}
}
} catch (Exception e) {
LOG.error("get normal nodes error", e);
}
}
use of com.bonree.brfs.common.rebalance.route.NormalRoute in project BRFS by zhangnianli.
the class RouteCacheListener method childEvent.
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
// 过滤事件,并不是所有的事件都得响应
if (event.getType().equals(TreeCacheEvent.Type.NODE_ADDED) && event.getData() != null && event.getData().getData() != null && event.getData().getData().length > 0) {
String path = event.getData().getPath();
List<String> splitPaths = Lists.newArrayList(Splitter.on(Constants.SEPARATOR).omitEmptyStrings().trimResults().split(path));
String endStr = splitPaths.get(splitPaths.size() - 1);
if (endStr.length() > 16 && splitPaths.size() > 4) {
if (splitPaths.contains(Constants.VIRTUAL_ROUTE)) {
VirtualRoute route = JSON.parseObject(BrStringUtils.fromUtf8Bytes(event.getData().getData()), VirtualRoute.class);
routeRoleCache.getVirtualRouteCache().put(route.getVirtualID(), route);
} else if (splitPaths.contains(Constants.NORMAL_ROUTE)) {
NormalRoute route = JSON.parseObject(BrStringUtils.fromUtf8Bytes(event.getData().getData()), NormalRoute.class);
routeRoleCache.getNormalRouteCache().put(route.getSecondID(), route);
}
}
}
}
use of com.bonree.brfs.common.rebalance.route.NormalRoute in project BRFS by zhangnianli.
the class SecondLevelServerID method isExpire.
private boolean isExpire(String si, String secondServerID) throws Exception {
String normalPath = ZKPaths.makePath(baseRoutes, Constants.NORMAL_ROUTE);
String siPath = ZKPaths.makePath(normalPath, si);
if (client.checkExists().forPath(normalPath) != null && client.checkExists().forPath(siPath) != null) {
List<String> routeNodes = client.getChildren().forPath(siPath);
for (String routeNode : routeNodes) {
String routePath = ZKPaths.makePath(siPath, routeNode);
byte[] data = client.getData().forPath(routePath);
NormalRoute normalRoute = JsonUtils.toObject(data, NormalRoute.class);
if (normalRoute.getSecondID().equals(secondServerID)) {
return true;
}
}
}
return false;
}
use of com.bonree.brfs.common.rebalance.route.NormalRoute in project BRFS by zhangnianli.
the class TaskDispatcher method taskTerminal.
public void taskTerminal(CuratorFramework client, TreeCacheEvent event) {
if (leaderLath.hasLeadership()) {
CuratorClient curatorClient = CuratorClient.wrapClient(client);
LOG.info("leaderLath:" + getLeaderLatch().hasLeadership());
LOG.info("task Dispatch event detail:" + RebalanceUtils.convertEvent(event));
if (event.getType() == Type.NODE_UPDATED) {
if (event.getData() != null && event.getData().getData() != null) {
// 此处会检测任务是否完成
String eventPath = event.getData().getPath();
if (eventPath.substring(eventPath.lastIndexOf('/') + 1, eventPath.length()).equals(Constants.TASK_NODE)) {
return;
}
String parentPath = StringUtils.substring(eventPath, 0, eventPath.lastIndexOf('/'));
// 节点已经删除,则忽略
if (!curatorClient.checkExists(parentPath)) {
return;
}
BalanceTaskSummary bts = JsonUtils.toObjectQuietly(curatorClient.getData(parentPath), BalanceTaskSummary.class);
List<String> serverIds = curatorClient.getChildren(parentPath);
// 判断是否所有的节点做完任务
boolean finishFlag = true;
if (serverIds != null) {
if (serverIds.isEmpty()) {
LOG.info("taskoperation is not execute task!!!");
finishFlag = false;
} else {
for (String serverId : serverIds) {
String nodePath = ZKPaths.makePath(parentPath, serverId);
TaskDetail td = JsonUtils.toObjectQuietly(curatorClient.getData(nodePath), TaskDetail.class);
if (td.getStatus() != DataRecover.ExecutionStatus.FINISH) {
finishFlag = false;
break;
}
}
}
}
// 所有的服务都则发布迁移规则,并清理任务
if (finishFlag) {
// 先更新任务状态为finish
updateTaskStatus(bts, TaskStatus.FINISH);
// 发布路由规则
if (bts.getTaskType() == RecoverType.VIRTUAL) {
LOG.info("one virtual task finish,detail:" + RebalanceUtils.convertEvent(event));
String virtualRouteNode = ZKPaths.makePath(virtualRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
VirtualRoute route = new VirtualRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers().get(0), TaskVersion.V1);
LOG.info("add virtual route:" + route);
addRoute(virtualRouteNode, JsonUtils.toJsonBytesQuietly(route));
// 因共享节点,所以得将余下的所有virtual server id,注册新迁移的server。不足之处,可能为导致副本数的恢复大于服务数。
// String firstID = idManager.getOtherFirstID(bts.getInputServers().get(0), bts.getStorageIndex());
// List<String> normalVirtualIDs = idManager.listNormalVirtualID(bts.getStorageIndex());
// if (normalVirtualIDs != null && !normalVirtualIDs.isEmpty()) {
// for (String virtualID : normalVirtualIDs) {
// idManager.registerFirstID(bts.getStorageIndex(), virtualID, firstID);
// }
// }
// 删除virtual server ID
LOG.info("delete the virtual server id:" + bts.getServerId());
idManager.deleteVirtualID(bts.getStorageIndex(), bts.getServerId());
} else if (bts.getTaskType() == RecoverType.NORMAL) {
LOG.info("one normal task finish,detail:" + RebalanceUtils.convertEvent(event));
String normalRouteNode = ZKPaths.makePath(normalRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
NormalRoute route = new NormalRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers(), TaskVersion.V1);
LOG.info("add normal route:" + route);
addRoute(normalRouteNode, JsonUtils.toJsonBytesQuietly(route));
}
List<ChangeSummary> changeSummaries = cacheSummaryCache.get(bts.getStorageIndex());
// 清理zk上的变更
removeChange2History(bts);
// 清理change cache缓存
if (changeSummaries != null) {
Iterator<ChangeSummary> it = changeSummaries.iterator();
while (it.hasNext()) {
ChangeSummary cs = it.next();
if (cs.getChangeID().equals(bts.getChangeID())) {
changeSummaries.remove(cs);
}
}
}
// 删除zk上的任务节点
if (delBalanceTask(bts)) {
// 清理task缓存
removeRunTask(bts.getStorageIndex());
}
}
}
}
}
}
use of com.bonree.brfs.common.rebalance.route.NormalRoute in project BRFS by zhangnianli.
the class TaskDispatcher method fixTaskMeta.
public void fixTaskMeta(BalanceTaskSummary taskSummary) {
// task路径
String parentPath = ZKPaths.makePath(tasksPath, String.valueOf(taskSummary.getStorageIndex()), Constants.TASK_NODE);
// 节点已经删除,则忽略
if (!curatorClient.checkExists(parentPath)) {
return;
}
BalanceTaskSummary bts = JsonUtils.toObjectQuietly(curatorClient.getData(parentPath), BalanceTaskSummary.class);
// 所有的服务都则发布迁移规则,并清理任务
if (bts.getTaskStatus().equals(TaskStatus.FINISH)) {
if (bts.getTaskType() == RecoverType.VIRTUAL) {
LOG.info("one virtual task finish,detail:" + taskSummary);
String virtualRouteNode = ZKPaths.makePath(virtualRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
VirtualRoute route = new VirtualRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers().get(0), TaskVersion.V1);
LOG.info("add virtual route:" + route);
addRoute(virtualRouteNode, JsonUtils.toJsonBytesQuietly(route));
// 因共享节点,所以得将余下的所有virtual server id,注册新迁移的server。不足之处,可能为导致副本数的恢复大于服务数。
// String firstID = idManager.getOtherFirstID(bts.getInputServers().get(0), bts.getStorageIndex());
// List<String> normalVirtualIDs = idManager.listNormalVirtualID(bts.getStorageIndex());
// if (normalVirtualIDs != null && !normalVirtualIDs.isEmpty()) {
// for (String virtualID : normalVirtualIDs) {
// idManager.registerFirstID(bts.getStorageIndex(), virtualID, firstID);
// }
// }
// 删除virtual server ID
LOG.info("delete the virtual server id:" + bts.getServerId());
idManager.deleteVirtualID(bts.getStorageIndex(), bts.getServerId());
} else if (bts.getTaskType() == RecoverType.NORMAL) {
LOG.info("one normal task finish, detail:" + taskSummary);
String normalRouteNode = ZKPaths.makePath(normalRoutePath, String.valueOf(bts.getStorageIndex()), bts.getId());
NormalRoute route = new NormalRoute(bts.getChangeID(), bts.getStorageIndex(), bts.getServerId(), bts.getInputServers(), TaskVersion.V1);
LOG.info("add normal route:" + route);
addRoute(normalRouteNode, JsonUtils.toJsonBytesQuietly(route));
}
}
List<ChangeSummary> changeSummaries = cacheSummaryCache.get(bts.getStorageIndex());
// 清理zk上的变更
removeChange2History(bts);
// 清理change cache缓存
if (changeSummaries != null) {
Iterator<ChangeSummary> it = changeSummaries.iterator();
while (it.hasNext()) {
ChangeSummary cs = it.next();
if (cs.getChangeID().equals(bts.getChangeID())) {
changeSummaries.remove(cs);
}
}
}
// 删除zk上的任务节点
if (delBalanceTask(bts)) {
// 清理task缓存
removeRunTask(bts.getStorageIndex());
}
}
Aggregations