Search in sources :

Example 1 with PartitionUnknownException

use of io.crate.exceptions.PartitionUnknownException in project crate by crate.

the class CreateSnapshotAnalyzer method analyze.

public CreateSnapshotAnalyzedStatement analyze(CreateSnapshot node, Analysis analysis) {
    Optional<QualifiedName> repositoryName = node.name().getPrefix();
    // validate repository
    Preconditions.checkArgument(repositoryName.isPresent(), "Snapshot must be specified by \"<repository_name>\".\"<snapshot_name>\"");
    Preconditions.checkArgument(repositoryName.get().getParts().size() == 1, String.format(Locale.ENGLISH, "Invalid repository name '%s'", repositoryName.get()));
    repositoryService.failIfRepositoryDoesNotExist(repositoryName.get().toString());
    // snapshot existence in repo is validated upon execution
    String snapshotName = node.name().getSuffix();
    SnapshotId snapshotId = new SnapshotId(repositoryName.get().toString(), snapshotName);
    // validate and extract settings
    Settings settings = GenericPropertiesConverter.settingsFromProperties(node.properties(), analysis.parameterContext(), SETTINGS).build();
    boolean ignoreUnavailable = settings.getAsBoolean(IGNORE_UNAVAILABLE.name(), IGNORE_UNAVAILABLE.defaultValue());
    // iterate tables
    if (node.tableList().isPresent()) {
        List<Table> tableList = node.tableList().get();
        Set<String> snapshotIndices = new HashSet<>(tableList.size());
        for (Table table : tableList) {
            TableInfo tableInfo;
            try {
                tableInfo = schemas.getTableInfo(TableIdent.of(table, analysis.sessionContext().defaultSchema()));
            } catch (ResourceUnknownException e) {
                if (ignoreUnavailable) {
                    LOGGER.info("ignoring: {}", e.getMessage());
                    continue;
                } else {
                    throw e;
                }
            }
            if (!(tableInfo instanceof DocTableInfo)) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot create snapshot of tables in schema '%s'", tableInfo.ident().schema()));
            }
            Operation.blockedRaiseException(tableInfo, Operation.READ);
            DocTableInfo docTableInfo = (DocTableInfo) tableInfo;
            if (table.partitionProperties().isEmpty()) {
                snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
            } else {
                PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, table.partitionProperties(), analysis.parameterContext().parameters());
                if (!docTableInfo.partitions().contains(partitionName)) {
                    if (!ignoreUnavailable) {
                        throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
                    } else {
                        LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.tableIdent(), partitionName.ident());
                    }
                } else {
                    snapshotIndices.add(partitionName.asIndexName());
                }
            }
        }
        /**
             * For now, we always (in case there are indices to restore) include the globalMetaData,
             * not only if one of the tables in the table list is partitioned.
             * Previously we only included it in snapshots of full partitioned tables.
             * However, to make restoring of shapshots of single partitions work
             * we also need to include the global metadata (index templates).
             */
        return CreateSnapshotAnalyzedStatement.forTables(snapshotId, settings, ImmutableList.copyOf(snapshotIndices), !snapshotIndices.isEmpty());
    } else {
        for (SchemaInfo schemaInfo : schemas) {
            for (TableInfo tableInfo : schemaInfo) {
                // only check for user generated tables
                if (tableInfo instanceof DocTableInfo) {
                    Operation.blockedRaiseException(tableInfo, Operation.READ);
                }
            }
        }
        return CreateSnapshotAnalyzedStatement.all(snapshotId, settings);
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Table(io.crate.sql.tree.Table) QualifiedName(io.crate.sql.tree.QualifiedName) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionName(io.crate.metadata.PartitionName) SnapshotId(org.elasticsearch.cluster.metadata.SnapshotId) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SchemaInfo(io.crate.metadata.table.SchemaInfo)

Example 2 with PartitionUnknownException

use of io.crate.exceptions.PartitionUnknownException in project crate by crate.

the class TableAnalyzer method filteredIndices.

static Collection<String> filteredIndices(ParameterContext parameterContext, List<Assignment> partitionProperties, DocTableInfo tableInfo) {
    if (partitionProperties.isEmpty()) {
        return Arrays.asList(tableInfo.concreteIndices());
    } else {
        DocTableInfo docTableInfo = tableInfo;
        PartitionName partitionName = PartitionPropertiesAnalyzer.toPartitionName(docTableInfo, partitionProperties, parameterContext.parameters());
        if (!docTableInfo.partitions().contains(partitionName)) {
            throw new PartitionUnknownException(tableInfo.ident().fqn(), partitionName.ident());
        }
        return Collections.singletonList(partitionName.asIndexName());
    }
}
Also used : PartitionName(io.crate.metadata.PartitionName) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException)

