Search in sources :

Example 1 with TcpDiskNodeClient

use of com.bonree.brfs.disknode.client.TcpDiskNodeClient in project BRFS by zhangnianli.

the class FileRecoveryMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    FileRecoveryMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), FileRecoveryMessage.class);
    if (message == null) {
        LOG.error("decode recover message error");
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    String filePath = null;
    try {
        filePath = context.getConcreteFilePath(message.getFilePath());
        LOG.info("starting recover file[{}]", filePath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
        if (binding == null) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        binding.first().position(fileFormater.absoluteOffset(message.getOffset()));
        byte[] bytes = null;
        for (String stateString : message.getSources()) {
            FileObjectSyncState state = SyncStateCodec.fromString(stateString);
            Service service = serviceManager.getServiceById(state.getServiceGroup(), state.getServiceId());
            if (service == null) {
                LOG.error("can not get service with[{}:{}]", state.getServiceGroup(), state.getServiceId());
                continue;
            }
            DiskNodeClient client = null;
            try {
                LOG.info("get data from{} to recover...", service);
                TcpClient<ReadObject, FileContentPart> readClient = clientGroup.createClient(new AsyncFileReaderCreateConfig() {

                    @Override
                    public SocketAddress remoteAddress() {
                        return new InetSocketAddress(service.getHost(), service.getExtraPort());
                    }

                    @Override
                    public int connectTimeoutMillis() {
                        return 3000;
                    }

                    @Override
                    public int maxPendingRead() {
                        return 0;
                    }
                }, ForkJoinPool.commonPool());
                client = new TcpDiskNodeClient(null, readClient);
                long lackBytes = state.getFileLength() - message.getOffset();
                CompletableFuture<byte[]> byteFuture = new CompletableFuture<byte[]>();
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                client.readData(state.getFilePath(), message.getOffset(), (int) lackBytes, new ByteConsumer() {

                    @Override
                    public void error(Throwable e) {
                        byteFuture.completeExceptionally(e);
                    }

                    @Override
                    public void consume(byte[] bytes, boolean endOfConsume) {
                        try {
                            output.write(bytes);
                            if (endOfConsume) {
                                byteFuture.complete(output.toByteArray());
                                output.close();
                            }
                        } catch (Exception e) {
                            byteFuture.completeExceptionally(e);
                        }
                    }
                });
                bytes = byteFuture.get();
                if (bytes != null) {
                    LOG.info("read bytes length[{}], require[{}]", bytes.length, lackBytes);
                    break;
                }
            } catch (Exception e) {
                LOG.error("recover file[{}] error", filePath, e);
            } finally {
                CloseUtils.closeQuietly(client);
            }
        }
        if (bytes == null) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        int offset = 0;
        int size = 0;
        while ((size = FileDecoder.getOffsets(offset, bytes)) > 0) {
            LOG.info("rewrite data[offset={}, size={}] to file[{}]", offset, size, filePath);
            binding.first().write(bytes, offset, size);
            offset += size;
            size = 0;
        }
        if (offset != bytes.length) {
            LOG.error("perhaps datas that being recoverd is not correct! get [{}], but recoverd[{}]", bytes.length, offset);
        }
        writer.write(new BaseResponse(ResponseCode.OK));
    } catch (Exception e) {
        LOG.error("recover file[{}] error", filePath, e);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) DiskNodeClient(com.bonree.brfs.disknode.client.DiskNodeClient) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) ByteConsumer(com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer) CompletableFuture(java.util.concurrent.CompletableFuture) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) AsyncFileReaderCreateConfig(com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) FileObjectSyncState(com.bonree.brfs.common.filesync.FileObjectSyncState) Service(com.bonree.brfs.common.service.Service) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileRecoveryMessage(com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 2 with TcpDiskNodeClient

use of com.bonree.brfs.disknode.client.TcpDiskNodeClient in project BRFS by zhangnianli.

the class TcpClientUtils method getClient.

public static TcpDiskNodeClient getClient(String host, int port, int export, int timeout) throws InterruptedException, IOException {
    TcpClient<BaseMessage, BaseResponse> tcpClient = group.createClient(new TcpClientConfig() {

        @Override
        public SocketAddress remoteAddress() {
            return new InetSocketAddress(host, port);
        }

        @Override
        public int connectTimeoutMillis() {
            return timeout;
        }
    });
    TcpClient<ReadObject, FileContentPart> readerClient = group2.createClient(new AsyncFileReaderCreateConfig() {

        @Override
        public SocketAddress remoteAddress() {
            return new InetSocketAddress(host, export);
        }

        @Override
        public int connectTimeoutMillis() {
            return timeout;
        }

        @Override
        public int maxPendingRead() {
            return 0;
        }
    });
    return new TcpDiskNodeClient(tcpClient, readerClient);
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) AsyncFileReaderCreateConfig(com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with TcpDiskNodeClient

use of com.bonree.brfs.disknode.client.TcpDiskNodeClient in project BRFS by zhangnianli.

the class CopyRecovery method copyFrom.

/**
 * 概述:恢复数据文件
 * @param host 远程主机
 * @param port 端口
 * @param export
 * @param timeout
 * @param remotePath
 * @param localPath
 * @return
 * @user <a href=mailto:zhucg@bonree.com>朱成岗</a>
 */
public static boolean copyFrom(String host, int port, int export, int timeout, String remotePath, String localPath) {
    TcpDiskNodeClient client = null;
    try {
        client = TcpClientUtils.getClient(host, port, export, timeout);
        LOG.debug("{}:{},{}:{}, read {} to local {}", host, port, host, export, remotePath, localPath);
        LocalByteStreamConsumer consumer = new LocalByteStreamConsumer(localPath);
        client.readFile(remotePath, consumer);
        return consumer.getResult().get();
    } catch (InterruptedException | IOException | ExecutionException e) {
        EmailPool emailPool = EmailPool.getInstance();
        MailWorker.Builder builder = MailWorker.newBuilder(emailPool.getProgramInfo());
        builder.setModel("collect file execute 模块服务发生问题");
        builder.setException(e);
        ManagerContralFactory mcf = ManagerContralFactory.getInstance();
        builder.setMessage(mcf.getGroupName() + "(" + mcf.getServerId() + ")服务 执行任务时发生问题");
        Map<String, String> map = new HashedMap();
        map.put("remote ", host);
        map.put("remote path", remotePath);
        map.put("local path", localPath);
        map.put("connectTimeout", String.valueOf(timeout));
        builder.setVariable(map);
        emailPool.sendEmail(builder);
        LOG.error("copy from error {}", e);
        return false;
    } finally {
        if (client != null) {
            try {
                client.closeFile(remotePath);
                client.close();
            } catch (IOException e) {
                LOG.error("close error ", e);
            }
        }
    }
}
Also used : TcpDiskNodeClient(com.bonree.brfs.disknode.client.TcpDiskNodeClient) IOException(java.io.IOException) EmailPool(com.bonree.brfs.email.EmailPool) ExecutionException(java.util.concurrent.ExecutionException) HashedMap(org.apache.commons.collections.map.HashedMap) ManagerContralFactory(com.bonree.brfs.schedulers.ManagerContralFactory) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap)

Aggregations

TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)3 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)2 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)2 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)1 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)1 Service (com.bonree.brfs.common.service.Service)1 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)1 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)1 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)1 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)1 FileRecoveryMessage (com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage)1 EmailPool (com.bonree.brfs.email.EmailPool)1 ManagerContralFactory (com.bonree.brfs.schedulers.ManagerContralFactory)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1