use of org.eclipse.hono.client.command.kafka.KafkaBasedCommand in project hono by eclipse.
the class KafkaBasedMappingAndDelegatingCommandHandler method mapAndDelegateIncomingCommandMessage.
/**
* Delegates an incoming command to the protocol adapter instance that the target
* device is connected to.
* <p>
* Determines the target gateway (if applicable) and protocol adapter instance for an incoming command
* and delegates the command to the resulting protocol adapter instance.
*
* @param consumerRecord The consumer record corresponding to the command.
* @return A future indicating the outcome of the operation.
* @throws NullPointerException if any of the parameters is {@code null}.
*/
public Future<Void> mapAndDelegateIncomingCommandMessage(final KafkaConsumerRecord<String, Buffer> consumerRecord) {
Objects.requireNonNull(consumerRecord);
final Timer.Sample timer = getMetrics().startTimer();
final KafkaBasedCommand command;
try {
command = KafkaBasedCommand.from(consumerRecord);
} catch (final IllegalArgumentException exception) {
log.debug("command record is invalid", exception);
return Future.failedFuture("command record is invalid");
}
final SpanContext spanContext = KafkaTracingHelper.extractSpanContext(tracer, consumerRecord);
final Span currentSpan = createSpan(command.getTenant(), command.getDeviceId(), spanContext);
KafkaTracingHelper.setRecordTags(currentSpan, consumerRecord);
final KafkaBasedCommandContext commandContext = new KafkaBasedCommandContext(command, kafkaBasedCommandResponseSender, currentSpan);
command.logToSpan(currentSpan);
if (!command.isValid()) {
log.debug("received invalid command record [{}]", command);
return tenantClient.get(command.getTenant(), currentSpan.context()).compose(tenantConfig -> {
commandContext.put(CommandContext.KEY_TENANT_CONFIG, tenantConfig);
return Future.failedFuture("command is invalid");
}).onComplete(ar -> {
commandContext.reject("malformed command message");
reportInvalidCommand(commandContext, timer);
}).mapEmpty();
}
log.trace("received valid command record [{}]", command);
commandQueue.add(commandContext);
final Promise<Void> resultPromise = Promise.promise();
final long timerId = vertx.setTimer(PROCESSING_TIMEOUT.toMillis(), tid -> {
if (commandQueue.remove(commandContext) || !commandContext.isCompleted()) {
log.info("command processing timed out after {}s [{}]", PROCESSING_TIMEOUT.toSeconds(), commandContext.getCommand());
TracingHelper.logError(commandContext.getTracingSpan(), String.format("command processing timed out after %ds", PROCESSING_TIMEOUT.toSeconds()));
final ServerErrorException error = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "command processing timed out");
commandContext.release(error);
resultPromise.tryFail(error);
}
});
mapAndDelegateIncomingCommand(commandContext, timer).onComplete(ar -> {
vertx.cancelTimer(timerId);
if (ar.failed()) {
commandQueue.remove(commandContext);
}
Futures.tryHandleResult(resultPromise, ar);
});
return resultPromise.future();
}
use of org.eclipse.hono.client.command.kafka.KafkaBasedCommand in project hono by eclipse.
the class KafkaCommandProcessingQueueTest method getTestCommandContext.
@SuppressWarnings("unchecked")
private KafkaBasedCommandContext getTestCommandContext(final int offset) {
final String deviceId = "deviceId";
final List<KafkaHeader> headers = new ArrayList<>(List.of(KafkaRecordHelper.createDeviceIdHeader(deviceId), KafkaRecordHelper.createSubjectHeader("subject_" + offset), KafkaRecordHelper.createCorrelationIdHeader("correlationId")));
final KafkaConsumerRecord<String, Buffer> consumerRecord = mock(KafkaConsumerRecord.class);
when(consumerRecord.headers()).thenReturn(headers);
when(consumerRecord.topic()).thenReturn(topic);
when(consumerRecord.key()).thenReturn(deviceId);
when(consumerRecord.offset()).thenReturn((long) offset);
when(consumerRecord.partition()).thenReturn(0);
final KafkaBasedCommand cmd = KafkaBasedCommand.from(consumerRecord);
return new KafkaBasedCommandContext(cmd, mock(CommandResponseSender.class), mock(Span.class)) {
@Override
public String toString() {
return "Command " + offset;
}
};
}
Aggregations