Search in sources :

Example 1 with VirtualRoute

use of com.bonree.brfs.common.rebalance.route.VirtualRoute 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);
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 2 with VirtualRoute

use of com.bonree.brfs.common.rebalance.route.VirtualRoute 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);
            }
        }
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 3 with VirtualRoute

use of com.bonree.brfs.common.rebalance.route.VirtualRoute 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());
                    }
                }
            }
        }
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) CuratorClient(com.bonree.brfs.common.zookeeper.curator.CuratorClient) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 4 with VirtualRoute

use of com.bonree.brfs.common.rebalance.route.VirtualRoute 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());
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 5 with VirtualRoute

use of com.bonree.brfs.common.rebalance.route.VirtualRoute in project BRFS by zhangnianli.

the class SecondIDParser method getAliveSecondID.

public String[] getAliveSecondID(String partfid) {
    try {
        String[] splitStr = partfid.split("_");
        String fileUUID = splitStr[0];
        List<String> fileServerIDs = new ArrayList<>(splitStr.length - 1);
        for (int i = 1; i < splitStr.length; i++) {
            fileServerIDs.add(splitStr[i]);
        }
        for (int i = 1; i < splitStr.length; i++) {
            // 处理所有的副本
            String serverID = splitStr[i];
            if (serverID.charAt(0) == Constants.VIRTUAL_ID) {
                VirtualRoute virtualRoute = virtualRouteDetail.get(serverID);
                if (virtualRoute != null) {
                    // 副本发生了迁移
                    // 找到迁移后的serverID
                    serverID = virtualRoute.getNewSecondID();
                    fileServerIDs.set(i - 1, serverID);
                    // 查看该serverID是否迁移
                    NormalRoute normalRoute = normalRouteDetail.get(serverID);
                    List<String> newServerIDS = null;
                    while (normalRoute != null) {
                        // 只要发生了迁移,则继续计算迁移位置
                        newServerIDS = normalRoute.getNewSecondIDs();
                        serverID = RebalanceUtils.newServerID(fileUUID, newServerIDS, fileServerIDs);
                        fileServerIDs.set(i - 1, serverID);
                        normalRoute = normalRouteDetail.get(serverID);
                    }
                }
            } else if (serverID.charAt(0) == Constants.MULTI_ID) {
                // 查看该serverID是否迁移
                NormalRoute normalRoute = normalRouteDetail.get(serverID);
                List<String> newServerIDS = null;
                while (normalRoute != null) {
                    // 只要发生了迁移,则继续计算迁移位置
                    newServerIDS = normalRoute.getNewSecondIDs();
                    serverID = RebalanceUtils.newServerID(fileUUID, newServerIDS, fileServerIDs);
                    fileServerIDs.set(i - 1, serverID);
                    normalRoute = normalRouteDetail.get(serverID);
                }
            }
        }
        return fileServerIDs.toArray(new String[0]);
    } catch (Exception e) {
        throw new IllegalStateException("parse partFid error!!", e);
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Aggregations

VirtualRoute (com.bonree.brfs.common.rebalance.route.VirtualRoute)11 NormalRoute (com.bonree.brfs.common.rebalance.route.NormalRoute)9 ArrayList (java.util.ArrayList)4 CuratorClient (com.bonree.brfs.common.zookeeper.curator.CuratorClient)1 List (java.util.List)1