Example 3 with PartitionUnknownException

use of io.crate.exceptions.PartitionUnknownException in project crate by crate.

the class RefreshTablePlan method executeOrFail.

@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row parameters, SubQueryResults subQueryResults) {
    if (analysis.tables().isEmpty()) {
        consumer.accept(InMemoryBatchIterator.empty(SENTINEL), null);
        return;
    }
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(plannerContext.transactionContext(), plannerContext.nodeContext(), x, parameters, subQueryResults);
    ArrayList<String> toRefresh = new ArrayList<>();
    for (Map.Entry<Table<Symbol>, DocTableInfo> table : analysis.tables().entrySet()) {
        var tableInfo = table.getValue();
        var tableSymbol = table.getKey();
        if (tableSymbol.partitionProperties().isEmpty()) {
            toRefresh.addAll(Arrays.asList(tableInfo.concreteOpenIndices()));
        } else {
            var partitionName = toPartitionName(tableInfo, Lists2.map(tableSymbol.partitionProperties(), p -> p.map(eval)));
            if (!tableInfo.partitions().contains(partitionName)) {
                throw new PartitionUnknownException(partitionName);
            }
            toRefresh.add(partitionName.asIndexName());
        }
    }
    RefreshRequest request = new RefreshRequest(toRefresh.toArray(String[]::new));
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    var transportRefreshAction = dependencies.transportActionProvider().transportRefreshAction();
    transportRefreshAction.execute(request, new OneRowActionListener<>(consumer, response -> new Row1(toRefresh.isEmpty() ? -1L : (long) toRefresh.size())));
}
Also used : AnalyzedRefreshTable(io.crate.analyze.AnalyzedRefreshTable) DocTableInfo(io.crate.metadata.doc.DocTableInfo) Arrays(java.util.Arrays) SENTINEL(io.crate.data.SentinelRow.SENTINEL) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) Table(io.crate.sql.tree.Table) Function(java.util.function.Function) Lists2(io.crate.common.collections.Lists2) ArrayList(java.util.ArrayList) RowConsumer(io.crate.data.RowConsumer) DependencyCarrier(io.crate.planner.DependencyCarrier) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) Map(java.util.Map) SubQueryResults(io.crate.planner.operators.SubQueryResults) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) OneRowActionListener(io.crate.execution.support.OneRowActionListener) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) Row1(io.crate.data.Row1) RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) AnalyzedRefreshTable(io.crate.analyze.AnalyzedRefreshTable) Table(io.crate.sql.tree.Table) ArrayList(java.util.ArrayList) Row1(io.crate.data.Row1) Map(java.util.Map)

Example 4 with PartitionUnknownException

use of io.crate.exceptions.PartitionUnknownException in project crate by crate.

the class CreateSnapshotPlan method createRequest.

