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