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;
}
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));
}
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());
}
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));
}
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);
}
});
}
Aggregations