You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by "rythm-sachdeva (via GitHub)" <gi...@apache.org> on 2026/01/24 09:23:50 UTC

[PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

rythm-sachdeva opened a new pull request, #2606:
URL: https://github.com/apache/iggy/pull/2606

   ## Description
   Implements connection pooling for `AsyncTcpConnection` using Netty's `FixedChannelPool` to handle concurrent async operations efficiently.
   
   ## Changes
   - **Connection Pooling**: Replaced single channel with `FixedChannelPool` for concurrent request handling
   - **Pool Metrics**: Added `PoolMetrics` class to track:
     - Active connections
     - Total requests and errors
     - Wait times (min/max/average in nanoseconds, microseconds, and milliseconds)
     - Error rate percentage
   - **Configuration**: Added `TCPConnectionPoolConfig` with builder pattern for customizing:
     - Max connections (default: 5)
     - Max pending acquires (default: 1000)
     - Acquire timeout (default: 3000ms)
   - **Request Correlation**: Implemented FIFO queue-based request/response matching using `ConcurrentLinkedQueue`
   - **Broadcast Operations**: Added `broadcastAsync()` method to execute commands across all pooled connections (useful for authentication)
   - **Client Integration**: Exposed pool configuration via `AsyncIggyTcpClient.Builder.connectionPoolSize()`
   
   ## Implementation Details
   - Uses Netty's `FixedChannelPool` with `ChannelHealthChecker.ACTIVE`
   - Each channel has its own `IggyResponseHandler` with a response queue
   - Pool automatically releases channels after request completion
   - Handles connection errors gracefully with proper cleanup
   
   ## Testing
   - Added integration test `testConnectionPoolMetrics()` to verify metrics tracking
   - Existing async integration tests validate concurrent operations
   - Pool properly handles concurrent sends and polls
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3983022569

   > Hi @rythm-sachdeva, please run tests/lints before pushing out the changes.
   
   Sorry, will surely do it next time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-4002990364

   @rythm-sachdeva please fix the lints. I see that there's IllegalStateException in the code instead of Iggy's equivalent. Also, any commented out code should be removed.
   And please remove PoolMetrics as I asked in the comment above. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

Posted by "ex172000 (via GitHub)" <gi...@apache.org>.
ex172000 commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2730265644


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -92,108 +114,177 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
+                .remoteAddress(this.host, this.port);
+    }
 
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
+    }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+    /**
+     * Returns Pool metrics.
+     */
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
     }
 
     /**
-     * Connects to the server asynchronously.
+     * BroadCast Command to each connection

Review Comment:
   very minor nit: `Broadcasts` to keep it consistent :)



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -95,7 +95,13 @@ public static Builder builder() {
      * Connects to the Iggy server asynchronously.
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        // 1. Create the pool configuration from builder/client fields
+        AsyncTcpConnection.TCPConnectionPoolConfig poolConfig = new AsyncTcpConnection.TCPConnectionPoolConfig(
+                connectionPoolSize.orElse(5), // Default to 5 if not provided
+                1000, // maxPendingAcquires
+                connectionTimeout.map(Duration::toMillis).orElse(3000L) // map Duration to millis

Review Comment:
   Do we want to 1) make the params configurable? 2) or put them as static final constants?
   Also seems we have the default provided by the builder already?



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -92,108 +114,177 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
+                .remoteAddress(this.host, this.port);
+    }
 
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
+    }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+    /**
+     * Returns Pool metrics.
+     */
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
     }
 
     /**
-     * Connects to the server asynchronously.
+     * BroadCast Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is closed"));
+        }
+        if (channelPool == null) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client not connected call connect first"));
+        }
+        List<CompletableFuture<ByteBuf>> lastFuture = new ArrayList<>();
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
-            }
-        });
+        int poolSize = poolConfig.getMaxConnections();
 
-        return future;
+        // 1. Drain the Pool (Acquire all connections)
+        for (int i = 0; i < poolSize; i++) {
+            lastFuture.add(sendAsync(commandCode, payload.retainedDuplicate()));
+        }
+        // 2. Return the last Future (caller can treat this like a single request)
+        return lastFuture.get(lastFuture.size() - 1);

Review Comment:
   Not sure if I understand correctly, but how does the last future guarantee on the behavior of all futures?



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -58,21 +64,37 @@ public class AsyncTcpConnection {
     private final SslContext sslContext;
     private final EventLoopGroup eventLoopGroup;
     private final Bootstrap bootstrap;
-    private Channel channel;
-    private final AtomicLong requestIdGenerator = new AtomicLong(0);
-    private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();
+    // private Channel channel;
+    // private ChannelHealthChecker channelHealthChecker;
+
+    // Pooling System;
+    private SimpleChannelPool channelPool;
+    private final TCPConnectionPoolConfig poolConfig;
+    private final PoolMetrics poolMetrics;
+
+    private final AtomicBoolean isClosed = new AtomicBoolean(false);
+    // private final AtomicLong requestIdGenerator = new AtomicLong(0);
+    // private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();

Review Comment:
   Do we want to remove them?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3982897287

   Hi @rythm-sachdeva, please run tests/lints before pushing out the changes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3844576275

   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs.
   
   If you need a review, please ensure CI is green and the PR is rebased on the latest master. Don't hesitate to ping the maintainers - either @core on Discord or by mentioning them directly here on the PR.
   
   Thank you for your contribution!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski merged PR #2606:
URL: https://github.com/apache/iggy/pull/2606


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2812866671


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -58,21 +64,37 @@ public class AsyncTcpConnection {
     private final SslContext sslContext;
     private final EventLoopGroup eventLoopGroup;
     private final Bootstrap bootstrap;
-    private Channel channel;
-    private final AtomicLong requestIdGenerator = new AtomicLong(0);
-    private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();
+    // private Channel channel;
+    // private ChannelHealthChecker channelHealthChecker;
+
+    // Pooling System;
+    private SimpleChannelPool channelPool;
+    private final TCPConnectionPoolConfig poolConfig;
+    private final PoolMetrics poolMetrics;
+
+    private final AtomicBoolean isClosed = new AtomicBoolean(false);
+    // private final AtomicLong requestIdGenerator = new AtomicLong(0);
+    // private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();

Review Comment:
   If this code isn't being used, please remove it rather than leaving it commented out.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-4000905411

   Hi @mmodzelewski can you check once I have updated the pr


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2850278534


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -158,7 +159,15 @@ public static AsyncIggyTcpClientBuilder builder() {
      * @return a {@link CompletableFuture} that completes when the connection is established
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        TCPConnectionPoolConfig.Builder poolConfigBuilder = new TCPConnectionPoolConfig.Builder();
+        if (connectionPoolSize.isPresent()) {
+            poolConfigBuilder.setMaxConnections(connectionPoolSize.get());
+        }
+        if (connectionTimeout.isPresent()) {
+            poolConfigBuilder.setAcquireTimeoutMillis(connectionTimeout.get().toMillis());

Review Comment:
   I think by mistake I have given the wrong name it should be acquireTimeout
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2849167099


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -242,36 +334,94 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
             int status = msg.readIntLE();
             int length = msg.readIntLE();
 
-            // Since Iggy doesn't use request IDs, we process responses in order
-            // Get the oldest pending request
-            if (!pendingRequests.isEmpty()) {
-                Long oldestRequestId =
-                        pendingRequests.keySet().stream().min(Long::compare).orElse(null);
+            CompletableFuture<ByteBuf> future = responseQueue.poll();
 
-                if (oldestRequestId != null) {
-                    CompletableFuture<ByteBuf> future = pendingRequests.remove(oldestRequestId);
+            if (future != null) {
 
-                    if (status == 0) {
-                        // Success - pass the remaining buffer as response
-                        future.complete(msg.retainedSlice());
-                    } else {
-                        // Error - the payload contains the error message
-                        byte[] errorBytes = length > 0 ? new byte[length] : new byte[0];
-                        if (length > 0) {
-                            msg.readBytes(errorBytes);
-                        }
+                if (status == 0) {
+                    // Success - pass the remaining buffer as response
+                    future.complete(msg.retainedSlice());
+                } else {
+                    // Error - the payload contains the error message
+                    if (length > 0) {
+                        byte[] errorBytes = new byte[length];
+                        msg.readBytes(errorBytes);
                         future.completeExceptionally(IggyServerException.fromTcpResponse(status, errorBytes));
+                    } else {
+                        future.completeExceptionally(new IggyServerException(status));
                     }
                 }
+            } else {
+                log.error(
+                        "Received response on channel {} but no request was waiting!",
+                        ctx.channel().id());
+            }
+            if (pool != null) {
+                pool.release(ctx.channel());
             }
         }
 
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

Review Comment:
   A channel should also be released to the pool in case of exception



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2732307293


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -92,108 +114,177 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
+                .remoteAddress(this.host, this.port);
+    }
 
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
+    }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+    /**
+     * Returns Pool metrics.
+     */
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
     }
 
     /**
-     * Connects to the server asynchronously.
+     * BroadCast Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is closed"));
+        }
+        if (channelPool == null) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client not connected call connect first"));
+        }
+        List<CompletableFuture<ByteBuf>> lastFuture = new ArrayList<>();
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
-            }
-        });
+        int poolSize = poolConfig.getMaxConnections();
 
-        return future;
+        // 1. Drain the Pool (Acquire all connections)
+        for (int i = 0; i < poolSize; i++) {
+            lastFuture.add(sendAsync(commandCode, payload.retainedDuplicate()));
+        }
+        // 2. Return the last Future (caller can treat this like a single request)
+        return lastFuture.get(lastFuture.size() - 1);

Review Comment:
   Does not guarantee, I assumed that if last request is fulfilled the previous requests have been successful. Though not a good approach. If there is any better approach  you suggest I am happy to take feedback and implement it in an optimal way.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-4003349515

   Sure @mmodzelewski will do that.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2848996887


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
     }
 
     /**
-     * Connects to the server asynchronously.
+     * Returns Pool metrics.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
+    }
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
+    /**
+     * BroadCasts Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
+     */
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {
+        try {
+            if (isClosed.get()) {
+                return CompletableFuture.failedFuture(new IllegalStateException("Client is closed"));
             }
-        });
+            if (channelPool == null) {
+                return CompletableFuture.failedFuture(
+                        new IllegalStateException("Client not connected call connect first"));
+            }
+            List<CompletableFuture<ByteBuf>> lastFuture = new ArrayList<>();
+
+            int poolSize = poolConfig.getMaxConnections();
 
