Search in sources :

Example 1 with FileBasedLock

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");
    }
}
Also used : HashMap(java.util.HashMap) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) PrestoException(io.prestosql.spi.PrestoException) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Lock(java.util.concurrent.locks.Lock) HashSet(java.util.HashSet)

Example 2 with FileBasedLock

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);
}
Also used : Path(java.nio.file.Path) HetuMetastore(io.prestosql.spi.metastore.HetuMetastore) Logger(io.airlift.log.Logger) HetuFileSystemClient(io.prestosql.spi.filesystem.HetuFileSystemClient) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IndexConstants(io.hetu.core.heuristicindex.util.IndexConstants) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) ArrayList(java.util.ArrayList) BTreeIndex(io.hetu.core.plugin.heuristicindex.index.btree.BTreeIndex) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) IndexClient(io.prestosql.spi.heuristicindex.IndexClient) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) LinkedList(java.util.LinkedList) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord) Path(java.nio.file.Path) ImmutableMap(com.google.common.collect.ImmutableMap) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Pair(io.prestosql.spi.heuristicindex.Pair) UncheckedIOException(java.io.UncheckedIOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) CreateIndexMetadata(io.prestosql.spi.connector.CreateIndexMetadata) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) Index(io.prestosql.spi.heuristicindex.Index) Collections(java.util.Collections) SecurePathWhiteList(io.hetu.core.common.util.SecurePathWhiteList) InputStream(java.io.InputStream) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Lock(java.util.concurrent.locks.Lock)

Example 3 with FileBasedLock

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());
}
Also used : Path(java.nio.file.Path) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 4 with FileBasedLock

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());
}
Also used : Path(java.nio.file.Path) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) OutputStream(java.io.OutputStream) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Example 5 with FileBasedLock

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();
}
Also used : Path(java.nio.file.Path) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest)

Aggregations

FileBasedLock (io.prestosql.spi.filesystem.FileBasedLock)17 Path (java.nio.file.Path)13 AfterTest (org.testng.annotations.AfterTest)10 Test (org.testng.annotations.Test)10 Lock (java.util.concurrent.locks.Lock)7 IOException (java.io.IOException)6 OutputStream (java.io.OutputStream)6 UncheckedIOException (java.io.UncheckedIOException)5 HashSet (java.util.HashSet)5 BeforeTest (org.testng.annotations.BeforeTest)5 Seed (io.prestosql.spi.seedstore.Seed)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 ImmutableList (com.google.common.collect.ImmutableList)3 Logger (io.airlift.log.Logger)3 SecurePathWhiteList (io.hetu.core.common.util.SecurePathWhiteList)3 HetuFileSystemClient (io.prestosql.spi.filesystem.HetuFileSystemClient)3 Paths (java.nio.file.Paths)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3