Search in sources :

Example 6 with NormalRoute

use of com.bonree.brfs.common.rebalance.route.NormalRoute 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)

Example 7 with NormalRoute

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

the class SecondIDParser method updateRoute.

public void updateRoute() {
    // load virtual id
    String virtualPath = baseRoutesPath + Constants.SEPARATOR + Constants.VIRTUAL_ROUTE + Constants.SEPARATOR + snID;
    List<String> virtualNodes = curatorClient.getChildren(virtualPath);
    if (curatorClient.checkExists(virtualPath)) {
        if (virtualNodes != null && !virtualNodes.isEmpty()) {
            for (String virtualNode : virtualNodes) {
                String dataPath = virtualPath + Constants.SEPARATOR + virtualNode;
                byte[] data = curatorClient.getData(dataPath);
                VirtualRoute virtual = JsonUtils.toObjectQuietly(data, VirtualRoute.class);
                virtualRouteDetail.put(virtual.getVirtualID(), virtual);
            }
        }
    }
    // load normal id
    String normalPath = baseRoutesPath + Constants.SEPARATOR + Constants.NORMAL_ROUTE + Constants.SEPARATOR + snID;
    if (curatorClient.checkExists(normalPath)) {
        List<String> normalNodes = curatorClient.getChildren(normalPath);
        if (normalNodes != null && !normalNodes.isEmpty()) {
            for (String normalNode : normalNodes) {
                String dataPath = normalPath + Constants.SEPARATOR + normalNode;
                byte[] data = curatorClient.getData(dataPath);
                NormalRoute normal = JsonUtils.toObjectQuietly(data, NormalRoute.class);
                normalRouteDetail.put(normal.getSecondID(), normal);
            }
        }
    }
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 8 with NormalRoute

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

the class MultiRecover3 method loadVirualRoutes.

public void loadVirualRoutes() {
    // load virtual id
    String virtualPath = baseRoutesPath + Constants.SEPARATOR + Constants.VIRTUAL_ROUTE + Constants.SEPARATOR + balanceSummary.getStorageIndex();
    List<String> virtualNodes = client.getChildren(virtualPath);
    if (client.checkExists(virtualPath)) {
        if (virtualNodes != null && !virtualNodes.isEmpty()) {
            for (String virtualNode : virtualNodes) {
                String dataPath = virtualPath + Constants.SEPARATOR + virtualNode;
                byte[] data = client.getData(dataPath);
                VirtualRoute virtual = JsonUtils.toObjectQuietly(data, VirtualRoute.class);
                virtualRoutes.put(virtual.getVirtualID(), virtual);
            }
        }
    }
    LOG.info("virtual routes:" + virtualRoutes);
    // load normal id
    String normalPath = baseRoutesPath + Constants.SEPARATOR + Constants.NORMAL_ROUTE + Constants.SEPARATOR + balanceSummary.getStorageIndex();
    if (client.checkExists(normalPath)) {
        List<String> normalNodes = client.getChildren(normalPath);
        if (normalNodes != null && !normalNodes.isEmpty()) {
            for (String normalNode : normalNodes) {
                String dataPath = normalPath + Constants.SEPARATOR + normalNode;
                byte[] data = client.getData(dataPath);
                NormalRoute normal = JsonUtils.toObjectQuietly(data, NormalRoute.class);
                normalRoutes.put(normal.getSecondID(), normal);
            }
        }
    }
    LOG.info("normal routes:" + normalRoutes);
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 9 with NormalRoute

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

the class MultiRecover method loadVirualRoutes.

public void loadVirualRoutes() {
    // load virtual id
    String virtualPath = baseRoutesPath + Constants.SEPARATOR + Constants.VIRTUAL_ROUTE + Constants.SEPARATOR + balanceSummary.getStorageIndex();
    List<String> virtualNodes = client.getChildren(virtualPath);
    if (client.checkExists(virtualPath)) {
        if (virtualNodes != null && !virtualNodes.isEmpty()) {
            for (String virtualNode : virtualNodes) {
                String dataPath = virtualPath + Constants.SEPARATOR + virtualNode;
                byte[] data = client.getData(dataPath);
                VirtualRoute virtual = JsonUtils.toObjectQuietly(data, VirtualRoute.class);
                virtualRoutes.put(virtual.getVirtualID(), virtual);
            }
        }
    }
    LOG.info("virtual routes:" + virtualRoutes);
    // load normal id
    String normalPath = baseRoutesPath + Constants.SEPARATOR + Constants.NORMAL_ROUTE + Constants.SEPARATOR + balanceSummary.getStorageIndex();
    if (client.checkExists(normalPath)) {
        List<String> normalNodes = client.getChildren(normalPath);
        if (normalNodes != null && !normalNodes.isEmpty()) {
            for (String normalNode : normalNodes) {
                String dataPath = normalPath + Constants.SEPARATOR + normalNode;
                byte[] data = client.getData(dataPath);
                NormalRoute normal = JsonUtils.toObjectQuietly(data, NormalRoute.class);
                normalRoutes.put(normal.getSecondID(), normal);
            }
        }
    }
    LOG.info("normal routes:" + normalRoutes);
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Example 10 with NormalRoute

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

the class RouteParser method findServerID.

public String findServerID(String searchServerID, String namePart, String[] serverIds, List<String> aliveServers) {
    // fid分为单副本serverID,多副本serverID,虚拟serverID。
    // 单副本不需要查找路由
    // 多副本需要查找路由,查找路由方式不同
    String secondID = searchServerID;
    if (Constants.VIRTUAL_ID == secondID.charAt(0)) {
        LOG.debug("2260 a virtual serverid : {}", secondID);
        VirtualRoute virtualRoute = routeCache.getVirtualRoute(secondID);
        if (virtualRoute == null) {
            LOG.debug("2260 no virtualRoute");
            // 虚拟serverID没有进行迁移,所以返回null,标识找不到可用的server
            return null;
        }
        secondID = virtualRoute.getNewSecondID();
        LOG.debug("2260 newsecondid: {}", secondID);
    }
    // 说明该secondID存活,不需要路由查找
    LOG.info("2260 aliveServers : {}", aliveServers);
    if (aliveServers.contains(secondID)) {
        LOG.debug("2260 aliver secondid: {}", secondID);
        return secondID;
    }
    LOG.debug("2260 dead secondID: {}", secondID);
    // secondID不存活,需要寻找该secondID的存活ID
    NormalRoute routeRole = routeCache.getRouteRole(secondID);
    if (routeRole == null) {
        // 若没有迁移记录,可能没有迁移完成
        LOG.debug("2260 no normalRoute");
        // 不是存活的secondid,并没有发生迁移,返回null,标识不可用
        return null;
    }
    // 提取副本数
    int replicas = serverIds.length;
    // 提取出该文件所存储的服务
    List<String> fileServerIds = new ArrayList<>();
    for (String serverId : serverIds) {
        if (serverId == null) {
            continue;
        }
        // virtual server ID 分为已经解析或者还未解析的
        if (Constants.VIRTUAL_ID == serverId.charAt(0)) {
            if (serverId.equals(searchServerID)) {
                // 前面解析过
                fileServerIds.add(secondID);
            } else {
                // 需要解析
                VirtualRoute virtualRoute = routeCache.getVirtualRoute(secondID);
                if (virtualRoute == null) {
                    // 如果有普通迁移,那么肯定无虚拟迁移,或者虚拟迁移都已经进行完成
                    LOG.error("2260 gain serverid error!something impossible!!!");
                    return null;
                }
                fileServerIds.add(virtualRoute.getNewSecondID());
            }
        }
        LOG.debug("2260 construct a source second server id list : {}", fileServerIds);
    }
    // 提取需要查询的serverID的位置
    final int serverIDPot = fileServerIds.indexOf(secondID);
    LOG.debug("2260 this server id's pot is : {}", serverIDPot);
    // 这里要判断一个副本是否需要进行迁移
    // 挑选出的可迁移的servers
    String selectMultiId = null;
    // 可获取的server,可能包括自身
    List<String> recoverableServerList = null;
    // 排除掉自身或已有的servers
    List<String> exceptionServerIds = null;
    // 真正可选择的servers
    List<String> selectableServerList = null;
    while (RebalanceUtils.needRecover(fileServerIds, replicas, aliveServers)) {
        LOG.info("2260 check need to route,fileServerIDs: {} -----> aliverServers: {}", fileServerIds, aliveServers);
        for (String deadServer : fileServerIds) {
            // 将所有挂掉的服务尝试恢复一次
            if (!aliveServers.contains(deadServer)) {
                // 尝试去路由该挂掉的服务的新服务中
                int pot = fileServerIds.indexOf(deadServer);
                NormalRoute newRoute = routeCache.getRouteRole(deadServer);
                if (newRoute == null) {
                    LOG.debug("2260 deadServer: {},is no finished the recover!!!", deadServer);
                    int deadIndex = fileServerIds.indexOf(deadServer);
                    if (deadIndex == serverIDPot) {
                        LOG.debug("2260 a deadServer no finished the recover,but try to read data from the second's server: {}", deadServer);
                        return null;
                    } else {
                        continue;
                    }
                }
                LOG.debug("2260 normal route: {}", newRoute);
                recoverableServerList = newRoute.getNewSecondIDs();
                exceptionServerIds = new ArrayList<>();
                exceptionServerIds.addAll(fileServerIds);
                exceptionServerIds.remove(deadServer);
                selectableServerList = RebalanceUtils.getSelectedList(recoverableServerList, exceptionServerIds);
                int index = RebalanceUtils.hashFileName(namePart, selectableServerList.size());
                selectMultiId = selectableServerList.get(index);
                LOG.debug("2260 partFile: {}, deadserver: {} to newServer:{}", namePart, deadServer, selectMultiId);
                fileServerIds.set(pot, selectMultiId);
                // 判断选取的新节点是否存活
                if (aliveServers.contains(selectMultiId)) {
                    // 判断选取的新节点是否为本节点,该serverID是否在相应的位置
                    if (pot == serverIDPot) {
                        LOG.debug("2260 select a right server id : {} , for indexPot:{}", selectMultiId, serverIDPot);
                        break;
                    }
                }
            }
        }
    }
    return selectMultiId;
}
Also used : VirtualRoute(com.bonree.brfs.common.rebalance.route.VirtualRoute) ArrayList(java.util.ArrayList) NormalRoute(com.bonree.brfs.common.rebalance.route.NormalRoute)

Aggregations

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