Search in sources :

Example 6 with FileBasedLock

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

Example 7 with FileBasedLock

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

Example 8 with FileBasedLock

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

Example 9 with FileBasedLock

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

Example 10 with FileBasedLock

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();
    }
}
Also used : Path(java.nio.file.Path) Logger(io.airlift.log.Logger) HetuFileSystemClient(io.prestosql.spi.filesystem.HetuFileSystemClient) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) SeedStore(io.prestosql.spi.seedstore.SeedStore) Collection(java.util.Collection) Seed(io.prestosql.spi.seedstore.Seed) Set(java.util.Set) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Paths(java.nio.file.Paths) JsonCodec.listJsonCodec(io.airlift.json.JsonCodec.listJsonCodec) BufferedReader(java.io.BufferedReader) CREATE_NEW(java.nio.file.StandardOpenOption.CREATE_NEW) SecurePathWhiteList(io.hetu.core.common.util.SecurePathWhiteList) JsonCodec(io.airlift.json.JsonCodec) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Lock(java.util.concurrent.locks.Lock) HashSet(java.util.HashSet)

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