You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/24 03:11:19 UTC

[GitHub] [pulsar] gaozhangmin commented on a diff in pull request #16165: [improve][client] [PIP-165] Auto release client useless connections

gaozhangmin commented on code in PR #16165:
URL: https://github.com/apache/pulsar/pull/16165#discussion_r904978622


##########
pulsar-client-api/src/main/java/org/apache/pulsar/client/api/ClientBuilder.java:
##########
@@ -566,4 +572,9 @@ ClientBuilder authentication(String authPluginClassName, Map<String, String> aut
      * @return
      */
     ClientBuilder socks5ProxyPassword(String socks5ProxyPassword);
+
+    /**
+     * Disable The "auto release useless connections" feature.

Review Comment:
   ```suggestion
        * Disable The "auto release idle connections" feature.
   ```



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ClientCnx.java:
##########
@@ -161,6 +162,16 @@ public class ClientCnx extends PulsarHandler {
     protected AuthenticationDataProvider authenticationDataProvider;
     private TransactionBufferHandler transactionBufferHandler;
 
+    /** Create time. **/
+    private final long createTime;
+    /** The time when marks the connection is idle. **/

Review Comment:
   ```suggestion
       /** The time when the connection is marked as idle. **/
   ```



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ClientCnx.java:
##########
@@ -1188,4 +1201,168 @@ private void checkRequestTimeout() {
     }
 
     private static final Logger log = LoggerFactory.getLogger(ClientCnx.class);
+
+    /**
+     * Check client connection is now free. This method may change the state to idle.
+     * This method will not change the state to idle.
+     * @return this connection is idle now.
+     */
+    public boolean idleCheck(){
+        if (pendingRequests != null && !pendingRequests.isEmpty()){
+            return false;
+        }
+        if (waitingLookupRequests != null  && !waitingLookupRequests.isEmpty()){
+            return false;
+        }
+        if (!consumers.isEmpty()){
+            return false;
+        }
+        if (!producers.isEmpty()){
+            return false;
+        }
+        if (!transactionMetaStoreHandlers.isEmpty()){
+            return false;
+        }
+        return true;
+    }
+    /**
+     * Get idle-stat.
+     * @return connection idle-stat
+     */
+    public IdleState getIdleStat(){
+        return STATE_UPDATER.get(this);
+    }
+    /**
+     * Compare and switch idle-stat.
+     * @return Whether the update is successful.Because there may be other threads competing, possible return false.
+     */
+    public boolean compareAndSetIdleStat(IdleState originalStat, IdleState newStat){
+        return STATE_UPDATER.compareAndSet(this, originalStat, newStat);
+    }
+
+    /**
+     * Indicates the usage status of the connection and whether it has been released.
+     */
+    public enum IdleState {
+        /** The connection is in use. **/
+        USING,
+        /** The connection is in idle. **/
+        IDLE_MARKED,
+        /** The connection is in idle and will be released soon. **/
+        BEFORE_RELEASE,
+        /** The connection has already been released. **/
+        RELEASED;
+    }
+
+    /**
+     * @return Whether this connection is in use.
+     */
+    public boolean isUsing(){
+        return getIdleStat() == IdleState.USING;
+    }
+
+    /**
+     * @return Whether this connection is in idle.
+     */
+    public boolean isIdle(){
+        return getIdleStat() == IdleState.IDLE_MARKED;
+    }
+
+    /**
+     * @return Whether this connection is in idle and will be released soon.
+     */
+    public boolean isWillBeRelease(){
+        return getIdleStat() == IdleState.BEFORE_RELEASE;
+    }
+
+    /**
+     * @return Whether this connection has already been released.
+     */
+    public boolean alreadyRelease(){
+        return getIdleStat() == IdleState.RELEASED;
+    }
+
+    /**
+     * Changes the idle-state of the connection to #{@link IdleState#IDLE_MARKED}, This method only changes this
+     * connection from the #{@link IdleState#USING} state to the #{@link IdleState#IDLE_MARKED} state. if the
+     * idle-status is successfully changed, "idleMarkTime" is changed to current time.
+     * @return Whether change idle-stat to #{@link IdleState#IDLE_MARKED} success.
+     */
+    public boolean tryMarkIdle(){
+        if (!compareAndSetIdleStat(IdleState.USING, IdleState.IDLE_MARKED)){
+            return isIdle();
+        }
+        this.idleMarkTime = System.currentTimeMillis();
+        return true;
+    }

Review Comment:
   ```suggestion
       /**
        * Try to transform the state of the connection to #{@link IdleState#IDLE_MARKED}, state should only be transformed to #{@link IdleState#IDLE_MARKED} from state  #{@link IdleState#USING}. if the
        * state is successfully transformed, "idleMarkTime" will be  assigned to current time.
        */
       public void tryMarkIdle(){
           if (compareAndSetIdleStat(IdleState.USING, IdleState.IDLE_MARKED)){
               this.idleMarkTime = System.currentTimeMillis();
           }
       }
   ```



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ClientCnx.java:
##########
@@ -1188,4 +1201,168 @@ private void checkRequestTimeout() {
     }
 
     private static final Logger log = LoggerFactory.getLogger(ClientCnx.class);
+
+    /**
+     * Check client connection is now free. This method may change the state to idle.
+     * This method will not change the state to idle.
+     * @return this connection is idle now.
+     */

Review Comment:
   ```suggestion
   
       /**
        * Check if the client connection is eligible for being marked as idle. 
        *  if so, {@link org.apache.pulsar.client.impl.ClientCnx#tryMarkIdle()} will be invoked next.
        * @return if the connection is idle.
        */
   ```



-- 
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@pulsar.apache.org

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