From 26fc46da4785b170636dc3ae891b168e19896ac0 Mon Sep 17 00:00:00 2001 From: SanHalacogluImproving Date: Wed, 24 Jan 2024 14:30:44 -0800 Subject: [PATCH] Spotless. --- .../src/main/java/glide/api/RedisClient.java | 48 ++++++++------- .../BaseClientConfiguration.java | 28 ++++----- .../connectors/handlers/ChannelHandler.java | 30 +++++----- .../connectors/resources/EpollResource.java | 20 +++---- .../resources/KQueuePoolResource.java | 20 +++---- .../glide/connectors/resources/Platform.java | 18 +++--- .../resources/ThreadPoolResource.java | 16 ++--- .../ThreadPoolResourceAllocator.java | 60 +++++++++---------- .../java/glide/api/RedisClientCreateTest.java | 40 ++++++------- .../ThreadPoolResourceAllocatorTest.java | 56 ++++++++--------- 10 files changed, 169 insertions(+), 167 deletions(-) diff --git a/java/client/src/main/java/glide/api/RedisClient.java b/java/client/src/main/java/glide/api/RedisClient.java index 5673e6bf63..e09244a7ec 100644 --- a/java/client/src/main/java/glide/api/RedisClient.java +++ b/java/client/src/main/java/glide/api/RedisClient.java @@ -20,31 +20,33 @@ */ public class RedisClient extends BaseClient implements BaseCommands { - /** - * Request an async (non-blocking) Redis client in Standalone mode. - * - * @param config - Redis Client Configuration - * @return a Future to connect and return a RedisClient - */ - public static CompletableFuture CreateClient(RedisClientConfiguration config) { - ThreadPoolResource threadPoolResource = config.getThreadPoolResource(); - if (threadPoolResource == null) { - threadPoolResource = - ThreadPoolResourceAllocator.getOrCreate(Platform.getThreadPoolResourceSupplier()); + /** + * Request an async (non-blocking) Redis client in Standalone mode. + * + * @param config - Redis Client Configuration + * @return a Future to connect and return a RedisClient + */ + public static CompletableFuture CreateClient(RedisClientConfiguration config) + throws InterruptedException { + ThreadPoolResource threadPoolResource = config.getThreadPoolResource(); + if (threadPoolResource == null) { + threadPoolResource = + ThreadPoolResourceAllocator.getOrCreate(Platform.getThreadPoolResourceSupplier()); + } + ChannelHandler channelHandler = buildChannelHandler(threadPoolResource); + var connectionManager = buildConnectionManager(channelHandler); + var commandManager = buildCommandManager(channelHandler); + // TODO: Support exception throwing, including interrupted exceptions + return connectionManager + .connectToRedis(config) + .thenApply(ignore -> new RedisClient(connectionManager, commandManager)); } - ChannelHandler channelHandler = buildChannelHandler(threadPoolResource); - var connectionManager = buildConnectionManager(channelHandler); - var commandManager = buildCommandManager(channelHandler); - // TODO: Support exception throwing, including interrupted exceptions - return connectionManager - .connectToRedis(config) - .thenApply(ignore -> new RedisClient(connectionManager, commandManager)); - } - protected static ChannelHandler buildChannelHandler(ThreadPoolResource threadPoolResource) { - CallbackDispatcher callbackDispatcher = new CallbackDispatcher(); - return new ChannelHandler(callbackDispatcher, getSocket(), threadPoolResource); - } + protected static ChannelHandler buildChannelHandler(ThreadPoolResource threadPoolResource) + throws InterruptedException { + CallbackDispatcher callbackDispatcher = new CallbackDispatcher(); + return new ChannelHandler(callbackDispatcher, getSocket(), threadPoolResource); + } protected static ConnectionManager buildConnectionManager(ChannelHandler channelHandler) { return new ConnectionManager(channelHandler); diff --git a/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java b/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java index a79f43b67e..ac3741f829 100644 --- a/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java +++ b/java/client/src/main/java/glide/api/models/configuration/BaseClientConfiguration.java @@ -2,8 +2,6 @@ import glide.connectors.resources.ThreadPoolResource; import java.util.List; - -import glide.connectors.resources.ThreadPoolResource; import lombok.Builder; import lombok.Getter; import lombok.NonNull; @@ -46,18 +44,18 @@ public abstract class BaseClientConfiguration { */ private final RedisCredentials credentials; - /** - * The duration in milliseconds that the client should wait for a request to complete. This - * duration encompasses sending the request, awaiting for a response from the server, and any - * required reconnections or retries. If the specified timeout is exceeded for a pending request, - * it will result in a timeout error. If not set, a default value will be used. - */ - private final Integer requestTimeout; + /** + * The duration in milliseconds that the client should wait for a request to complete. This + * duration encompasses sending the request, awaiting for a response from the server, and any + * required reconnections or retries. If the specified timeout is exceeded for a pending request, + * it will result in a timeout error. If not set, a default value will be used. + */ + private final Integer requestTimeout; - /** - * Advanced users can pass an extended {@link glide.connectors.resources.ThreadPoolResource} to - * pass a user-defined event loop group. Users are responsible for shutting the resource down when - * no longer in use. - */ - private final ThreadPoolResource threadPoolResource; + /** + * Advanced users can pass an extended {@link glide.connectors.resources.ThreadPoolResource} to + * pass a user-defined event loop group. Users are responsible for shutting the resource down when + * no longer in use. + */ + private final ThreadPoolResource threadPoolResource; } diff --git a/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java b/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java index fa44cc8c9c..1fe8a99353 100644 --- a/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java +++ b/java/client/src/main/java/glide/connectors/handlers/ChannelHandler.java @@ -23,25 +23,27 @@ public class ChannelHandler { /** * Open a new channel for a new client and running it on the provided EventLoopGroup. + * * @param callbackDispatcher - dispatcher to handle callbacks * @param socketPath - address to connect * @param threadPoolResource - resource to choose ELG and domainSocketChannelClass */ - public ChannelHandler( - CallbackDispatcher callbackDispatcher, - String socketPath, - ThreadPoolResource threadPoolResource) throws InterruptedException { + public ChannelHandler( + CallbackDispatcher callbackDispatcher, + String socketPath, + ThreadPoolResource threadPoolResource) + throws InterruptedException { - channel = - new Bootstrap() - .group(threadPoolResource.getEventLoopGroup()) - .channel(threadPoolResource.getDomainSocketChannelClass()) - .handler(new ProtobufSocketChannelInitializer(callbackDispatcher)) - .connect(new DomainSocketAddress(socketPath)) - .sync() - .channel(); - this.callbackDispatcher = callbackDispatcher; - } + channel = + new Bootstrap() + .group(threadPoolResource.getEventLoopGroup()) + .channel(threadPoolResource.getDomainSocketChannelClass()) + .handler(new ProtobufSocketChannelInitializer(callbackDispatcher)) + .connect(new DomainSocketAddress(socketPath)) + .sync() + .channel(); + this.callbackDispatcher = callbackDispatcher; + } /** * Complete a protobuf message and write it to the channel (to UDS). diff --git a/java/client/src/main/java/glide/connectors/resources/EpollResource.java b/java/client/src/main/java/glide/connectors/resources/EpollResource.java index f3a77deebc..e4ce9de72b 100644 --- a/java/client/src/main/java/glide/connectors/resources/EpollResource.java +++ b/java/client/src/main/java/glide/connectors/resources/EpollResource.java @@ -9,16 +9,16 @@ * configurations. */ public class EpollResource extends ThreadPoolResource { - private static final String EPOLL_EVENT_LOOP_IDENTIFIER = "glide-channel-epoll-elg"; + private static final String EPOLL_EVENT_LOOP_IDENTIFIER = "glide-channel-epoll-elg"; - public EpollResource() { - this( - new EpollEventLoopGroup( - Runtime.getRuntime().availableProcessors(), - new DefaultThreadFactory(EPOLL_EVENT_LOOP_IDENTIFIER, true))); - } + public EpollResource() { + this( + new EpollEventLoopGroup( + Runtime.getRuntime().availableProcessors(), + new DefaultThreadFactory(EPOLL_EVENT_LOOP_IDENTIFIER, true))); + } - public EpollResource(EpollEventLoopGroup epollEventLoopGroup) { - super(epollEventLoopGroup, EpollDomainSocketChannel.class); - } + public EpollResource(EpollEventLoopGroup epollEventLoopGroup) { + super(epollEventLoopGroup, EpollDomainSocketChannel.class); + } } diff --git a/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java b/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java index 078e65a69e..95392942e9 100644 --- a/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java +++ b/java/client/src/main/java/glide/connectors/resources/KQueuePoolResource.java @@ -9,16 +9,16 @@ * configurations. */ public class KQueuePoolResource extends ThreadPoolResource { - private static final String KQUEUE_EVENT_LOOP_IDENTIFIER = "glide-channel-kqueue-elg"; + private static final String KQUEUE_EVENT_LOOP_IDENTIFIER = "glide-channel-kqueue-elg"; - public KQueuePoolResource() { - this( - new KQueueEventLoopGroup( - Runtime.getRuntime().availableProcessors(), - new DefaultThreadFactory(KQUEUE_EVENT_LOOP_IDENTIFIER, true))); - } + public KQueuePoolResource() { + this( + new KQueueEventLoopGroup( + Runtime.getRuntime().availableProcessors(), + new DefaultThreadFactory(KQUEUE_EVENT_LOOP_IDENTIFIER, true))); + } - public KQueuePoolResource(KQueueEventLoopGroup eventLoopGroup) { - super(eventLoopGroup, KQueueDomainSocketChannel.class); - } + public KQueuePoolResource(KQueueEventLoopGroup eventLoopGroup) { + super(eventLoopGroup, KQueueDomainSocketChannel.class); + } } diff --git a/java/client/src/main/java/glide/connectors/resources/Platform.java b/java/client/src/main/java/glide/connectors/resources/Platform.java index 9403064678..5be95cf0cc 100644 --- a/java/client/src/main/java/glide/connectors/resources/Platform.java +++ b/java/client/src/main/java/glide/connectors/resources/Platform.java @@ -54,15 +54,15 @@ private static boolean isEPollAvailable() { } } - public static Supplier getThreadPoolResourceSupplier() { - if (Platform.getCapabilities().isKQueueAvailable()) { - return KQueuePoolResource::new; - } + public static Supplier getThreadPoolResourceSupplier() { + if (Platform.getCapabilities().isKQueueAvailable()) { + return KQueuePoolResource::new; + } - if (Platform.getCapabilities().isEPollAvailable()) { - return EpollResource::new; + if (Platform.getCapabilities().isEPollAvailable()) { + return EpollResource::new; + } + // TODO support IO-Uring and NIO + throw new RuntimeException("Current platform supports no known thread pool resources"); } - // TODO support IO-Uring and NIO - throw new RuntimeException("Current platform supports no known thread pool resources"); - } } diff --git a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java index badfebf8c8..52c01b87ed 100644 --- a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java +++ b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResource.java @@ -11,13 +11,13 @@ */ @Getter public abstract class ThreadPoolResource { - private EventLoopGroup eventLoopGroup; - private Class domainSocketChannelClass; + private EventLoopGroup eventLoopGroup; + private Class domainSocketChannelClass; - public ThreadPoolResource( - @NonNull EventLoopGroup eventLoopGroup, - @NonNull Class domainSocketChannelClass) { - this.eventLoopGroup = eventLoopGroup; - this.domainSocketChannelClass = domainSocketChannelClass; - } + public ThreadPoolResource( + @NonNull EventLoopGroup eventLoopGroup, + @NonNull Class domainSocketChannelClass) { + this.eventLoopGroup = eventLoopGroup; + this.domainSocketChannelClass = domainSocketChannelClass; + } } diff --git a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java index 7837d4e3d5..b231f45437 100644 --- a/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java +++ b/java/client/src/main/java/glide/connectors/resources/ThreadPoolResourceAllocator.java @@ -4,40 +4,40 @@ /** A class responsible to allocating and deallocating the default Thread Pool Resource. */ public class ThreadPoolResourceAllocator { - private static final Object lock = new Object(); - private static ThreadPoolResource defaultThreadPoolResource = null; + private static final Object lock = new Object(); + private static ThreadPoolResource defaultThreadPoolResource = null; - public static ThreadPoolResource getOrCreate(Supplier supplier) { - // once the default is set, we want to avoid hitting the lock - if (defaultThreadPoolResource != null) { - return defaultThreadPoolResource; - } + public static ThreadPoolResource getOrCreate(Supplier supplier) { + // once the default is set, we want to avoid hitting the lock + if (defaultThreadPoolResource != null) { + return defaultThreadPoolResource; + } - synchronized (lock) { - if (defaultThreadPoolResource == null) { - defaultThreadPoolResource = supplier.get(); - } - } + synchronized (lock) { + if (defaultThreadPoolResource == null) { + defaultThreadPoolResource = supplier.get(); + } + } - return defaultThreadPoolResource; - } + return defaultThreadPoolResource; + } - /** - * A JVM shutdown hook to be registered. It is responsible for closing connection and freeing - * resources. It is recommended to use a class instead of lambda to ensure that it is called.
- * See {@link Runtime#addShutdownHook}. - */ - protected static class ShutdownHook implements Runnable { - @Override - public void run() { - if (defaultThreadPoolResource != null) { - defaultThreadPoolResource.getEventLoopGroup().shutdownGracefully(); - defaultThreadPoolResource = null; - } + /** + * A JVM shutdown hook to be registered. It is responsible for closing connection and freeing + * resources. It is recommended to use a class instead of lambda to ensure that it is called.
+ * See {@link Runtime#addShutdownHook}. + */ + protected static class ShutdownHook implements Runnable { + @Override + public void run() { + if (defaultThreadPoolResource != null) { + defaultThreadPoolResource.getEventLoopGroup().shutdownGracefully(); + defaultThreadPoolResource = null; + } + } } - } - static { - Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "Glide-shutdown-hook")); - } + static { + Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "Glide-shutdown-hook")); + } } diff --git a/java/client/src/test/java/glide/api/RedisClientCreateTest.java b/java/client/src/test/java/glide/api/RedisClientCreateTest.java index 051e2d1894..e96c4ca4c5 100644 --- a/java/client/src/test/java/glide/api/RedisClientCreateTest.java +++ b/java/client/src/test/java/glide/api/RedisClientCreateTest.java @@ -28,25 +28,25 @@ public class RedisClientCreateTest { - private MockedStatic mockedClient; - private ChannelHandler channelHandler; - private ConnectionManager connectionManager; - private CommandManager commandManager; - private ThreadPoolResource threadPoolResource; + private MockedStatic mockedClient; + private ChannelHandler channelHandler; + private ConnectionManager connectionManager; + private CommandManager commandManager; + private ThreadPoolResource threadPoolResource; @BeforeEach public void init() { mockedClient = Mockito.mockStatic(RedisClient.class); - channelHandler = mock(ChannelHandler.class); - commandManager = mock(CommandManager.class); - connectionManager = mock(ConnectionManager.class); - threadPoolResource = mock(ThreadPoolResource.class); + channelHandler = mock(ChannelHandler.class); + commandManager = mock(CommandManager.class); + connectionManager = mock(ConnectionManager.class); + threadPoolResource = mock(ThreadPoolResource.class); - mockedClient.when(() -> buildChannelHandler(any())).thenReturn(channelHandler); - mockedClient.when(() -> buildConnectionManager(channelHandler)).thenReturn(connectionManager); - mockedClient.when(() -> buildCommandManager(channelHandler)).thenReturn(commandManager); - } + mockedClient.when(() -> buildChannelHandler(any())).thenReturn(channelHandler); + mockedClient.when(() -> buildConnectionManager(channelHandler)).thenReturn(connectionManager); + mockedClient.when(() -> buildCommandManager(channelHandler)).thenReturn(commandManager); + } @AfterEach public void teardown() { @@ -57,11 +57,11 @@ public void teardown() { @SneakyThrows public void createClient_withConfig_successfullyReturnsRedisClient() { - // setup - CompletableFuture connectToRedisFuture = new CompletableFuture<>(); - connectToRedisFuture.complete(null); - RedisClientConfiguration config = - RedisClientConfiguration.builder().threadPoolResource(threadPoolResource).build(); + // setup + CompletableFuture connectToRedisFuture = new CompletableFuture<>(); + connectToRedisFuture.complete(null); + RedisClientConfiguration config = + RedisClientConfiguration.builder().threadPoolResource(threadPoolResource).build(); when(connectionManager.connectToRedis(eq(config))).thenReturn(connectToRedisFuture); mockedClient.when(() -> CreateClient(config)).thenCallRealMethod(); @@ -84,8 +84,8 @@ public void createClient_errorOnConnectionThrowsExecutionException() { connectToRedisFuture.completeExceptionally(exception); RedisClientConfiguration config = RedisClientConfiguration.builder().build(); - when(connectionManager.connectToRedis(any())).thenReturn(connectToRedisFuture); - mockedClient.when(() -> CreateClient(any())).thenCallRealMethod(); + when(connectionManager.connectToRedis(any())).thenReturn(connectToRedisFuture); + mockedClient.when(() -> CreateClient(any())).thenCallRealMethod(); // exercise CompletableFuture result = CreateClient(config); diff --git a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java index 068d55e5ef..7bbcd6a0b1 100644 --- a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java +++ b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java @@ -12,32 +12,32 @@ public class ThreadPoolResourceAllocatorTest { - ThreadPoolResourceAllocator service; - - @Test - public void getOrCreateReturnsDefault() { - new ThreadPoolResourceAllocator.ShutdownHook().run(); - - ThreadPoolResource mockedThreadPool = mock(ThreadPoolResource.class); - Supplier threadPoolSupplier = mock(Supplier.class); - when(threadPoolSupplier.get()).thenReturn(mockedThreadPool); - - ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); - assertEquals(mockedThreadPool, theResource); - - ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); - assertEquals(mockedThreadPool, theSameResource); - - // and test that the supplier isn't used any longer once the default is set - Supplier notUsedSupplier = mock(Supplier.class); - ThreadPoolResource firstResource = service.getOrCreate(notUsedSupplier); - verify(notUsedSupplier, times(0)).get(); - assertEquals(mockedThreadPool, firstResource); - - // teardown - // remove the mocked resource - EventLoopGroup mockedELG = mock(EventLoopGroup.class); - when(mockedThreadPool.getEventLoopGroup()).thenReturn(mockedELG); - new ThreadPoolResourceAllocator.ShutdownHook().run(); - } + ThreadPoolResourceAllocator service; + + @Test + public void getOrCreateReturnsDefault() { + new ThreadPoolResourceAllocator.ShutdownHook().run(); + + ThreadPoolResource mockedThreadPool = mock(ThreadPoolResource.class); + Supplier threadPoolSupplier = mock(Supplier.class); + when(threadPoolSupplier.get()).thenReturn(mockedThreadPool); + + ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); + assertEquals(mockedThreadPool, theResource); + + ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); + assertEquals(mockedThreadPool, theSameResource); + + // and test that the supplier isn't used any longer once the default is set + Supplier notUsedSupplier = mock(Supplier.class); + ThreadPoolResource firstResource = service.getOrCreate(notUsedSupplier); + verify(notUsedSupplier, times(0)).get(); + assertEquals(mockedThreadPool, firstResource); + + // teardown + // remove the mocked resource + EventLoopGroup mockedELG = mock(EventLoopGroup.class); + when(mockedThreadPool.getEventLoopGroup()).thenReturn(mockedELG); + new ThreadPoolResourceAllocator.ShutdownHook().run(); + } }