Search in sources :

Example 1 with FileNode

use of com.bonree.brfs.duplication.filenode.FileNode in project BRFS by zhangnianli.

the class FileNodeDistributor method serviceRemoved.

@Override
public void serviceRemoved(Service service) {
    LOG.info("Service removed#######{}", service.getServiceId());
    serviceTimeTable.remove(service.getServiceGroup(), service.getServiceId());
    PathChildrenCache childWatcher = childWatchers.get(service.getServiceId());
    CloseUtils.closeQuietly(childWatcher);
    // 删除服务对应的文件槽
    try {
        client.delete().quietly().deletingChildrenIfNeeded().forPath(ZkFileCoordinatorPaths.buildServiceSinkPath(service));
    } catch (Exception e) {
        LOG.warn("Can not delete the sink of crushed service[{}]", service.getServiceId(), e);
    }
    // 把崩溃的Service持有的文件节点放入列表
    executor.submit(new Runnable() {

        @Override
        public void run() {
            for (FileNode node : fileStorer.listFileNodes(new ServiceFileNodeFilter(service))) {
                wildFileNodes.add(node);
            }
        }
    });
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) FileNode(com.bonree.brfs.duplication.filenode.FileNode)

Example 2 with FileNode

use of com.bonree.brfs.duplication.filenode.FileNode in project BRFS by zhangnianli.

the class FileNodeDistributor method handleFileNode.

private boolean handleFileNode(FileNode fileNode) {
    LOG.info("handling wild FileNode[{}]", JsonUtils.toJsonStringQuietly(fileNode));
    List<Service> serviceList = getServiceWithStorageRegionName(fileNode.getStorageName());
    Service target = serviceSelector.selectWith(fileNode, serviceList);
    if (target == null) {
        LOG.info("no service to accept filenode[{}], add it to wild list", fileNode.getName());
        return false;
    }
    LOG.info("transfer fileNode[{}] to service[{}]", fileNode.getName(), target.getServiceId());
    FileNode newFileNode = FileNode.newBuilder(fileNode).setServiceId(target.getServiceId()).setServiceTime(target.getRegisterTime()).build();
    try {
        fileStorer.update(newFileNode);
    } catch (Exception e) {
        LOG.error("update file node[{}] info error", fileNode.getName(), e);
        return false;
    }
    try {
        // 在Sink中放入分配的文件名
        String path = client.create().forPath(ZkFileCoordinatorPaths.buildSinkFileNodePath(newFileNode), JsonUtils.toJsonBytes(newFileNode));
        LOG.info("filenode[{}] add to sink[{}]", newFileNode.getName(), path);
        return true;
    } catch (Exception e) {
        LOG.error("add filenode[{}] to sink error", newFileNode.getName(), e);
        try {
            fileStorer.update(fileNode);
        } catch (Exception e1) {
            LOG.error("roll back to original info of filenode[{}] error", fileNode.getName(), e1);
        }
        return false;
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) Service(com.bonree.brfs.common.service.Service) FileNode(com.bonree.brfs.duplication.filenode.FileNode) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException)

Example 3 with FileNode

use of com.bonree.brfs.duplication.filenode.FileNode in project BRFS by zhangnianli.

the class ZkFileNodeStorer method listFileNodes.

@Override
public List<FileNode> listFileNodes(FileNodeFilter filter) {
    List<FileNode> fileNodes = new ArrayList<FileNode>();
    for (String fileName : getFileNameList()) {
        FileNode node = getFileNode(fileName);
        if (node == null) {
            continue;
        }
        if (filter != null && !filter.filter(node)) {
            continue;
        }
        fileNodes.add(node);
    }
    return fileNodes;
}
Also used : ArrayList(java.util.ArrayList) FileNode(com.bonree.brfs.duplication.filenode.FileNode)

Example 4 with FileNode

use of com.bonree.brfs.duplication.filenode.FileNode in project BRFS by zhangnianli.

the class DefaultFileObjectFactory method createFile.

@Override
public FileObject createFile(StorageRegion storageRegion) {
    DuplicateNode[] nodes = duplicationNodeSelector.getDuplicationNodes(storageRegion.getId(), storageRegion.getReplicateNum());
    if (nodes.length == 0) {
        LOG.error("No available duplication node to build FileNode");
        // 没有磁盘节点可用
        return null;
    }
    FileNode.Builder fileNodeBuilder = FileNode.newBuilder().setStorageName(storageRegion.getName()).setStorageId(storageRegion.getId()).setServiceId(service.getServiceId()).setServiceGroup(service.getServiceGroup()).setName(FileNameBuilder.createFile(idManager, storageRegion, nodes)).setDuplicateNodes(nodes).setTimeDuration(Duration.parse(storageRegion.getFilePartitionDuration()).toMillis());
    long capacity = -1;
    for (DuplicateNode node : nodes) {
        LOG.info("start init node[{}] for file[{}]", node, fileNodeBuilder.build().getName());
        DiskNodeConnection connection = connectionPool.getConnection(node.getGroup(), node.getId());
        if (connection == null || connection.getClient() == null) {
            LOG.info("can not write header for file[{}] because [{}] is disconnected", fileNodeBuilder.build().getName(), node);
            continue;
        }
        String serverId = idManager.getOtherSecondID(node.getId(), storageRegion.getId());
        String filePath = FilePathBuilder.buildFilePath(fileNodeBuilder.build(), serverId);
        LOG.info("client opening file [{}]", filePath);
        long result = connection.getClient().openFile(filePath, storageRegion.getFileCapacity());
        if (result < 0) {
            continue;
        }
        LOG.info("open file[{}] from datanode[{}] with capacity[{}]", filePath, node, result);
        if (capacity < 0) {
            capacity = result;
            continue;
        }
        if (capacity != result) {
            LOG.error("different capacity be received from different dupcate nodes");
            capacity = -1;
            break;
        }
    }
    // 如果没有一个磁盘节点写入头数据成功,则放弃使用此文件节点
    if (capacity < 0) {
        LOG.error("can not open file at any duplicate node for file[{}]", fileNodeBuilder.build().getName());
        return null;
    }
    FileNode fileNode = fileNodeBuilder.setCapacity(capacity).build();
    try {
        fileNodeStorer.save(fileNode);
        return new FileObject(fileNode);
    } catch (Exception e) {
        LOG.error("store file node[{}] error!", fileNode.getName(), e);
    }
    return null;
}
Also used : DuplicateNode(com.bonree.brfs.duplication.filenode.duplicates.DuplicateNode) DiskNodeConnection(com.bonree.brfs.duplication.datastream.connection.DiskNodeConnection) FileNode(com.bonree.brfs.duplication.filenode.FileNode)

Aggregations

FileNode (com.bonree.brfs.duplication.filenode.FileNode)4 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)2 Service (com.bonree.brfs.common.service.Service)1 DiskNodeConnection (com.bonree.brfs.duplication.datastream.connection.DiskNodeConnection)1 DuplicateNode (com.bonree.brfs.duplication.filenode.duplicates.DuplicateNode)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)1