use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class EmbeddedStateStoreLauncher method launchStateStore.
@Override
public void launchStateStore() throws Exception {
if (bootstrapper == null) {
LOG.info("No available bootstrapper, skip launching state store");
return;
}
if (STATE_STORE_LAUNCHER_CONFIGURATION.exists()) {
Map<String, String> properties = new HashMap<>(loadPropertiesFrom(STATE_STORE_LAUNCHER_CONFIGURATION.getPath()));
Set<String> staticSeeds = getStateStoreStaticSeeds(properties);
if (staticSeeds.size() > 0) {
launchStateStore(staticSeeds, properties);
} else if (seedStoreManager.getSeedStore(SeedStoreSubType.HAZELCAST) != null) {
// Set seed store name
seedStoreManager.getSeedStore(SeedStoreSubType.HAZELCAST).setName(properties.get(STATE_STORE_CLUSTER_PROPERTY_NAME));
// Clear expired seeds
seedStoreManager.clearExpiredSeeds(SeedStoreSubType.HAZELCAST);
// Use lock to control synchronization of state store launch among all coordinators
Lock launcherLock = new FileBasedLock(seedStoreManager.getFileSystemClient(), Paths.get(LAUNCHER_LOCK_FILE_PATH));
try {
launcherLock.lock();
launchStateStoreFromSeedStore(properties);
} finally {
launcherLock.unlock();
}
} else {
launchStateStore(new HashSet<>(), properties);
}
if (stateStore == null) {
throw new PrestoException(STATE_STORE_FAILURE, "Unable to launch state store, please check your configuration");
}
} else {
LOG.info("No configuration file found, skip launching state store");
}
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class HeuristicIndexClient method deleteIndex.
@Override
public void deleteIndex(String indexName, List<String> partitionsToDelete) throws IOException {
IndexRecord indexRecord = indexRecordManager.lookUpIndexRecord(indexName);
// dir structure example: root/catalog.schema.table/column1,column2/BLOOM
Path tablePath = root.resolve(indexRecord.qualifiedTable);
Path columnPath = tablePath.resolve(String.join(",", indexRecord.columns));
Path indexLevelPath = columnPath.resolve(indexRecord.indexType);
if (!fs.exists(indexLevelPath)) {
indexRecordManager.deleteIndexRecord(indexName, partitionsToDelete);
return;
}
Lock lock = new FileBasedLock(fs, indexLevelPath.getParent());
try {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
lock.unlock();
try {
fs.close();
} catch (IOException e) {
throw new UncheckedIOException("Error closing FileSystem Client: " + fs.getClass().getName(), e);
}
}));
lock.lock();
if (partitionsToDelete.isEmpty()) {
fs.deleteRecursively(indexLevelPath);
} else {
List<Path> toDeletePartitions = fs.walk(indexLevelPath).filter(fs::isDirectory).filter(path -> partitionsToDelete.contains(path.getFileName().toString())).collect(Collectors.toList());
for (Path path : toDeletePartitions) {
fs.deleteRecursively(path);
}
// if all partitions have been deleted, remove index path
if (fs.walk(indexLevelPath).allMatch(fs::isDirectory)) {
fs.deleteRecursively(indexLevelPath);
}
}
try {
// clean empty directories
if (fs.list(columnPath).allMatch(FileBasedLock::isLockUtilFile)) {
fs.deleteRecursively(columnPath);
}
if (fs.list(tablePath).allMatch(FileBasedLock::isLockUtilFile)) {
fs.deleteRecursively(tablePath);
}
} catch (Exception e) {
LOG.debug("failed to clean empty index directory", e);
}
} finally {
lock.unlock();
}
indexRecordManager.deleteIndexRecord(indexName, partitionsToDelete);
}
use of io.prestosql.spi.filesystem.FileBasedLock in project hetu-core by openlookeng.
the class TestFileBasedLockOnHdfs method testTwoLocks.
@Test
public void testTwoLocks() throws IOException {
Path testDir = Paths.get(testLockRoot + "/testRaceCondition");
FileBasedLock lock1 = new FileBasedLock(fs, testDir, 2000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
FileBasedLock lock2 = new FileBasedLock(fs, testDir, 2000L, FileBasedLock.DEFAULT_RETRY_INTERVAL, FileBasedLock.DEFAULT_REFRESH_RATE);
lock1.lock();
assertTrue(lock1.isLocked());
assertTrue(lock1.acquiredLock());
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 TestFileBasedLockOnHdfs 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 TestFileBasedLockOnHdfs 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();
}
Aggregations