use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class FileBasedSeedStore method add.
@Override
public Set<Seed> add(Collection<Seed> seeds) throws IOException {
Lock lock = new FileBasedLock(fs, seedDir.resolve(name));
try {
lock.lock();
Set<String> writtens = new HashSet<>(0);
Set<Seed> outputs = getSeeds();
// overwrite all seeds
outputs.removeAll(seeds);
outputs.addAll(seeds);
for (Seed seed : outputs) {
writtens.add(seed.serialize());
}
writeToFile(seedFilePath, String.join(COMMA, writtens), true);
return outputs;
} catch (UncheckedIOException e) {
throw new IOException(e);
} finally {
lock.unlock();
}
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class FileBasedSeedStore method remove.
@Override
public Set<Seed> remove(Collection<Seed> seeds) throws IOException {
Lock lock = new FileBasedLock(fs, seedDir.resolve(name));
try {
lock.lock();
Set<String> writtens = new HashSet<>(0);
Set<Seed> outputs = getSeeds();
outputs.removeAll(seeds);
for (Seed seed : outputs) {
writtens.add(seed.serialize());
}
writeToFile(seedFilePath, String.join(COMMA, writtens), true);
return outputs;
} catch (UncheckedIOException e) {
throw new IOException(e);
} finally {
lock.unlock();
}
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnHdfs 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 testReuse.
@Test(expectedExceptions = IllegalStateException.class)
public void testReuse() throws IOException {
Path testDir = Paths.get(testLockRoot + "/testReuse");
FileBasedLock lock = new FileBasedLock(fs, testDir);
lock.lock();
lock.unlock();
// Should throw an exception
lock.lock();
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnLocal method testTryLock.
@Test
public void testTryLock() throws InterruptedException, IOException {
Path testDir = Paths.get(testLockRoot + "/testTryLock");
FileBasedLock lock = new FileBasedLock(fs, testDir, 1000, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
if (lock.tryLock()) {
assertTrue(lock.isLocked());
lock.unlock();
}
assertFalse(lock.isLocked());
lock = new FileBasedLock(fs, testDir, 1000, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
if (lock.tryLock(1500, TimeUnit.MILLISECONDS)) {
assertTrue(lock.isLocked());
lock.unlock();
}
assertFalse(lock.isLocked());
}
Aggregations