use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnHdfs method testTryLock.
@Test
public void testTryLock() throws InterruptedException, IOException {
Path testDir = Paths.get(testLockRoot + "/testTryLock");
FileBasedLock lock = new FileBasedLock(fs, testDir);
if (lock.tryLock()) {
assertTrue(lock.isLocked());
lock.unlock();
}
assertFalse(lock.isLocked());
lock = new FileBasedLock(fs, testDir);
if (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
assertTrue(lock.isLocked());
lock.unlock();
}
assertFalse(lock.isLocked());
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnLocal method testAcquiredLock.
@Test
public void testAcquiredLock() throws IOException, InterruptedException {
Path testDir = Paths.get(testLockRoot + "/testAcquiredLock");
FileBasedLock lock = new FileBasedLock(fs, testDir, 1000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
OutputStream os = fs.newOutputStream(testDir.resolve(".lockInfo"));
os.write("test".getBytes(StandardCharsets.UTF_8));
os.close();
assertFalse(lock.acquiredLock());
Thread.sleep(1200L);
// Expired, can be acquired
assertTrue(lock.acquiredLock());
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnLocal method testIsLocked.
@Test
public void testIsLocked() throws IOException, InterruptedException {
Path testDir = Paths.get(testLockRoot + "/testIsLocked");
FileBasedLock lock = new FileBasedLock(fs, testDir, 1000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
OutputStream os = fs.newOutputStream(testDir.resolve(".lockFile"));
os.write("test".getBytes(StandardCharsets.UTF_8));
os.close();
assertTrue(lock.isLocked());
Thread.sleep(1200L);
// Expired, not locked
assertFalse(lock.isLocked());
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnLocal method testTwoLocks.
@Test
public void testTwoLocks() throws InterruptedException, IOException {
Path testDir = Paths.get(testLockRoot + "/testRaceCondition");
FileBasedLock lock1 = new FileBasedLock(fs, testDir, 10000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
FileBasedLock lock2 = new FileBasedLock(fs, testDir, 10000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
lock1.lock();
assertTrue(lock1.isLocked());
assertTrue(lock1.acquiredLock());
Thread.sleep(500L);
assertTrue(lock2.isLocked());
assertFalse(lock2.acquiredLock());
lock1.unlock();
assertFalse(lock2.isLocked());
lock2.lock();
assertTrue(lock2.isLocked());
assertTrue(lock2.acquiredLock());
lock2.unlock();
assertFalse(lock1.isLocked());
assertFalse(lock2.isLocked());
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class FileBasedSeedStoreOnYarn method add.
@Override
public Set<Seed> add(Collection<Seed> seeds) throws IOException {
LOG.debug("FileBasedOnYarnSeedStore::add() invoked.");
Path lockPath = seedFileFolder.resolve(name);
checkArgument(!lockPath.toString().contains("../"), "Lock path must be absolute and at user workspace " + SecurePathWhiteList.getSecurePathWhiteList().toString());
checkArgument(SecurePathWhiteList.isSecurePath(lockPath.toString()), "Lock path must be at user workspace " + SecurePathWhiteList.getSecurePathWhiteList().toString());
Lock lock = new FileBasedLock(fs, lockPath);
try {
lock.lock();
Set<FileBasedSeedOnYarn> latestSeeds = new HashSet<>();
// add all new seeds
latestSeeds.addAll(seeds.stream().filter(s -> (s instanceof FileBasedSeedOnYarn)).map(s -> (FileBasedSeedOnYarn) s).collect(Collectors.toList()));
// load existing seeds and filter out repeated seed compared to new seeds
if (fs.exists(seedFilePath)) {
String json = loadFromFile(seedFilePath);
List<FileBasedSeedOnYarn> existingSeeds = LIST_FILE_BASED_SEED_CODEC.fromJson(json);
latestSeeds.addAll(existingSeeds.stream().filter(s -> !latestSeeds.contains(s)).collect(Collectors.toList()));
}
String output = LIST_FILE_BASED_SEED_CODEC.toJson(ImmutableList.copyOf(latestSeeds));
writeToFile(seedFilePath, output, true);
return new HashSet<>(latestSeeds);
} catch (UncheckedIOException e) {
throw new IOException(e);
} finally {
lock.unlock();
}
}
Aggregations