Search in sources :

Example 6 with FileMapping

use of com.baidu.hugegraph.entity.load.FileMapping in project incubator-hugegraph-toolchain by apache.

the class FileMappingService method deleteUnfinishedFile.

@Async
@Scheduled(fixedRate = 10 * 60 * 1000)
public void deleteUnfinishedFile() {
    QueryWrapper<FileMapping> query = Wrappers.query();
    query.in("file_status", FileMappingStatus.UPLOADING.getValue());
    List<FileMapping> mappings = this.mapper.selectList(query);
    long threshold = this.config.get(HubbleOptions.UPLOAD_FILE_MAX_TIME_CONSUMING) * 1000;
    Date now = HubbleUtil.nowDate();
    for (FileMapping mapping : mappings) {
        Date updateTime = mapping.getUpdateTime();
        long duration = now.getTime() - updateTime.getTime();
        if (duration > threshold) {
            String filePath = mapping.getPath();
            try {
                FileUtils.forceDelete(new File(filePath));
            } catch (IOException e) {
                log.warn("Failed to delete expired uploading file {}", filePath, e);
            }
            this.remove(mapping.getId());
            // Delete corresponding uploading tokens
            Iterator<Map.Entry<String, ReadWriteLock>> iter;
            iter = this.uploadingTokenLocks.entrySet().iterator();
            iter.forEachRemaining(entry -> {
                String token = entry.getKey();
                if (token.startsWith(mapping.getName())) {
                    iter.remove();
                }
            });
        }
    }
}
Also used : FileMapping(com.baidu.hugegraph.entity.load.FileMapping) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled) Async(org.springframework.scheduling.annotation.Async)

Example 7 with FileMapping

use of com.baidu.hugegraph.entity.load.FileMapping in project incubator-hugegraph-toolchain by apache.

the class FileUploadController method delete.

@DeleteMapping
public Boolean delete(@PathVariable("connId") int connId, @PathVariable("jobId") int jobId, @RequestParam("name") String fileName, @RequestParam("token") String token) {
    JobManager jobEntity = this.jobService.get(jobId);
    Ex.check(jobEntity != null, "job-manager.not-exist.id", jobId);
    Ex.check(jobEntity.getJobStatus() == JobStatus.UPLOADING || jobEntity.getJobStatus() == JobStatus.MAPPING || jobEntity.getJobStatus() == JobStatus.SETTING, "deleted.file.no-permission");
    FileMapping mapping = this.service.get(connId, jobId, fileName);
    Ex.check(mapping != null, "load.file-mapping.not-exist.name", fileName);
    ReadWriteLock lock = this.uploadingTokenLocks().get(token);
    if (lock != null) {
        lock.writeLock().lock();
    }
    try {
        this.service.deleteDiskFile(mapping);
        log.info("Prepare to remove file mapping {}", mapping.getId());
        this.service.remove(mapping.getId());
        long jobSize = jobEntity.getJobSize() - mapping.getTotalSize();
        jobEntity.setJobSize(jobSize);
        this.jobService.update(jobEntity);
        if (lock != null) {
            this.uploadingTokenLocks().remove(token);
        }
        return true;
    } finally {
        if (lock != null) {
            lock.writeLock().unlock();
        }
    }
}
Also used : FileMapping(com.baidu.hugegraph.entity.load.FileMapping) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) JobManager(com.baidu.hugegraph.entity.load.JobManager) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 8 with FileMapping

use of com.baidu.hugegraph.entity.load.FileMapping in project incubator-hugegraph-toolchain by apache.

the class FileUploadController method checkFileValid.

