Search in sources :

Example 1 with StoreTimeoutException

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

the class KafkaSchemaRegistry method deleteSubject.

@Override
public List<Integer> deleteSubject(String subject, boolean permanentDelete) throws SchemaRegistryException {
    // Ensure cache is up-to-date before any potential writes
    try {
        if (isReadOnlyMode(subject)) {
            throw new OperationNotPermittedException("Subject " + subject + " is in read-only mode");
        }
        kafkaStore.waitUntilKafkaReaderReachesLastOffset(subject, kafkaStoreTimeoutMs);
        List<Integer> deletedVersions = new ArrayList<>();
        int deleteWatermarkVersion = 0;
        Iterator<Schema> schemasToBeDeleted = getAllVersions(subject, permanentDelete);
        while (schemasToBeDeleted.hasNext()) {
            deleteWatermarkVersion = schemasToBeDeleted.next().getVersion();
            SchemaKey key = new SchemaKey(subject, deleteWatermarkVersion);
            if (!lookupCache.referencesSchema(key).isEmpty()) {
                throw new ReferenceExistsException(key.toString());
            }
            if (permanentDelete) {
                SchemaValue schemaValue = (SchemaValue) lookupCache.get(key);
                if (schemaValue != null && !schemaValue.isDeleted()) {
                    throw new SubjectNotSoftDeletedException(subject);
                }
            }
            deletedVersions.add(deleteWatermarkVersion);
        }
        if (!permanentDelete) {
            DeleteSubjectKey key = new DeleteSubjectKey(subject);
            DeleteSubjectValue value = new DeleteSubjectValue(subject, deleteWatermarkVersion);
            kafkaStore.put(key, value);
            if (getMode(subject) != null) {
                deleteMode(subject);
            }
            if (getCompatibilityLevel(subject) != null) {
                deleteCompatibility(subject);
            }
        } else {
            for (Integer version : deletedVersions) {
                kafkaStore.put(new SchemaKey(subject, version), null);
            }
        }
        return deletedVersions;
    } catch (StoreTimeoutException te) {
        throw new SchemaRegistryTimeoutException("Write to the Kafka store timed out while", te);
    } catch (StoreException e) {
        throw new SchemaRegistryStoreException("Error while deleting the subject in the" + " backend Kafka store", e);
    }
}
Also used : ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) 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) SubjectNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SubjectNotSoftDeletedException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) OperationNotPermittedException(io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException)

Example 2 with StoreTimeoutException

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

the class KafkaSchemaRegistry method register.

