You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by fl...@apache.org on 2019/01/13 17:39:41 UTC

[tinkerpop] 01/01: Explain connection pool exceptions TINKERPOP-2131

This is an automated email from the ASF dual-hosted git repository.

florianhockmann pushed a commit to branch TINKERPOP-2131
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit a9a81642796f7f90f98d8a06e17a76695d14473b
Author: Florian Hockmann <fh...@florian-hockmann.de>
AuthorDate: Sun Jan 13 18:19:10 2019 +0100

    Explain connection pool exceptions TINKERPOP-2131
    
    Two new exception classes are added to better reveal the reason for a
    NoConnectionAvailableException:
    * The pool is empty because the server is unavailable
        -> ServerUnavailableException
    * Are connections have reached the max in-flight requests limit
        -> ConnectionPoolBusyException
    Both extend NoConnectionAvailableException so this is a non-breaking
    change.
---
 .../src/Gremlin.Net/Driver/ConnectionPool.cs       | 15 +++---
 .../Exceptions/ConnectionPoolBusyException.cs      | 60 ++++++++++++++++++++++
 .../Exceptions/ServerUnavailableException.cs       | 45 ++++++++++++++++
 .../Driver/ConnectionPoolTests.cs                  |  6 ++-
 4 files changed, 116 insertions(+), 10 deletions(-)

diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
index 708580b..4ea57e4 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
@@ -61,9 +61,7 @@ namespace Gremlin.Net.Driver
         public async Task<IConnection> GetAvailableConnectionAsync()
         {
             await EnsurePoolIsPopulatedAsync().ConfigureAwait(false);
-            if (TryGetConnectionFromPool(out var connection))
-                return ProxiedConnection(connection);
-            throw new NoConnectionAvailableException("no connection available!");
+            return ProxiedConnection(GetConnectionFromPool());
         }
 
         private async Task EnsurePoolIsPopulatedAsync()
@@ -113,15 +111,16 @@ namespace Gremlin.Net.Driver
             return newConnection;
         }
 
-        private bool TryGetConnectionFromPool(out Connection connection)
+        private Connection GetConnectionFromPool()
         {
             while (true)
             {
-                connection = SelectLeastUsedConnection();
-                if (connection == null) return false; // _connections is empty
+                var connection = SelectLeastUsedConnection();
+                if (connection == null)
+                    throw new ServerUnavailableException();
                 if (connection.NrRequestsInFlight >= _maxInProcessPerConnection)
-                    return false;
-                if (connection.IsOpen) return true;
+                    throw new ConnectionPoolBusyException(_poolSize, _maxInProcessPerConnection);
+                if (connection.IsOpen) return connection;
                 DefinitelyDestroyConnection(connection);
             }
         }
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ConnectionPoolBusyException.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ConnectionPoolBusyException.cs
new file mode 100644
index 0000000..6cd40ff
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ConnectionPoolBusyException.cs
@@ -0,0 +1,60 @@
+#region License
+
+/*
+ * 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.
+ */
+
+#endregion
+
+namespace Gremlin.Net.Driver.Exceptions
+{
+    /// <summary>
+    ///     The exception that is thrown when all connections in the pool have reached their maximum number of in-flight
+    ///     requests.
+    /// </summary>
+    public class ConnectionPoolBusyException : NoConnectionAvailableException
+    {
+        /// <summary>
+        ///     Gets the size of the connection pool.
+        /// </summary>
+        public int PoolSize { get; }
+
+        /// <summary>
+        ///     Gets the maximum number of in-flight requests that can occur on a connection.
+        /// </summary>
+        public int MaxInProcessPerConnection { get; }
+
+        /// <summary>
+        ///     Initializes a new instance of the <see cref="ConnectionPoolBusyException" /> class.
+        /// </summary>
+        public ConnectionPoolBusyException(int poolSize, int maxInProcessPerConnection)
+            : base(CreateMessage(poolSize, maxInProcessPerConnection))
+        {
+            PoolSize = poolSize;
+            MaxInProcessPerConnection = maxInProcessPerConnection;
+        }
+
+        private static string CreateMessage(int poolSize, int maxInProcessPerConnection)
+        {
+            return $"All {poolSize} connections have reached their " +
+                   $"{nameof(ConnectionPoolSettings.MaxInProcessPerConnection)} limit of {maxInProcessPerConnection}." +
+                   $" Consider increasing either the {nameof(ConnectionPoolSettings.PoolSize)} or the " +
+                   $"{nameof(ConnectionPoolSettings.MaxInProcessPerConnection)} limit.";
+        }
+    }
+}
\ No newline at end of file
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ServerUnavailableException.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ServerUnavailableException.cs
new file mode 100644
index 0000000..a4b467a
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Exceptions/ServerUnavailableException.cs
@@ -0,0 +1,45 @@
+#region License
+
+/*
+ * 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.
+ */
+
+#endregion
+
+namespace Gremlin.Net.Driver.Exceptions
+{
+    /// <summary>
+    ///     The exception that is thrown when no connection is available to the <see cref="GremlinServer"/>.
+    /// </summary>
+    public class ServerUnavailableException : NoConnectionAvailableException
+    {
+        /// <summary>
+        ///     Initializes a new instance of the <see cref="ServerUnavailableException" /> class.
+        /// </summary>
+        public ServerUnavailableException()
+            : base(CreateMessage())
+        {
+        }
+
+        private static string CreateMessage()
+        {
+            return
+                "No connection to the server available which most likely means that the server is completely unavailable.";
+        }
+    }
+}
\ No newline at end of file
diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/ConnectionPoolTests.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/ConnectionPoolTests.cs
index a4c8f04..c4f5f7c 100644
--- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/ConnectionPoolTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/ConnectionPoolTests.cs
@@ -63,15 +63,17 @@ namespace Gremlin.Net.IntegrationTest.Driver
         }
 
         [Fact]
-        public async Task ShouldThrowNoConnectionAvailableExceptionWhenNoConnectionIsAvailable()
+        public async Task ShouldThrowConnectionPoolBusyExceptionWhenPoolIsBusy()
         {
             const int nrParallelRequests = 2;
             using (var gremlinClient = CreateGremlinClient(connectionPoolSize: 1, maxInProcessPerConnection: 1))
             {
                 const int sleepTime = 100;
 
-                await Assert.ThrowsAsync<NoConnectionAvailableException>(() =>
+                var thrownException = await Assert.ThrowsAsync<ConnectionPoolBusyException>(() =>
                     ExecuteMultipleLongRunningRequestsInParallel(gremlinClient, nrParallelRequests, sleepTime));
+                Assert.Contains(nameof(ConnectionPoolSettings.MaxInProcessPerConnection), thrownException.Message);
+                Assert.Contains(nameof(ConnectionPoolSettings.PoolSize), thrownException.Message);
             }
         }