From e1e2a7df4543689e486342870a97959e0dee0256 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 9 Feb 2023 17:22:10 +0100 Subject: [PATCH] Make DeviceTransactionManagerImpl configurable Add an @ObjectClassDefinition and @Designate the configuration to use. This the manager to be configured at runtime. It will be restarted whenever the configuration changes. Change-Id: I1fdd40c9fe11c3204af3794a3d0220b653973427 Signed-off-by: Robert Varga --- common/pom.xml | 4 ++ .../device/DeviceTransactionManagerImpl.java | 50 +++++++++++++------ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/common/pom.xml b/common/pom.xml index 5bb0e995d..9d961519c 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -160,6 +160,10 @@ org.osgi org.osgi.service.component.annotations + + org.osgi + org.osgi.service.metatype.annotations + diff --git a/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransactionManagerImpl.java b/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransactionManagerImpl.java index 1e92c51c6..7a49866f4 100644 --- a/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransactionManagerImpl.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/device/DeviceTransactionManagerImpl.java @@ -39,46 +39,66 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component +@Designate(ocd = DeviceTransactionManagerImpl.Configuration.class) public final class DeviceTransactionManagerImpl implements DeviceTransactionManager { + @ObjectClassDefinition + public @interface Configuration { + @AttributeDefinition(description = "Minimum number of threads in the checking pool", min = "0") + int checkingMinThreads() default DEFAULT_CHECKING_MIN_THREADS; + @AttributeDefinition(description = "Number of threads in the listening pool", min = "1") + int listeningThreads() default DEFAULT_LISTENING_THREADS; + @AttributeDefinition(description = "Maximum time to wait for transaction submit, in milliseconds", min = "0") + long maxDurationToSubmit() default DEFAULT_MAX_DURATION_TO_SUBMIT; + @AttributeDefinition(description = "Maximum time to wait for get-data submit, in milliseconds", min = "0") + long maxDurationToGetData() default DEFAULT_MAX_DURATION_TO_GET_DATA; + } // TODO cache device data brokers // TODO remove disconnected devices from maps private static final Logger LOG = LoggerFactory.getLogger(DeviceTransactionManagerImpl.class); - // TODO add an @ObjectClassDefinition to make these configurable at runtime - private static final int NUMBER_OF_THREADS = 4; - private static final long GET_DATA_SUBMIT_TIMEOUT = 3000; - private static final TimeUnit GET_DATA_SUBMIT_TIME_UNIT = TimeUnit.MILLISECONDS; - // TODO set reasonable value here for maxDurationToSubmitTransaction - private static final long MAX_DURATION_TO_SUBMIT = 15000; - private static final TimeUnit MAX_DURATION_TO_SUBMIT_TIMEUNIT = TimeUnit.MILLISECONDS; + private static final long DEFAULT_MAX_DURATION_TO_GET_DATA = 3000; + private static final long DEFAULT_MAX_DURATION_TO_SUBMIT = 15000; + private static final int DEFAULT_CHECKING_MIN_THREADS = 4; + private static final int DEFAULT_LISTENING_THREADS = 4; private final MountPointService mountPointService; private final ScheduledExecutorService checkingExecutor; private final ListeningExecutorService listeningExecutor; private final ConcurrentMap deviceLocks = new ConcurrentHashMap<>(); private final long maxDurationToSubmitTransaction; + private final long maxDurationToGetData; @Activate - public DeviceTransactionManagerImpl(@Reference MountPointService mountPointService) { - this(mountPointService, MAX_DURATION_TO_SUBMIT); + public DeviceTransactionManagerImpl(@Reference MountPointService mountPointService, Configuration configuration) { + this(mountPointService, configuration.maxDurationToSubmit(), configuration.maxDurationToGetData(), + configuration.checkingMinThreads(), configuration.listeningThreads()); + } + + public DeviceTransactionManagerImpl(MountPointService mountPointService, long maxDurationToSubmitTransaction) { + this(mountPointService, maxDurationToSubmitTransaction, DEFAULT_MAX_DURATION_TO_GET_DATA, + DEFAULT_CHECKING_MIN_THREADS, DEFAULT_LISTENING_THREADS); } - public DeviceTransactionManagerImpl(MountPointService mountPointService, - long maxDurationToSubmitTransaction) { + public DeviceTransactionManagerImpl(MountPointService mountPointService, long maxDurationToSubmitTransaction, + long maxDurationToGetData, int checkingPoolMinThreads, int listeningPoolThreads) { this.mountPointService = requireNonNull(mountPointService); this.maxDurationToSubmitTransaction = maxDurationToSubmitTransaction; - this.checkingExecutor = Executors.newScheduledThreadPool(NUMBER_OF_THREADS); - this.listeningExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS)); + this.maxDurationToGetData = maxDurationToGetData; + this.checkingExecutor = Executors.newScheduledThreadPool(checkingPoolMinThreads); + this.listeningExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(listeningPoolThreads)); } @Override public Future> getDeviceTransaction(String deviceId) { - return getDeviceTransaction(deviceId, maxDurationToSubmitTransaction, MAX_DURATION_TO_SUBMIT_TIMEUNIT); + return getDeviceTransaction(deviceId, maxDurationToSubmitTransaction, TimeUnit.MILLISECONDS); } @Override @@ -175,7 +195,7 @@ public final class DeviceTransactionManagerImpl implements DeviceTransactionMana } catch (InterruptedException | ExecutionException | TimeoutException e) { LOG.error("Exception thrown while reading data from device {}! IID: {}", deviceId, path, e); } finally { - deviceTx.commit(GET_DATA_SUBMIT_TIMEOUT, GET_DATA_SUBMIT_TIME_UNIT); + deviceTx.commit(maxDurationToGetData, TimeUnit.MILLISECONDS); } } else { LOG.error("Could not obtain transaction for device {}!", deviceId); -- 2.36.6