@Override
public int register(String subject, Schema schema, boolean normalize) throws SchemaRegistryException {
    try {
        checkRegisterMode(subject, schema);
        // Ensure cache is up-to-date before any potential writes
        kafkaStore.waitUntilKafkaReaderReachesLastOffset(subject, kafkaStoreTimeoutMs);
        int schemaId = schema.getId();
        ParsedSchema parsedSchema = canonicalizeSchema(schema, schemaId < 0, normalize);
        // see if the schema to be registered already exists
        SchemaIdAndSubjects schemaIdAndSubjects = this.lookupCache.schemaIdAndSubjects(schema);
        if (schemaIdAndSubjects != null) {
            if (schemaId >= 0 && schemaId != schemaIdAndSubjects.getSchemaId()) {
                throw new IdDoesNotMatchException(schemaIdAndSubjects.getSchemaId(), schema.getId());
            }
            if (schemaIdAndSubjects.hasSubject(subject) && !isSubjectVersionDeleted(subject, schemaIdAndSubjects.getVersion(subject))) {
                // return only if the schema was previously registered under the input subject
                return schemaIdAndSubjects.getSchemaId();
            } else {
                // need to register schema under the input subject
                schemaId = schemaIdAndSubjects.getSchemaId();
            }
        }
        // determine the latest version of the schema in the subject
        List<SchemaValue> allVersions = getAllSchemaValues(subject);
        Collections.reverse(allVersions);
        List<SchemaValue> deletedVersions = new ArrayList<>();
        List<ParsedSchema> undeletedVersions = new ArrayList<>();
        int newVersion = MIN_VERSION;
        for (SchemaValue schemaValue : allVersions) {
            newVersion = Math.max(newVersion, schemaValue.getVersion() + 1);
            if (schemaValue.isDeleted()) {
                deletedVersions.add(schemaValue);
            } else {
                ParsedSchema undeletedSchema = parseSchema(getSchemaEntityFromSchemaValue(schemaValue));
                if (parsedSchema.references().isEmpty() && !undeletedSchema.references().isEmpty() && parsedSchema.deepEquals(undeletedSchema)) {
                    // This handles the case where a schema is sent with all references resolved
                    return schemaValue.getId();
                }
                undeletedVersions.add(undeletedSchema);
            }
        }
        Collections.reverse(undeletedVersions);
        final List<String> compatibilityErrorLogs = isCompatibleWithPrevious(subject, parsedSchema, undeletedVersions);
        final boolean isCompatible = compatibilityErrorLogs.isEmpty();
        if (normalize) {
            parsedSchema = parsedSchema.normalize();
        }
        // Allow schema providers to modify the schema during compatibility checks
        schema.setSchema(parsedSchema.canonicalString());
        schema.setReferences(parsedSchema.references());
        if (isCompatible) {
            // save the context key
            QualifiedSubject qs = QualifiedSubject.create(tenant(), subject);
            if (qs != null && !DEFAULT_CONTEXT.equals(qs.getContext())) {
                ContextKey contextKey = new ContextKey(qs.getTenant(), qs.getContext());
                if (kafkaStore.get(contextKey) == null) {
                    ContextValue contextValue = new ContextValue(qs.getTenant(), qs.getContext());
                    kafkaStore.put(contextKey, contextValue);
                }
            }
            // assign a guid and put the schema in the kafka store
            if (schema.getVersion() <= 0) {
                schema.setVersion(newVersion);
            }
            SchemaKey schemaKey = new SchemaKey(subject, schema.getVersion());
            if (schemaId >= 0) {
                checkIfSchemaWithIdExist(schemaId, schema);
                schema.setId(schemaId);
                kafkaStore.put(schemaKey, new SchemaValue(schema));
            } else {
                int retries = 0;
                while (retries++ < kafkaStoreMaxRetries) {
                    int newId = idGenerator.id(new SchemaValue(schema));
                    // Verify id is not already in use
                    if (lookupCache.schemaKeyById(newId, subject) == null) {
                        schema.setId(newId);
                        if (retries > 1) {
                            log.warn(String.format("Retrying to register the schema with ID %s", newId));
                        }
                        kafkaStore.put(schemaKey, new SchemaValue(schema));
                        break;
                    }
                }
                if (retries >= kafkaStoreMaxRetries) {
                    throw new SchemaRegistryStoreException("Error while registering the schema due " + "to generating an ID that is already in use.");
                }
            }
            for (SchemaValue deleted : deletedVersions) {
                if (deleted.getId().equals(schema.getId()) && deleted.getVersion().compareTo(schema.getVersion()) < 0) {
                    // Tombstone previous version with the same ID
                    SchemaKey key = new SchemaKey(deleted.getSubject(), deleted.getVersion());
                    kafkaStore.put(key, null);
                }
            }
            return schema.getId();
        } else {
            throw new IncompatibleSchemaException(compatibilityErrorLogs.toString());
        }
    } catch (StoreTimeoutException te) {
        throw new SchemaRegistryTimeoutException("Write to the Kafka store timed out while", te);
    } catch (StoreException e) {
        throw new SchemaRegistryStoreException("Error while registering the schema in the" + " backend Kafka store", e);
    }
}
Also used : QualifiedSubject(io.confluent.kafka.schemaregistry.utils.QualifiedSubject) ArrayList(java.util.ArrayList) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) IncompatibleSchemaException(io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException) IdDoesNotMatchException(io.confluent.kafka.schemaregistry.exceptions.IdDoesNotMatchException) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException)

Example 3 with StoreTimeoutException

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

the class KafkaSchemaRegistry method deleteSchemaVersion.

