Search in sources :

Example 1 with HttpDiskNodeClient

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

the class RecoveryMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult handleResult = new HandleResult();
    String filePath = null;
    try {
        filePath = context.getConcreteFilePath(msg.getPath());
        LOG.info("starting recover file[{}]", filePath);
        String lengthParam = msg.getParams().get("length");
        if (lengthParam == null) {
            handleResult.setSuccess(false);
            callback.completed(handleResult);
            return;
        }
        long fileLength = Long.parseLong(msg.getParams().get("length"));
        List<String> fullStates = Splitter.on(',').omitEmptyStrings().trimResults().splitToList(msg.getParams().get("fulls"));
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
        if (binding == null) {
            handleResult.setSuccess(false);
            callback.completed(handleResult);
            return;
        }
        binding.first().position(fileFormater.absoluteOffset(fileLength));
        byte[] bytes = null;
        for (String stateString : fullStates) {
            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);
                client = new HttpDiskNodeClient(service.getHost(), service.getPort());
                long lackBytes = state.getFileLength() - fileLength;
                CompletableFuture<byte[]> byteFuture = new CompletableFuture<byte[]>();
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                client.readData(state.getFilePath(), fileLength, (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) {
            handleResult.setSuccess(false);
            callback.completed(handleResult);
            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);
        }
        handleResult.setSuccess(true);
    } catch (Exception e) {
        LOG.error("recover file[{}] error", filePath, e);
        handleResult.setSuccess(false);
    } finally {
        callback.completed(handleResult);
    }
}
Also used : HttpDiskNodeClient(com.bonree.brfs.disknode.client.HttpDiskNodeClient) FileObjectSyncState(com.bonree.brfs.common.filesync.FileObjectSyncState) Service(com.bonree.brfs.common.service.Service) HandleResult(com.bonree.brfs.common.net.http.HandleResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DiskNodeClient(com.bonree.brfs.disknode.client.DiskNodeClient) HttpDiskNodeClient(com.bonree.brfs.disknode.client.HttpDiskNodeClient) ByteConsumer(com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer) CompletableFuture(java.util.concurrent.CompletableFuture) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 2 with HttpDiskNodeClient

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

the class HttpDiskNodeConnection method connect.

public void connect() {
    ClientConfig clientConfig = ClientConfig.builder().setResponseTimeout(DEFAULT_RESPONSE_TIMEOUT_MILLIS).setMaxConnectionPerRoute(MAX_CONNECTION_RER_ROUTE).setMaxConnection(MAX_CONNECTION_RER_ROUTE * 3).build();
    client = new HttpDiskNodeClient(address, port, clientConfig);
}
Also used : HttpDiskNodeClient(com.bonree.brfs.disknode.client.HttpDiskNodeClient) ClientConfig(com.bonree.brfs.common.net.http.client.ClientConfig)

Example 3 with HttpDiskNodeClient

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

the class DataEmitter method emit.

public void emit(byte[] data, FileNode fileNode, WriteCallback callback) {
    int[] duplicates = fileNode.getDuplicates();
    DiskNodeClient[] clients = new DiskNodeClient[duplicates.length];
    for (int i = 0; i < duplicates.length; i++) {
        if (!clientCaches.containsKey(duplicates[i])) {
            DiskNodeClient client = new HttpDiskNodeClient("localhost", 8080);
            clientCaches.put(duplicates[i], client);
        }
        clients[i] = clientCaches.get(duplicates[i]);
    }
    for (DiskNodeClient client : clients) {
        ResultGather gather = new ResultGather(clients.length, callback);
        ListenableFuture<WriteResult> f = execs.submit(new WriteTask(client, fileNode, data));
        Futures.addCallback(f, gather, execs);
    }
}
Also used : DiskNodeClient(com.bonree.brfs.disknode.client.DiskNodeClient) HttpDiskNodeClient(com.bonree.brfs.disknode.client.HttpDiskNodeClient) HttpDiskNodeClient(com.bonree.brfs.disknode.client.HttpDiskNodeClient) WriteResult(com.bonree.brfs.disknode.client.WriteResult)

Aggregations

HttpDiskNodeClient (com.bonree.brfs.disknode.client.HttpDiskNodeClient)3 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)2 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)1 HandleResult (com.bonree.brfs.common.net.http.HandleResult)1 ClientConfig (com.bonree.brfs.common.net.http.client.ClientConfig)1 Service (com.bonree.brfs.common.service.Service)1 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)1 WriteResult (com.bonree.brfs.disknode.client.WriteResult)1 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)1 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CompletableFuture (java.util.concurrent.CompletableFuture)1