-        return future;
+            for (int i = 0; i < poolSize; i++) {
+                lastFuture.add(send(commandCode, payload.retainedDuplicate()));
+            }
+            for (int i = 0; i < poolSize - 1; i++) {
+                lastFuture.get(i).thenAccept(response -> response.release());
+            }
+            return lastFuture.get(lastFuture.size() - 1);
+        } finally {
+            payload.release();
+        }
     }
 
     /**
      * Sends a command asynchronously and returns the response.
      * Uses Netty's EventLoop to ensure thread-safe sequential request processing with FIFO response matching.
      */
     public CompletableFuture<ByteBuf> send(int commandCode, ByteBuf payload) {
-        if (channel == null || !channel.isActive()) {
-            payload.release();
+        if (isClosed.get()) {

Review Comment:
   the payload should be released in the early returns



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -242,36 +334,94 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
             int status = msg.readIntLE();
             int length = msg.readIntLE();
 
-            // Since Iggy doesn't use request IDs, we process responses in order
-            // Get the oldest pending request
-            if (!pendingRequests.isEmpty()) {
-                Long oldestRequestId =
-                        pendingRequests.keySet().stream().min(Long::compare).orElse(null);
+            CompletableFuture<ByteBuf> future = responseQueue.poll();
 
-                if (oldestRequestId != null) {
-                    CompletableFuture<ByteBuf> future = pendingRequests.remove(oldestRequestId);
+            if (future != null) {
 
-                    if (status == 0) {
-                        // Success - pass the remaining buffer as response
-                        future.complete(msg.retainedSlice());
-                    } else {
-                        // Error - the payload contains the error message
-                        byte[] errorBytes = length > 0 ? new byte[length] : new byte[0];
-                        if (length > 0) {
-                            msg.readBytes(errorBytes);
-                        }
+                if (status == 0) {
+                    // Success - pass the remaining buffer as response
+                    future.complete(msg.retainedSlice());
+                } else {
+                    // Error - the payload contains the error message
+                    if (length > 0) {
+                        byte[] errorBytes = new byte[length];
+                        msg.readBytes(errorBytes);
                         future.completeExceptionally(IggyServerException.fromTcpResponse(status, errorBytes));
+                    } else {
+                        future.completeExceptionally(new IggyServerException(status));
                     }
                 }
+            } else {
+                log.error(
+                        "Received response on channel {} but no request was waiting!",
+                        ctx.channel().id());
+            }
+            if (pool != null) {
+                pool.release(ctx.channel());
             }
         }
 
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
-            // Fail all pending requests
-            pendingRequests.values().forEach(future -> future.completeExceptionally(cause));
-            pendingRequests.clear();
+            // If the connection dies, fail ALL waiting requests for this connection
+            CompletableFuture<ByteBuf> f;
+            while ((f = responseQueue.poll()) != null) {
+                f.completeExceptionally(cause);
+            }
             ctx.close();
         }
     }
+
+    // Inner Class for Channel pool configurations
+    public static class TCPConnectionPoolConfig {
+        private final int maxConnections;
+        private final int maxPendingAcquires;
+        private final long acquireTimeoutMillis;
+
+        public TCPConnectionPoolConfig(int maxConnections, int maxPendingAcquires, long acquireTimeoutMillis) {
+            this.maxConnections = maxConnections;
+            this.maxPendingAcquires = maxPendingAcquires;
+            this.acquireTimeoutMillis = acquireTimeoutMillis;
+        }
+
+        public int getMaxConnections() {
+            return this.maxConnections;
+        }
+
+        public int getMaxPendingAcquires() {
+            return this.maxPendingAcquires;
+        }
+
+        public long getAcquireTimeoutMillis() {
+            return this.acquireTimeoutMillis;
+        }
+
+        // Builder Class for TCPConnectionPoolConfig
+        public static final class Builder {
+            private int maxConnections = 5;
+            private int maxPendingAcquires = 1000;
+            private long acquireTimeoutMillis = 5000;

Review Comment:
   assign above values to constants, and use them in no-args constructor for the config



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));

Review Comment:
   in the SDK code, please only use exceptions that inherit from `IggyException`



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -207,33 +300,32 @@ public CompletableFuture<ByteBuf> send(int commandCode, ByteBuf payload) {
      * Closes the connection and releases resources.
      */
     public CompletableFuture<Void> close() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
-
-        if (channel != null && channel.isActive()) {
-            channel.close().addListener((ChannelFutureListener) channelFuture -> {
-                eventLoopGroup.shutdownGracefully();
-                if (channelFuture.isSuccess()) {
-                    future.complete(null);
-                } else {
-                    future.completeExceptionally(channelFuture.cause());
-                }
-            });
-        } else {
-            eventLoopGroup.shutdownGracefully();
-            future.complete(null);
+        if (isClosed.compareAndSet(false, true)) {
+            if (channelPool != null) {
+                channelPool.close();
+            }
+            return CompletableFuture.runAsync(eventLoopGroup::shutdownGracefully);

Review Comment:
   It would make sense to wait for the result of `shutdownGracefully`. Currently, it is ignored.



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -158,7 +159,15 @@ public static AsyncIggyTcpClientBuilder builder() {
      * @return a {@link CompletableFuture} that completes when the connection is established
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        TCPConnectionPoolConfig.Builder poolConfigBuilder = new TCPConnectionPoolConfig.Builder();
+        if (connectionPoolSize.isPresent()) {
+            poolConfigBuilder.setMaxConnections(connectionPoolSize.get());
+        }
+        if (connectionTimeout.isPresent()) {
+            poolConfigBuilder.setAcquireTimeoutMillis(connectionTimeout.get().toMillis());

Review Comment:
   seems like connection timeout should map to `CONNECT_TIMEOUT_MILLIS`, acquire timeout is semantically different thing



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -158,7 +159,15 @@ public static AsyncIggyTcpClientBuilder builder() {
      * @return a {@link CompletableFuture} that completes when the connection is established
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        TCPConnectionPoolConfig.Builder poolConfigBuilder = new TCPConnectionPoolConfig.Builder();
+        if (connectionPoolSize.isPresent()) {

Review Comment:
   instead of `isPresent/get`, it would be more idiomatic to just call `ifPresent` with an update lambda



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -242,36 +334,94 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
             int status = msg.readIntLE();
             int length = msg.readIntLE();
 
-            // Since Iggy doesn't use request IDs, we process responses in order
-            // Get the oldest pending request
-            if (!pendingRequests.isEmpty()) {
-                Long oldestRequestId =
-                        pendingRequests.keySet().stream().min(Long::compare).orElse(null);
+            CompletableFuture<ByteBuf> future = responseQueue.poll();
 
-                if (oldestRequestId != null) {
-                    CompletableFuture<ByteBuf> future = pendingRequests.remove(oldestRequestId);
+            if (future != null) {
 
-                    if (status == 0) {
-                        // Success - pass the remaining buffer as response
-                        future.complete(msg.retainedSlice());
-                    } else {
-                        // Error - the payload contains the error message
-                        byte[] errorBytes = length > 0 ? new byte[length] : new byte[0];
-                        if (length > 0) {
-                            msg.readBytes(errorBytes);
-                        }
+                if (status == 0) {
+                    // Success - pass the remaining buffer as response
+                    future.complete(msg.retainedSlice());
+                } else {
+                    // Error - the payload contains the error message
+                    if (length > 0) {
+                        byte[] errorBytes = new byte[length];
+                        msg.readBytes(errorBytes);
                         future.completeExceptionally(IggyServerException.fromTcpResponse(status, errorBytes));
+                    } else {
+                        future.completeExceptionally(new IggyServerException(status));
                     }
                 }
+            } else {
+                log.error(
+                        "Received response on channel {} but no request was waiting!",
+                        ctx.channel().id());
+            }
+            if (pool != null) {
+                pool.release(ctx.channel());
             }
         }
 
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
-            // Fail all pending requests
-            pendingRequests.values().forEach(future -> future.completeExceptionally(cause));
-            pendingRequests.clear();
+            // If the connection dies, fail ALL waiting requests for this connection
+            CompletableFuture<ByteBuf> f;
+            while ((f = responseQueue.poll()) != null) {
+                f.completeExceptionally(cause);
+            }
             ctx.close();
         }
     }
+
+    // Inner Class for Channel pool configurations

Review Comment:
   Please drop all comments that do not introduce any new information. Comments are justified in situations where code is not obvious, something is done in a non-standard way, etc.



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/PoolMetrics.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.iggy.client.async.tcp;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Metrics for monitoring connection pool performance.
+ * Tracks active connections, wait times, and errors.
+ */
+public class PoolMetrics {

Review Comment:
   I'd drop the metrics for now. Let's make sure that the connection pool is working fine first. This can be a follow up.



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
     }
 
     /**
-     * Connects to the server asynchronously.
+     * Returns Pool metrics.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
+    }
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
+    /**
+     * BroadCasts Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
+     */
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {
+        try {
+            if (isClosed.get()) {
+                return CompletableFuture.failedFuture(new IllegalStateException("Client is closed"));
             }
-        });
+            if (channelPool == null) {
+                return CompletableFuture.failedFuture(
+                        new IllegalStateException("Client not connected call connect first"));
+            }
+            List<CompletableFuture<ByteBuf>> lastFuture = new ArrayList<>();
+
+            int poolSize = poolConfig.getMaxConnections();
 
-        return future;
+            for (int i = 0; i < poolSize; i++) {
+                lastFuture.add(send(commandCode, payload.retainedDuplicate()));
+            }
+            for (int i = 0; i < poolSize - 1; i++) {
+                lastFuture.get(i).thenAccept(response -> response.release());
+            }
+            return lastFuture.get(lastFuture.size() - 1);
+        } finally {
+            payload.release();
+        }
     }
 
     /**
      * Sends a command asynchronously and returns the response.
      * Uses Netty's EventLoop to ensure thread-safe sequential request processing with FIFO response matching.
      */
     public CompletableFuture<ByteBuf> send(int commandCode, ByteBuf payload) {
-        if (channel == null || !channel.isActive()) {
-            payload.release();
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(
+                    new IggyNotConnectedException("Connection not established or closed"));
+        }
+        if (channelPool == null) {
             return CompletableFuture.failedFuture(
                     new IggyNotConnectedException("Connection not established or closed"));
         }
 
+        // Starting the response clock
+        long starttime = System.nanoTime();
+
         CompletableFuture<ByteBuf> responseFuture = new CompletableFuture<>();
 
-        // Execute on channel's EventLoop to ensure sequential processing
-        // This is necessary because Iggy protocol doesn't include request IDs,
-        // and responses are matched using FIFO order
-        channel.eventLoop().execute(() -> {
-            // Since Iggy doesn't use request IDs, we'll just use a simple queue
-            // Each request will get the next response in order
-            long requestId = requestIdGenerator.incrementAndGet();
-            pendingRequests.put(requestId, responseFuture);
-
-            // Build the request frame exactly like the blocking client
-            // Frame format: [payload_size:4][command:4][payload:N]
-            // where payload_size = 4 (command size) + N (payload size)
-            int payloadSize = payload.readableBytes();
-            int framePayloadSize = 4 + payloadSize; // command (4 bytes) + payload
-
-            ByteBuf frame = channel.alloc().buffer(4 + framePayloadSize);
-            frame.writeIntLE(framePayloadSize); // Length field (includes command)
-            frame.writeIntLE(commandCode); // Command
-            frame.writeBytes(payload, payload.readerIndex(), payloadSize); // Payload
-
-            // Debug: print frame bytes
-            if (log.isTraceEnabled()) {
-                byte[] frameBytes = new byte[Math.min(frame.readableBytes(), 30)];
-                frame.getBytes(0, frameBytes);
-                StringBuilder hex = new StringBuilder();
-                for (byte b : frameBytes) {
-                    hex.append(String.format("%02x ", b));
-                }
-                log.trace(
-                        "Sending frame with command: {}, payload size: {}, frame payload size (with command): {}, total frame size: {}",
-                        commandCode,
-                        payloadSize,
-                        framePayloadSize,
-                        frame.readableBytes());
-                log.trace("Frame bytes (hex): {}", hex.toString());
+        channelPool.acquire().addListener((FutureListener<Channel>) f -> {
+
+            // Stoping the watch to record waitime
+            long waitTime = System.nanoTime() - starttime;
+            poolMetrics.recordWaitTime(waitTime);
+
+            if (!f.isSuccess()) {
+                poolMetrics.recordError();
+                responseFuture.completeExceptionally(f.cause());
+                return;
             }
 
-            payload.release();
+            // Connection Aquired
+            poolMetrics.incrementActive();
 
-            // Send the frame
-            channel.writeAndFlush(frame).addListener((ChannelFutureListener) future -> {
-                if (!future.isSuccess()) {
-                    log.error("Failed to send frame: {}", future.cause().getMessage());
-                    pendingRequests.remove(requestId);
-                    responseFuture.completeExceptionally(future.cause());
-                } else {
-                    log.trace("Frame sent successfully to {}", channel.remoteAddress());
+            Channel channel = f.getNow();
+            try {
+                IggyResponseHandler handler = channel.pipeline().get(IggyResponseHandler.class);
+
+                CompletableFuture<ByteBuf> trackedFuture = responseFuture.whenComplete((res, ex) -> {
+                    poolMetrics.decrementActive();
+                });
+                if (handler == null) {
+                    throw new IllegalStateException("Channel missing IggyResponseHandler");
                 }
-            });
+
+                // Enqueuing request so handler knows who to call back;
+                handler.enqueueRequest(responseFuture);
+
+                // Build the request frame exactly like the blocking client
+                // Frame format: [payload_size:4][command:4][payload:N]
+                // where payload_size = 4 (command size) + N (payload size)
+                int payloadSize = payload.readableBytes();
+                int framePayloadSize = 4 + payloadSize; // command (4 bytes) + payload
+
+                ByteBuf frame = channel.alloc().buffer(4 + framePayloadSize);
+                frame.writeIntLE(framePayloadSize); // Length field (includes command)
+                frame.writeIntLE(commandCode); // Command
+                frame.writeBytes(payload, payload.readerIndex(), payloadSize); // Payload
+
+                // Debug: print frame bytes
+                byte[] frameBytes = new byte[Math.min(frame.readableBytes(), 30)];

Review Comment:
   This array should be allocated within the if trace block, otherwise it's allocated but never used.



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -61,21 +67,31 @@ public class AsyncTcpConnection {
     private final SslContext sslContext;
     private final EventLoopGroup eventLoopGroup;
     private final Bootstrap bootstrap;
-    private Channel channel;
-    private final AtomicLong requestIdGenerator = new AtomicLong(0);
-    private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();
+    private SimpleChannelPool channelPool;
+    private final TCPConnectionPoolConfig poolConfig;
+    private final PoolMetrics poolMetrics;
+
+    private final AtomicBoolean isClosed = new AtomicBoolean(false);
 
     public AsyncTcpConnection(String host, int port) {
-        this(host, port, false, Optional.empty());
+        this(host, port, false, Optional.empty(), new TCPConnectionPoolConfig(5, 1000, 1000));

Review Comment:
   Please create a no-args constructor for the config, and use it here instead.



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -242,36 +334,94 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
             int status = msg.readIntLE();
             int length = msg.readIntLE();
 
-            // Since Iggy doesn't use request IDs, we process responses in order
-            // Get the oldest pending request
-            if (!pendingRequests.isEmpty()) {
-                Long oldestRequestId =
-                        pendingRequests.keySet().stream().min(Long::compare).orElse(null);
+            CompletableFuture<ByteBuf> future = responseQueue.poll();
 
-                if (oldestRequestId != null) {
-                    CompletableFuture<ByteBuf> future = pendingRequests.remove(oldestRequestId);
+            if (future != null) {
 
-                    if (status == 0) {
-                        // Success - pass the remaining buffer as response
-                        future.complete(msg.retainedSlice());
-                    } else {
-                        // Error - the payload contains the error message
-                        byte[] errorBytes = length > 0 ? new byte[length] : new byte[0];
-                        if (length > 0) {
-                            msg.readBytes(errorBytes);
-                        }
+                if (status == 0) {
+                    // Success - pass the remaining buffer as response
+                    future.complete(msg.retainedSlice());
+                } else {
+                    // Error - the payload contains the error message
+                    if (length > 0) {
+                        byte[] errorBytes = new byte[length];
+                        msg.readBytes(errorBytes);
                         future.completeExceptionally(IggyServerException.fromTcpResponse(status, errorBytes));
+                    } else {
+                        future.completeExceptionally(new IggyServerException(status));
                     }
                 }
+            } else {
+                log.error(
+                        "Received response on channel {} but no request was waiting!",
+                        ctx.channel().id());
+            }
+            if (pool != null) {
+                pool.release(ctx.channel());
             }
         }
 
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
-            // Fail all pending requests
-            pendingRequests.values().forEach(future -> future.completeExceptionally(cause));
-            pendingRequests.clear();
+            // If the connection dies, fail ALL waiting requests for this connection
+            CompletableFuture<ByteBuf> f;
+            while ((f = responseQueue.poll()) != null) {
+                f.completeExceptionally(cause);
+            }
             ctx.close();
         }
     }
+
+    // Inner Class for Channel pool configurations
+    public static class TCPConnectionPoolConfig {
+        private final int maxConnections;
+        private final int maxPendingAcquires;
+        private final long acquireTimeoutMillis;
+
+        public TCPConnectionPoolConfig(int maxConnections, int maxPendingAcquires, long acquireTimeoutMillis) {
+            this.maxConnections = maxConnections;
+            this.maxPendingAcquires = maxPendingAcquires;
+            this.acquireTimeoutMillis = acquireTimeoutMillis;
+        }
+
+        public int getMaxConnections() {
+            return this.maxConnections;
+        }
+
+        public int getMaxPendingAcquires() {
+            return this.maxPendingAcquires;
+        }
+
+        public long getAcquireTimeoutMillis() {
+            return this.acquireTimeoutMillis;
+        }
+
+        // Builder Class for TCPConnectionPoolConfig
+        public static final class Builder {
+            private int maxConnections = 5;
+            private int maxPendingAcquires = 1000;
+            private long acquireTimeoutMillis = 5000;
+
+            public Builder() {}
+
+            public Builder setMaxConnections(int maxConnections) {

Review Comment:
   let's at least add some simple validations for the values inserted to the config



##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
     }
 
     /**
-     * Connects to the server asynchronously.
+     * Returns Pool metrics.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
+    }
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
+    /**
+     * BroadCasts Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
+     */
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {

Review Comment:
   `broadcastAsync()` for authentication is unreliable - `FixedChannelPool` creates channels lazily, so sending `poolSize` login requests may hit the same channel multiple times (it gets released in `channelRead0` and re-acquired) rather than reaching every distinct connection. Connections created later (e.g. reconnect after health-check failure) will also be unauthenticated.
   
   A simpler approach: authenticate lazily in `send()`. After acquiring a channel, check a channel attribute (e.g. `channel.attr(AUTH_KEY)`). If not authenticated, send login first, set the attribute on success, then send the actual command. This works naturally with the async model, handles all channel lifecycle events (pool growth, reconnection, idle eviction), and eliminates the need for `broadcastAsync()` entirely.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3990259286

   @rythm-sachdeva please ping me when the PR is ready for review. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2851693075


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
     }
 
     /**
-     * Connects to the server asynchronously.
+     * Returns Pool metrics.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
+    }
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
+    /**
+     * BroadCasts Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
+     */
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {

Review Comment:
   The user-facing API stays the same - `login()` is still called once. Internally, `login()` would store the credentials and authenticate the one channel it acquires. Then in `send()`, after acquiring a channel, you check a channel attribute (e.g. `AUTH_KEY`). If the channel isn't authenticated yet, send login with the stored credentials first, set the attribute, then send the actual command. This way, every channel gets authenticated transparently on first use, including channels created later by the pool.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-4012237847

   Hi @mmodzelewski can you review once.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-4015393227

   Hi @mmodzelewski Can you please re run the workflow I have fixed check style errors


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2732310019


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -58,21 +64,37 @@ public class AsyncTcpConnection {
     private final SslContext sslContext;
     private final EventLoopGroup eventLoopGroup;
     private final Bootstrap bootstrap;
-    private Channel channel;
-    private final AtomicLong requestIdGenerator = new AtomicLong(0);
-    private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();
+    // private Channel channel;
+    // private ChannelHealthChecker channelHealthChecker;
+
+    // Pooling System;
+    private SimpleChannelPool channelPool;
+    private final TCPConnectionPoolConfig poolConfig;
+    private final PoolMetrics poolMetrics;
+
+    private final AtomicBoolean isClosed = new AtomicBoolean(false);
+    // private final AtomicLong requestIdGenerator = new AtomicLong(0);
+    // private final ConcurrentHashMap<Long, CompletableFuture<ByteBuf>> pendingRequests = new ConcurrentHashMap<>();

Review Comment:
   Yes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3873928281

   > Hi @mmodzelewski Can you please review .
   
   Hey @rythm-sachdeva, I will, but I need some time. I might not get round to it until the beginning of next week.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): Implemented Async Connection Pooling using FixedChannelPool (#2232) [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2732314599


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -95,7 +95,13 @@ public static Builder builder() {
      * Connects to the Iggy server asynchronously.
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        // 1. Create the pool configuration from builder/client fields
+        AsyncTcpConnection.TCPConnectionPoolConfig poolConfig = new AsyncTcpConnection.TCPConnectionPoolConfig(
+                connectionPoolSize.orElse(5), // Default to 5 if not provided
+                1000, // maxPendingAcquires
+                connectionTimeout.map(Duration::toMillis).orElse(3000L) // map Duration to millis

Review Comment:
   1. Yes
   2. Yes
   3. Yes



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3916364623

   > @rythm-sachdeva Please see why CI is failing and fix the errors.
   
   @rythm-sachdeva I've added a verification if all ByteBufs are properly released (not leaked) and it appears that the new code does not release all resources. See the logs, or run the tests locally, you'll see reports of the leaks in there. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "mmodzelewski (via GitHub)" <gi...@apache.org>.
mmodzelewski commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2812850005


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncIggyTcpClient.java:
##########
@@ -95,7 +95,13 @@ public static Builder builder() {
      * Connects to the Iggy server asynchronously.
      */
     public CompletableFuture<Void> connect() {
-        connection = new AsyncTcpConnection(host, port, enableTls, tlsCertificate);
+        // 1. Create the pool configuration from builder/client fields
+        AsyncTcpConnection.TCPConnectionPoolConfig poolConfig = new AsyncTcpConnection.TCPConnectionPoolConfig(
+                connectionPoolSize.orElse(5), // Default to 5 if not provided
+                1000, // maxPendingAcquires
+                connectionTimeout.map(Duration::toMillis).orElse(3000L) // map Duration to millis

Review Comment:
   @rythm-sachdeva please address this comment. The numeric values should be declared as static final. If any value has a default in the builder, it should be aligned in both places. Also, please remove the inline comments, the code is self-explanatory. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on code in PR #2606:
URL: https://github.com/apache/iggy/pull/2606#discussion_r2850332429


##########
foreign/java/java-sdk/src/main/java/org/apache/iggy/client/async/tcp/AsyncTcpConnection.java:
##########
@@ -95,109 +111,186 @@ private void configureBootstrap() {
                 .group(eventLoopGroup)
                 .channel(NioSocketChannel.class)
                 .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                 .option(ChannelOption.SO_KEEPALIVE, true)
-                .handler(new ChannelInitializer<SocketChannel>() {
-                    @Override
-                    protected void initChannel(SocketChannel ch) {
-                        ChannelPipeline pipeline = ch.pipeline();
-
-                        if (enableTls) {
-                            pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
-                        }
+                .remoteAddress(this.host, this.port);
+    }
 
-                        // Custom frame decoder for Iggy protocol responses
-                        pipeline.addLast("frameDecoder", new IggyFrameDecoder());
+    /**
+     * Initialises Connection pool.
+     */
+    public CompletableFuture<Void> connect() {
+        if (isClosed.get()) {
+            return CompletableFuture.failedFuture(new IllegalStateException("Client is Closed"));
+        }
+        AbstractChannelPoolHandler poolHandler = new AbstractChannelPoolHandler() {
+            @Override
+            public void channelCreated(Channel ch) {
+                ChannelPipeline pipeline = ch.pipeline();
+                if (enableTls) {
+                    // adding ssl if ssl enabled
+                    pipeline.addLast("ssl", sslContext.newHandler(ch.alloc(), host, port));
+                }
+                // Adding the FrameDecoder to end of channel pipeline
+                pipeline.addLast("frameDecoder", new IggyFrameDecoder());
 
-                        // No encoder needed - we build complete frames following Iggy protocol
-                        // The protocol already includes the length field, so adding an encoder
-                        // would duplicate it. This matches the blocking client implementation.
+                // Adding Response Handler Now Statefull
+                pipeline.addLast("responseHandler", new IggyResponseHandler());
+            }
 
-                        // Response handler
-                        pipeline.addLast("responseHandler", new IggyResponseHandler(pendingRequests));
-                    }
-                });
+            @Override
+            public void channelAcquired(Channel ch) {
+                IggyResponseHandler handler = ch.pipeline().get(IggyResponseHandler.class);
+                handler.setPool(channelPool);
+            }
+        };
+
+        this.channelPool = new FixedChannelPool(
+                bootstrap,
+                poolHandler,
+                ChannelHealthChecker.ACTIVE, // Check If the connection is Active Before Lending
+                FixedChannelPool.AcquireTimeoutAction.FAIL, // Fail If we take too long
+                poolConfig.getAcquireTimeoutMillis(),
+                poolConfig.getMaxConnections(),
+                poolConfig.getMaxPendingAcquires());
+        log.info("Connection pool initialized with max connections: {}", poolConfig.getMaxConnections());
+        return CompletableFuture.completedFuture(null);
     }
 
     /**
-     * Connects to the server asynchronously.
+     * Returns Pool metrics.
      */
-    public CompletableFuture<Void> connect() {
-        CompletableFuture<Void> future = new CompletableFuture<>();
+    public PoolMetrics getMetrics() {
+        return this.poolMetrics;
+    }
 
-        bootstrap.connect(host, port).addListener((ChannelFutureListener) channelFuture -> {
-            if (channelFuture.isSuccess()) {
-                channel = channelFuture.channel();
-                future.complete(null);
-            } else {
-                future.completeExceptionally(channelFuture.cause());
+    /**
+     * BroadCasts Command to each connection
+     * (Mainly for login so that each connection in the pool is Authenticated)
+     * Returns the result of the LAST connection's execution, allowing the caller
+     * to treat this like a single request.
+     */
+    public CompletableFuture<ByteBuf> broadcastAsync(int commandCode, ByteBuf payload) {

Review Comment:
   Currently the login is called by the user once After client creation. In this case how would the flow work ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "slbotbm (via GitHub)" <gi...@apache.org>.
slbotbm commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3916156182

   @rythm-sachdeva Please see why CI is failing and fix the errors.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3940531928

   ## [Codecov](https://app.codecov.io/gh/apache/iggy/pull/2606?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `57.81991%` with `89 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 68.50%. Comparing base ([`aff29a5`](https://app.codecov.io/gh/apache/iggy/commit/aff29a544372fe5487598339830dafecb39aa14e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`181682b`](https://app.codecov.io/gh/apache/iggy/commit/181682b961c00ee22ce7fb39fdc6cb913958719b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/iggy/pull/2606?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...ache/iggy/client/async/tcp/AsyncTcpConnection.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FAsyncTcpConnection.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvQXN5bmNUY3BDb25uZWN0aW9uLmphdmE=) | 60.86% | [40 Missing and 14 partials :warning: ](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [.../org/apache/iggy/client/async/tcp/PoolMetrics.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FPoolMetrics.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvUG9vbE1ldHJpY3MuamF2YQ==) | 45.28% | [26 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...g/apache/iggy/client/async/tcp/UsersTcpClient.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FUsersTcpClient.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvVXNlcnNUY3BDbGllbnQuamF2YQ==) | 50.00% | [6 Missing :warning: ](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##             master    #2606      +/-   ##
   ============================================
   - Coverage     68.52%   68.50%   -0.02%     
   - Complexity      656      673      +17     
   ============================================
     Files           743      744       +1     
     Lines         62808    62934     +126     
     Branches      59221    59235      +14     
   ============================================
   + Hits          43039    43114      +75     
   - Misses        17656    17700      +44     
   - Partials       2113     2120       +7     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/iggy/pull/2606/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [java](https://app.codecov.io/gh/apache/iggy/pull/2606/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `52.42% <57.81%> (+0.23%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/iggy/pull/2606?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...ache/iggy/client/async/tcp/AsyncIggyTcpClient.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FAsyncIggyTcpClient.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvQXN5bmNJZ2d5VGNwQ2xpZW50LmphdmE=) | `98.11% <100.00%> (+0.28%)` | :arrow_up: |
   | [...g/apache/iggy/client/async/tcp/UsersTcpClient.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FUsersTcpClient.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvVXNlcnNUY3BDbGllbnQuamF2YQ==) | `73.33% <50.00%> (-3.59%)` | :arrow_down: |
   | [.../org/apache/iggy/client/async/tcp/PoolMetrics.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FPoolMetrics.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvUG9vbE1ldHJpY3MuamF2YQ==) | `45.28% <45.28%> (ø)` | |
   | [...ache/iggy/client/async/tcp/AsyncTcpConnection.java](https://app.codecov.io/gh/apache/iggy/pull/2606?src=pr&el=tree&filepath=foreign%2Fjava%2Fjava-sdk%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Figgy%2Fclient%2Fasync%2Ftcp%2FAsyncTcpConnection.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9qYXZhL2phdmEtc2RrL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9pZ2d5L2NsaWVudC9hc3luYy90Y3AvQXN5bmNUY3BDb25uZWN0aW9uLmphdmE=) | `61.62% <60.86%> (+3.44%)` | :arrow_up: |
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(java): implement Async Connection Pooling using FixedChannelPool [iggy]

Posted by "rythm-sachdeva (via GitHub)" <gi...@apache.org>.
rythm-sachdeva commented on PR #2606:
URL: https://github.com/apache/iggy/pull/2606#issuecomment-3872015233

   Hi @mmodzelewski Can you please review .


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org