Search in sources :

Example 11 with BaseResponse

use of com.bonree.brfs.common.net.tcp.BaseResponse 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 12 with BaseResponse

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

the class MetadataFetchMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    String path = BrStringUtils.fromUtf8Bytes(baseMessage.getBody());
    if (path == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    String filePath = context.getConcreteFilePath(path);
    LOG.info("GET metadata of file[{}]", filePath);
    List<RecordElement> recordInfo = getRecordElements(filePath);
    if (recordInfo == null) {
        LOG.info("can not get record elements of file[{}]", filePath);
        writer.write(new BaseResponse(ResponseCode.ERROR));
        return;
    }
    // 获取所有文件序列号
    RecordElement lastElement = null;
    if (recordInfo != null) {
        for (RecordElement element : recordInfo) {
            if (lastElement == null) {
                lastElement = element;
                continue;
            }
            if (lastElement.getOffset() < element.getOffset()) {
                lastElement = element;
            }
        }
    }
    if (lastElement == null) {
        LOG.info("no available record element of file[{}]", filePath);
        writer.write(new BaseResponse(ResponseCode.ERROR));
        return;
    }
    long fileLength = fileFormater.relativeOffset(lastElement.getOffset()) + lastElement.getSize();
    LOG.info("get file length[{}] from file[{}]", fileLength, filePath);
    BaseResponse response = new BaseResponse(ResponseCode.OK);
    response.setBody(Longs.toByteArray(fileLength));
    writer.write(response);
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) RecordElement(com.bonree.brfs.disknode.data.write.record.RecordElement)

Example 13 with BaseResponse

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

the class CloseFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    String path = BrStringUtils.fromUtf8Bytes(baseMessage.getBody());
    if (path == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    try {
        final String filePath = diskContext.getConcreteFilePath(path);
        LOG.info("CLOSE file[{}]", filePath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(filePath, false);
        if (binding == null) {
            LOG.info("no writer is found for file[{}], treat it as OK!", filePath);
            File dataFile = new File(filePath);
            if (!dataFile.exists()) {
                writer.write(new BaseResponse(ResponseCode.ERROR));
                return;
            }
            MappedByteBuffer buffer = Files.map(dataFile);
            try {
                buffer.position(fileFormater.fileHeader().length());
                buffer.limit(buffer.capacity() - fileFormater.fileTailer().length());
                BaseResponse response = new BaseResponse(ResponseCode.OK);
                response.setBody(Longs.toByteArray(ByteUtils.crc(buffer)));
                BufferUtils.release(buffer);
                writer.write(response);
                return;
            } finally {
                BufferUtils.release(buffer);
            }
        }
        binding.second().put(new WriteTask<Long>() {

            @Override
            protected Long execute() throws Exception {
                LOG.info("start writing file tailer for {}", filePath);
                binding.first().flush();
                byte[] fileBytes = DataFileReader.readFile(filePath, fileFormater.fileHeader().length());
                long crcCode = ByteUtils.crc(fileBytes);
                LOG.info("final crc code[{}] by bytes[{}] of file[{}]", crcCode, fileBytes.length, filePath);
                byte[] tailer = Bytes.concat(FileEncoder.validate(crcCode), FileEncoder.tail());
                binding.first().write(tailer);
                binding.first().flush();
                LOG.info("close over for file[{}]", filePath);
                writerManager.close(filePath);
                return crcCode;
            }

            @Override
            protected void onPostExecute(Long result) {
                BaseResponse response = new BaseResponse(ResponseCode.OK);
                response.setBody(Longs.toByteArray(result));
                writer.write(response);
            }

            @Override
            protected void onFailed(Throwable e) {
                BaseResponse response = new BaseResponse(ResponseCode.ERROR);
                writer.write(response);
            }
        });
    } catch (IOException e) {
        LOG.error("close file[{}] error!", path);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) MappedByteBuffer(java.nio.MappedByteBuffer) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 14 with BaseResponse

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

the class ListFileMessageHandler method handleMessage.

@Override
public void handleMessage(BaseMessage baseMessage, ResponseWriter<BaseResponse> writer) {
    ListFileMessage message = ProtoStuffUtils.deserialize(baseMessage.getBody(), ListFileMessage.class);
    if (message == null) {
        writer.write(new BaseResponse(ResponseCode.ERROR_PROTOCOL));
        return;
    }
    String dirPath = null;
    try {
        dirPath = context.getConcreteFilePath(message.getPath());
        File dir = new File(dirPath);
        if (!dir.exists()) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        if (!dir.isDirectory()) {
            writer.write(new BaseResponse(ResponseCode.ERROR));
            return;
        }
        FileInfo dirInfo = new FileInfo();
        dirInfo.setLevel(0);
        dirInfo.setType(FileInfo.TYPE_DIR);
        dirInfo.setPath(dirPath);
        fileList.addLast(dirInfo);
        ArrayList<FileInfo> fileInfoList = new ArrayList<FileInfo>();
        traverse(message.getLevel(), fileInfoList);
        BaseResponse response = new BaseResponse(ResponseCode.OK);
        response.setBody(JsonUtils.toJsonBytes(fileInfoList));
        writer.write(response);
    } catch (Exception e) {
        LOG.error("list dir[{}] error", dirPath, e);
        writer.write(new BaseResponse(ResponseCode.ERROR));
    }
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) FileInfo(com.bonree.brfs.disknode.server.handler.data.FileInfo) ArrayList(java.util.ArrayList) ListFileMessage(com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage) File(java.io.File)

Example 15 with BaseResponse

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

the class TcpDiskNodeConnectionPool method getConnection.

@Override
public DiskNodeConnection getConnection(String serviceGroup, String serviceId) {
    TcpDiskNodeConnection connection = connectionCache.get(serviceGroup, serviceId);
    if (connection != null) {
        if (connection.isValid()) {
            return connection;
        }
        synchronized (connectionCache) {
            connectionCache.remove(serviceGroup, serviceId);
        }
    }
    try {
        synchronized (connectionCache) {
            Service service = serviceManager.getServiceById(serviceGroup, serviceId);
            if (service == null) {
                return null;
            }
            TcpClient<BaseMessage, BaseResponse> client = tcpClientGroup.createClient(new TcpClientConfig() {

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

                @Override
                public int connectTimeoutMillis() {
                    return 3000;
                }
            }, executor);
            if (client == null) {
                return null;
            }
            connection = new TcpDiskNodeConnection(client);
            connectionCache.put(serviceGroup, serviceId, connection);
        }
        return connection;
    } catch (Exception e) {
        LOG.error("connect tcp connection to disk node error", e);
    }
    return null;
}
Also used : BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) TcpClientConfig(com.bonree.brfs.common.net.tcp.client.TcpClientConfig) InetSocketAddress(java.net.InetSocketAddress) Service(com.bonree.brfs.common.service.Service) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)22 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)13 IOException (java.io.IOException)13 CompletableFuture (java.util.concurrent.CompletableFuture)11 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)4 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)4 WriteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.WriteFileMessage)3 File (java.io.File)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 TcpClientConfig (com.bonree.brfs.common.net.tcp.client.TcpClientConfig)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 Service (com.bonree.brfs.common.service.Service)2 TcpDiskNodeClient (com.bonree.brfs.disknode.client.TcpDiskNodeClient)2 DeleteFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.DeleteFileMessage)2 FileRecoveryMessage (com.bonree.brfs.disknode.server.tcp.handler.data.FileRecoveryMessage)2 ListFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.ListFileMessage)2 OpenFileMessage (com.bonree.brfs.disknode.server.tcp.handler.data.OpenFileMessage)2