@VisibleForTesting
public static CreateSnapshotRequest createRequest(AnalyzedCreateSnapshot createSnapshot, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx, Row parameters, SubQueryResults subQueryResults, Schemas schemas) {
    Function<? super Symbol, Object> eval = x -> SymbolEvaluator.evaluate(txnCtx, nodeCtx, x, parameters, subQueryResults);
    Settings settings = GenericPropertiesConverter.genericPropertiesToSettings(createSnapshot.properties().map(eval), SnapshotSettings.SETTINGS);
    boolean ignoreUnavailable = IGNORE_UNAVAILABLE.get(settings);
    final HashSet<String> snapshotIndices;
    final HashSet<String> templates = new HashSet<>();
    if (createSnapshot.tables().isEmpty()) {
        for (SchemaInfo schemaInfo : schemas) {
            for (TableInfo tableInfo : schemaInfo.getTables()) {
                // only check for user generated tables
                if (tableInfo instanceof DocTableInfo) {
                    Operation.blockedRaiseException(tableInfo, Operation.READ);
                }
            }
        }
        snapshotIndices = new HashSet<>(AnalyzedCreateSnapshot.ALL_INDICES);
    } else {
        snapshotIndices = new HashSet<>(createSnapshot.tables().size());
        for (Table<Symbol> table : createSnapshot.tables()) {
            DocTableInfo docTableInfo;
            try {
                docTableInfo = (DocTableInfo) schemas.resolveTableInfo(table.getName(), Operation.CREATE_SNAPSHOT, txnCtx.sessionContext().sessionUser(), txnCtx.sessionContext().searchPath());
            } catch (Exception e) {
                if (ignoreUnavailable && e instanceof ResourceUnknownException) {
                    LOGGER.info("Ignore unknown relation '{}' for the '{}' snapshot'", table.getName(), createSnapshot.snapshot());
                    continue;
                } else {
                    throw e;
                }
            }
            if (docTableInfo.isPartitioned()) {
                templates.add(PartitionName.templateName(docTableInfo.ident().schema(), docTableInfo.ident().name()));
            }
            if (table.partitionProperties().isEmpty()) {
                snapshotIndices.addAll(Arrays.asList(docTableInfo.concreteIndices()));
            } else {
                var partitionName = toPartitionName(docTableInfo, Lists2.map(table.partitionProperties(), x -> x.map(eval)));
                if (!docTableInfo.partitions().contains(partitionName)) {
                    if (!ignoreUnavailable) {
                        throw new PartitionUnknownException(partitionName);
                    } else {
                        LOGGER.info("ignoring unknown partition of table '{}' with ident '{}'", partitionName.relationName(), partitionName.ident());
                    }
                } else {
                    snapshotIndices.add(partitionName.asIndexName());
                }
            }
        }
    }
    return new CreateSnapshotRequest(createSnapshot.snapshot().getRepository(), createSnapshot.snapshot().getSnapshotId().getName()).includeGlobalState(createSnapshot.tables().isEmpty()).waitForCompletion(WAIT_FOR_COMPLETION.get(settings)).indices(snapshotIndices.toArray(new String[0])).indicesOptions(IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, IndicesOptions.lenientExpandOpen())).templates(templates.stream().toList()).settings(settings);
}
Also used : Arrays(java.util.Arrays) Operation(io.crate.metadata.table.Operation) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) PartitionName(io.crate.metadata.PartitionName) DependencyCarrier(io.crate.planner.DependencyCarrier) HashSet(java.util.HashSet) WAIT_FOR_COMPLETION(io.crate.analyze.SnapshotSettings.WAIT_FOR_COMPLETION) SymbolEvaluator(io.crate.analyze.SymbolEvaluator) Settings(org.elasticsearch.common.settings.Settings) AnalyzedCreateSnapshot(io.crate.analyze.AnalyzedCreateSnapshot) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) GenericPropertiesConverter(io.crate.analyze.GenericPropertiesConverter) SnapshotState(org.elasticsearch.snapshots.SnapshotState) OneRowActionListener(io.crate.execution.support.OneRowActionListener) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) PartitionPropertiesAnalyzer.toPartitionName(io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName) SchemaInfo(io.crate.metadata.table.SchemaInfo) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) Table(io.crate.sql.tree.Table) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) Lists2(io.crate.common.collections.Lists2) SnapshotSettings(io.crate.analyze.SnapshotSettings) RowConsumer(io.crate.data.RowConsumer) Logger(org.apache.logging.log4j.Logger) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) IGNORE_UNAVAILABLE(io.crate.analyze.SnapshotSettings.IGNORE_UNAVAILABLE) Plan(io.crate.planner.Plan) SubQueryResults(io.crate.planner.operators.SubQueryResults) Schemas(io.crate.metadata.Schemas) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) LogManager(org.apache.logging.log4j.LogManager) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) Symbol(io.crate.expression.symbol.Symbol) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) PartitionUnknownException(io.crate.exceptions.PartitionUnknownException) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Settings(org.elasticsearch.common.settings.Settings) SnapshotSettings(io.crate.analyze.SnapshotSettings) HashSet(java.util.HashSet) SchemaInfo(io.crate.metadata.table.SchemaInfo) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Aggregations

PartitionUnknownException (io.crate.exceptions.PartitionUnknownException)4 DocTableInfo (io.crate.metadata.doc.DocTableInfo)4 PartitionName (io.crate.metadata.PartitionName)3 Table (io.crate.sql.tree.Table)3 PartitionPropertiesAnalyzer.toPartitionName (io.crate.analyze.PartitionPropertiesAnalyzer.toPartitionName)2 SymbolEvaluator (io.crate.analyze.SymbolEvaluator)2 Lists2 (io.crate.common.collections.Lists2)2 Row (io.crate.data.Row)2 Row1 (io.crate.data.Row1)2 RowConsumer (io.crate.data.RowConsumer)2 ResourceUnknownException (io.crate.exceptions.ResourceUnknownException)2 OneRowActionListener (io.crate.execution.support.OneRowActionListener)2 Symbol (io.crate.expression.symbol.Symbol)2 SchemaInfo (io.crate.metadata.table.SchemaInfo)2 TableInfo (io.crate.metadata.table.TableInfo)2 DependencyCarrier (io.crate.planner.DependencyCarrier)2 Plan (io.crate.planner.Plan)2 PlannerContext (io.crate.planner.PlannerContext)2 SubQueryResults (io.crate.planner.operators.SubQueryResults)2 Arrays (java.util.Arrays)2