/** * @return a session ID */ public static long createSessionId() { return getRandomNonNegativeLong(); }
/** * @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); } }
/** * @return a random long which is guaranteed to be non negative (zero is allowed) */ public static long createMountId() { return getRandomNonNegativeLong(); }
/** * 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; }
@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; }
/** * 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();
nextId = IdUtils.getRandomNonNegativeLong(); final long newId = nextId; try {
@Test public void get() throws Exception { final long blockId = IdUtils.getRandomNonNegativeLong(); final AlluxioURI fileUri = new AlluxioURI("/mnt/file"); final String localFilePath = new AlluxioURI(mLocalUfsPath).join("file").getPath(); mLocalUfs.create(localFilePath); final List<String> ufsLocations = mLocalUfs.getFileLocations(localFilePath); for (String location : ufsLocations) { System.out.println(location); } Assert.assertNull(mUfsBlockLocationCache.get(blockId)); List<String> locations = mUfsBlockLocationCache.get(blockId, fileUri, 0); Assert.assertArrayEquals(ufsLocations.toArray(), locations.toArray()); locations = mUfsBlockLocationCache.get(blockId); Assert.assertArrayEquals(ufsLocations.toArray(), locations.toArray()); mUfsBlockLocationCache.invalidate(blockId); Assert.assertNull(mUfsBlockLocationCache.get(blockId)); } }
/** * Sets up a new {@link AsyncUfsAbsentPathCache} before a test runs. */ @Before public void before() throws Exception { mLocalUfsPath = Files.createTempDir().getAbsolutePath(); mUfsManager = new MasterUfsManager(); mMountTable = new MountTable(mUfsManager, new MountInfo(new AlluxioURI("/"), new AlluxioURI("/ufs"), 1, MountContext.defaults().getOptions().build())); mUfsAbsentPathCache = new AsyncUfsAbsentPathCache(mMountTable, THREADS); mMountId = IdUtils.getRandomNonNegativeLong(); MountPOptions options = MountContext.defaults().getOptions().build(); mUfsManager.addMount(mMountId, new AlluxioURI(mLocalUfsPath), UnderFileSystemConfiguration.defaults().setReadOnly(options.getReadOnly()) .setShared(options.getShared()) .createMountSpecificConf(Collections.<String, String>emptyMap())); mMountTable.add(NoopJournalContext.INSTANCE, new AlluxioURI("/mnt"), new AlluxioURI(mLocalUfsPath), mMountId, options); }
/** * Sets up a new {@link AsyncUfsAbsentPathCache} before a test runs. */ @Before public void before() throws Exception { mLocalUfsPath = Files.createTempDir().getAbsolutePath(); mLocalUfs = UnderFileSystem.Factory.create(mLocalUfsPath, ServerConfiguration.global()); mMountId = IdUtils.getRandomNonNegativeLong(); mUfsManager = new MasterUfsManager(); MountPOptions options = MountContext.defaults().getOptions().build(); mUfsManager.addMount(mMountId, new AlluxioURI(mLocalUfsPath), UnderFileSystemConfiguration.defaults().setReadOnly(options.getReadOnly()) .setShared(options.getShared()) .createMountSpecificConf(Collections.<String, String>emptyMap())); mMountTable = new MountTable(mUfsManager, new MountInfo(new AlluxioURI("/"), new AlluxioURI("/ufs"), 1, MountContext.defaults().getOptions().build())); mMountTable.add(NoopJournalContext.INSTANCE, new AlluxioURI("/mnt"), new AlluxioURI(mLocalUfsPath), mMountId, options); mUfsBlockLocationCache = new LazyUfsBlockLocationCache(mMountTable); }
@Test public void removeMountPoint() throws Exception { String ufsBase = "/a/b"; String alluxioBase = "/mnt" + ufsBase; // Create ufs directories Assert.assertTrue((new File(mLocalUfsPath + ufsBase)).mkdirs()); // 'base + /c' will be the first absent path addAbsent(new AlluxioURI(alluxioBase + "/c/d")); checkAbsentPaths(new AlluxioURI(alluxioBase + "/c")); // Unmount Assert.assertTrue(mMountTable.delete(NoopJournalContext.INSTANCE, new AlluxioURI("/mnt"))); // Re-mount the same ufs long newMountId = IdUtils.getRandomNonNegativeLong(); MountPOptions options = MountContext.defaults().getOptions().build(); mUfsManager.addMount(newMountId, new AlluxioURI(mLocalUfsPath), UnderFileSystemConfiguration.defaults().setReadOnly(options.getReadOnly()) .setShared(options.getShared()) .createMountSpecificConf(Collections.<String, String>emptyMap())); mMountTable.add(NoopJournalContext.INSTANCE, new AlluxioURI("/mnt"), new AlluxioURI(mLocalUfsPath), newMountId, options); // The cache should not contain any paths now. Assert.assertFalse(mUfsAbsentPathCache.isAbsent(new AlluxioURI("/mnt/a/b/c/d"))); Assert.assertFalse(mUfsAbsentPathCache.isAbsent(new AlluxioURI("/mnt/a/b/c"))); Assert.assertFalse(mUfsAbsentPathCache.isAbsent(new AlluxioURI("/mnt/a/b"))); Assert.assertFalse(mUfsAbsentPathCache.isAbsent(new AlluxioURI("/mnt/a"))); Assert.assertFalse(mUfsAbsentPathCache.isAbsent(new AlluxioURI("/mnt/"))); }
/** * @return a session ID */ public static long createSessionId() { return getRandomNonNegativeLong(); }
/** * @return a random long which is guaranteed to be non negative (zero is allowed) */ public static long createMountId() { return getRandomNonNegativeLong(); }
/** * 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; }
@Override public long getWorkerId(WorkerNetAddress workerNetAddress) { // TODO(gpang): Clone WorkerNetAddress in case thrift re-uses the object. Does thrift re-use it? 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; }
long masterId = IdUtils.getRandomNonNegativeLong(); while (!mMasters.add(new MasterInfo(masterId, address))) { masterId = IdUtils.getRandomNonNegativeLong();