You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by GitBox <gi...@apache.org> on 2021/04/20 15:13:46 UTC

[GitHub] [tinkerpop] spmallette opened a new pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

spmallette opened a new pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418


   https://issues.apache.org/jira/browse/TINKERPOP-2550
   
   This change fixes a potential deadlock issue on `Client` initialization and may have some impact on https://issues.apache.org/jira/browse/TINKERPOP-2504 as well. I won't re-document the points of this change here as I went to considerable lengths to explain it in code comments. Please see the rationale for the changes there.
   
   VOTE +1


-- 
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.

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



[GitHub] [tinkerpop] divijvaidya commented on a change in pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
divijvaidya commented on a change in pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#discussion_r617508796



##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);

Review comment:
       starting and shutting down executor is an expensive operation which will impact the client start up/shut down time which will impact use cases when customers have short running clients such as in AWS Lambda. Is there an alternative approach that can avoid this?




-- 
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.

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



[GitHub] [tinkerpop] spmallette commented on a change in pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
spmallette commented on a change in pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#discussion_r618615663



##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);
             try {
                 CompletableFuture.allOf(cluster.allHosts().stream()
-                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), cluster.executor()))
+                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), hostExecutor))
                         .toArray(CompletableFuture[]::new))
                         .join();
             } catch (CompletionException ex) {
-                Throwable cause = null;
-                Throwable result = ex;
-                if (null != (cause = ex.getCause())) {
-                    result = cause;
-                }
-
-                logger.error("", result);
+                logger.error("", ExceptionUtils.getRootCause(ex));

Review comment:
       really?! ok - that's not what i thought happened. i will switch back. 




-- 
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.

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



[GitHub] [tinkerpop] spmallette merged pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
spmallette merged pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418


   


-- 
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.

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



[GitHub] [tinkerpop] spmallette commented on a change in pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
spmallette commented on a change in pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#discussion_r618618406



##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);
             try {
                 CompletableFuture.allOf(cluster.allHosts().stream()
-                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), cluster.executor()))
+                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), hostExecutor))
                         .toArray(CompletableFuture[]::new))
                         .join();
             } catch (CompletionException ex) {
-                Throwable cause = null;
-                Throwable result = ex;
-                if (null != (cause = ex.getCause())) {
-                    result = cause;
-                }
-
-                logger.error("", result);
+                logger.error("", ExceptionUtils.getRootCause(ex));

Review comment:
       man...we use `getRootCause()` everywhere....................kinda want to fix that everywhere now. how many places is this a problem hiding 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.

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



[GitHub] [tinkerpop] divijvaidya commented on pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
divijvaidya commented on pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#issuecomment-825660854


   Vote +1


-- 
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.

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



[GitHub] [tinkerpop] divijvaidya commented on a change in pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
divijvaidya commented on a change in pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#discussion_r618612970



##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);

Review comment:
       That's fair. I am fine with your proposed change.

##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);
             try {
                 CompletableFuture.allOf(cluster.allHosts().stream()
-                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), cluster.executor()))
+                        .map(host -> CompletableFuture.runAsync(() -> initializeConnectionSetupForHost.accept(host), hostExecutor))
                         .toArray(CompletableFuture[]::new))
                         .join();
             } catch (CompletionException ex) {
-                Throwable cause = null;
-                Throwable result = ex;
-                if (null != (cause = ex.getCause())) {
-                    result = cause;
-                }
-
-                logger.error("", result);
+                logger.error("", ExceptionUtils.getRootCause(ex));

Review comment:
       ExceptionUtils.getRootCause returns null if there is no caused by in the stack trace. That is the reason, we had the prior custom written code logic.




-- 
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.

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



[GitHub] [tinkerpop] spmallette commented on a change in pull request #1418: TINKERPOP-2550 Fixed bug in java Client initialization

Posted by GitBox <gi...@apache.org>.
spmallette commented on a change in pull request #1418:
URL: https://github.com/apache/tinkerpop/pull/1418#discussion_r617542428



##########
File path: gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Client.java
##########
@@ -511,19 +515,24 @@ protected Connection chooseConnection(final RequestMessage msg) throws TimeoutEx
          */
         @Override
         protected void initializeImplementation() {
+            // use a special executor here to initialize the Host instances as the worker thread pool may be
+            // insufficiently sized for this task and the parallel initialization of the ConnectionPool. if too small
+            // tasks may be schedule in such a way as to produce a deadlock: TINKERPOP-2550
+            //
+            // the cost of this single threaded executor here should be fairly small because it is only used once at
+            // initialization and shutdown. since users will typically construct a Client once for the life of their
+            // application there shouldn't be tons of thread pools being created and destroyed.
+            final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("gremlin-driver-init-%d").build();
+            final ExecutorService hostExecutor = Executors.newSingleThreadExecutor(threadFactory);

Review comment:
       can we not take a few milliseconds to fix this in a simple way? in multiple tests of 3.4.10 vs this change, the longest time difference i've seen is like 3.5 milliseconds extra time per open/close and many times the gap is even less. too much still?




-- 
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.

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