/** * @return a session ID */ public static long createSessionId() { return getRandomNonNegativeLong(); }
/** * Creates a file ID from a block ID. * * @param blockId the block ID * @return the file ID */ public static long fileIdFromBlockId(long blockId) { return createFileId(BlockId.getContainerId(blockId)); }
/** * * @param conf an alluxio configuration with the USER_APP_ID property key * @return a string representing the USER_APP_ID */ public static String createOrGetAppIdFromConfig(AlluxioConfiguration conf) { if (conf.isSet(PropertyKey.USER_APP_ID)) { return conf.get(PropertyKey.USER_APP_ID); } else { return IdUtils.createFileSystemContextId(); } } }
@Override public void validateInodeBlocks(boolean repair) throws UnavailableException { mBlockMaster.validateBlocks((blockId) -> { long fileId = IdUtils.fileIdFromBlockId(blockId); return mInodeTree.inodeIdExists(fileId); }, repair); }
ExceptionMessage.MOUNT_POINT_ALREADY_EXISTS.getMessage(inodePath.getUri())); long mountId = IdUtils.createMountId(); mountInternal(rpcContext, inodePath, ufsPath, mountId, context); boolean loadMetadataSucceeded = false;
/** * The method the worker should periodically execute to heartbeat back to the master. * * @param metrics a list of client metrics */ public void heartbeat(final List<Metric> metrics) throws IOException { connect(); try { MetricsHeartbeatPRequest.Builder request = MetricsHeartbeatPRequest.newBuilder(); request.setClientId(IdUtils.createOrGetAppIdFromConfig(mContext.getConf())); request.setHostname(NetworkAddressUtils.getClientHostName(mContext.getConf())); request.setOptions(MetricsHeartbeatPOptions.newBuilder().addAllMetrics(metrics).build()); mClient.metricsHeartbeat(request.build()); } catch (io.grpc.StatusRuntimeException e) { throw new UnavailableException(e); } } }
/** * Updates the UFS input stream given an offset to read. * * @param offset the read offset within the block */ private void updateUnderFileSystemInputStream(long offset) throws IOException { if ((mUnderFileSystemInputStream != null) && offset != mInStreamPos) { mUfsInstreamManager.release(mUnderFileSystemInputStream); mUnderFileSystemInputStream = null; mInStreamPos = -1; } if (mUnderFileSystemInputStream == null && offset < mBlockMeta.getBlockSize()) { UnderFileSystem ufs = mUfsResource.get(); mUnderFileSystemInputStream = mUfsInstreamManager.acquire(ufs, mBlockMeta.getUnderFileSystemPath(), IdUtils.fileIdFromBlockId(mBlockMeta.getBlockId()), OpenOptions.defaults().setOffset(mBlockMeta.getOffset() + offset)); mInStreamPos = offset; } }
ExceptionMessage.MOUNT_POINT_ALREADY_EXISTS.getMessage(inodePath.getUri())); long mountId = IdUtils.createMountId(); mountInternal(inodePath, ufsPath, mountId, false /* not replayed */, options); boolean loadMetadataSucceeded = false;
/** * @return a session id used internally in workers */ public static long createInternalSessionId() { return INTERNAL_SESSION_ID_BASE - IdUtils.getRandomNonNegativeLong() % (Long.MAX_VALUE + INTERNAL_SESSION_ID_BASE); } }
@Override public List<Long> getLostFiles() { Set<Long> lostFiles = new HashSet<>(); for (long blockId : mBlockMaster.getLostBlocks()) { // the file id is the container id of the block id long containerId = BlockId.getContainerId(blockId); long fileId = IdUtils.createFileId(containerId); lostFiles.add(fileId); } return new ArrayList<>(lostFiles); }
@Override public void validateInodeBlocks(boolean repair) throws UnavailableException { mBlockMaster.validateBlocks((blockId) -> { long fileId = IdUtils.fileIdFromBlockId(blockId); return mInodeTree.inodeIdExists(fileId); }, repair); }
/** * Creates a file system context with a subject. * * @param ctx the parent subject, set to null if not present */ private FileSystemContext(ClientContext ctx) { mClientContext = ctx; mExecutorService = Executors.newFixedThreadPool(1, ThreadFactoryUtils.build("metrics-master-heartbeat-%d", true)); mClosed = new AtomicBoolean(false); mAppId = ctx.getConf().isSet(PropertyKey.USER_APP_ID) ? ctx.getConf().get(PropertyKey.USER_APP_ID) : IdUtils.createFileSystemContextId(); LOG.info("Created filesystem context with id {}. This ID will be used for identifying info " + "from the client, such as metrics. It can be set manually through the {} property", mAppId, PropertyKey.Name.USER_APP_ID); }
/** * @return a random long which is guaranteed to be non negative (zero is allowed) */ public static long createMountId() { return getRandomNonNegativeLong(); }
/** * Tests if output of {@link IdUtils#createFileId(long)} is valid. */ @Test public void createFileId() throws Exception { long containerId = 1; long fileId = IdUtils.createFileId(containerId); Assert.assertNotEquals(-1, fileId); }
/** * Constructs a new {@link AtomicFileOutputStream}. * * @param path path being written to * @param ufs the calling {@link UnderFileSystem} * @param options create options for destination file */ public AtomicFileOutputStream(String path, AtomicFileOutputStreamCallback ufs, CreateOptions options) throws IOException { mOptions = options; mPermanentPath = path; mTemporaryPath = PathUtils.temporaryFileName(IdUtils.getRandomNonNegativeLong(), path); mTemporaryOutputStream = ufs.createDirect(mTemporaryPath, options); mUfs = ufs; }
/** * Creates a file ID from a block ID. * * @param blockId the block ID * @return the file ID */ public static long fileIdFromBlockId(long blockId) { return createFileId(BlockId.getContainerId(blockId)); }
@Override public long getWorkerId(WorkerNetAddress workerNetAddress) { MasterWorkerInfo existingWorker = mWorkers.getFirstByField(ADDRESS_INDEX, workerNetAddress); if (existingWorker != null) { // This worker address is already mapped to a worker id. long oldWorkerId = existingWorker.getId(); LOG.warn("The worker {} already exists as id {}.", workerNetAddress, oldWorkerId); return oldWorkerId; } existingWorker = findUnregisteredWorker(workerNetAddress); if (existingWorker != null) { return existingWorker.getId(); } // Generate a new worker id. long workerId = IdUtils.getRandomNonNegativeLong(); while (!mTempWorkers.add(new MasterWorkerInfo(workerId, workerNetAddress))) { workerId = IdUtils.getRandomNonNegativeLong(); } LOG.info("getWorkerId(): WorkerNetAddress: {} id: {}", workerNetAddress, workerId); return workerId; }
@Override public List<Long> getLostFiles() { Set<Long> lostFiles = new HashSet<>(); for (long blockId : mBlockMaster.getLostBlocks()) { // the file id is the container id of the block id long containerId = BlockId.getContainerId(blockId); long fileId = IdUtils.createFileId(containerId); lostFiles.add(fileId); } return new ArrayList<>(lostFiles); }
/** * Tests if output of {@link IdUtils#getRandomNonNegativeLong()} is non-negative. * Also tests for randomness property. */ @Test public void getRandomNonNegativeLong() throws Exception { long first = IdUtils.getRandomNonNegativeLong(); long second = IdUtils.getRandomNonNegativeLong(); Assert.assertTrue(first >= 0); Assert.assertTrue(second >= 0); Assert.assertTrue(first != second); }
long masterId = IdUtils.getRandomNonNegativeLong(); while (!mMasters.add(new MasterInfo(masterId, address))) { masterId = IdUtils.getRandomNonNegativeLong();