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