Search in sources :

Example 6 with SchemaRegistryStoreException

use of io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException in project schema-registry by confluentinc.

the class SchemasResource method getSchemas.

@GET
@Operation(summary = "Get the schemas.", responses = { @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend " + "data store\n") })
@PerformanceMetric("schemas.get-schemas")
public List<Schema> getSchemas(@Parameter(description = "Filters results by the respective subject prefix") @DefaultValue("") @QueryParam("subjectPrefix") String subjectPrefix, @Parameter(description = "Whether to return soft deleted schemas") @DefaultValue("false") @QueryParam("deleted") boolean lookupDeletedSchema, @Parameter(description = "Whether to return latest schema versions only for each matching subject") @DefaultValue("false") @QueryParam("latestOnly") boolean latestOnly, @Parameter(description = "Pagination offset for results") @DefaultValue("0") @QueryParam("offset") int offset, @Parameter(description = "Pagination size for results. Ignored if negative") @DefaultValue("-1") @QueryParam("limit") int limit) {
    Iterator<Schema> schemas;
    List<Schema> filteredSchemas = new ArrayList<>();
    String errorMessage = "Error while getting schemas for prefix " + subjectPrefix;
    try {
        schemas = schemaRegistry.getVersionsWithSubjectPrefix(subjectPrefix, lookupDeletedSchema, latestOnly);
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException(errorMessage, e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
    int fromIndex = offset;
    int toIndex = limit > 0 ? offset + limit : Integer.MAX_VALUE;
    int index = 0;
    while (schemas.hasNext() && index < toIndex) {
        Schema schema = schemas.next();
        if (index >= offset) {
            filteredSchemas.add(schema);
        }
        index++;
    }
    return filteredSchemas;
}
Also used : Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) ArrayList(java.util.ArrayList) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Example 7 with SchemaRegistryStoreException

use of io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException in project schema-registry by confluentinc.

the class SubjectVersionsResource method deleteSchemaVersion.

@DELETE
@Path("/{version}")
@PerformanceMetric("subjects.versions.deleteSchemaVersion-schema")
@Operation(summary = "Deletes a specific version of the schema registered under this subject. " + "This only deletes the version and the schema ID remains intact making it still possible " + "to decode data using the schema ID. This API is recommended to be used only in " + "development environments or under extreme circumstances where-in, its required to delete " + "a previously registered schema for compatibility purposes or re-register previously " + "registered schema.", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = int.class))), @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend " + "data store") })
public void deleteSchemaVersion(@Suspended final AsyncResponse asyncResponse, @Context HttpHeaders headers, @Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subject, @Parameter(description = VERSION_PARAM_DESC, required = true) @PathParam("version") String version, @Parameter(description = "Whether to perform a permanent delete") @QueryParam("permanent") boolean permanentDelete) {
    log.info("Deleting schema version {} from subject {}", version, subject);
    subject = QualifiedSubject.normalize(schemaRegistry.tenant(), subject);
    VersionId versionId;
    try {
        versionId = new VersionId(version);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    }
    Schema schema;
    String errorMessage = "Error while retrieving schema for subject " + subject + " with version " + version + " from the schema registry";
    try {
        if (schemaRegistry.schemaVersionExists(subject, versionId, true)) {
            if (!permanentDelete && !schemaRegistry.schemaVersionExists(subject, versionId, false)) {
                throw Errors.schemaVersionSoftDeletedException(subject, version);
            }
        }
        schema = schemaRegistry.get(subject, versionId.getVersionId(), true);
        if (schema == null) {
            if (!schemaRegistry.hasSubjects(subject, true)) {
                throw Errors.subjectNotFoundException(subject);
            } else {
                throw Errors.versionNotFoundException(versionId.getVersionId());
            }
        }
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
    try {
        Map<String, String> headerProperties = requestHeaderBuilder.buildRequestHeaders(headers, schemaRegistry.config().whitelistHeaders());
        schemaRegistry.deleteSchemaVersionOrForward(headerProperties, subject, schema, permanentDelete);
    } catch (SchemaVersionNotSoftDeletedException e) {
        throw Errors.schemaVersionNotSoftDeletedException(e.getSubject(), e.getVersion());
    } catch (SchemaRegistryTimeoutException e) {
        throw Errors.operationTimeoutException("Delete Schema Version operation timed out", e);
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Delete Schema Version operation failed while writing" + " to the Kafka store", e);
    } catch (SchemaRegistryRequestForwardingException e) {
        throw Errors.requestForwardingFailedException("Error while forwarding delete schema version request" + " to the leader", e);
    } catch (ReferenceExistsException e) {
        throw Errors.referenceExistsException(e.getMessage());
    } catch (UnknownLeaderException e) {
        throw Errors.unknownLeaderException("Leader not known.", e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while deleting Schema Version", e);
    }
    asyncResponse.resume(schema.getVersion());
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) SchemaRegistryRequestForwardingException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException) UnknownLeaderException(io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) SchemaVersionNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException) Path(javax.ws.rs.Path) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) DELETE(javax.ws.rs.DELETE) Operation(io.swagger.v3.oas.annotations.Operation)

