Search in sources :

Example 1 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject in project BRFS by zhangnianli.

the class TcpDiskNodeClient method readData.

@Override
public void readData(String path, long offset, int size, ByteConsumer consumer) throws IOException {
    if (readClient == null) {
        throw new UnsupportedOperationException("no read client is set");
    }
    ReadObject object = new ReadObject();
    object.setFilePath(path);
    object.setOffset(offset);
    object.setLength(size);
    try {
        readClient.sendMessage(object, new ResponseHandler<FileContentPart>() {

            @Override
            public void handle(FileContentPart response) {
                consumer.consume(response.content(), response.endOfContent());
            }

            @Override
            public void error(Throwable t) {
                consumer.error(t);
            }
        });
    } catch (Exception e) {
        throw new IOException("can not send read message", e);
    }
}
Also used : ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject 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 3 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject in project BRFS by zhangnianli.

the class TcpDiskNodeClient method readFile.

@Override
public void readFile(String path, ByteConsumer consumer) throws IOException {
    if (readClient == null) {
        throw new UnsupportedOperationException("no read client is set");
    }
    ReadObject object = new ReadObject();
    object.setFilePath(path);
    object.setOffset(0);
    object.setLength(Integer.MAX_VALUE);
    object.setRaw(ReadObject.RAW_OFFSET);
    try {
        readClient.sendMessage(object, new ResponseHandler<FileContentPart>() {

            @Override
            public void handle(FileContentPart response) {
                consumer.consume(response.content(), response.endOfContent());
            }

            @Override
            public void error(Throwable t) {
                consumer.error(t);
            }
        });
    } catch (Exception e) {
        throw new IOException("can not send read message", e);
    }
}
Also used : ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject) FileContentPart(com.bonree.brfs.common.net.tcp.file.client.FileContentPart) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject in project BRFS by zhangnianli.

the class ConnectionPool method getConnection.

@SuppressWarnings("unchecked")
public TcpClient<ReadObject, FileContentPart> getConnection(Service service) {
    TcpClient<ReadObject, FileContentPart>[] clients = clientCache.get(service.getServiceId());
    TcpClient<ReadObject, FileContentPart> client = null;
    int index = random.nextInt(connectionPerRoute);
    if (clients == null) {
        clientCache.putIfAbsent(service.getServiceId(), (TcpClient<ReadObject, FileContentPart>[]) Array.newInstance(TcpClient.class, connectionPerRoute));
        clients = clientCache.get(service.getServiceId());
    }
    client = clients[index];
    if (client != null) {
        return client;
    }
    synchronized (clients) {
        if (clients[index] != null) {
            return clients[index];
        }
        try {
            client = group.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 1000 * 100;
                }
            });
            if (client == null) {
                return null;
            }
            client.setClientCloseListener(new TcpClientCloseListener() {

                @Override
                public void clientClosed() {
                    TcpClient<ReadObject, FileContentPart>[] clientArray = clientCache.get(service.getServiceId());
                    synchronized (clientArray) {
                        clientArray[index] = null;
                    }
                }
            });
            clients[index] = client;
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : TcpClientCloseListener(com.bonree.brfs.common.net.tcp.client.TcpClientCloseListener) TcpClient(com.bonree.brfs.common.net.tcp.client.TcpClient) 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)

Example 5 with ReadObject

use of com.bonree.brfs.common.net.tcp.file.ReadObject in project BRFS by zhangnianli.

the class ReadObjectEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, TokenMessage<ReadObject> object, ByteBuf out) throws Exception {
    ReadObject readObject = object.message();
    readObject.setToken(object.messageToken());
    // out.writeBytes(JsonUtils.toJsonBytes(object.message()));
    out.writeBytes(Joiner.on(';').useForNull("-").join(readObject.getSn(), readObject.getIndex(), readObject.getTime(), readObject.getDuration(), readObject.getFileName(), readObject.getFilePath(), readObject.getOffset(), readObject.getLength(), readObject.getRaw(), readObject.getToken(), "\n").getBytes(Charsets.UTF_8));
}
Also used : ReadObject(com.bonree.brfs.common.net.tcp.file.ReadObject)

Aggregations

ReadObject (com.bonree.brfs.common.net.tcp.file.ReadObject)7 FileContentPart (com.bonree.brfs.common.net.tcp.file.client.FileContentPart)5 AsyncFileReaderCreateConfig (com.bonree.brfs.common.net.tcp.file.client.AsyncFileReaderCreateConfig)3 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)2 Service (com.bonree.brfs.common.service.Service)2 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)2 SocketAddress (java.net.SocketAddress)2 InputItem (com.bonree.brfs.client.InputItem)1 ServiceMetaInfo (com.bonree.brfs.client.route.ServiceMetaInfo)1 BRFSException (com.bonree.brfs.common.exception.BRFSException)1 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)1 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 TcpClient (com.bonree.brfs.common.net.tcp.client.TcpClient)1 TcpClientCloseListener (com.bonree.brfs.common.net.tcp.client.TcpClientCloseListener)1 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)1 Fid (com.bonree.brfs.common.proto.FileDataProtos.Fid)1 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)1 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)1