Search in sources :

Example 1 with HandleResult

use of com.bonree.brfs.common.net.http.HandleResult in project BRFS by zhangnianli.

the class ListMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    String dirPath = null;
    try {
        dirPath = context.getConcreteFilePath(msg.getPath());
        int level = Integer.parseInt(msg.getParams().getOrDefault("level", "1"));
        File dir = new File(dirPath);
        if (!dir.exists()) {
            result.setSuccess(false);
            result.setCause(new FileNotFoundException(msg.getPath()));
            return;
        }
        if (!dir.isDirectory()) {
            result.setSuccess(false);
            result.setCause(new IllegalAccessException("[" + msg.getPath() + "] is not directory"));
            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(level, fileInfoList);
        result.setSuccess(true);
        result.setData(JsonUtils.toJsonBytes(fileInfoList));
    } catch (Exception e) {
        LOG.error("list dir[{}] error", dirPath, e);
        result.setSuccess(false);
    } finally {
        callback.completed(result);
    }
}
Also used : FileInfo(com.bonree.brfs.disknode.server.handler.data.FileInfo) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) HandleResult(com.bonree.brfs.common.net.http.HandleResult) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with HandleResult

use of com.bonree.brfs.common.net.http.HandleResult 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 3 with HandleResult

use of com.bonree.brfs.common.net.http.HandleResult in project BRFS by zhangnianli.

the class CloseMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    String filePath = null;
    try {
        filePath = diskContext.getConcreteFilePath(msg.getPath());
        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()) {
                result.setData(Longs.toByteArray(0));
                result.setSuccess(true);
                return;
            }
            MappedByteBuffer buffer = Files.map(dataFile);
            try {
                buffer.position(fileFormater.fileHeader().length());
                buffer.limit(buffer.capacity() - fileFormater.fileTailer().length());
                result.setData(Longs.toByteArray(ByteUtils.crc(buffer)));
                result.setSuccess(true);
                return;
            } finally {
                BufferUtils.release(buffer);
            }
        }
        LOG.info("start writing file tailer for {}", filePath);
        binding.first().flush();
        byte[] fileBytes = DataFileReader.readFile(filePath, 2);
        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);
        result.setData(Longs.toByteArray(crcCode));
        result.setSuccess(true);
    } catch (IOException e) {
        result.setSuccess(false);
        LOG.error("close file[{}] error!", filePath, e);
    } finally {
        callback.completed(result);
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) HandleResult(com.bonree.brfs.common.net.http.HandleResult) IOException(java.io.IOException) File(java.io.File) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker)

Example 4 with HandleResult

use of com.bonree.brfs.common.net.http.HandleResult in project BRFS by zhangnianli.

the class FileLengthMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    LOG.info("GET sequences of file[{}]", msg.getPath());
    String filePath = context.getConcreteFilePath(msg.getPath());
    threadPool.submit(new Runnable() {

        @Override
        public void run() {
            List<RecordElement> recordInfo = getRecordElements(filePath);
            if (recordInfo == null) {
                LOG.info("can not get record elements of file[{}]", filePath);
                callback.completed(new HandleResult(false));
                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);
                callback.completed(new HandleResult(false));
                return;
            }
            HandleResult result = new HandleResult(true);
            long fileLength = fileFormater.relativeOffset(lastElement.getOffset()) + lastElement.getSize();
            LOG.info("get file length[{}] from file[{}]", fileLength, filePath);
            result.setData(Longs.toByteArray(fileLength));
            callback.completed(result);
        }
    });
}
Also used : RecordElement(com.bonree.brfs.disknode.data.write.record.RecordElement) ArrayList(java.util.ArrayList) List(java.util.List) HandleResult(com.bonree.brfs.common.net.http.HandleResult)

Example 5 with HandleResult

use of com.bonree.brfs.common.net.http.HandleResult in project BRFS by zhangnianli.

the class OpenMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    String realPath = null;
    try {
        String capacityParam = msg.getParams().get("capacity");
        if (capacityParam == null) {
            result.setSuccess(false);
            return;
        }
        long capacity = Long.parseLong(capacityParam);
        FileFormater fileFormater = new SimpleFileFormater(Math.min(capacity, MAX_CAPACITY));
        realPath = diskContext.getConcreteFilePath(msg.getPath());
        LOG.info("open file [{}]", realPath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(realPath, true);
        if (binding == null) {
            LOG.error("get file writer for file[{}] error!", realPath);
            result.setSuccess(false);
            return;
        }
        binding.first().write(fileFormater.fileHeader().getBytes());
        binding.first().flush();
        result.setData(Longs.toByteArray(fileFormater.maxBodyLength()));
        result.setSuccess(true);
    } catch (Exception e) {
        LOG.error("write header to file[{}] error!", realPath);
        result.setSuccess(false);
    } finally {
        callback.completed(result);
    }
}
Also used : FileFormater(com.bonree.brfs.disknode.fileformat.FileFormater) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater) RecordFileWriter(com.bonree.brfs.disknode.data.write.RecordFileWriter) HandleResult(com.bonree.brfs.common.net.http.HandleResult) WriteWorker(com.bonree.brfs.disknode.data.write.worker.WriteWorker) SimpleFileFormater(com.bonree.brfs.disknode.fileformat.impl.SimpleFileFormater)

Aggregations

HandleResult (com.bonree.brfs.common.net.http.HandleResult)15 StorageRegion (com.bonree.brfs.duplication.storageregion.StorageRegion)5 RecordFileWriter (com.bonree.brfs.disknode.data.write.RecordFileWriter)4 WriteWorker (com.bonree.brfs.disknode.data.write.worker.WriteWorker)4 File (java.io.File)4 Service (com.bonree.brfs.common.service.Service)3 ReturnCode (com.bonree.brfs.common.ReturnCode)2 StorageRegionConfig (com.bonree.brfs.duplication.storageregion.StorageRegionConfig)2 StorageNameNonexistentException (com.bonree.brfs.duplication.storageregion.exception.StorageNameNonexistentException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 FileObjectSyncState (com.bonree.brfs.common.filesync.FileObjectSyncState)1 JsonException (com.bonree.brfs.common.utils.JsonUtils.JsonException)1 DataItem (com.bonree.brfs.common.write.data.DataItem)1 WriteDataMessage (com.bonree.brfs.common.write.data.WriteDataMessage)1 DiskNodeClient (com.bonree.brfs.disknode.client.DiskNodeClient)1 ByteConsumer (com.bonree.brfs.disknode.client.DiskNodeClient.ByteConsumer)1 HttpDiskNodeClient (com.bonree.brfs.disknode.client.HttpDiskNodeClient)1 RecordElement (com.bonree.brfs.disknode.data.write.record.RecordElement)1