@Override protected FileSystemMasterClient createNewResource() { return new FileSystemMasterClient(mMasterAddress, ClientContext.getConf()); } }
/** * Marks the file as complete. * * @param fid the file id * @throws IOException if the underlying master RPC fails */ synchronized void completeFile(long fid) throws IOException { try { mFSMasterClient.completeFile(fid); } catch (TachyonException e) { throw new IOException(e); } }
/** * @return the under file system address * @throws IOException if an I/O error occurs */ public synchronized String getUfsAddress() throws IOException { int retry = 0; while (!mClosed && (retry ++) <= RPC_MAX_NUM_RETRY) { connect(); try { return mClient.getUfsAddress(); } catch (TException e) { LOG.error(e.getMessage(), e); mConnected = false; } } throw new IOException("Failed after " + retry + " retries."); }
/** * Creates a new file in the file system. * * @param path The path of the file * @param ufsPath The path of the file in the under file system. If this is empty, the file does * not exist in the under file system yet. * @param blockSizeBytes The size of the block in bytes. It is -1 iff ufsPath is non-empty. * @param recursive Creates necessary parent folders if true, not otherwise. * @return The file id, which is globally unique * @throws IOException if the underlying master RPC fails */ @Override public synchronized long createFile(TachyonURI path, TachyonURI ufsPath, long blockSizeBytes, boolean recursive) throws IOException { validateUri(path); try { if (blockSizeBytes > 0) { CreateOptions options = new CreateOptions.Builder(ClientContext.getConf()).setBlockSizeBytes(blockSizeBytes) .setRecursive(recursive).build(); return mFSMasterClient.create(path.getPath(), options); } else { return mFSMasterClient.loadMetadata(path.getPath(), recursive); } } catch (TachyonException e) { throw new IOException(e); } }
private FileInfo getFileInfo() throws IOException { FileSystemMasterClient client = mContext.acquireMasterClient(); try { return client.getFileInfo(mFileId); } catch (TachyonException e) { throw new IOException(e.getMessage()); } finally { mContext.releaseMasterClient(client); } } }
/** * If the given file id is INVALID_FILE_ID, tries to fetch the file id for the given path * * @param fileId * @param path the path to search for if fileId is INVALID_FILE_ID * @return the original fileId if it wasn't INVALID_FILE_ID, or a new fileId corresponding to the * given path * @throws IOException if the given fileId is INVALID_FILE_ID and no fileId was found for the * given path */ private long getValidFileId(long fileId, String path) throws IOException { if (fileId == IdUtils.INVALID_FILE_ID) { fileId = mFSMasterClient.getFileId(path); if (fileId == IdUtils.INVALID_FILE_ID) { throw new IOException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path)); } } return fileId; }
/** * Gets a ClientBlockInfo by the file and index. * * @param fileId the id of the file * @param blockIndex the index of the block in the file, starting from 0 * @return the ClientBlockInfo of the specified block * @throws IOException if the underlying master RPC fails */ synchronized FileBlockInfo getClientBlockInfo(long fileId, int blockIndex) throws IOException { try { return mFSMasterClient.getFileBlockInfo(fileId, blockIndex); } catch (TachyonException e) { // TODO: Should not cast this to Tachyon Exception throw new IOException(e); } }
/** * Frees an in-memory file or folder. * * @param fileId The id of the file / folder. If it is not INVALID_FILE_ID, path parameter is * ignored. Otherwise, the method uses the path parameter. * @param path The path of the file / folder. It could be empty iff id is not INVALID_FILE_ID. * @param recursive If fileId or path represents a non-empty folder, free the folder recursively * or not * @return true if in-memory free successfully, false otherwise * @throws IOException if the underlying master RPC fails */ @Override public synchronized boolean freepath(long fileId, TachyonURI path, boolean recursive) throws IOException { validateUri(path); fileId = getValidFileId(fileId, path.getPath()); try { return mFSMasterClient.free(fileId, recursive); } catch (TachyonException e) { throw new IOException(e); } }
/** * Deletes a file or folder. * * @param fileId The id of the file / folder. If it is not INVALID_FILE_ID, path parameter is * ignored. Otherwise, the method uses the path parameter. * @param path The path of the file / folder. It could be empty iff id is not INVALID_FILE_ID. * @param recursive If fileId or path represents a non-empty folder, delete the folder recursively * or not. * @return true if deletes successfully, false otherwise * @throws IOException if the underlying master RPC fails */ @Override public synchronized boolean delete(long fileId, TachyonURI path, boolean recursive) throws IOException { validateUri(path); fileId = getValidFileId(fileId, path.getPath()); try { return mFSMasterClient.deleteFile(fileId, recursive); } catch (TachyonException e) { throw new IOException(e); } }
/** * Gets all the blocks' info of the file. * * @param fid the file id * @return the list of the blocks' info * @throws IOException if the underlying master RPC fails */ public synchronized List<FileBlockInfo> getFileBlocks(long fid) throws IOException { // TODO(hy) Should read from mClientFileInfos if possible. // TODO(hy) Should add timeout to improve this. try { return mFSMasterClient.getFileBlockInfoList(fid); } catch (TachyonException e) { throw new IOException(e); } }
@Override public TachyonFile create(TachyonURI path, CreateOptions options) throws FileAlreadyExistsException, IOException, InvalidPathException, TachyonException { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { final long fileId = masterClient.create(path.getPath(), options); return new TachyonFile(fileId); } catch (TachyonException e) { if (e.getType() == TachyonExceptionType.BLOCK_INFO) { throw new FileAlreadyExistsException(e.getMessage(), e); } else { TachyonException.unwrap(e, FileAlreadyExistsException.class); TachyonException.unwrap(e, InvalidPathException.class); throw e; } } finally { mContext.releaseMasterClient(masterClient); } }
info = mFSMasterClient.getFileInfo(fileId); } catch (TachyonException e) { throw new IOException(e);
@Override public TachyonFile openIfExists(TachyonURI path, OpenOptions openOptions) throws IOException, TachyonException { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { long fileId = masterClient.getFileId(path.getPath()); if (fileId == -1) { return null; } return new TachyonFile(fileId); } finally { mContext.releaseMasterClient(masterClient); } }
/** * Get the block id by the file id and block index. it will check whether the file and the block * exist. * * @param fileId the file id * @param blockIndex The index of the block in the file. * @return the block id if exists * @throws IOException if the file does not exist, or connection issue was encountered */ public synchronized long getBlockId(long fileId, int blockIndex) throws IOException { FileInfo info = getFileStatus(fileId, true); if (info == null) { throw new IOException("File " + fileId + " does not exist."); } if (info.blockIds.size() > blockIndex) { return info.blockIds.get(blockIndex); } try { return mFSMasterClient.getFileBlockInfo(fileId, blockIndex).blockInfo.getBlockId(); } catch (TachyonException e) { throw new IOException(e); } }
/** * {@inheritDoc} * * This method is asynchronous and will be propagated to the workers through their heartbeats. */ @Override public void free(TachyonFile file, FreeOptions options) throws IOException, FileDoesNotExistException, TachyonException { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { masterClient.free(file.getFileId(), options.isRecursive()); LOG.info("Removed file " + file.getFileId() + " from Tachyon Storage"); } catch (TachyonException e) { TachyonException.unwrap(e, FileDoesNotExistException.class); throw e; } finally { mContext.releaseMasterClient(masterClient); } }
/** * {@inheritDoc} * * The delete will abort on a failure, but previous deletes (if deleting more than one file) that * occurred will still be effective. The delete will only synchronously be propagated to the * master. The file metadata will not be available after this call, but the data in Tachyon or * under storage space may still reside until the delete is propagated and all current readers * have relinquished their locks. */ @Override public void delete(TachyonFile file, DeleteOptions options) throws IOException, FileDoesNotExistException, TachyonException { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { masterClient.deleteFile(file.getFileId(), options.isRecursive()); LOG.info( "Deleted file " + file.getFileId() + " from both Tachyon Storage and under file system"); } catch (TachyonException e) { TachyonException.unwrap(e, FileDoesNotExistException.class); throw e; } finally { mContext.releaseMasterClient(masterClient); } }
/** * @param path the path * @return the file id for the given path, or -1 if the path does not point to a file * @throws IOException if an I/O error occurs */ public synchronized long getFileId(String path) throws IOException { int retry = 0; while (!mClosed && (retry ++) <= RPC_MAX_NUM_RETRY) { connect(); try { return mClient.getFileId(path); } catch (TException e) { LOG.error(e.getMessage(), e); mConnected = false; } } throw new IOException("Failed after " + retry + " retries."); }
/** * {@inheritDoc} * * The file info is a snapshot of the file metadata, and the locations, last modified time, and * path are possibly inconsistent. */ @Override public FileInfo getInfo(TachyonFile file, GetInfoOptions options) throws IOException, FileDoesNotExistException, TachyonException { FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { return masterClient.getFileInfo(file.getFileId()); } catch (TachyonException e) { TachyonException.unwrap(e, FileDoesNotExistException.class); throw e; } finally { mContext.releaseMasterClient(masterClient); } }
FileSystemMasterClient masterClient = mContext.acquireMasterClient(); try { masterClient.completeFile(mFileId); } catch (TachyonException e) { throw new IOException(e);
/** * Unmounts the given Tachyon path. * * @param tachyonPath the Tachyon path * @throws IOException an I/O error occurs */ public synchronized boolean unmount(TachyonURI tachyonPath) throws IOException { int retry = 0; while (!mClosed && (retry ++) <= RPC_MAX_NUM_RETRY) { connect(); try { return mClient.unmount(tachyonPath.toString()); } catch (TException e) { LOG.error(e.getMessage(), e); mConnected = false; } } throw new IOException("Failed after " + retry + " retries."); } }