private void checkFileValid(int connId, int jobId, JobManager jobEntity, MultipartFile file, String fileName) {
    Ex.check(jobEntity != null, "job-manager.not-exist.id", jobId);
    Ex.check(jobEntity.getJobStatus() == JobStatus.DEFAULT || jobEntity.getJobStatus() == JobStatus.UPLOADING || jobEntity.getJobStatus() == JobStatus.MAPPING || jobEntity.getJobStatus() == JobStatus.SETTING, "load.upload.file.no-permission");
    // Now allowed to upload empty file
    Ex.check(!file.isEmpty(), "load.upload.file.cannot-be-empty");
    // Difficult: how to determine whether the file is csv or text
    log.debug("File content type: {}", file.getContentType());
    String format = FilenameUtils.getExtension(fileName);
    List<String> formatWhiteList = this.config.get(HubbleOptions.UPLOAD_FILE_FORMAT_LIST);
    Ex.check(formatWhiteList.contains(format), "load.upload.file.format.unsupported");
    long fileSize = file.getSize();
    long singleFileSizeLimit = this.config.get(HubbleOptions.UPLOAD_SINGLE_FILE_SIZE_LIMIT);
    Ex.check(fileSize <= singleFileSizeLimit, "load.upload.file.exceed-single-size", FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
    // Check is there a file with the same name
    FileMapping oldMapping = this.service.get(connId, jobId, fileName);
    Ex.check(oldMapping == null || oldMapping.getFileStatus() == FileMappingStatus.UPLOADING, "load.upload.file.existed", fileName);
    long totalFileSizeLimit = this.config.get(HubbleOptions.UPLOAD_TOTAL_FILE_SIZE_LIMIT);
    List<FileMapping> fileMappings = this.service.listAll();
    long currentTotalSize = fileMappings.stream().map(FileMapping::getTotalSize).reduce(0L, (Long::sum));
    Ex.check(fileSize + currentTotalSize <= totalFileSizeLimit, "load.upload.file.exceed-single-size", FileUtils.byteCountToDisplaySize(totalFileSizeLimit));
}
Also used : FileMapping(com.baidu.hugegraph.entity.load.FileMapping)

Example 9 with FileMapping

use of com.baidu.hugegraph.entity.load.FileMapping in project incubator-hugegraph-toolchain by apache.

the class LoadTaskController method reason.

@GetMapping("{id}/reason")
public Response reason(@PathVariable("connId") int connId, @PathVariable("jobId") int jobId, @PathVariable("id") int id) {
    LoadTask task = this.service.get(id);
    if (task == null) {
        throw new ExternalException("load.task.not-exist.id", id);
    }
    JobManager jobEntity = this.jobService.get(jobId);
    Ex.check(jobEntity != null, "job-manager.not-exist.id", jobId);
    Integer fileId = task.getFileId();
    FileMapping mapping = this.fmService.get(fileId);
    String reason = this.service.readLoadFailedReason(mapping);
    return Response.builder().status(Constant.STATUS_OK).data(reason).build();
}
Also used : LoadTask(com.baidu.hugegraph.entity.load.LoadTask) FileMapping(com.baidu.hugegraph.entity.load.FileMapping) JobManager(com.baidu.hugegraph.entity.load.JobManager) ExternalException(com.baidu.hugegraph.exception.ExternalException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with FileMapping

use of com.baidu.hugegraph.entity.load.FileMapping in project incubator-hugegraph-toolchain by apache.

the class LoadTaskController method start.

@PostMapping("start")
public List<LoadTask> start(@PathVariable("connId") int connId, @PathVariable("jobId") int jobId, @RequestParam("file_mapping_ids") List<Integer> fileIds) {
    GraphConnection connection = this.connService.get(connId);
    if (connection == null) {
        throw new ExternalException("graph-connection.not-exist.id", connId);
    }
    JobManager jobEntity = this.jobService.get(jobId);
    Ex.check(jobEntity != null, "job-manager.not-exist.id", jobId);
    Ex.check(jobEntity.getJobStatus() == JobStatus.SETTING, "load.task.start.no-permission");
    boolean existError = false;
    try {
        List<LoadTask> tasks = new ArrayList<>();
        for (Integer fileId : fileIds) {
            FileMapping fileMapping = this.fmService.get(fileId);
            if (fileMapping == null) {
                throw new ExternalException("file-mapping.not-exist.id", fileId);
            }
            tasks.add(this.service.start(connection, fileMapping));
        }
        return tasks;
    } catch (Exception e) {
        existError = true;
        throw e;
    } finally {
        if (existError) {
            jobEntity.setJobStatus(JobStatus.FAILED);
        } else {
            jobEntity.setJobStatus(JobStatus.LOADING);
        }
        jobEntity.setUpdateTime(HubbleUtil.nowDate());
        this.jobService.update(jobEntity);
    }
}
Also used : LoadTask(com.baidu.hugegraph.entity.load.LoadTask) FileMapping(com.baidu.hugegraph.entity.load.FileMapping) GraphConnection(com.baidu.hugegraph.entity.GraphConnection) ArrayList(java.util.ArrayList) JobManager(com.baidu.hugegraph.entity.load.JobManager) ExternalException(com.baidu.hugegraph.exception.ExternalException) ExternalException(com.baidu.hugegraph.exception.ExternalException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

FileMapping (com.baidu.hugegraph.entity.load.FileMapping)16 ExternalException (com.baidu.hugegraph.exception.ExternalException)11 PostMapping (org.springframework.web.bind.annotation.PostMapping)6 JobManager (com.baidu.hugegraph.entity.load.JobManager)5 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 LoadTask (com.baidu.hugegraph.entity.load.LoadTask)3 EdgeMapping (com.baidu.hugegraph.entity.load.EdgeMapping)2 VertexMapping (com.baidu.hugegraph.entity.load.VertexMapping)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 GraphConnection (com.baidu.hugegraph.entity.GraphConnection)1 FileSetting (com.baidu.hugegraph.entity.load.FileSetting)1 FileUploadResult (com.baidu.hugegraph.entity.load.FileUploadResult)1 JobManagerReasonResult (com.baidu.hugegraph.entity.load.JobManagerReasonResult)1 LoadParameter (com.baidu.hugegraph.entity.load.LoadParameter)1