From eb2a7c97c0007b013bd1784ac17d57be02b63d03 Mon Sep 17 00:00:00 2001 From: Michael DÜrre Date: Thu, 10 Mar 2022 10:39:16 +0100 Subject: migrate sdnr features to phosphorus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix mapper and switch to dom api Updated to use phosphorus version of parent poms Issue-ID: CCSDK-3566 Signed-off-by: Michael DÜrre Change-Id: I98c5bef9286622e0d66b53db687557d798cd53f5 Signed-off-by: Michael DÜrre --- sdnr/wt/common/pom.xml | 2 +- .../common/threading/GenericRunnableFactory.java | 32 ++++++++ .../threading/GenericRunnableFactoryCallback.java | 27 +++++++ .../wt/common/threading/KeyBasedThreadpool.java | 93 ++++++++++++++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactory.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactoryCallback.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/KeyBasedThreadpool.java (limited to 'sdnr/wt/common') diff --git a/sdnr/wt/common/pom.xml b/sdnr/wt/common/pom.xml index 762a3fbb8..1847e6f11 100644 --- a/sdnr/wt/common/pom.xml +++ b/sdnr/wt/common/pom.xml @@ -28,7 +28,7 @@ org.onap.ccsdk.parent binding-parent - 2.3.2 + 2.3.3-SNAPSHOT diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactory.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactory.java new file mode 100644 index 000000000..c163facbb --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactory.java @@ -0,0 +1,32 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.common.threading; + +public abstract class GenericRunnableFactory { + + public GenericRunnableFactory() { + } + + public Runnable create(S arg, GenericRunnableFactoryCallback callback) { + return null; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactoryCallback.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactoryCallback.java new file mode 100644 index 000000000..bd0ce1eec --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/GenericRunnableFactoryCallback.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.common.threading; + +public interface GenericRunnableFactoryCallback { + + void onFinish(T key); +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/KeyBasedThreadpool.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/KeyBasedThreadpool.java new file mode 100644 index 000000000..f41a9038f --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/threading/KeyBasedThreadpool.java @@ -0,0 +1,93 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.common.threading; + +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Threadpool for running n instances per key T + * + * @author jack + * + * @param + * @param + */ +public class KeyBasedThreadpool implements GenericRunnableFactoryCallback { + + private final Queue> queue; + private final List runningKeys; + private final int keyPoolSize; + private final GenericRunnableFactory factory; + private final ExecutorService executor; + + /** + * + * @param poolSize overall maximum amount of threads + * @param keyPoolSize amount of threads per key + * @param runner runnable to start + */ + public KeyBasedThreadpool(int poolSize, int keyPoolSize, GenericRunnableFactory factory) { + this.queue = new ConcurrentLinkedQueue<>(); + this.keyPoolSize = keyPoolSize; + this.factory = factory; + this.executor = Executors.newFixedThreadPool(poolSize); + this.runningKeys = new ArrayList<>(); + } + + public void execute(T key, S arg) { + if (this.isKeyPoolSizeReached(key)) { + queue.add(new SimpleEntry<>(key, arg)); + } else { + this.runningKeys.add(key); + this.executor.execute(this.factory.create(arg, this)); + } + + } + + private void executeNext() { + Entry entry = this.queue.peek(); + if (!this.isKeyPoolSizeReached(entry.getKey())) { + this.queue.poll(); + this.runningKeys.add(entry.getKey()); + this.executor.execute(this.factory.create(entry.getValue(), this)); + } + } + + private boolean isKeyPoolSizeReached(T key) { + return this.runningKeys.stream().filter(e -> e == key).count() >= this.keyPoolSize; + } + + @Override + public void onFinish(T key) { + this.runningKeys.remove(key); + this.executeNext(); + } + + +} -- cgit 1.2.3-korg