Search in sources :

Example 1 with InternalErrorException

use of com.bakdata.quick.common.exception.InternalErrorException in project quick by bakdata.

the class TopicRegistryInitializer method onStartUp.

/**
 * Ensures the topic registry itself and its topic are created.
 */
@EventListener
@Async
public void onStartUp(final StartupEvent event) {
    // the topic registry is a basically just a mirror application
    // therefore, it needs its own kafka topic
    final Properties properties = new Properties();
    properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaConfig.getBootstrapServer());
    boolean registryExists = false;
    try (final AdminClient admin = AdminClient.create(properties)) {
        final NewTopic immutableTopic = this.topicRegistryConfig.toNewKafkaTopic();
        admin.createTopics(List.of(immutableTopic));
    } catch (final TopicExistsException ignored) {
        log.info("Internal registry topic already exists");
        registryExists = true;
    } catch (final KafkaException e) {
        throw new InternalErrorException("Kafka could not be reached: " + e.getMessage());
    }
    // register the avro schema of the topic data class with the schema registry
    try {
        final String subject = VALUE.asSubject(this.topicRegistryConfig.getTopicName());
        final Schema topicDataSchema = AvroTopicData.getClassSchema();
        this.schemaRegistryClient.register(subject, topicDataSchema);
    } catch (final IOException | RestClientException exception) {
        if (!registryExists) {
            throw new InternalErrorException("Could not register schema for internal topic registry topic");
        }
    }
    final MirrorCreationData topicRegistryCreationData = new MirrorCreationData(this.topicRegistryConfig.getServiceName(), this.topicRegistryConfig.getTopicName(), 1, // null means we use the default tag
    null, null);
    // create topic-registry mirror
    // no retention time
    this.mirrorService.createInternalMirror(topicRegistryCreationData).subscribe(() -> log.info("Deployed internal topic-registry service"), e -> log.info("Could not deploy internal topic-registry service: {}", e.getMessage())).dispose();
}
Also used : MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) Async(io.micronaut.scheduling.annotation.Async) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) KafkaException(org.apache.kafka.common.KafkaException) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) AdminClient(org.apache.kafka.clients.admin.AdminClient) KafkaConfig(com.bakdata.quick.common.config.KafkaConfig) EventListener(io.micronaut.runtime.event.annotation.EventListener) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) AvroTopicData(com.bakdata.quick.common.api.model.AvroTopicData) MirrorService(com.bakdata.quick.manager.mirror.MirrorService) Requires(io.micronaut.context.annotation.Requires) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) TopicRegistryConfig(com.bakdata.quick.common.config.TopicRegistryConfig) Properties(java.util.Properties) Schema(org.apache.avro.Schema) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) IOException(java.io.IOException) VALUE(com.bakdata.quick.common.api.model.KeyValueEnum.VALUE) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) StartupEvent(io.micronaut.context.event.StartupEvent) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient) Schema(org.apache.avro.Schema) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) IOException(java.io.IOException) Properties(java.util.Properties) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) MirrorCreationData(com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) NewTopic(org.apache.kafka.clients.admin.NewTopic) KafkaException(org.apache.kafka.common.KafkaException) AdminClient(org.apache.kafka.clients.admin.AdminClient) Async(io.micronaut.scheduling.annotation.Async) EventListener(io.micronaut.runtime.event.annotation.EventListener)

Example 2 with InternalErrorException

use of com.bakdata.quick.common.exception.InternalErrorException in project quick by bakdata.

the class KubernetesManagerClient method handleDeploymentError.

private static Completable handleDeploymentError(final Throwable ex, final ResourceType type, final HasMetadata object) {
    if (ex instanceof KubernetesClientException) {
        final String reason = ((KubernetesClientException) ex).getStatus().getReason();
        return Completable.error(new BadArgumentException(reason));
    }
    final String resourceName = object.getMetadata().getName();
    final String errorMessage = String.format("%s %s could not be deployed.", type.getName(), resourceName);
    return Completable.error(new InternalErrorException(errorMessage));
}
Also used : BadArgumentException(com.bakdata.quick.common.exception.BadArgumentException) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 3 with InternalErrorException

use of com.bakdata.quick.common.exception.InternalErrorException in project quick by bakdata.

