Search in sources :

Example 16 with IndexRecord

use of io.prestosql.spi.heuristicindex.IndexRecord in project hetu-core by openlookeng.

the class SplitFiltering method preloadCache.

public static void preloadCache(IndexClient indexClient, List<String> preloadIndexNames) throws IOException {
    List<IndexRecord> indexToPreload = new ArrayList<>(preloadIndexNames.size());
    if (preloadIndexNames.contains(PRELOAD_ALL_KEY)) {
        indexToPreload = indexClient.getAllIndexRecords();
        LOG.info("Preloading all indices: " + indexToPreload.stream().map(r -> r.name).collect(Collectors.joining(",")));
    } else {
        for (String indexName : preloadIndexNames) {
            IndexRecord record = indexClient.lookUpIndexRecord(indexName);
            if (record != null) {
                indexToPreload.add(indexClient.lookUpIndexRecord(indexName));
            } else {
                LOG.info("Index " + indexName + " is not found. Preloading skipped.");
            }
        }
    }
    for (IndexRecord record : indexToPreload) {
        LOG.info("Preloading index %s to cache...", record.name);
        Duration timeElapsed = indexCache.loadIndexToCache(record);
        LOG.info("Index %s was loaded to cache. (Time elapsed: %s)", record.name, timeElapsed.toString());
    }
}
Also used : SerializationUtils.deserializeStripeSymbol(io.prestosql.spi.heuristicindex.SerializationUtils.deserializeStripeSymbol) Arrays(java.util.Arrays) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) Duration(io.airlift.units.Duration) CallExpression(io.prestosql.spi.relation.CallExpression) FilterNode(io.prestosql.spi.plan.FilterNode) HetuConstant(io.prestosql.spi.HetuConstant) OperatorType(io.prestosql.spi.function.OperatorType) IndexClient(io.prestosql.spi.heuristicindex.IndexClient) Map(java.util.Map) URI(java.net.URI) Path(java.nio.file.Path) IndexFilter(io.prestosql.spi.heuristicindex.IndexFilter) TableScanNode(io.prestosql.spi.plan.TableScanNode) Set(java.util.Set) PropertyService(io.prestosql.spi.service.PropertyService) PlanNode(io.prestosql.spi.plan.PlanNode) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Pair(io.prestosql.spi.heuristicindex.Pair) CacheLoader(com.google.common.cache.CacheLoader) FunctionHandle(io.prestosql.spi.function.FunctionHandle) List(java.util.List) IS_DISTINCT_FROM(io.prestosql.spi.function.OperatorType.IS_DISTINCT_FROM) Optional(java.util.Optional) Queue(java.util.Queue) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) SerializationUtils(io.prestosql.spi.heuristicindex.SerializationUtils) Logger(io.airlift.log.Logger) HashMap(java.util.HashMap) Split(io.prestosql.metadata.Split) TableHandle(io.prestosql.spi.metadata.TableHandle) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) SplitSource(io.prestosql.split.SplitSource) Signature(io.prestosql.spi.function.Signature) LinkedList(java.util.LinkedList) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord) SpecialForm(io.prestosql.spi.relation.SpecialForm) Symbol(io.prestosql.spi.plan.Symbol) PlanFragment(io.prestosql.sql.planner.PlanFragment) Iterator(java.util.Iterator) IOException(java.io.IOException) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) AtomicLong(java.util.concurrent.atomic.AtomicLong) RangeUtil(io.prestosql.utils.RangeUtil) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Paths(java.nio.file.Paths) RowExpression(io.prestosql.spi.relation.RowExpression) IndexCacheKey(io.prestosql.spi.heuristicindex.IndexCacheKey) SqlStageExecution(io.prestosql.execution.SqlStageExecution) IndexLookUpException(io.prestosql.spi.heuristicindex.IndexLookUpException) Comparator(java.util.Comparator) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Duration(io.airlift.units.Duration) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord)

Example 17 with IndexRecord

use of io.prestosql.spi.heuristicindex.IndexRecord in project hetu-core by openlookeng.

the class StatementAnalyzer method validateUpdateIndex.

