Search in sources :

Example 51 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class ManagementStore method delete.

/**
 * Delete the tenant.
 *
 * @param tenantId The tenant to delete.
 * @param resourceVersion The version of the resource to delete.
 * @param spanContext The span to contribute to.
 * @return The future, tracking the outcome of the operation.
 */
public Future<UpdateResult> delete(final String tenantId, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "delete tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final Statement statement;
    if (resourceVersion.isPresent()) {
        statement = this.deleteVersionedStatement;
    } else {
        statement = this.deleteStatement;
    }
    final var expanded = statement.expand(map -> {
        map.put("tenant_id", tenantId);
        resourceVersion.ifPresent(version -> map.put("expected_version", version));
    });
    log.debug("delete - statement: {}", expanded);
    final var result = expanded.trace(this.tracer, span.context()).update(this.client);
    return checkOptimisticLock(result, span, resourceVersion, checkSpan -> readTenantEntryById(this.client, tenantId, checkSpan.context())).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JDBCClient(io.vertx.ext.jdbc.JDBCClient) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLOperations(io.vertx.ext.sql.SQLOperations) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) Span(io.opentracing.Span)

Example 52 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class ManagementStore method update.

/**
 * Create a new tenant.
 * <p>
 * The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException} if the
 * specified tenant does not exist.
 * <p>
 * The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.DuplicateKeyException} if a
 * tenant with the ID or trust anchor already exists.
 * <p>
 * The operation may fail with an {@link org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException} if
 * an expected resource version was provided, but the current version did not match.
 *
 * @param tenantId The ID of the new tenant.
 * @param tenant The tenant information.
 * @param resourceVersion An optional resource version.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<Versioned<Void>> update(final String tenantId, final Tenant tenant, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final var json = tenantToJson(tenant);
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    final var nextVersion = UUID.randomUUID().toString();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final Statement statement = resourceVersion.isPresent() ? this.updateVersionedStatement : this.updateStatement;
    return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> updateJsonField(connection, tenantId, statement, json, resourceVersion, nextVersion, span).flatMap(r -> {
        if (r.getUpdated() <= 0) {
            return Future.failedFuture(new EntityNotFoundException());
        } else {
            return Future.succeededFuture();
        }
    }).flatMap(x -> deleteAllTrustAnchors(connection, tenantId, span)).flatMap(r -> insertAllTrustAnchors(connection, tenantId, tenant, span))).map(new Versioned<Void>(nextVersion, null)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JDBCClient(io.vertx.ext.jdbc.JDBCClient) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLOperations(io.vertx.ext.sql.SQLOperations) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) Span(io.opentracing.Span)

Example 53 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class TableManagementStore method updateDevice.

/**
 * Update device registration information.
 * <p>
 * This called the {@link #updateJsonField(DeviceKey, Statement, String, Optional, String, Span)} method
 * with either the {@code updateRegistration} or {@code updateRegistrationVersioned}
 * statement.
 *
 * @param key The key of the device to update.
 * @param device The device data to store.
 * @param resourceVersion The optional resource version.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<Versioned<Void>> updateDevice(final DeviceKey key, final Device device, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final var memberOf = Optional.ofNullable(device.getMemberOf()).<Set<String>>map(HashSet::new).orElse(Collections.emptySet());
    final JdbcBasedDeviceDto deviceDto = JdbcBasedDeviceDto.forUpdate(key, device, DeviceRegistryUtils.getUniqueIdentifier());
    return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> readDeviceForUpdate(connection, key, context).compose(result -> extractVersionForUpdate(result, resourceVersion)).compose(version -> deleteGroups(connection, key, context).map(version)).compose(version -> createGroups(connection, key, memberOf, context).map(version)).compose(version -> this.updateRegistrationVersionedStatement.expand(map -> {
        map.put("tenant_id", deviceDto.getTenantId());
        map.put("device_id", deviceDto.getDeviceId());
        map.put("data", deviceDto.getDeviceJson());
        map.put("expected_version", version);
        map.put("next_version", deviceDto.getVersion());
        map.put("updated_on", Timestamp.from(deviceDto.getUpdatedOn()));
        map.put("auto_provisioning_notification_sent", deviceDto.isAutoProvisioningNotificationSent());
    }).trace(this.tracer, span.context()).update(connection).compose(TableManagementStore::checkUpdateOutcome).map(version))).map(x -> new Versioned<Void>(deviceDto.getVersion(), null)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Map(java.util.Map) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Timestamp(java.sql.Timestamp) Promise(io.vertx.core.Promise) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) Span(io.opentracing.Span)

Example 54 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class TableManagementStore method deleteDevice.

/**
 * Delete a single device.
 * <p>
 * This will execute the {@code delete} or {@code deleteVersioned} SQL statement and provide
 * the named parameters {@code tenant_id}, {@code device_id}, and {@code expected_version} (if set).
 * It will return the plain update result of the operation.
 *
 * @param key The key of the device to delete.
 * @param resourceVersion An optional resource version.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<UpdateResult> deleteDevice(final DeviceKey key, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "delete device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final Statement statement;
    if (resourceVersion.isPresent()) {
        statement = this.deleteVersionedStatement;
    } else {
        statement = this.deleteStatement;
    }
    final var expanded = statement.expand(map -> {
        map.put("tenant_id", key.getTenantId());
        map.put("device_id", key.getDeviceId());
        resourceVersion.ifPresent(version -> map.put("expected_version", version));
    });
    log.debug("delete - statement: {}", expanded);
    final var result = expanded.trace(this.tracer, span.context()).update(this.client);
    return checkOptimisticLock(result, span, resourceVersion, checkSpan -> readDevice(this.client, key, checkSpan)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Map(java.util.Map) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Timestamp(java.sql.Timestamp) Promise(io.vertx.core.Promise) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) Span(io.opentracing.Span)

Example 55 with SpanContext

use of io.opentracing.SpanContext in project hono by eclipse.

the class ProtonBasedCommandRouterClient method unregisterCommandConsumer.

@Override
public Future<Void> unregisterCommandConsumer(final String tenantId, final String deviceId, final String adapterInstanceId, final SpanContext context) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(adapterInstanceId);
    final Map<String, Object> properties = createDeviceIdProperties(deviceId);
    properties.put(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
    final Span currentSpan = newChildSpan(context, "unregister command consumer");
    TracingHelper.setDeviceTags(currentSpan, tenantId, deviceId);
    currentSpan.setTag(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
    return getOrCreateClient(tenantId).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.UNREGISTER_COMMAND_CONSUMER.getSubject(), properties, null, null, this::getRequestResponseResult, currentSpan)).recover(t -> {
        Tags.HTTP_STATUS.set(currentSpan, ServiceInvocationException.extractStatusCode(t));
        TracingHelper.logError(currentSpan, t);
        return Future.failedFuture(t);
    }).map(resultValue -> {
        Tags.HTTP_STATUS.set(currentSpan, resultValue.getStatus());
        if (resultValue.isError() && resultValue.getStatus() != HttpURLConnection.HTTP_PRECON_FAILED) {
            Tags.ERROR.set(currentSpan, Boolean.TRUE);
        }
        switch(resultValue.getStatus()) {
            case HttpURLConnection.HTTP_NO_CONTENT:
                return null;
            default:
                throw StatusCodeMapper.from(resultValue);
        }
    }).onComplete(v -> currentSpan.finish()).mapEmpty();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) Duration(java.time.Duration) Map(java.util.Map) Pair(org.eclipse.hono.util.Pair) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) HonoConnection(org.eclipse.hono.client.HonoConnection) AbstractRequestResponseServiceClient(org.eclipse.hono.client.amqp.AbstractRequestResponseServiceClient) Logger(org.slf4j.Logger) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) RequestResponseClient(org.eclipse.hono.client.amqp.RequestResponseClient) CachingClientFactory(org.eclipse.hono.client.impl.CachingClientFactory) Set(java.util.Set) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Clock(java.time.Clock) Optional(java.util.Optional) Span(io.opentracing.Span) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span)

Aggregations

SpanContext (io.opentracing.SpanContext)118 Span (io.opentracing.Span)81 Future (io.vertx.core.Future)70 Tracer (io.opentracing.Tracer)60 Objects (java.util.Objects)57 HttpURLConnection (java.net.HttpURLConnection)56 JsonObject (io.vertx.core.json.JsonObject)55 TracingHelper (org.eclipse.hono.tracing.TracingHelper)55 Logger (org.slf4j.Logger)54 LoggerFactory (org.slf4j.LoggerFactory)54 List (java.util.List)51 Promise (io.vertx.core.Promise)45 Optional (java.util.Optional)45 Map (java.util.Map)40 ClientErrorException (org.eclipse.hono.client.ClientErrorException)39 MessageHelper (org.eclipse.hono.util.MessageHelper)33 UUID (java.util.UUID)31 Collectors (java.util.stream.Collectors)31 HashMap (java.util.HashMap)25 Vertx (io.vertx.core.Vertx)24