From 593b4db0e12137995d5dc2e2a1fa984feca5ed2d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 2 Mar 2023 02:00:14 +0100 Subject: [PATCH] Remove most of io.atomix.utils.concurrent Most of the stuff in the concurrent package is not used, remove it. JIRA: CONTROLLER-2071 Change-Id: Ife0ea74b9327075dac9dfb605364bf2a9301362f Signed-off-by: Robert Varga --- .../atomix/utils/concurrent/AtomixThread.java | 56 ----------- .../utils/concurrent/AtomixThreadFactory.java | 30 ------ .../utils/concurrent/ComposableFuture.java | 82 ---------------- .../utils/concurrent/NullThreadContext.java | 58 ----------- .../utils/concurrent/OrderedExecutor.java | 60 ------------ .../io/atomix/utils/concurrent/Retries.java | 96 ------------------ .../utils/concurrent/RetryingFunction.java | 62 ------------ .../io/atomix/utils/concurrent/Scheduled.java | 31 ------ .../io/atomix/utils/concurrent/Scheduler.java | 70 ------------- .../utils/concurrent/ThreadContext.java | 97 ------------------- .../concurrent/ThreadContextFactory.java | 36 ------- .../io/atomix/utils/concurrent/Threads.java | 42 -------- .../concurrent/RetryingFunctionTest.java | 92 ------------------ 13 files changed, 812 deletions(-) delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThread.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThreadFactory.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ComposableFuture.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/NullThreadContext.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/OrderedExecutor.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Retries.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/RetryingFunction.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduled.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduler.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContext.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContextFactory.java delete mode 100644 third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Threads.java delete mode 100644 third-party/atomix/utils/src/test/java/io/atomix/utils/concurrent/RetryingFunctionTest.java diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThread.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThread.java deleted file mode 100644 index 21f47f22d1..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThread.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.lang.ref.WeakReference; - -/** - * Atomix thread. - *

- * The Atomix thread primarily serves to store a {@link ThreadContext} for the current thread. - * The context is stored in a {@link WeakReference} in order to allow the thread to be garbage collected. - *

- * There is no {@link ThreadContext} associated with the thread when it is first created. - * It is the responsibility of thread creators to {@link #setContext(ThreadContext) set} the thread context when appropriate. - * - * @author Jordan Halterman - */ -public class AtomixThread extends Thread { - private WeakReference context; - - public AtomixThread(Runnable target) { - super(target); - } - - /** - * Sets the thread context. - * - * @param context The thread context. - */ - public void setContext(ThreadContext context) { - this.context = new WeakReference<>(context); - } - - /** - * Returns the thread context. - * - * @return The thread {@link ThreadContext} or {@code null} if no context has been configured. - */ - public ThreadContext getContext() { - return context != null ? context.get() : null; - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThreadFactory.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThreadFactory.java deleted file mode 100644 index eda5a21740..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/AtomixThreadFactory.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.concurrent.ThreadFactory; - -/** - * Named thread factory. - * - * @author Jordan Halterman - */ -public class AtomixThreadFactory implements ThreadFactory { - @Override - public Thread newThread(Runnable r) { - return new AtomixThread(r); - } -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ComposableFuture.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ComposableFuture.java deleted file mode 100644 index f789018ccb..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ComposableFuture.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; -import java.util.function.BiConsumer; -import java.util.function.Consumer; - -/** - * Special implementation of {@link CompletableFuture} with missing utility methods. - * - * @author Jordan Halterman - */ -public class ComposableFuture extends CompletableFuture implements BiConsumer { - - @Override - public void accept(T result, Throwable error) { - if (error == null) { - complete(result); - } else { - completeExceptionally(error); - } - } - - /** - * Sets a consumer to be called when the future is failed. - * - * @param consumer The consumer to call. - * @return A new future. - */ - public CompletableFuture except(Consumer consumer) { - return whenComplete((result, error) -> { - if (error != null) { - consumer.accept(error); - } - }); - } - - /** - * Sets a consumer to be called asynchronously when the future is failed. - * - * @param consumer The consumer to call. - * @return A new future. - */ - public CompletableFuture exceptAsync(Consumer consumer) { - return whenCompleteAsync((result, error) -> { - if (error != null) { - consumer.accept(error); - } - }); - } - - /** - * Sets a consumer to be called asynchronously when the future is failed. - * - * @param consumer The consumer to call. - * @param executor The executor with which to call the consumer. - * @return A new future. - */ - public CompletableFuture exceptAsync(Consumer consumer, Executor executor) { - return whenCompleteAsync((result, error) -> { - if (error != null) { - consumer.accept(error); - } - }, executor); - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/NullThreadContext.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/NullThreadContext.java deleted file mode 100644 index 9ee1d75227..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/NullThreadContext.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2018-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.time.Duration; - -/** - * Null thread context. - */ -public class NullThreadContext implements ThreadContext { - @Override - public Scheduled schedule(Duration delay, Runnable callback) { - return null; - } - - @Override - public Scheduled schedule(Duration initialDelay, Duration interval, Runnable callback) { - return null; - } - - @Override - public boolean isBlocked() { - return false; - } - - @Override - public void block() { - - } - - @Override - public void unblock() { - - } - - @Override - public void close() { - - } - - @Override - public void execute(Runnable command) { - - } -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/OrderedExecutor.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/OrderedExecutor.java deleted file mode 100644 index c3cee5a5be..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/OrderedExecutor.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.LinkedList; -import java.util.concurrent.Executor; - -/** - * Executor that executes tasks in order on a shared thread pool. - *

- * The ordered executor behaves semantically like a single-threaded executor, but multiplexes tasks on a shared thread - * pool, ensuring blocked threads in the shared thread pool don't block individual ordered executors. - */ -public class OrderedExecutor implements Executor { - private final Executor parent; - private final LinkedList tasks = new LinkedList<>(); - private boolean running; - - public OrderedExecutor(Executor parent) { - this.parent = parent; - } - - private void run() { - for (;;) { - final Runnable task; - synchronized (tasks) { - task = tasks.poll(); - if (task == null) { - running = false; - return; - } - } - task.run(); - } - } - - @Override - public void execute(Runnable command) { - synchronized (tasks) { - tasks.add(command); - if (!running) { - running = true; - parent.execute(this::run); - } - } - } -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Retries.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Retries.java deleted file mode 100644 index bd42a2b9fa..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Retries.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.concurrent.ThreadLocalRandom; -import java.util.function.Function; -import java.util.function.Supplier; - -/** - * Retry utilities. - */ -public final class Retries { - - /** - * Returns a function that retries execution on failure. - * @param base base function - * @param exceptionClass type of exception for which to retry - * @param maxRetries max number of retries before giving up - * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from - * the interval (0, maxDelayBetweenRetries] - * @return function - * @param type of function input - * @param type of function output - */ - public static Function retryable(Function base, - Class exceptionClass, - int maxRetries, - int maxDelayBetweenRetries) { - return new RetryingFunction<>(base, exceptionClass, maxRetries, maxDelayBetweenRetries); - } - - /** - * Returns a Supplier that retries execution on failure. - * @param base base supplier - * @param exceptionClass type of exception for which to retry - * @param maxRetries max number of retries before giving up - * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from - * the interval (0, maxDelayBetweenRetries] - * @return supplier - * @param type of supplied result - */ - public static Supplier retryable(Supplier base, - Class exceptionClass, - int maxRetries, - int maxDelayBetweenRetries) { - return () -> new RetryingFunction<>(v -> base.get(), - exceptionClass, - maxRetries, - maxDelayBetweenRetries).apply(null); - } - - /** - * Suspends the current thread for a random number of millis between 0 and - * the indicated limit. - * - * @param ms max number of millis - */ - public static void randomDelay(int ms) { - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(ms)); - } catch (InterruptedException e) { - throw new RuntimeException("Interrupted", e); - } - } - - /** - * Suspends the current thread for a specified number of millis and nanos. - * - * @param ms number of millis - * @param nanos number of nanos - */ - public static void delay(int ms, int nanos) { - try { - Thread.sleep(ms, nanos); - } catch (InterruptedException e) { - throw new RuntimeException("Interrupted", e); - } - } - - private Retries() { - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/RetryingFunction.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/RetryingFunction.java deleted file mode 100644 index 9ef40f4504..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/RetryingFunction.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.function.Function; - -import static com.google.common.base.Throwables.throwIfUnchecked; - -/** - * Function that retries execution on failure. - * - * @param input type - * @param output type - */ -public class RetryingFunction implements Function { - private final Function baseFunction; - private final Class exceptionClass; - private final int maxRetries; - private final int maxDelayBetweenRetries; - - public RetryingFunction(Function baseFunction, - Class exceptionClass, - int maxRetries, - int maxDelayBetweenRetries) { - this.baseFunction = baseFunction; - this.exceptionClass = exceptionClass; - this.maxRetries = maxRetries; - this.maxDelayBetweenRetries = maxDelayBetweenRetries; - } - - @SuppressWarnings("squid:S1181") - // Yes we really do want to catch Throwable - @Override - public V apply(U input) { - int retryAttempts = 0; - while (true) { - try { - return baseFunction.apply(input); - } catch (Throwable t) { - if (!exceptionClass.isAssignableFrom(t.getClass()) || retryAttempts == maxRetries) { - throwIfUnchecked(t); - throw new RuntimeException(t); - } - Retries.randomDelay(maxDelayBetweenRetries); - retryAttempts++; - } - } - } -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduled.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduled.java deleted file mode 100644 index 02bfd29e36..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduled.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ - -package io.atomix.utils.concurrent; - -/** - * Scheduled task. - * - * @author Jordan Halterman - */ -public interface Scheduled { - - /** - * Cancels the scheduled task. - */ - void cancel(); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduler.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduler.java deleted file mode 100644 index f581f3528e..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Scheduler.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.time.Duration; -import java.util.concurrent.TimeUnit; - -/** - * Scheduler. - */ -public interface Scheduler { - - /** - * Schedules a runnable after a delay. - * - * @param delay the delay after which to run the callback - * @param timeUnit the time unit - * @param callback the callback to run - * @return the scheduled callback - */ - default Scheduled schedule(long delay, TimeUnit timeUnit, Runnable callback) { - return schedule(Duration.ofMillis(timeUnit.toMillis(delay)), callback); - } - - /** - * Schedules a runnable after a delay. - * - * @param delay the delay after which to run the callback - * @param callback the callback to run - * @return the scheduled callback - */ - Scheduled schedule(Duration delay, Runnable callback); - - /** - * Schedules a runnable at a fixed rate. - * - * @param initialDelay the initial delay - * @param interval the interval at which to run the callback - * @param timeUnit the time unit - * @param callback the callback to run - * @return the scheduled callback - */ - default Scheduled schedule(long initialDelay, long interval, TimeUnit timeUnit, Runnable callback) { - return schedule(Duration.ofMillis(timeUnit.toMillis(initialDelay)), Duration.ofMillis(timeUnit.toMillis(interval)), callback); - } - - /** - * Schedules a runnable at a fixed rate. - * - * @param initialDelay the initial delay - * @param interval the interval at which to run the callback - * @param callback the callback to run - * @return the scheduled callback - */ - Scheduled schedule(Duration initialDelay, Duration interval, Runnable callback); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContext.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContext.java deleted file mode 100644 index 2cb4189229..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContext.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import java.util.concurrent.Executor; - -import static com.google.common.base.Preconditions.checkState; - -/** - * Thread context. - *

- * The thread context is used by Catalyst to determine the correct thread on which to execute asynchronous callbacks. - * All threads created within Catalyst must be instances of {@link AtomixThread}. Once - * a thread has been created, the context is stored in the thread object via - * {@link AtomixThread#setContext(ThreadContext)}. This means there is a one-to-one relationship - * between a context and a thread. That is, a context is representative of a thread and provides an interface for firing - * events on that thread. - *

- * Components of the framework that provide custom threads should use {@link AtomixThreadFactory} - * to allocate new threads and provide a custom {@link ThreadContext} implementation. - * - * @author Jordan Halterman - */ -public interface ThreadContext extends AutoCloseable, Executor, Scheduler { - - /** - * Returns the current thread context. - * - * @return The current thread context or {@code null} if no context exists. - */ - static ThreadContext currentContext() { - Thread thread = Thread.currentThread(); - return thread instanceof AtomixThread ? ((AtomixThread) thread).getContext() : null; - } - - /** - * @throws IllegalStateException if the current thread is not a catalyst thread - */ - static ThreadContext currentContextOrThrow() { - ThreadContext context = currentContext(); - checkState(context != null, "not on a Catalyst thread"); - return context; - } - - /** - * Returns a boolean indicating whether the current thread is in this context. - * - * @return Indicates whether the current thread is in this context. - */ - default boolean isCurrentContext() { - return currentContext() == this; - } - - /** - * Checks that the current thread is the correct context thread. - */ - default void checkThread() { - checkState(currentContext() == this, "not on a Catalyst thread"); - } - - /** - * Returns whether the thread context is currently marked blocked. - * - * @return whether the thread context is currently marked blocked - */ - boolean isBlocked(); - - /** - * Marks the thread context as blocked. - */ - void block(); - - /** - * Marks the thread context as unblocked. - */ - void unblock(); - - /** - * Closes the context. - */ - @Override - void close(); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContextFactory.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContextFactory.java deleted file mode 100644 index 09b625696d..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ThreadContextFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -/** - * Thread context factory. - */ -public interface ThreadContextFactory { - - /** - * Creates a new thread context. - * - * @return a new thread context - */ - ThreadContext createContext(); - - /** - * Closes the factory. - */ - default void close() { - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Threads.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Threads.java deleted file mode 100644 index 360ceeaf37..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/Threads.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.slf4j.Logger; - -import java.util.concurrent.ThreadFactory; - -/** - * Thread utilities. - */ -public final class Threads { - - /** - * Returns a thread factory that produces threads named according to the - * supplied name pattern. - * - * @param pattern name pattern - * @return thread factory - */ - public static ThreadFactory namedThreads(String pattern, Logger log) { - return new ThreadFactoryBuilder() - .setNameFormat(pattern) - .setThreadFactory(new AtomixThreadFactory()) - .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) - .build(); - } -} diff --git a/third-party/atomix/utils/src/test/java/io/atomix/utils/concurrent/RetryingFunctionTest.java b/third-party/atomix/utils/src/test/java/io/atomix/utils/concurrent/RetryingFunctionTest.java deleted file mode 100644 index 8d70cccdcd..0000000000 --- a/third-party/atomix/utils/src/test/java/io/atomix/utils/concurrent/RetryingFunctionTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ -package io.atomix.utils.concurrent; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -/** - * Retrying function test. - */ -public class RetryingFunctionTest { - private int round; - - @Before - public void setUp() { - round = 1; - } - - @After - public void tearDown() { - round = 0; - } - - @Test(expected = RetryableException.class) - public void testNoRetries() { - new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 0, 10).apply(null); - } - - @Test - public void testSuccessAfterOneRetry() { - new RetryingFunction<>(this::succeedAfterOneFailure, RetryableException.class, 1, 10).apply(null); - } - - @Test(expected = RetryableException.class) - public void testFailureAfterOneRetry() { - new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 1, 10).apply(null); - } - - @Test - public void testFailureAfterTwoRetries() { - new RetryingFunction<>(this::succeedAfterTwoFailures, RetryableException.class, 2, 10).apply(null); - } - - @Test(expected = NonRetryableException.class) - public void testFailureWithNonRetryableFailure() { - new RetryingFunction<>(this::failCompletely, RetryableException.class, 2, 10).apply(null); - } - - private String succeedAfterOneFailure(String input) { - if (round++ <= 1) { - throw new RetryableException(); - } else { - return "pass"; - } - } - - private String succeedAfterTwoFailures(String input) { - if (round++ <= 2) { - throw new RetryableException(); - } else { - return "pass"; - } - } - - private String failCompletely(String input) { - if (round++ <= 1) { - throw new NonRetryableException(); - } else { - return "pass"; - } - } - - private static class RetryableException extends RuntimeException { - } - - private static class NonRetryableException extends RuntimeException { - } -} -- 2.36.6