int FileCreator::openForBlocks()

in util/FileCreator.cpp [125:170]


int FileCreator::openForBlocks(ThreadCtx &threadCtx,
                               BlockDetails const *blockDetails) {
  if (blockDetails->allocationStatus == TO_BE_DELETED) {
    const std::string path = getFullPath(blockDetails->fileName);
    int status;
    {
      PerfStatCollector statCollector(threadCtx, PerfStatReport::UNLINK);
      status = ::unlink(path.c_str());
    }
    if (status != 0) {
      WPLOG(ERROR) << "Failed to delete file " << path;
    } else {
      WLOG(INFO) << "Successfully deleted file " << path;
    }
    return -1;
  }
  lock_.lock();
  auto it = fileStatusMap_.find(blockDetails->seqId);
  if (blockDetails->allocationStatus == EXISTS_CORRECT_SIZE &&
      it == fileStatusMap_.end()) {
    it =
        fileStatusMap_
            .insert(std::make_pair(blockDetails->seqId, FileCreator::ALLOCATED))
            .first;
  }
  if (it == fileStatusMap_.end()) {
    // allocation has not started for this file
    fileStatusMap_.insert(
        std::make_pair(blockDetails->seqId, threadCtx.getThreadIndex()));
    lock_.unlock();
    return openForFirstBlock(threadCtx, blockDetails);
  }
  auto statusOrThreadIdx = it->second;
  lock_.unlock();
  if (statusOrThreadIdx == FAILED) {
    // allocation failed previously
    return -1;
  }
  if (statusOrThreadIdx != ALLOCATED) {
    // allocation in progress
    if (!waitForAllocationFinish(statusOrThreadIdx, blockDetails->seqId)) {
      return -1;
    }
  }
  return openExistingFile(threadCtx, blockDetails->fileName);
}