Search in sources :

Example 1 with DuplicatedStorageException

use of org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException in project nakadi by zalando.

the class StorageService method createStorage.

public Result<Void> createStorage(final JSONObject json) throws DbWriteOperationsBlockedException {
    if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
        throw new DbWriteOperationsBlockedException("Cannot create storage: write operations on DB " + "are blocked by feature flag.");
    }
    final String type;
    final String id;
    final JSONObject configuration;
    try {
        id = json.getString("id");
        type = json.getString("storage_type");
        switch(type) {
            case "kafka":
                configuration = json.getJSONObject("kafka_configuration");
                break;
            default:
                return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, "Type '" + type + "' is not a valid storage type"));
        }
    } catch (JSONException e) {
        return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, e.getMessage()));
    }
    final Storage storage = new Storage();
    storage.setId(id);
    storage.setType(Storage.Type.valueOf(type.toUpperCase()));
    try {
        storage.parseConfiguration(objectMapper, configuration.toString());
    } catch (final IOException e) {
        return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, e.getMessage()));
    }
    try {
        storageDbRepository.createStorage(storage);
    } catch (final RepositoryProblemException e) {
        LOG.error("DB error occurred when creating storage", e);
        return Result.problem(Problem.valueOf(INTERNAL_SERVER_ERROR, e.getMessage()));
    } catch (final DuplicatedStorageException e) {
        return Result.problem(Problem.valueOf(CONFLICT, e.getMessage()));
    }
    return Result.ok();
}
Also used : Storage(org.zalando.nakadi.domain.Storage) DefaultStorage(org.zalando.nakadi.domain.DefaultStorage) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) DuplicatedStorageException(org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)

Example 2 with DuplicatedStorageException

use of org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException in project nakadi by zalando.

the class NakadiConfig method defaultStorage.

@Bean
@Qualifier("default_storage")
public DefaultStorage defaultStorage(final StorageDbRepository storageDbRepository, final Environment environment, final ZooKeeperHolder zooKeeperHolder) throws InternalNakadiException {
    final String storageId = getStorageId(zooKeeperHolder, environment);
    final Optional<Storage> storageOpt = storageDbRepository.getStorage(storageId);
    if (!storageOpt.isPresent()) {
        LOGGER.info("Creating timelines storage `{}` from defaults", storageId);
        final Storage storage = new Storage();
        storage.setId(storageId);
        storage.setType(Storage.Type.KAFKA);
        storage.setConfiguration(new Storage.KafkaConfiguration(environment.getProperty("nakadi.zookeeper.exhibitor.brokers"), Integer.valueOf(environment.getProperty("nakadi.zookeeper.exhibitor.port", "0")), environment.getProperty("nakadi.zookeeper.brokers"), environment.getProperty("nakadi.zookeeper.kafkaNamespace", "")));
        try {
            storageDbRepository.createStorage(storage);
        } catch (final DuplicatedStorageException e) {
            LOGGER.info("Creation of default storage failed: {}", e.getMessage());
        }
        return new DefaultStorage(storage);
    } else {
        return new DefaultStorage(storageOpt.get());
    }
}
Also used : DefaultStorage(org.zalando.nakadi.domain.DefaultStorage) Storage(org.zalando.nakadi.domain.Storage) DefaultStorage(org.zalando.nakadi.domain.DefaultStorage) DuplicatedStorageException(org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Bean(org.springframework.context.annotation.Bean)

Example 3 with DuplicatedStorageException

use of org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException in project nakadi by zalando.

the class BaseAT method createDefaultStorage.

@BeforeClass
public static void createDefaultStorage() {
    final Storage storage = new Storage();
    storage.setId("default");
    storage.setType(Storage.Type.KAFKA);
    storage.setConfiguration(new Storage.KafkaConfiguration(null, null, ZOOKEEPER_URL, ""));
    try {
        STORAGE_DB_REPOSITORY.createStorage(storage);
    } catch (final DuplicatedStorageException ignore) {
    }
}
Also used : Storage(org.zalando.nakadi.domain.Storage) DuplicatedStorageException(org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException) BeforeClass(org.junit.BeforeClass)

Aggregations

Storage (org.zalando.nakadi.domain.Storage)3 DuplicatedStorageException (org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException)3 DefaultStorage (org.zalando.nakadi.domain.DefaultStorage)2 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 BeforeClass (org.junit.BeforeClass)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1 Bean (org.springframework.context.annotation.Bean)1 DbWriteOperationsBlockedException (org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)1 RepositoryProblemException (org.zalando.nakadi.exceptions.runtime.RepositoryProblemException)1