Search in sources :

Example 1 with ResourceUnknownException

use of io.crate.exceptions.ResourceUnknownException 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 ResourceUnknownException

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

the class DropBlobTableAnalyzer method analyze.

public DropBlobTableAnalyzedStatement analyze(DropBlobTable node) {
    TableIdent tableIdent = tableToIdent(node.table());
    BlobTableInfo tableInfo = null;
    boolean isNoop = false;
    try {
        tableInfo = (BlobTableInfo) schemas.getTableInfo(tableIdent);
    } catch (ResourceUnknownException e) {
        if (node.ignoreNonExistentTable()) {
            isNoop = true;
        } else {
            throw e;
        }
    }
    return new DropBlobTableAnalyzedStatement(tableInfo, isNoop, node.ignoreNonExistentTable());
}
Also used : BlobTableInfo(io.crate.metadata.blob.BlobTableInfo) TableIdent(io.crate.metadata.TableIdent) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException)

Example 3 with ResourceUnknownException

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

the class DropTableAnalyzer method analyze.

public DropTableAnalyzedStatement analyze(DropTable node, @Nullable String defaultSchema) {
    TableIdent tableIdent = TableIdent.of(node.table(), defaultSchema);
    DocTableInfo tableInfo = null;
    boolean isNoop = false;
    try {
        tableInfo = schemas.getDroppableTable(tableIdent);
    } catch (ResourceUnknownException e) {
        if (node.dropIfExists()) {
            isNoop = true;
        } else {
            throw e;
        }
    }
    if (!isNoop) {
        Operation.blockedRaiseException(tableInfo, Operation.DROP);
    }
    return new DropTableAnalyzedStatement(tableInfo, isNoop, node.dropIfExists());
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableIdent(io.crate.metadata.TableIdent) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException)

Example 4 with ResourceUnknownException

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

the class ShardReferenceResolver method addPartitions.

private void addPartitions(Index index, Schemas schemas, ImmutableMap.Builder<ReferenceIdent, ReferenceImplementation> builder) {
    PartitionName partitionName;
    try {
        partitionName = PartitionName.fromIndexOrTemplate(index.name());
    } catch (IllegalArgumentException e) {
        throw new UnhandledServerException(String.format(Locale.ENGLISH, "Unable to load PARTITIONED BY columns from partition %s", index.name()), e);
    }
    TableIdent tableIdent = partitionName.tableIdent();
    try {
        DocTableInfo info = (DocTableInfo) schemas.getTableInfo(tableIdent);
        if (!schemas.isOrphanedAlias(info)) {
            assert info.isPartitioned() : "table must be partitioned";
            int i = 0;
            int numPartitionedColumns = info.partitionedByColumns().size();
            assert partitionName.values().size() == numPartitionedColumns : "invalid number of partitioned columns";
            for (Reference partitionedInfo : info.partitionedByColumns()) {
                builder.put(partitionedInfo.ident(), new PartitionedColumnExpression(partitionedInfo, partitionName.values().get(i)));
                i++;
            }
        } else {
            LOGGER.error("Orphaned partition '{}' with missing table '{}' found", index, tableIdent.fqn());
        }
    } catch (ResourceUnknownException e) {
        LOGGER.error("Orphaned partition '{}' with missing table '{}' found", index, tableIdent.fqn());
    }
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) PartitionedColumnExpression(io.crate.operation.reference.partitioned.PartitionedColumnExpression) UnhandledServerException(io.crate.exceptions.UnhandledServerException) ResourceUnknownException(io.crate.exceptions.ResourceUnknownException)

Aggregations

ResourceUnknownException (io.crate.exceptions.ResourceUnknownException)4 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3 TableIdent (io.crate.metadata.TableIdent)2 PartitionUnknownException (io.crate.exceptions.PartitionUnknownException)1 UnhandledServerException (io.crate.exceptions.UnhandledServerException)1 PartitionName (io.crate.metadata.PartitionName)1 BlobTableInfo (io.crate.metadata.blob.BlobTableInfo)1 SchemaInfo (io.crate.metadata.table.SchemaInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 PartitionedColumnExpression (io.crate.operation.reference.partitioned.PartitionedColumnExpression)1 QualifiedName (io.crate.sql.tree.QualifiedName)1 Table (io.crate.sql.tree.Table)1 SnapshotId (org.elasticsearch.cluster.metadata.SnapshotId)1 Settings (org.elasticsearch.common.settings.Settings)1