use of org.eclipse.hono.client.registry.TenantDisabledOrNotRegisteredException in project hono by eclipse.
the class LoraProtocolAdapter method handleTenantTimeout.
private void handleTenantTimeout(final Message<String> msg) {
final String tenantId = msg.body();
log.debug("check command subscriptions on timeout of tenant [{}]", tenantId);
final Span span = TracingHelper.buildSpan(tracer, null, "check command subscriptions on tenant timeout", getClass().getSimpleName()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start();
TracingHelper.setDeviceTags(span, tenantId, null);
// check if tenant still exists
getTenantConfiguration(tenantId, span.context()).recover(thr -> {
if (thr instanceof TenantDisabledOrNotRegisteredException) {
log.debug("tenant [{}] disabled or removed, removing corresponding command consumers", tenantId);
span.log("tenant disabled or removed, corresponding command consumers will be closed");
@SuppressWarnings("rawtypes") final List<Future> consumerCloseFutures = new LinkedList<>();
for (final var iter = commandSubscriptions.entrySet().iterator(); iter.hasNext(); ) {
final var entry = iter.next();
if (entry.getKey().getTenant().equals(tenantId)) {
final CommandConsumer commandConsumer = entry.getValue().one();
consumerCloseFutures.add(commandConsumer.close(span.context()));
iter.remove();
}
}
return CompositeFuture.join(consumerCloseFutures).mapEmpty();
} else {
return Future.failedFuture(thr);
}
}).onFailure(thr -> TracingHelper.logError(span, thr)).onComplete(ar -> span.finish());
}
use of org.eclipse.hono.client.registry.TenantDisabledOrNotRegisteredException in project hono by eclipse.
the class AbstractMappingAndDelegatingCommandHandler method mapAndDelegateIncomingCommand.
/**
* 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 commandContext The context of the command to send.
* @param timer The timer indicating the amount of time used for processing the command message.
* @return A future indicating the outcome of the operation.
* @throws NullPointerException if any of the parameters are {@code null}.
*/
protected final Future<Void> mapAndDelegateIncomingCommand(final CommandContext commandContext, final Timer.Sample timer) {
Objects.requireNonNull(commandContext);
Objects.requireNonNull(timer);
final Command command = commandContext.getCommand();
// determine last used gateway device id
if (log.isTraceEnabled()) {
log.trace("determine command target gateway/adapter for [{}]", command);
}
final Future<TenantObject> tenantObjectFuture = tenantClient.get(command.getTenant(), commandContext.getTracingContext());
return tenantObjectFuture.compose(tenantObject -> {
TenantTraceSamplingHelper.applyTraceSamplingPriority(tenantObject, null, commandContext.getTracingSpan());
commandContext.put(CommandContext.KEY_TENANT_CONFIG, tenantObject);
// check whether the handler messaging type is equal to the messaging type of the tenant (if set)
final MessagingType tenantMessagingType = Optional.ofNullable(tenantObject.getProperty(TenantConstants.FIELD_EXT, JsonObject.class)).map(ext -> ext.getString(TenantConstants.FIELD_EXT_MESSAGING_TYPE)).map(MessagingType::valueOf).orElse(null);
if (tenantMessagingType != null && getMessagingType() != tenantMessagingType) {
log.info("command received via {} but tenant is configured to use {} [{}]", getMessagingType(), tenantMessagingType, commandContext.getCommand());
commandContext.getTracingSpan().log(String.format("command received via %s but tenant is configured to use %s", getMessagingType(), tenantMessagingType));
}
return commandTargetMapper.getTargetGatewayAndAdapterInstance(command.getTenant(), command.getDeviceId(), commandContext.getTracingContext());
}).recover(cause -> {
final Throwable error;
if (tenantObjectFuture.failed() && ServiceInvocationException.extractStatusCode(cause) == HttpURLConnection.HTTP_NOT_FOUND) {
error = new TenantDisabledOrNotRegisteredException(command.getTenant(), HttpURLConnection.HTTP_NOT_FOUND);
} else if (cause instanceof DeviceDisabledOrNotRegisteredException) {
error = cause;
} else if (ServiceInvocationException.extractStatusCode(cause) == HttpURLConnection.HTTP_NOT_FOUND) {
log.debug("no target adapter instance found for command with device id " + command.getDeviceId(), cause);
error = new NoConsumerException("no target adapter instance found");
} else {
log.debug("error getting target gateway and adapter instance for command with device id " + command.getDeviceId(), cause);
error = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "error getting target gateway and adapter instance", cause);
}
if (error instanceof ClientErrorException) {
commandContext.reject(error);
} else {
commandContext.release(error);
}
reportCommandProcessingError(command, tenantObjectFuture.result(), error, timer);
return Future.failedFuture(cause);
}).compose(result -> {
final String targetAdapterInstanceId = result.getString(DeviceConnectionConstants.FIELD_ADAPTER_INSTANCE_ID);
final String targetDeviceId = result.getString(DeviceConnectionConstants.FIELD_PAYLOAD_DEVICE_ID);
final String targetGatewayId = targetDeviceId.equals(command.getDeviceId()) ? null : targetDeviceId;
if (Objects.isNull(targetGatewayId)) {
log.trace("determined target adapter instance [{}] for [{}] (command not mapped to gateway)", targetAdapterInstanceId, command);
} else {
command.setGatewayId(targetGatewayId);
log.trace("determined target gateway [{}] and adapter instance [{}] for [{}]", targetGatewayId, targetAdapterInstanceId, command);
commandContext.getTracingSpan().log("determined target gateway [" + targetGatewayId + "]");
}
return sendCommand(commandContext, targetAdapterInstanceId, tenantObjectFuture.result(), timer);
});
}
use of org.eclipse.hono.client.registry.TenantDisabledOrNotRegisteredException in project hono by eclipse.
the class KafkaBasedInternalCommandConsumer method handleCommandMessage.
void handleCommandMessage(final KafkaConsumerRecord<String, Buffer> record) {
// get partition/offset of the command record - related to the tenant-based topic the command was originally received in
final Integer commandPartition = KafkaRecordHelper.getOriginalPartitionHeader(record.headers()).orElse(null);
final Long commandOffset = KafkaRecordHelper.getOriginalOffsetHeader(record.headers()).orElse(null);
if (commandPartition == null || commandOffset == null) {
LOG.warn("command record is invalid - missing required original partition/offset headers");
return;
}
final KafkaBasedCommand command;
try {
command = KafkaBasedCommand.fromRoutedCommandRecord(record);
} catch (final IllegalArgumentException e) {
LOG.warn("command record is invalid [tenant-id: {}, device-id: {}]", KafkaRecordHelper.getTenantId(record.headers()).orElse(null), KafkaRecordHelper.getDeviceId(record.headers()).orElse(null), e);
return;
}
// check whether command has already been received and handled;
// partition index and offset here are related to the *tenant-based* topic the command was originally received in
// therefore they are stored in a map with the tenant as key
final Map<Integer, Long> lastHandledPartitionOffsets = lastHandledPartitionOffsetsPerTenant.computeIfAbsent(command.getTenant(), k -> new HashMap<>());
final Long lastHandledOffset = lastHandledPartitionOffsets.get(commandPartition);
if (lastHandledOffset != null && commandOffset <= lastHandledOffset) {
if (LOG.isDebugEnabled()) {
LOG.debug("ignoring command - record partition offset {} <= last handled offset {} [{}]", commandOffset, lastHandledOffset, command);
}
} else {
lastHandledPartitionOffsets.put(commandPartition, commandOffset);
final CommandHandlerWrapper commandHandler = commandHandlers.getCommandHandler(command.getTenant(), command.getGatewayOrDeviceId());
if (commandHandler != null && commandHandler.getGatewayId() != null) {
// Gateway information set in command handler means a gateway has subscribed for commands for a specific device.
// This information isn't getting set in the record (by the Command Router) and therefore has to be adopted manually here.
command.setGatewayId(commandHandler.getGatewayId());
}
final SpanContext spanContext = KafkaTracingHelper.extractSpanContext(tracer, record);
final SpanContext followsFromSpanContext = commandHandler != null ? commandHandler.getConsumerCreationSpanContext() : null;
final Span currentSpan = CommandContext.createSpan(tracer, command, spanContext, followsFromSpanContext, getClass().getSimpleName());
currentSpan.setTag(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
KafkaTracingHelper.TAG_OFFSET.set(currentSpan, record.offset());
final var commandContext = new KafkaBasedCommandContext(command, commandResponseSender, currentSpan);
tenantClient.get(command.getTenant(), spanContext).onFailure(t -> {
if (ServiceInvocationException.extractStatusCode(t) == HttpURLConnection.HTTP_NOT_FOUND) {
commandContext.reject(new TenantDisabledOrNotRegisteredException(command.getTenant(), HttpURLConnection.HTTP_NOT_FOUND));
} else {
commandContext.release(new ServerErrorException(command.getTenant(), HttpURLConnection.HTTP_UNAVAILABLE, "error retrieving tenant configuration", t));
}
}).onSuccess(tenantConfig -> {
commandContext.put(CommandContext.KEY_TENANT_CONFIG, tenantConfig);
if (commandHandler != null) {
LOG.trace("using [{}] for received command [{}]", commandHandler, command);
// command.isValid() check not done here - it is to be done in the command handler
commandHandler.handleCommand(commandContext);
} else {
LOG.info("no command handler found for command [{}]", command);
commandContext.release(new NoConsumerException("no command handler found for command"));
}
});
}
}
Aggregations