Search in sources :

Example 11 with NakadiRuntimeException

use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.

the class HashPartitionStrategy method calculatePartition.

@Override
public String calculatePartition(final EventType eventType, final JSONObject event, final List<String> partitions) throws InvalidPartitionKeyFieldsException {
    final List<String> partitionKeyFields = eventType.getPartitionKeyFields();
    if (partitionKeyFields.isEmpty()) {
        throw new RuntimeException("Applying " + this.getClass().getSimpleName() + " although event type " + "has no partition key fields configured.");
    }
    try {
        final JsonPathAccess traversableJsonEvent = new JsonPathAccess(event);
        final int hashValue = partitionKeyFields.stream().map(pkf -> EventCategory.DATA.equals(eventType.getCategory()) ? DATA_PATH_PREFIX + pkf : pkf).map(Try.wrap(okf -> {
            final String fieldValue = traversableJsonEvent.get(okf).toString();
            return stringHash.hashCode(fieldValue);
        })).map(Try::getOrThrow).mapToInt(hc -> hc).sum();
        int partitionIndex = abs(hashValue) % partitions.size();
        partitionIndex = hashPartitioningCrutch.adjustPartitionIndex(partitionIndex, partitions.size());
        final List<String> sortedPartitions = partitions.stream().sorted().collect(Collectors.toList());
        return sortedPartitions.get(partitionIndex);
    } catch (NakadiRuntimeException e) {
        final Exception original = e.getException();
        if (original instanceof InvalidPartitionKeyFieldsException) {
            throw (InvalidPartitionKeyFieldsException) original;
        } else {
            throw e;
        }
    }
}
Also used : EventCategory(org.zalando.nakadi.domain.EventCategory) EventType(org.zalando.nakadi.domain.EventType) JsonSchemaEnrichment(org.zalando.nakadi.validation.JsonSchemaEnrichment) Autowired(org.springframework.beans.factory.annotation.Autowired) Try(org.zalando.nakadi.exceptions.Try) Math.abs(java.lang.Math.abs) Collectors(java.util.stream.Collectors) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) JSONObject(org.json.JSONObject) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) Try(org.zalando.nakadi.exceptions.Try) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException)

Example 12 with NakadiRuntimeException

use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.

the class AbstractZkSubscriptionClient method registerSession.

@Override
public final void registerSession(final Session session) {
    getLog().info("Registering session " + session);
    try {
        final String clientPath = getSubscriptionPath("/sessions/" + session.getId());
        final byte[] sessionData = serializeSession(session);
        getCurator().create().withMode(CreateMode.EPHEMERAL).forPath(clientPath, sessionData);
    } catch (final Exception e) {
        throw new NakadiRuntimeException(e);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 13 with NakadiRuntimeException

use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.

the class AbstractZkSubscriptionClient method fillEmptySubscription.

@Override
public final void fillEmptySubscription(final Collection<SubscriptionCursorWithoutToken> cursors) {
    try {
        // Delete root subscription node, if it was erroneously created
        if (null != getCurator().checkExists().forPath(getSubscriptionPath(""))) {
            deleteSubscription();
        }
        getLog().info("Creating sessions root");
        getCurator().create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(getSubscriptionPath("/sessions"));
        final byte[] topologyData = createTopologyAndOffsets(cursors);
        getCurator().create().withMode(CreateMode.PERSISTENT).forPath(getSubscriptionPath(NODE_TOPOLOGY), topologyData);
        getLog().info("updating state");
        getCurator().create().forPath(getSubscriptionPath("/state"), STATE_INITIALIZED.getBytes(UTF_8));
    } catch (final Exception e) {
        throw new NakadiRuntimeException(e);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 14 with NakadiRuntimeException

use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.

the class AbstractZkSubscriptionClient method deleteSubscription.

@Override
public final void deleteSubscription() {
    try {
        final String subscriptionPath = getSubscriptionPath("");
        getCurator().delete().guaranteed().deletingChildrenIfNeeded().forPath(subscriptionPath);
    } catch (final KeeperException.NoNodeException nne) {
        getLog().warn("Subscription to delete is not found in Zookeeper: {}", subscriptionId);
    } catch (final Exception e) {
        throw new NakadiRuntimeException(e);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 15 with NakadiRuntimeException

use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.

the class NewZkSubscriptionClient method updatePartitionsConfiguration.

@Override
public void updatePartitionsConfiguration(final String newSessionsHash, final Partition[] partitions) throws NakadiRuntimeException, SubscriptionNotInitializedException {
    final Topology newTopology = getTopology().withUpdatedPartitions(newSessionsHash, partitions);
    try {
        getLog().info("Updating topology to {}", newTopology);
        getCurator().setData().forPath(getSubscriptionPath(NODE_TOPOLOGY), objectMapper.writeValueAsBytes(newTopology));
    } catch (final Exception ex) {
        throw new NakadiRuntimeException(ex);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Aggregations

NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)18 IOException (java.io.IOException)13 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)11 KeeperException (org.apache.zookeeper.KeeperException)10 ServiceTemporarilyUnavailableException (org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)10 UnableProcessException (org.zalando.nakadi.exceptions.UnableProcessException)8 OperationInterruptedException (org.zalando.nakadi.exceptions.runtime.OperationInterruptedException)8 OperationTimeoutException (org.zalando.nakadi.exceptions.runtime.OperationTimeoutException)8 RequestInProgressException (org.zalando.nakadi.exceptions.runtime.RequestInProgressException)8 ZookeeperException (org.zalando.nakadi.exceptions.runtime.ZookeeperException)8 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)6 Closeable (java.io.Closeable)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5 Collectors (java.util.stream.Collectors)5 MyNakadiRuntimeException1 (org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1)5 Arrays (java.util.Arrays)4