Skip to content

Commit

Permalink
feat: use new Config injection mechanism everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Nov 18, 2024
1 parent 1209257 commit b5a9a79
Show file tree
Hide file tree
Showing 83 changed files with 621 additions and 791 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class BootServicesExtension implements ServiceExtension {

public static final String NAME = "Boot Services";

@Setting(value = "Configures the participant id this runtime is operating on behalf of")
@Setting(description = "Configures the participant id this runtime is operating on behalf of")
public static final String PARTICIPANT_ID = "edc.participant.id";

@Setting(value = "Configures the runtime id. This should be fully or partly randomized, and need not be stable across restarts. It is recommended to leave this value blank.", defaultValue = "<random UUID>")
@Setting(description = "Configures the runtime id. This should be fully or partly randomized, and need not be stable across restarts. It is recommended to leave this value blank.", defaultValue = "<random UUID>")
public static final String RUNTIME_ID = "edc.runtime.id";

@Setting(value = "Configures this component's ID. This should be a unique, stable and deterministic identifier.", defaultValue = "<random UUID>")
@Setting(description = "Configures this component's ID. This should be a unique, stable and deterministic identifier.", defaultValue = "<random UUID>")
public static final String COMPONENT_ID = "edc.component.id";

private HealthCheckServiceImpl healthCheckService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Object resolve(ServiceExtensionContext context, DefaultServiceSupplier de

} else { // all other classes MUST have a default constructor.
try {
var pojoClass = Class.forName(configurationObject.getType().getName());
var pojoClass = configurationObject.getType();
var defaultCtor = pojoClass.getDeclaredConstructor();
defaultCtor.setAccessible(true);
var instance = defaultCtor.newInstance();
Expand All @@ -145,8 +145,7 @@ public Object resolve(ServiceExtensionContext context, DefaultServiceSupplier de
return instance;
} catch (NoSuchMethodException e) {
throw new EdcInjectionException("Configuration objects must declare a default constructor, but '%s' does not.".formatted(configurationObject.getType()));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new EdcInjectionException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class InjectionPointDefaultServiceSupplier implements DefaultServiceSuppl
public @Nullable Object provideFor(InjectionPoint<?> injectionPoint, ServiceExtensionContext context) {
var defaultService = injectionPoint.getDefaultValueProvider();
if (injectionPoint.isRequired() && defaultService == null) {
throw new EdcInjectionException("No default provider for required service " + injectionPoint.getType());
throw new EdcInjectionException("No default provider for required injection point " + injectionPoint);
}
return ofNullable(defaultService).map(vp -> vp.get(context)).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,19 @@ public String toString() {

private Object parseEntry(String string, Class<?> valueType) {
try {
if (valueType == Long.class) {
if (valueType == Long.class || valueType == long.class) {
return Long.parseLong(string);
}
if (valueType == Integer.class) {
if (valueType == Integer.class || valueType == int.class) {
return Integer.parseInt(string);
}
if (valueType == Double.class) {
if (valueType == Double.class || valueType == double.class) {
return Double.parseDouble(string);
}
} catch (NumberFormatException e) {
throw new EdcInjectionException("Config field '%s' is of type '%s', but the value resolved from key '%s' is \"%s\" which cannot be interpreted as %s.".formatted(targetField.getName(), valueType, annotationValue.key(), string, valueType));
}
if (valueType == Boolean.class) {
if (valueType == Boolean.class || valueType == boolean.class) {
return Boolean.parseBoolean(string);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.eclipse.edc.http.client.EdcHttpClientImpl;
import org.eclipse.edc.http.spi.EdcHttpClient;
import org.eclipse.edc.participant.spi.ParticipantIdMapper;
import org.eclipse.edc.runtime.metamodel.annotation.Configuration;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
Expand All @@ -49,52 +49,16 @@ public class CoreDefaultServicesExtension implements ServiceExtension {

public static final String NAME = "Core Default Services";

private static final int DEFAULT_RETRY_POLICY_MAX_RETRIES = 5;
private static final int DEFAULT_RETRY_POLICY_BACKOFF_MIN_MILLIS = 500;
private static final int DEFAULT_RETRY_POLICY_BACKOFF_MAX_MILLIS = 10000;
private static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRY = false;
private static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRY_SCHEDULED = false;
private static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED = false;
private static final boolean DEFAULT_RETRY_POLICY_LOG_ON_FAILED_ATTEMPT = false;
private static final boolean DEFAULT_RETRY_POLICY_LOG_ON_ABORT = false;
private static final int DEFAULT_OK_HTTP_CLIENT_TIMEOUT_CONNECT = 30;
private static final int DEFAULT_OK_HTTP_CLIENT_TIMEOUT_READ = 30;
private static final boolean DEFAULT_OK_HTTP_CLIENT_HTTPS_ENFORCE = false;
private static final int DEFAULT_OK_HTTP_CLIENT_SEND_BUFFER_SIZE = 0;
private static final int DEFAULT_OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE = 0;

@Setting(value = "RetryPolicy: Maximum retries before a failure is propagated", defaultValue = DEFAULT_RETRY_POLICY_MAX_RETRIES + "", type = "int")
private static final String RETRY_POLICY_MAX_RETRIES = "edc.core.retry.retries.max";
@Setting(value = "RetryPolicy: Minimum number of milliseconds for exponential backoff", defaultValue = DEFAULT_RETRY_POLICY_BACKOFF_MIN_MILLIS + "", type = "int")
private static final String RETRY_POLICY_BACKOFF_MIN_MILLIS = "edc.core.retry.backoff.min";
@Setting(value = "RetryPolicy: Maximum number of milliseconds for exponential backoff", defaultValue = DEFAULT_RETRY_POLICY_BACKOFF_MAX_MILLIS + "", type = "int")
private static final String RETRY_POLICY_BACKOFF_MAX_MILLIS = "edc.core.retry.backoff.max";
@Setting(value = "RetryPolicy: Log onRetry events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRY + "", type = "boolean")
private static final String RETRY_POLICY_LOG_ON_RETRY = "edc.core.retry.log.on.retry";
@Setting(value = "RetryPolicy: Log onRetryScheduled events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRY_SCHEDULED + "", type = "boolean")
private static final String RETRY_POLICY_LOG_ON_RETRY_SCHEDULED = "edc.core.retry.log.on.retry.scheduled";
@Setting(value = "RetryPolicy: Log onRetriesExceeded events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED + "", type = "boolean")
private static final String RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED = "edc.core.retry.log.on.retries.exceeded";
@Setting(value = "RetryPolicy: Log onFailedAttempt events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_FAILED_ATTEMPT + "", type = "boolean")
private static final String RETRY_POLICY_LOG_ON_FAILED_ATTEMPT = "edc.core.retry.log.on.failed.attempt";
@Setting(value = "RetryPolicy: Log onAbort events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_ABORT + "", type = "boolean")
private static final String RETRY_POLICY_LOG_ON_ABORT = "edc.core.retry.log.on.abort";
@Setting(value = "OkHttpClient: If true, enable HTTPS call enforcement", defaultValue = DEFAULT_OK_HTTP_CLIENT_HTTPS_ENFORCE + "", type = "boolean")
private static final String OK_HTTP_CLIENT_HTTPS_ENFORCE = "edc.http.client.https.enforce";
@Setting(value = "OkHttpClient: connect timeout, in seconds", defaultValue = DEFAULT_OK_HTTP_CLIENT_TIMEOUT_CONNECT + "", type = "int")
private static final String OK_HTTP_CLIENT_TIMEOUT_CONNECT = "edc.http.client.timeout.connect";
@Setting(value = "OkHttpClient: read timeout, in seconds", defaultValue = DEFAULT_OK_HTTP_CLIENT_TIMEOUT_READ + "", type = "int")
private static final String OK_HTTP_CLIENT_TIMEOUT_READ = "edc.http.client.timeout.read";
@Setting(value = "OkHttpClient: send buffer size, in bytes", defaultValue = DEFAULT_OK_HTTP_CLIENT_SEND_BUFFER_SIZE + "", type = "int", min = 1)
private static final String OK_HTTP_CLIENT_SEND_BUFFER_SIZE = "edc.http.client.send.buffer.size";
@Setting(value = "OkHttpClient: receive buffer size, in bytes", defaultValue = DEFAULT_OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE + "", type = "int", min = 1)
private static final String OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE = "edc.http.client.receive.buffer.size";

/**
* An optional OkHttp {@link EventListener} that can be used to instrument OkHttp client for collecting metrics.
*/
@Inject(required = false)
private EventListener okHttpEventListener;
@Configuration
private OkHttpClientConfiguration configuration;
@Configuration
private RetryPolicyConfiguration retryPolicyConfiguration;

@Override
public String name() {
Expand Down Expand Up @@ -134,31 +98,13 @@ public ControlClientAuthenticationProvider controlClientAuthenticationProvider()

@Provider
public OkHttpClient okHttpClient(ServiceExtensionContext context) {
var configuration = OkHttpClientConfiguration.Builder.newInstance()
.enforceHttps(context.getSetting(OK_HTTP_CLIENT_HTTPS_ENFORCE, DEFAULT_OK_HTTP_CLIENT_HTTPS_ENFORCE))
.connectTimeout(context.getSetting(OK_HTTP_CLIENT_TIMEOUT_CONNECT, DEFAULT_OK_HTTP_CLIENT_TIMEOUT_CONNECT))
.readTimeout(context.getSetting(OK_HTTP_CLIENT_TIMEOUT_READ, DEFAULT_OK_HTTP_CLIENT_TIMEOUT_READ))
.sendBufferSize(context.getSetting(OK_HTTP_CLIENT_SEND_BUFFER_SIZE, DEFAULT_OK_HTTP_CLIENT_SEND_BUFFER_SIZE))
.receiveBufferSize(context.getSetting(OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE, DEFAULT_OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE))
.build();

return OkHttpClientFactory.create(configuration, okHttpEventListener, context.getMonitor());
}

@Provider
public <T> RetryPolicy<T> retryPolicy(ServiceExtensionContext context) {
var configuration = RetryPolicyConfiguration.Builder.newInstance()
.maxRetries(context.getSetting(RETRY_POLICY_MAX_RETRIES, DEFAULT_RETRY_POLICY_MAX_RETRIES))
.minBackoff(context.getSetting(RETRY_POLICY_BACKOFF_MIN_MILLIS, DEFAULT_RETRY_POLICY_BACKOFF_MIN_MILLIS))
.maxBackoff(context.getSetting(RETRY_POLICY_BACKOFF_MAX_MILLIS, DEFAULT_RETRY_POLICY_BACKOFF_MAX_MILLIS))
.logOnRetry(context.getSetting(RETRY_POLICY_LOG_ON_RETRY, DEFAULT_RETRY_POLICY_LOG_ON_RETRY))
.logOnRetryScheduled(context.getSetting(RETRY_POLICY_LOG_ON_RETRY_SCHEDULED, DEFAULT_RETRY_POLICY_LOG_ON_RETRY_SCHEDULED))
.logOnRetriesExceeded(context.getSetting(RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED, DEFAULT_RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED))
.logOnFailedAttempt(context.getSetting(RETRY_POLICY_LOG_ON_FAILED_ATTEMPT, DEFAULT_RETRY_POLICY_LOG_ON_FAILED_ATTEMPT))
.logOnAbort(context.getSetting(RETRY_POLICY_LOG_ON_ABORT, DEFAULT_RETRY_POLICY_LOG_ON_ABORT))
.build();

return RetryPolicyFactory.create(configuration, context.getMonitor());

return RetryPolicyFactory.create(retryPolicyConfiguration, context.getMonitor());
}

@Provider(isDefault = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,30 @@

package org.eclipse.edc.connector.core.base;

import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.runtime.metamodel.annotation.Settings;

@Settings
public class OkHttpClientConfiguration {
public static final boolean DEFAULT_OK_HTTP_CLIENT_HTTPS_ENFORCE = false;
public static final int DEFAULT_OK_HTTP_CLIENT_TIMEOUT_CONNECT = 30;
public static final int DEFAULT_OK_HTTP_CLIENT_TIMEOUT_READ = 30;
public static final int DEFAULT_OK_HTTP_CLIENT_SEND_BUFFER_SIZE = 0;
public static final int DEFAULT_OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE = 0;

@Setting(description = "OkHttpClient: If true, enable HTTPS call enforcement", defaultValue = DEFAULT_OK_HTTP_CLIENT_HTTPS_ENFORCE + "", key = "edc.http.client.https.enforce")
private boolean enforceHttps;

@Setting(description = "OkHttpClient: connect timeout, in seconds", defaultValue = DEFAULT_OK_HTTP_CLIENT_TIMEOUT_CONNECT + "", key = "edc.http.client.timeout.connect")
private int connectTimeout;
@Setting(description = "OkHttpClient: read timeout, in seconds", defaultValue = DEFAULT_OK_HTTP_CLIENT_TIMEOUT_READ + "", key = "edc.http.client.timeout.read")
private int readTimeout;
@Setting(description = "OkHttpClient: send buffer size, in bytes", defaultValue = DEFAULT_OK_HTTP_CLIENT_SEND_BUFFER_SIZE + "", key = "edc.http.client.send.buffer.size", min = 1)
private int sendBufferSize;
@Setting(description = "OkHttpClient: receive buffer size, in bytes", defaultValue = DEFAULT_OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE + "", key = "edc.http.client.receive.buffer.size", min = 1)
private int receiveBufferSize;

private OkHttpClientConfiguration() {
public OkHttpClientConfiguration() {
}

public boolean isEnforceHttps() {
Expand All @@ -45,15 +60,20 @@ public int getReceiveBufferSize() {
return receiveBufferSize;
}

public Builder toBuilder() {
return new Builder(this);
}

public static class Builder {

private final OkHttpClientConfiguration instance = new OkHttpClientConfiguration();
private final OkHttpClientConfiguration instance;

public static Builder newInstance() {
return new Builder();
private Builder(OkHttpClientConfiguration okHttpClientConfiguration) {
this.instance = okHttpClientConfiguration;
}

private Builder() {
public static Builder newInstance() {
return new Builder(new OkHttpClientConfiguration());
}

public Builder enforceHttps(boolean enforceHttps) {
Expand Down Expand Up @@ -85,4 +105,5 @@ public OkHttpClientConfiguration build() {
return instance;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,38 @@

package org.eclipse.edc.connector.core.base;

public class RetryPolicyConfiguration {
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.runtime.metamodel.annotation.Settings;

@Settings
public class RetryPolicyConfiguration {
public static final int DEFAULT_RETRY_POLICY_MAX_RETRIES = 5;
public static final int DEFAULT_RETRY_POLICY_BACKOFF_MIN_MILLIS = 500;
public static final int DEFAULT_RETRY_POLICY_BACKOFF_MAX_MILLIS = 10000;
public static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRY = false;
public static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRY_SCHEDULED = false;
public static final boolean DEFAULT_RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED = false;
public static final boolean DEFAULT_RETRY_POLICY_LOG_ON_FAILED_ATTEMPT = false;
public static final boolean DEFAULT_RETRY_POLICY_LOG_ON_ABORT = false;

@Setting(description = "RetryPolicy: Maximum retries before a failure is propagated", defaultValue = DEFAULT_RETRY_POLICY_MAX_RETRIES + "", key = "edc.core.retry.retries.max")
private int maxRetries;
@Setting(description = "RetryPolicy: Minimum number of milliseconds for exponential backoff", defaultValue = DEFAULT_RETRY_POLICY_BACKOFF_MIN_MILLIS + "", key = "edc.core.retry.backoff.min")
private int minBackoff = 1;
@Setting(description = "RetryPolicy: Maximum number of milliseconds for exponential backoff", defaultValue = DEFAULT_RETRY_POLICY_BACKOFF_MAX_MILLIS + "", key = "edc.core.retry.backoff.max")
private int maxBackoff = Integer.MAX_VALUE;
@Setting(description = "RetryPolicy: Log onRetry events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRY + "", key = "edc.core.retry.log.on.retry")
private boolean logOnRetry;
@Setting(description = "RetryPolicy: Log onRetryScheduled events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRY_SCHEDULED + "", key = "edc.core.retry.log.on.retry.scheduled")
private boolean logOnRetryScheduled;
@Setting(description = "RetryPolicy: Log onRetriesExceeded events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED + "", key = "edc.core.retry.log.on.retries.exceeded")
private boolean logOnRetriesExceeded;
@Setting(description = "RetryPolicy: Log onFailedAttempt events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_FAILED_ATTEMPT + "", key = "edc.core.retry.log.on.failed.attempt")
private boolean logOnFailedAttempt;
@Setting(description = "RetryPolicy: Log onAbort events", defaultValue = DEFAULT_RETRY_POLICY_LOG_ON_ABORT + "", key = "edc.core.retry.log.on.abort")
private boolean logOnAbort;

private RetryPolicyConfiguration() {
public RetryPolicyConfiguration() {
}

public int getMaxRetries() {
Expand Down Expand Up @@ -60,15 +80,20 @@ public boolean isLogOnAbort() {
return logOnAbort;
}

public Builder toBuilder() {
return new Builder(this);
}

public static class Builder {

private final RetryPolicyConfiguration instance = new RetryPolicyConfiguration();
private final RetryPolicyConfiguration instance;

public static Builder newInstance() {
return new Builder();
private Builder(RetryPolicyConfiguration instance) {
this.instance = instance;
}

private Builder() {
public static Builder newInstance() {
return new Builder(new RetryPolicyConfiguration());
}

public Builder maxRetries(int maxRetries) {
Expand Down
Loading

0 comments on commit b5a9a79

Please sign in to comment.