private void validateUpdateIndex(Table table, Optional<Scope> scope) {
    UpdateIndex updateIndex = (UpdateIndex) analysis.getOriginalStatement();
    IndexRecord indexRecord;
    try {
        indexRecord = heuristicIndexerManager.getIndexClient().lookUpIndexRecord(updateIndex.getIndexName().toString());
    } catch (IOException e) {
        throw new UncheckedIOException("Error reading index records, ", e);
    }
    QualifiedObjectName tableFullName = QualifiedObjectName.valueOf(indexRecord.qualifiedTable);
    accessControl.checkCanCreateIndex(session.getRequiredTransactionId(), session.getIdentity(), tableFullName);
    String tableName = tableFullName.toString();
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableFullName);
    if (!tableHandle.isPresent()) {
        throw new SemanticException(MISSING_ATTRIBUTE, table, "Unable to update index. " + "Index table '%s' may have been dropped from outside OLK. Index should also be dropped.", tableFullName);
    }
    List<Pair<String, Type>> indexColumns = new LinkedList<>();
    for (String i : indexRecord.columns) {
        indexColumns.add(new Pair<>(i, UNKNOWN));
    }
    try {
        // Use this place holder to check the existence of index and lock the place
        Properties properties = new Properties();
        properties.setProperty(INPROGRESS_PROPERTY_KEY, "TRUE");
        CreateIndexMetadata placeHolder = new CreateIndexMetadata(updateIndex.getIndexName().toString(), tableName, indexRecord.indexType, 0L, indexColumns, indexRecord.partitions, properties, session.getUser(), UNDEFINED);
        synchronized (StatementAnalyzer.class) {
            IndexClient.RecordStatus recordStatus = heuristicIndexerManager.getIndexClient().lookUpIndexRecord(placeHolder);
            switch(recordStatus) {
                case IN_PROGRESS_SAME_NAME:
                    throw new SemanticException(INDEX_ALREADY_EXISTS, updateIndex, "Index '%s' is being created by another user. Check running queries for details. If there is no running query for this index, " + "the index may be in an unexpected error state and should be dropped using 'DROP INDEX %s'", updateIndex.getIndexName().toString(), updateIndex.getIndexName().toString());
                case IN_PROGRESS_SAME_CONTENT:
                    throw new SemanticException(INDEX_ALREADY_EXISTS, updateIndex, "Index with same (table,column,indexType) is being created by another user. Check running queries for details. " + "If there is no running query for this index, the index may be in an unexpected error state and should be dropped using 'DROP INDEX'");
                case IN_PROGRESS_SAME_INDEX_PART_CONFLICT:
                    if (indexRecord.partitions.isEmpty()) {
                        throw new SemanticException(INDEX_ALREADY_EXISTS, updateIndex, "Index with same (table,column,indexType) is being created by another user. Check running queries for details. " + "If there is no running query for this index, the index may be in an unexpected error state and should be dropped using 'DROP INDEX %s'", updateIndex.getIndexName().toString());
                    }
                // allow different queries to run with explicitly same partitions
                case NOT_FOUND:
                    throw new SemanticException(MISSING_INDEX, updateIndex, "Index with name '%s' does not exist", updateIndex.getIndexName().toString());
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : CreateIndexMetadata(io.prestosql.spi.connector.CreateIndexMetadata) IndexClient(io.prestosql.spi.heuristicindex.IndexClient) UncheckedIOException(java.io.UncheckedIOException) UpdateIndex(io.prestosql.sql.tree.UpdateIndex) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) NodeUtils.mapFromProperties(io.prestosql.sql.NodeUtils.mapFromProperties) SystemSessionProperties(io.prestosql.SystemSessionProperties) Properties(java.util.Properties) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) LinkedList(java.util.LinkedList) TableHandle(io.prestosql.spi.metadata.TableHandle) Pair(io.prestosql.spi.heuristicindex.Pair)

Example 18 with IndexRecord

use of io.prestosql.spi.heuristicindex.IndexRecord in project hetu-core by openlookeng.

the class HeuristicIndexClient method getLastModifiedTimes.

@Override
public Map<String, String> getLastModifiedTimes(String indexName) throws IOException {
    IndexRecord indexRecord = lookUpIndexRecord(indexName);
    CreateIndexMetadata.Level createLevel = indexRecord.getLevel();
    Path pathToIndex = Paths.get(root.toString(), indexRecord.qualifiedTable, indexRecord.columns[0], indexRecord.indexType);
    // check required for security scan since we are constructing a path using input
    checkArgument(!pathToIndex.toString().contains("../"), pathToIndex + " must be absolute and under one of the following whitelisted directories:  " + SecurePathWhiteList.getSecurePathWhiteList().toString());
    checkArgument(SecurePathWhiteList.isSecurePath(pathToIndex), pathToIndex + " must be under one of the following whitelisted directories: " + SecurePathWhiteList.getSecurePathWhiteList().toString());
    List<Path> paths = fs.walk(pathToIndex).filter(p -> !fs.isDirectory(p)).collect(Collectors.toList());
    switch(createLevel) {
        case STRIPE:
            return paths.stream().collect(Collectors.toMap(path -> "/" + path.subpath(pathToIndex.getNameCount(), path.getNameCount() - 1), path -> {
                String filename = path.getFileName().toString();
                return filename.replaceAll("\\D", "");
            }));
        case PARTITION:
            return paths.stream().collect(Collectors.toMap(path -> path.subpath(pathToIndex.getNameCount(), path.getNameCount() - 1).toString(), path -> {
                String filename = path.getFileName().toString();
                return filename.replaceAll("\\D", "");
            }));
        case TABLE:
            return paths.stream().filter(path -> path.getNameCount() - 1 == pathToIndex.getNameCount()).collect(Collectors.toMap(path -> indexRecord.qualifiedTable, path -> {
                String filename = path.getFileName().toString();
                return filename.replaceAll("\\D", "");
            }));
        default:
            return Collections.emptyMap();
    }
}
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) CreateIndexMetadata(io.prestosql.spi.connector.CreateIndexMetadata) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord)

