Search in sources :

Example 11 with Seed

use of io.prestosql.spi.seedstore.Seed 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)

Example 12 with Seed

use of io.prestosql.spi.seedstore.Seed in project hetu-core by openlookeng.

the class FileBasedSeedStoreOnYarn method get.

@Override
public Set<Seed> get() throws IOException {
    LOG.debug("FileBasedOnYarnSeedStore::get() invoked.");
    try {
        Set<Seed> outputs = new HashSet<>();
        if (fs.exists(seedFilePath)) {
            String json = loadFromFile(seedFilePath);
            outputs.addAll(LIST_FILE_BASED_SEED_CODEC.fromJson(json));
        }
        return outputs;
    } catch (UncheckedIOException e) {
        throw new IOException(e);
    }
}
Also used : Seed(io.prestosql.spi.seedstore.Seed) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet)

Example 13 with Seed

use of io.prestosql.spi.seedstore.Seed 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();
    }
}
Also used : FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Seed(io.prestosql.spi.seedstore.Seed) 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)

Example 14 with Seed

use of io.prestosql.spi.seedstore.Seed 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();
    }
}
Also used : FileBasedLock(io.prestosql.spi.filesystem.FileBasedLock) Seed(io.prestosql.spi.seedstore.Seed) 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)

Example 15 with Seed

use of io.prestosql.spi.seedstore.Seed in project hetu-core by openlookeng.

the class SeedStoreManager method clearExpiredSeeds.

/**
 * Clear expired seed in the seed store
 *
 * @throws IOException
 */
public void clearExpiredSeeds(SeedStoreSubType subType) throws IOException {
    SeedStore seedStore = getSeedStore(subType);
    if (seedStore == null) {
        throw new PrestoException(SEED_STORE_FAILURE, "Seed store is null");
    }
    try {
        Collection<Seed> expiredSeeds = seedStore.get().stream().filter(s -> (System.currentTimeMillis() - s.getTimestamp() > seedHeartBeatTimeout)).collect(Collectors.toList());
        if (expiredSeeds.size() > 0) {
            LOG.info("Expired seeds=%s will be cleared", expiredSeeds);
            seedStore.remove(expiredSeeds);
        }
    } catch (RuntimeException e) {
        LOG.warn("clearExpiredSeed failed with following message: %s", e.getMessage());
    }
}
Also used : Logger(io.airlift.log.Logger) HetuFileSystemClient(io.prestosql.spi.filesystem.HetuFileSystemClient) SEED_STORE_FAILURE(io.prestosql.spi.StandardErrorCode.SEED_STORE_FAILURE) Inject(com.google.inject.Inject) SeedStoreSubType(io.prestosql.spi.seedstore.SeedStoreSubType) HashMap(java.util.HashMap) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Collectors.toCollection(java.util.stream.Collectors.toCollection) ConfigurationLoader.loadPropertiesFrom(io.airlift.configuration.ConfigurationLoader.loadPropertiesFrom) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) InvalidParameterException(java.security.InvalidParameterException) Locale(java.util.Locale) Threads.daemonThreadsNamed(io.airlift.concurrent.Threads.daemonThreadsNamed) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PrestoException(io.prestosql.spi.PrestoException) SeedStore(io.prestosql.spi.seedstore.SeedStore) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Seed(io.prestosql.spi.seedstore.Seed) IOException(java.io.IOException) ThreadContextClassLoader(io.prestosql.spi.classloader.ThreadContextClassLoader) Collectors(java.util.stream.Collectors) File(java.io.File) String.format(java.lang.String.format) SeedStoreFactory(io.prestosql.spi.seedstore.SeedStoreFactory) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Paths(java.nio.file.Paths) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) StateStoreConstants(io.prestosql.statestore.StateStoreConstants) Seed(io.prestosql.spi.seedstore.Seed) SeedStore(io.prestosql.spi.seedstore.SeedStore) PrestoException(io.prestosql.spi.PrestoException)

Aggregations

Seed (io.prestosql.spi.seedstore.Seed)29 SeedStore (io.prestosql.spi.seedstore.SeedStore)21 HashSet (java.util.HashSet)20 IOException (java.io.IOException)15 Map (java.util.Map)12 PrestoException (io.prestosql.spi.PrestoException)11 Collection (java.util.Collection)11 HashMap (java.util.HashMap)10 HetuFileSystemClient (io.prestosql.spi.filesystem.HetuFileSystemClient)8 Set (java.util.Set)8 Test (org.testng.annotations.Test)8 Logger (io.airlift.log.Logger)7 SeedStoreSubType (io.prestosql.spi.seedstore.SeedStoreSubType)7 File (java.io.File)7 Collectors (java.util.stream.Collectors)7 BeforeTest (org.testng.annotations.BeforeTest)7 FileBasedLock (io.prestosql.spi.filesystem.FileBasedLock)6 SeedStoreFactory (io.prestosql.spi.seedstore.SeedStoreFactory)6 Paths (java.nio.file.Paths)6 Lock (java.util.concurrent.locks.Lock)6