@Override
public void deleteSchemaVersion(String subject, Schema schema, boolean permanentDelete) throws SchemaRegistryException {
    try {
        if (isReadOnlyMode(subject)) {
            throw new OperationNotPermittedException("Subject " + subject + " is in read-only mode");
        }
        SchemaKey key = new SchemaKey(subject, schema.getVersion());
        if (!lookupCache.referencesSchema(key).isEmpty()) {
            throw new ReferenceExistsException(key.toString());
        }
        SchemaValue schemaValue = (SchemaValue) lookupCache.get(key);
        if (permanentDelete && schemaValue != null && !schemaValue.isDeleted()) {
            throw new SchemaVersionNotSoftDeletedException(subject, schema.getVersion().toString());
        }
        // Ensure cache is up-to-date before any potential writes
        kafkaStore.waitUntilKafkaReaderReachesLastOffset(subject, kafkaStoreTimeoutMs);
        if (!permanentDelete) {
            schemaValue = new SchemaValue(schema);
            schemaValue.setDeleted(true);
            kafkaStore.put(key, schemaValue);
            if (!getAllVersions(subject, false).hasNext()) {
                if (getMode(subject) != null) {
                    deleteMode(subject);
                }
                if (getCompatibilityLevel(subject) != null) {
                    deleteCompatibility(subject);
                }
            }
        } else {
            kafkaStore.put(key, null);
        }
    } catch (StoreTimeoutException te) {
        throw new SchemaRegistryTimeoutException("Write to the Kafka store timed out while", te);
    } catch (StoreException e) {
        throw new SchemaRegistryStoreException("Error while deleting the schema for subject '" + subject + "' in the backend Kafka store", e);
    }
}
Also used : ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) OperationNotPermittedException(io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) SchemaVersionNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)

Example 4 with StoreTimeoutException

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

the class KafkaStore method put.

@Override
public V put(K key, V value) throws StoreTimeoutException, StoreException {
    assertInitialized();
    if (key == null) {
        throw new StoreException("Key should not be null");
    }
    V oldValue = get(key);
    // write to the Kafka topic
    ProducerRecord<byte[], byte[]> producerRecord = null;
    try {
        producerRecord = new ProducerRecord<byte[], byte[]>(topic, 0, this.serializer.serializeKey(key), value == null ? null : this.serializer.serializeValue(value));
    } catch (SerializationException e) {
        throw new StoreException("Error serializing schema while creating the Kafka produce " + "record", e);
    }
    boolean knownSuccessfulWrite = false;
    try {
        log.trace("Sending record to KafkaStore topic: " + producerRecord);
        Future<RecordMetadata> ack = producer.send(producerRecord);
        RecordMetadata recordMetadata = ack.get(timeout, TimeUnit.MILLISECONDS);
        log.trace("Waiting for the local store to catch up to offset " + recordMetadata.offset());
        this.lastWrittenOffset = recordMetadata.offset();
        if (key instanceof SubjectKey) {
            setLastOffset(((SubjectKey) key).getSubject(), recordMetadata.offset());
        }
        waitUntilKafkaReaderReachesOffset(recordMetadata.offset(), timeout);
        knownSuccessfulWrite = true;
    } catch (InterruptedException e) {
        throw new StoreException("Put operation interrupted while waiting for an ack from Kafka", e);
    } catch (ExecutionException e) {
        throw new StoreException("Put operation failed while waiting for an ack from Kafka", e);
    } catch (TimeoutException e) {
        throw new StoreTimeoutException("Put operation timed out while waiting for an ack from Kafka", e);
    } catch (KafkaException ke) {
        throw new StoreException("Put operation to Kafka failed", ke);
    } finally {
        if (!knownSuccessfulWrite) {
            markLastWrittenOffsetInvalid();
        }
    }
    return oldValue;
}
Also used : SerializationException(io.confluent.kafka.schemaregistry.storage.exceptions.SerializationException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) KafkaException(org.apache.kafka.common.KafkaException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException)

Aggregations

StoreException (io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)4 StoreTimeoutException (io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException)4 SchemaRegistryStoreException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException)3 SchemaRegistryTimeoutException (io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException)3 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)2 OperationNotPermittedException (io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException)2 ReferenceExistsException (io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException)2 ArrayList (java.util.ArrayList)2 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 Schema (io.confluent.kafka.schemaregistry.client.rest.entities.Schema)1 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)1 IdDoesNotMatchException (io.confluent.kafka.schemaregistry.exceptions.IdDoesNotMatchException)1 IncompatibleSchemaException (io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException)1 SchemaVersionNotSoftDeletedException (io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException)1 SubjectNotSoftDeletedException (io.confluent.kafka.schemaregistry.exceptions.SubjectNotSoftDeletedException)1 SerializationException (io.confluent.kafka.schemaregistry.storage.exceptions.SerializationException)1 QualifiedSubject (io.confluent.kafka.schemaregistry.utils.QualifiedSubject)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1