Example 19 with IndexRecord

use of io.prestosql.spi.heuristicindex.IndexRecord in project hetu-core by openlookeng.

the class IndexRecordManager method lookUpIndexRecord.

/**
 * Look up index record according to what it is for (triplet of [table, column, type]).
 */
public IndexRecord lookUpIndexRecord(String table, String[] columns, String indexType) {
    String[] tableQualified = table.split("\\.");
    if (tableQualified.length != 3) {
        throw new IllegalArgumentException(String.format("Illegal table name: %s", table));
    }
    Optional<TableEntity> tableEntity = metastore.getTable(tableQualified[0], tableQualified[1], tableQualified[2]);
    if (tableEntity.isPresent()) {
        for (Map.Entry<String, String> parameter : tableEntity.get().getParameters().entrySet()) {
            IndexRecord read = new IndexRecord(tableEntity.get(), parameter);
            if (Arrays.equals(read.columns, columns) && read.indexType.equals(indexType)) {
                return read;
            }
        }
    }
    return null;
}
Also used : TableEntity(io.prestosql.spi.metastore.model.TableEntity) Map(java.util.Map) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord)

Example 20 with IndexRecord

use of io.prestosql.spi.heuristicindex.IndexRecord in project hetu-core by openlookeng.

the class TestIndexRecordManager method testIndexRecordAddLookUpHelper.

private void testIndexRecordAddLookUpHelper(String name, String user, String table, String[] columns, String indexType, List<String> indexProperties, List<String> partitions) throws IOException {
    try (TempFolder folder = new TempFolder()) {
        folder.create();
        HetuMetastore testMetaStore = new HetuFsMetastore(new HetuFsMetastoreConfig().setHetuFileSystemMetastorePath(folder.getRoot().getPath()), FILE_SYSTEM_CLIENT);
        IndexRecordManager indexRecordManager = new IndexRecordManager(testMetaStore);
        IndexRecord expected = new IndexRecord(name, user, table, columns, indexType, 0L, indexProperties, partitions);
        indexRecordManager.addIndexRecord(name, user, table, columns, indexType, 0L, indexProperties, partitions);
        IndexRecord actual1 = indexRecordManager.lookUpIndexRecord(name);
        assertNotNull(actual1);
        assertEquals(actual1, expected);
        IndexRecord actual2 = indexRecordManager.lookUpIndexRecord(table, columns, indexType);
        assertNotNull(actual2);
        assertEquals(actual2, expected);
    }
}
Also used : HetuFsMetastoreConfig(io.hetu.core.metastore.hetufilesystem.HetuFsMetastoreConfig) TempFolder(io.hetu.core.common.filesystem.TempFolder) HetuFsMetastore(io.hetu.core.metastore.hetufilesystem.HetuFsMetastore) IndexRecord(io.prestosql.spi.heuristicindex.IndexRecord) HetuMetastore(io.prestosql.spi.metastore.HetuMetastore) JdbcHetuMetastore(io.hetu.core.metastore.jdbc.JdbcHetuMetastore)

Aggregations

IndexRecord (io.prestosql.spi.heuristicindex.IndexRecord)20 IOException (java.io.IOException)10 CreateIndexMetadata (io.prestosql.spi.connector.CreateIndexMetadata)8 LinkedList (java.util.LinkedList)8 Map (java.util.Map)8 IndexMetadata (io.prestosql.spi.heuristicindex.IndexMetadata)7 ArrayList (java.util.ArrayList)7 IndexClient (io.prestosql.spi.heuristicindex.IndexClient)6 Pair (io.prestosql.spi.heuristicindex.Pair)6 UncheckedIOException (java.io.UncheckedIOException)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Logger (io.airlift.log.Logger)5 Path (java.nio.file.Path)5 ImmutableList (com.google.common.collect.ImmutableList)4 IndexCacheKey (io.prestosql.spi.heuristicindex.IndexCacheKey)4 HetuMetastore (io.prestosql.spi.metastore.HetuMetastore)4 Collections (java.util.Collections)4 Collectors (java.util.stream.Collectors)4 ImmutableMap (com.google.common.collect.ImmutableMap)3