Example 8 with SchemaRegistryStoreException

use of io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException in project schema-registry by confluentinc.

the class SubjectVersionsResource method getReferencedBy.

@GET
@Path("/{version}/referencedby")
@Operation(summary = "Get the schemas that reference the specified schema.", responses = { @ApiResponse(responseCode = "404", description = "Error code 40401 -- Subject not found\n" + "Error code 40402 -- Version not found"), @ApiResponse(responseCode = "422", description = "Error code 42202 -- Invalid version"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend data store") })
public List<Integer> getReferencedBy(@Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subject, @Parameter(description = VERSION_PARAM_DESC, required = true) @PathParam("version") String version) {
    Schema schema = getSchemaByVersion(subject, version, true);
    if (schema == null) {
        return new ArrayList<>();
    }
    VersionId versionId = null;
    try {
        versionId = new VersionId(schema.getVersion());
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    }
    String errorMessage = "Error while retrieving schemas that reference schema with subject " + subject + " and version " + version + " from the schema registry";
    try {
        return schemaRegistry.getReferencedBy(schema.getSubject(), versionId);
    } catch (SchemaRegistryStoreException e) {
        log.debug(errorMessage, e);
        throw Errors.storeException(errorMessage, e);
    } catch (InvalidVersionException e) {
        throw Errors.invalidVersionException(e.getMessage());
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException(errorMessage, e);
    }
}
Also used : VersionId(io.confluent.kafka.schemaregistry.rest.VersionId) InvalidVersionException(io.confluent.kafka.schemaregistry.exceptions.InvalidVersionException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Example 9 with SchemaRegistryStoreException

use of io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException in project schema-registry by confluentinc.

the class SubjectVersionsResource method register.

@POST
@PerformanceMetric("subjects.versions.register")
@Operation(summary = "Register a new schema under the specified subject. If successfully " + "registered, this returns the unique identifier of this schema in the registry. The " + "returned identifier should be used to retrieve this schema from the schemas resource and " + "is different from the schema's version which is associated with the subject. If the same " + "schema is registered under a different subject, the same identifier will be returned. " + "However, the version of the schema may be different under different subjects.\n" + "A schema should be compatible with the previously registered schema or schemas (if there " + "are any) as per the configured compatibility level. The configured compatibility level " + "can be obtained by issuing a GET http:get:: /config/(string: subject). If that returns " + "null, then GET http:get:: /config\n" + "When there are multiple instances of Schema Registry running in the same cluster, the " + "schema registration request will be forwarded to one of the instances designated as " + "the primary. If the primary is not available, the client will get an error code " + "indicating that the forwarding has failed.", responses = { @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = RegisterSchemaResponse.class))), @ApiResponse(responseCode = "409", description = "Incompatible schema"), @ApiResponse(responseCode = "422", description = "Error code 42201 -- Invalid schema or " + "schema type"), @ApiResponse(responseCode = "500", description = "Error code 50001 -- Error in the backend data store\n" + "Error code 50002 -- Operation timed out\n" + "Error code 50003 -- Error while forwarding the request to the primary") })
public void register(@Suspended final AsyncResponse asyncResponse, @Context HttpHeaders headers, @Parameter(description = "Name of the subject", required = true) @PathParam("subject") String subjectName, @Parameter(description = "Whether to register the normalized schema") @QueryParam("normalize") boolean normalize, @Parameter(description = "Schema", required = true) @NotNull RegisterSchemaRequest request) {
    log.info("Registering new schema: subject {}, version {}, id {}, type {}, schema size {}", subjectName, request.getVersion(), request.getId(), request.getSchemaType(), request.getSchema() == null ? 0 : request.getSchema().length());
    if (subjectName != null && (CharMatcher.javaIsoControl().matchesAnyOf(subjectName) || QualifiedSubject.create(this.schemaRegistry.tenant(), subjectName).getSubject().equals(GLOBAL_RESOURCE_NAME))) {
        throw Errors.invalidSubjectException(subjectName);
    }
    subjectName = QualifiedSubject.normalize(schemaRegistry.tenant(), subjectName);
    Map<String, String> headerProperties = requestHeaderBuilder.buildRequestHeaders(headers, schemaRegistry.config().whitelistHeaders());
    Schema schema = new Schema(subjectName, request.getVersion() != null ? request.getVersion() : 0, request.getId() != null ? request.getId() : -1, request.getSchemaType() != null ? request.getSchemaType() : AvroSchema.TYPE, request.getReferences(), request.getSchema());
    int id;
    try {
        id = schemaRegistry.registerOrForward(subjectName, schema, normalize, headerProperties);
    } catch (IdDoesNotMatchException e) {
        throw Errors.idDoesNotMatchException(e);
    } catch (InvalidSchemaException e) {
        throw Errors.invalidSchemaException(e);
    } catch (OperationNotPermittedException e) {
        throw Errors.operationNotPermittedException(e.getMessage());
    } catch (SchemaRegistryTimeoutException e) {
        throw Errors.operationTimeoutException("Register operation timed out", e);
    } catch (SchemaRegistryStoreException e) {
        throw Errors.storeException("Register schema operation failed while writing" + " to the Kafka store", e);
    } catch (SchemaRegistryRequestForwardingException e) {
        throw Errors.requestForwardingFailedException("Error while forwarding register schema request" + " to the leader", e);
    } catch (IncompatibleSchemaException e) {
        throw Errors.incompatibleSchemaException("Schema being registered is incompatible with" + " an earlier schema for subject \"" + subjectName + "\", details: " + e.getMessage(), e);
    } catch (UnknownLeaderException e) {
        throw Errors.unknownLeaderException("Leader not known.", e);
    } catch (SchemaRegistryException e) {
        throw Errors.schemaRegistryException("Error while registering schema", e);
    }
    RegisterSchemaResponse registerSchemaResponse = new RegisterSchemaResponse();
    registerSchemaResponse.setId(id);
    asyncResponse.resume(registerSchemaResponse);
}
Also used : SchemaRegistryRequestForwardingException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException) UnknownLeaderException(io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException) Schema(io.confluent.kafka.schemaregistry.client.rest.entities.Schema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) RegisterSchemaResponse(io.confluent.kafka.schemaregistry.client.rest.entities.requests.RegisterSchemaResponse) IncompatibleSchemaException(io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) IdDoesNotMatchException(io.confluent.kafka.schemaregistry.exceptions.IdDoesNotMatchException) OperationNotPermittedException(io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) PerformanceMetric(io.confluent.rest.annotations.PerformanceMetric) POST(javax.ws.rs.POST) Operation(io.swagger.v3.oas.annotations.Operation)