the class KafkaQueryService method get.

@Override
public Single<MirrorValue<V>> get(final String rawKey) {
    final K key = this.keyResolver.fromString(rawKey);
    final KeyQueryMetadata metadata;
    try {
        metadata = this.streams.queryMetadataForKey(this.storeName, key, this.keySerializer);
    } catch (final IllegalStateException exception) {
        throw new InternalErrorException("Store is not running");
    }
    if (metadata == null) {
        throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Metadata not found");
    }
    if (metadata.equals(KeyQueryMetadata.NOT_AVAILABLE)) {
        throw new InternalErrorException("Store currently not available");
    }
    // forward request if a different application is responsible for the rawKey
    if (!metadata.activeHost().equals(this.hostInfo) && !metadata.standbyHosts().contains(this.hostInfo)) {
        log.info("Forward request to {}", metadata.activeHost());
        return Single.fromCallable(() -> this.fetch(metadata.activeHost(), key)).map(MirrorValue::new).subscribeOn(Schedulers.io());
    }
    final ReadOnlyKeyValueStore<K, V> store = this.streams.store(this.storeQueryParameters);
    if (store == null) {
        log.warn("Store {} not found!", this.storeName);
        throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "No such store");
    }
    final V value = store.get(key);
    if (value == null) {
        throw new NotFoundException(String.format("Key %s does not exist in %s", rawKey, this.topicName));
    }
    return Single.just(new MirrorValue<>(value));
}
Also used : HttpStatusException(io.micronaut.http.exceptions.HttpStatusException) NotFoundException(com.bakdata.quick.common.exception.NotFoundException) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) KeyQueryMetadata(org.apache.kafka.streams.KeyQueryMetadata)

Example 4 with InternalErrorException

use of com.bakdata.quick.common.exception.InternalErrorException in project quick by bakdata.

the class KubernetesManagerClient method handleDeletionError.

/**
 * Handles errors that were thrown during the deletion of a resource.
 *
 * <p>
 * In most cases, an error occurs when a user attempts to delete a non-existing resource (e.g. because of a typo).
 *
 * @param ex   exception thrown during attempted deletion of resource
 * @param name name of the resource to delete
 * @param type type of the resource
 * @return completable with new error
 */
private static Completable handleDeletionError(final Throwable ex, final String name, final ResourceType type) {
    if (ex instanceof KubernetesClientException) {
        final KubernetesClientException k8sEx = (KubernetesClientException) ex;
        if (k8sEx.getStatus().getCode() == HttpStatus.NOT_FOUND.getCode()) {
            return Completable.error(new NotFoundException("Resource not found"));
        }
        log.warn("Kubernetes error during deletion:", k8sEx);
    }
    // return generic error message; something is seriously wrong
    final String errorMessage = String.format("Could not delete %s %s", type.getName(), name);
    return Completable.error(new InternalErrorException(errorMessage));
}
Also used : NotFoundException(com.bakdata.quick.common.exception.NotFoundException) InternalErrorException(com.bakdata.quick.common.exception.InternalErrorException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

InternalErrorException (com.bakdata.quick.common.exception.InternalErrorException)4 NotFoundException (com.bakdata.quick.common.exception.NotFoundException)2 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)2 AvroTopicData (com.bakdata.quick.common.api.model.AvroTopicData)1 VALUE (com.bakdata.quick.common.api.model.KeyValueEnum.VALUE)1 MirrorCreationData (com.bakdata.quick.common.api.model.manager.creation.MirrorCreationData)1 KafkaConfig (com.bakdata.quick.common.config.KafkaConfig)1 TopicRegistryConfig (com.bakdata.quick.common.config.TopicRegistryConfig)1 BadArgumentException (com.bakdata.quick.common.exception.BadArgumentException)1 MirrorService (com.bakdata.quick.manager.mirror.MirrorService)1 CachedSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)1 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 Requires (io.micronaut.context.annotation.Requires)1 StartupEvent (io.micronaut.context.event.StartupEvent)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)1 EventListener (io.micronaut.runtime.event.annotation.EventListener)1 Async (io.micronaut.scheduling.annotation.Async)1 IOException (java.io.IOException)1