Search in sources :

Example 1 with NotFoundException

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

the class KafkaQueryService method fetch.

private V fetch(final HostInfo replicaHostInfo, final K key) {
    final String host = String.format("%s:%s", replicaHostInfo.host(), replicaHostInfo.port());
    final MirrorHost mirrorHost = new MirrorHost(host, MirrorConfig.directAccess());
    final DefaultMirrorClient<K, V> client = new DefaultMirrorClient<>(mirrorHost, this.client, this.valueResolver.getElementType());
    final V value = client.fetchValue(key);
    if (value == null) {
        throw new NotFoundException("Key not found");
    }
    return value;
}
Also used : MirrorHost(com.bakdata.quick.common.api.model.mirror.MirrorHost) NotFoundException(com.bakdata.quick.common.exception.NotFoundException) DefaultMirrorClient(com.bakdata.quick.common.api.client.DefaultMirrorClient)

Example 2 with NotFoundException

use of com.bakdata.quick.common.exception.NotFoundException 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 3 with NotFoundException

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

the class KafkaTopicServiceTest method shouldThrowExceptionForNonExistingGateway.

@Test
void shouldThrowExceptionForNonExistingGateway() {
    final String topicName = UUID.randomUUID().toString();
    this.successfulMock();
    // error thrown when checking if gateway exists
    final Throwable error = new NotFoundException(String.format("Could not find resource %s", GATEWAY_SCHEMA.getGateway()));
    when(this.gatewayService.getGateway(GATEWAY_SCHEMA.getGateway())).thenReturn(Single.error(error));
    // resource doesn't exist, traefik cannot route it; This should not be called since we check existence before
    final HttpException clientException = new HttpClientResponseException("Not found", HttpResponse.notFound());
    when(this.gatewayClient.getWriteSchema(anyString(), anyString())).thenReturn(Single.error(clientException));
    final TopicCreationData requestData = new TopicCreationData(TopicWriteType.MUTABLE, GATEWAY_SCHEMA, null, null);
    final Completable completable = this.topicService.createTopic(topicName, QuickTopicType.DOUBLE, QuickTopicType.SCHEMA, requestData);
    final Throwable actual = completable.blockingGet();
    assertThat(actual).isNotNull().isInstanceOf(QuickException.class).hasMessageStartingWith("Could not find resource test");
    verify(this.gatewayClient, never()).getWriteSchema(anyString(), anyString());
}
Also used : TopicCreationData(com.bakdata.quick.common.api.model.manager.creation.TopicCreationData) Completable(io.reactivex.Completable) QuickException(com.bakdata.quick.common.exception.QuickException) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) NotFoundException(com.bakdata.quick.common.exception.NotFoundException) HttpException(io.micronaut.http.exceptions.HttpException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 4 with NotFoundException

use of com.bakdata.quick.common.exception.NotFoundException 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)

Example 5 with NotFoundException

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

the class KubernetesManagerClient method checkDeploymentExistence.

/**
 * Checks if a quick resource exists in the cluster or not.
 *
 * @param prefix determines the quick resource prefix {@link ResourcePrefix}
 * @param name   the name of the resource on the cluster
 * @return Completable and if the resource does not exist {@link NotFoundException} is thrown
 */
public Completable checkDeploymentExistence(final ResourcePrefix prefix, final String name) {
    final String resourceName = prefix.getPrefix() + name;
    final Single<Deployment> deployment = this.readDeployment(resourceName);
    return deployment.ignoreElement().onErrorResumeNext(error -> {
        // readDeployment didn't find a resource with that name and threw a null pointer
        if (error instanceof NullPointerException) {
            return Completable.error(new NotFoundException(String.format("Could not find resource %s", name)));
        } else {
            return Completable.error(error);
        }
    });
}
Also used : Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) NotFoundException(com.bakdata.quick.common.exception.NotFoundException)

Aggregations

NotFoundException (com.bakdata.quick.common.exception.NotFoundException)5 InternalErrorException (com.bakdata.quick.common.exception.InternalErrorException)2 DefaultMirrorClient (com.bakdata.quick.common.api.client.DefaultMirrorClient)1 TopicCreationData (com.bakdata.quick.common.api.model.manager.creation.TopicCreationData)1 MirrorHost (com.bakdata.quick.common.api.model.mirror.MirrorHost)1 QuickException (com.bakdata.quick.common.exception.QuickException)1 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)1 HttpException (io.micronaut.http.exceptions.HttpException)1 HttpStatusException (io.micronaut.http.exceptions.HttpStatusException)1 Completable (io.reactivex.Completable)1 KeyQueryMetadata (org.apache.kafka.streams.KeyQueryMetadata)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1