@Override public void cleanup() { if (sender != null) { try { sender.close().whenComplete((voidargs, error) -> { try { if (error != null) { logger.error("Exception during sender cleanup phase" + error.toString()); } ehClient.closeSync(); } catch (Exception e) { logger.error("Exception during ehclient cleanup phase" + e.toString()); } }).get(); } catch (InterruptedException e) { logger.error("Exception occured during cleanup phase" + e.toString()); } catch (ExecutionException e) { logger.error("Exception occured during cleanup phase" + e.toString()); } logger.info("Eventhub Bolt cleaned up"); sender = null; ehClient = null; } }
@Override public void execute(Tuple tuple) { try { EventData sendEvent = new EventData(boltConfig.getEventDataFormat().serialize(tuple)); if (boltConfig.getPartitionMode() && sender != null) { sender.sendSync(sendEvent); } else if (boltConfig.getPartitionMode() && sender == null) { throw new EventHubException("Sender is null"); } else if (!boltConfig.getPartitionMode() && ehClient != null) { ehClient.sendSync(sendEvent); } else if (!boltConfig.getPartitionMode() && ehClient == null) { throw new EventHubException("ehclient is null"); } collector.ack(tuple); } catch (EventHubException ex) { collector.reportError(ex); collector.fail(tuple); } catch (ServiceBusException e) { collector.reportError(e); collector.fail(tuple); } }
/** * Synchronous version of {@link #send(Iterable)} . * * @param eventDatas batch of events to send to EventHub * @throws EventHubException if Service Bus service encountered problems during the operation. */ default void sendSync(final Iterable<EventData> eventDatas) throws EventHubException { ExceptionUtil.syncVoid(() -> this.send(eventDatas).get()); }
/** * Creates an Empty Collection of {@link EventData}. * The same partitionKey must be used while sending these events using {@link PartitionSender#send(EventDataBatch)}. * * @return the empty {@link EventDataBatch}, after negotiating maximum message size with EventHubs service */ default EventDataBatch createBatch() { return this.createBatch(new BatchOptions()); }
/** * Synchronous version of {@link #send(EventDataBatch)} * * @param eventDatas EventDataBatch to send to EventHub * @throws EventHubException if Service Bus service encountered problems during the operation. */ default void sendSync(final EventDataBatch eventDatas) throws EventHubException { ExceptionUtil.syncVoid(() -> this.send(eventDatas).get()); }
/** * Synchronous version of {@link #send(EventData)} Api. * * @param data the {@link EventData} to be sent. * @throws PayloadSizeExceededException if the total size of the {@link EventData} exceeds a pre-defined limit set by the service. Default is 256k bytes. * @throws EventHubException if Service Bus service encountered problems during the operation. */ default void sendSync(final EventData data) throws EventHubException { ExceptionUtil.syncVoid(() -> this.send(data).get()); }
@Override public synchronized void stop() { LOG.info("Stopping producer."); streamPartitionSenders.values().forEach((streamPartitionSender) -> { List<CompletableFuture<Void>> futures = new ArrayList<>(); streamPartitionSender.forEach((key, value) -> futures.add(value.close())); CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); try { future.get(DEFAULT_SHUTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); } catch (ExecutionException | InterruptedException | TimeoutException e) { LOG.error("Closing the partition sender failed ", e); } }); perStreamEventHubClientManagers.values() .parallelStream() .forEach(ehClient -> ehClient.close(DEFAULT_SHUTDOWN_TIMEOUT_MILLIS)); perStreamEventHubClientManagers.clear(); if (config.getPerPartitionConnection(systemName)) { perPartitionEventHubClients.values() .stream() .flatMap(map -> map.values().stream()) .forEach(ehClient -> ehClient.close(DEFAULT_SHUTDOWN_TIMEOUT_MILLIS)); perPartitionEventHubClients.clear(); } isStarted = false; isInitialized = false; LOG.info("EventHubSystemProducer stopped."); }
private CompletableFuture<Void> doSend(String eventHubName, PartitionSupplier partitionSupplier, List<EventData> eventData) { try { EventHubClient client = this.clientFactory.getOrCreateClient(eventHubName); if (partitionSupplier == null) { return client.send(eventData); } else if (!Strings.isNullOrEmpty(partitionSupplier.getPartitionId())) { return this.clientFactory.getOrCreatePartitionSender(eventHubName, partitionSupplier.getPartitionId()) .send(eventData); } else if (!Strings.isNullOrEmpty(partitionSupplier.getPartitionKey())) { return client.send(eventData, partitionSupplier.getPartitionKey()); } else { return client.send(eventData); } } catch (EventHubRuntimeException e) { log.error(String.format("Failed to send to '%s' ", eventHubName), e); CompletableFuture<Void> future = new CompletableFuture<>(); future.completeExceptionally(e); return future; } }
private CompletableFuture<Void> doSend(String eventHubName, PartitionSupplier partitionSupplier, List<EventData> eventData) { try { EventHubClient client = this.clientFactory.getOrCreateClient(eventHubName); if (partitionSupplier == null) { return client.send(eventData); } else if (!Strings.isNullOrEmpty(partitionSupplier.getPartitionId())) { return this.clientFactory.getOrCreatePartitionSender(eventHubName, partitionSupplier.getPartitionId()) .send(eventData); } else if (!Strings.isNullOrEmpty(partitionSupplier.getPartitionKey())) { return client.send(eventData, partitionSupplier.getPartitionKey()); } else { return client.send(eventData); } } catch (EventHubRuntimeException e) { log.error(String.format("Failed to send to '%s' ", eventHubName), e); CompletableFuture<Void> future = new CompletableFuture<>(); future.completeExceptionally(e); return future; } }
private CompletableFuture<Void> sendToEventHub(String streamId, EventData eventData, Object partitionKey, EventHubClient eventHubClient) { if (PartitioningMethod.ROUND_ROBIN.equals(partitioningMethod)) { return eventHubClient.send(eventData); } else if (PartitioningMethod.EVENT_HUB_HASHING.equals(partitioningMethod)) { if (partitionKey == null) { throw new SamzaException("Partition key cannot be null for EventHub hashing"); } return eventHubClient.send(eventData, convertPartitionKeyToString(partitionKey)); } else if (PartitioningMethod.PARTITION_KEY_AS_PARTITION.equals(partitioningMethod)) { if (!(partitionKey instanceof Integer)) { String msg = "Partition key should be of type Integer"; throw new SamzaException(msg); } Integer numPartition = streamPartitionSenders.get(streamId).size(); Integer destinationPartition = (Integer) partitionKey % numPartition; PartitionSender sender = streamPartitionSenders.get(streamId).get(destinationPartition); return sender.send(eventData); } else { throw new SamzaException("Unknown partitioning method " + partitioningMethod); } }
PowerMockito.when(mockPartitionSender0.send(any(EventData.class))) .then((Answer<CompletableFuture<Void>>) invocationOnMock -> { EventData data = invocationOnMock.getArgumentAt(0, EventData.class); return new CompletableFuture<>(); }); PowerMockito.when(mockPartitionSender1.send(any(EventData.class))) .then((Answer<CompletableFuture<Void>>) invocationOnMock -> { EventData data = invocationOnMock.getArgumentAt(0, EventData.class);