Search in sources :

Example 11 with HandleResult

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

the class UpdateStorageRegionMessageHandler method handleMessage.

@Override
public void handleMessage(StorageRegionMessage msg, HandleResultCallback callback) {
    LOG.info("update storage region[{}] with attrs {}", msg.getName(), msg.getAttributes());
    StorageRegion region = storageRegionManager.findStorageRegionByName(msg.getName());
    if (region == null) {
        callback.completed(new HandleResult(false));
        return;
    }
    try {
        StorageRegionConfig config = new StorageRegionConfig(region);
        config.update(msg.getAttributes());
        storageRegionManager.updateStorageRegion(msg.getName(), config);
        callback.completed(new HandleResult(true));
    } catch (Exception e) {
        LOG.error("remove nonexist storage region");
        callback.completed(new HandleResult(false));
    }
}
Also used : StorageRegionConfig(com.bonree.brfs.duplication.storageregion.StorageRegionConfig) HandleResult(com.bonree.brfs.common.net.http.HandleResult) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion)

Example 12 with HandleResult

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

the class OpenStorageRegionMessageHandler method handleMessage.

@Override
public void handleMessage(StorageRegionMessage msg, HandleResultCallback callback) {
    LOG.info("open storage region[{}]", msg.getName());
    StorageRegion node = storageRegionManager.findStorageRegionByName(msg.getName());
    if (node == null || !node.isEnable()) {
        callback.completed(new HandleResult(false));
        return;
    }
    HandleResult result = new HandleResult();
    result.setSuccess(true);
    result.setData(Ints.toByteArray(node.getId()));
    callback.completed(result);
}
Also used : HandleResult(com.bonree.brfs.common.net.http.HandleResult) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion)

Example 13 with HandleResult

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

the class ReadMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    try {
        String filePath = diskContext.getConcreteFilePath(msg.getPath());
        File dataFile = new File(filePath);
        if (!dataFile.exists() || !dataFile.isFile()) {
            result.setSuccess(false);
            return;
        }
        String offsetParam = msg.getParams().get(PARAM_READ_OFFSET);
        String lengthParam = msg.getParams().get(PARAM_READ_LENGTH);
        int offset = offsetParam == null ? 0 : Integer.parseInt(offsetParam);
        int length = (int) (lengthParam == null ? fileFormater.maxBodyLength() : Integer.parseInt(lengthParam));
        length = (int) Math.min(length, dataFile.length() - 11);
        LOG.info("read data offset[{}], size[{}] from file[{}]", offset, length, filePath);
        byte[] data = null;
        if (length < READ_LENGTH_LIMIT) {
            data = Files.asByteSource(new File(filePath)).slice(fileFormater.absoluteOffset(offset), length).read();
        } else {
            data = DataFileReader.readFile(filePath, fileFormater.absoluteOffset(offset), length);
        }
        result.setSuccess(data.length != length ? false : true);
        result.setData(data);
    } catch (Exception e) {
        LOG.error("read message error", e);
        result.setSuccess(false);
    } finally {
        callback.completed(result);
    }
}
Also used : HandleResult(com.bonree.brfs.common.net.http.HandleResult) File(java.io.File)

Example 14 with HandleResult

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

the class WriteMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    try {
        String realPath = diskContext.getConcreteFilePath(msg.getPath());
        LOG.debug("writing to file [{}]", realPath);
        Pair<RecordFileWriter, WriteWorker> binding = writerManager.getBinding(realPath, false);
        if (binding == null) {
            // 运行到这,可能时打开文件时失败,导致写数据节点找不到writer
            LOG.warn("no file writer is found, maybe the file[{}] is not opened.", realPath);
            callback.completed(new HandleResult(false));
            return;
        }
        binding.second().put(new DataWriteTask(binding, msg, callback));
    } catch (Exception e) {
        LOG.error("EEEERRRRRR", e);
        HandleResult handleResult = new HandleResult();
        handleResult.setSuccess(false);
        handleResult.setCause(e);
        callback.completed(handleResult);
    }
}
Also used : 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) IOException(java.io.IOException)

Example 15 with HandleResult

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

the class DeleteDataMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    HandleResult result = new HandleResult();
    List<String> deleteInfo = Splitter.on("/").omitEmptyStrings().trimResults().splitToList(msg.getPath());
    if (deleteInfo.size() != 2) {
        result.setSuccess(false);
        result.setCause(new IllegalArgumentException(msg.getPath()));
        callback.completed(result);
        return;
    }
    int storageId = Integer.parseInt(deleteInfo.get(0));
    LOG.info("DELETE data for storage[{}]", storageId);
    StorageRegion sn = storageNameManager.findStorageRegionById(storageId);
    if (sn == null) {
        result.setSuccess(false);
        result.setCause(new StorageNameNonexistentException(storageId));
        callback.completed(result);
        LOG.info("storage[{}] is null", storageId);
        return;
    }
    List<String> times = Splitter.on("_").omitEmptyStrings().trimResults().splitToList(deleteInfo.get(1));
    ReturnCode code = checkTime(times.get(0), times.get(1), sn.getCreateTime(), Duration.parse(sn.getFilePartitionDuration()).toMillis());
    if (!ReturnCode.SUCCESS.equals(code)) {
        result.setSuccess(false);
        result.setData(BrStringUtils.toUtf8Bytes(code.name()));
        callback.completed(result);
        LOG.info("DELETE DATE Fail storage[{}] reason : {}", storageId, code.name());
        return;
    }
    long startTime = DateTime.parse(times.get(0)).getMillis();
    long endTime = DateTime.parse(times.get(1)).getMillis();
    LOG.info("DELETE DATA [{}-->{}]", times.get(0), times.get(1));
    List<Service> serviceList = serviceManager.getServiceListByGroup(Configs.getConfiguration().GetConfig(CommonConfigs.CONFIG_DATA_SERVICE_GROUP_NAME));
    code = TasksUtils.createUserDeleteTask(serviceList, zkPaths, sn, startTime, endTime, false);
    result.setSuccess(ReturnCode.SUCCESS.equals(code));
    result.setData(BrStringUtils.toUtf8Bytes(code.name()));
    callback.completed(result);
}
Also used : ReturnCode(com.bonree.brfs.common.ReturnCode) Service(com.bonree.brfs.common.service.Service) HandleResult(com.bonree.brfs.common.net.http.HandleResult) StorageRegion(com.bonree.brfs.duplication.storageregion.StorageRegion) StorageNameNonexistentException(com.bonree.brfs.duplication.storageregion.exception.StorageNameNonexistentException)

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