Example 10 with SchemaRegistryStoreException

use of io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException in project schema-registry by confluentinc.

the class KafkaSchemaRegistry method get.

public SchemaString get(int id, String subject, String format, boolean fetchMaxId) throws SchemaRegistryException {
    SchemaValue schema = null;
    try {
        SchemaKey subjectVersionKey = getSchemaKeyUsingContexts(id, subject);
        if (subjectVersionKey == null) {
            return null;
        }
        schema = (SchemaValue) kafkaStore.get(subjectVersionKey);
        if (schema == null) {
            return null;
        }
    } catch (StoreException e) {
        throw new SchemaRegistryStoreException("Error while retrieving schema with id " + id + " from the backend Kafka" + " store", e);
    }
    SchemaString schemaString = new SchemaString();
    schemaString.setSchemaType(schema.getSchemaType());
    List<io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference> refs = schema.getReferences() != null ? schema.getReferences().stream().map(ref -> new io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference(ref.getName(), ref.getSubject(), ref.getVersion())).collect(Collectors.toList()) : null;
    schemaString.setReferences(refs);
    if (format != null && !format.trim().isEmpty()) {
        ParsedSchema parsedSchema = parseSchema(schema.getSchemaType(), schema.getSchema(), refs, false);
        schemaString.setSchemaString(parsedSchema.formattedString(format));
    } else {
        schemaString.setSchemaString(schema.getSchema());
    }
    if (fetchMaxId) {
        schemaString.setMaxId(idGenerator.getMaxId(schema));
    }
    return schemaString;
}
Also used : SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema)

Aggregations

SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)37 Operation (io.swagger.v3.oas.annotations.Operation)19 StoreException (io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)16 OperationNotPermittedException (io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException)14 Path (javax.ws.rs.Path)13 SchemaRegistryException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException)12 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)11 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)10 SchemaRegistryRequestForwardingException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException)9 UnknownLeaderException (io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException)9 GET (javax.ws.rs.GET)9 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)8 SchemaRegistryTimeoutException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException)8 PerformanceMetric (io.confluent.rest.annotations.PerformanceMetric)8 ArrayList (java.util.ArrayList)8 CompatibilityLevel (io.confluent.kafka.schemaregistry.CompatibilityLevel)7 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)5 VersionId (io.confluent.kafka.schemaregistry.rest.VersionId)5 Config (io.confluent.kafka.schemaregistry.client.rest.entities.Config)4 ReferenceExistsException (io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException)4