You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2020/01/08 21:37:58 UTC

[airavata] 01/02: AIRAVATA-3186 Log abandoned thrift clients to slf4j logger

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

machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit 98efce7587cbc5680175e5697c7d359cb2c09771
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Jan 8 16:33:09 2020 -0500

    AIRAVATA-3186 Log abandoned thrift clients to slf4j logger
---
 .../apache/airavata/common/utils/ThriftClientPool.java  | 17 +++++++++++++++++
 .../airavata/common/utils/ThriftClientPoolTest.java     | 11 +++++++++++
 2 files changed, 28 insertions(+)

diff --git a/modules/commons/src/main/java/org/apache/airavata/common/utils/ThriftClientPool.java b/modules/commons/src/main/java/org/apache/airavata/common/utils/ThriftClientPool.java
index 6a0bc2a..f4980a3 100644
--- a/modules/commons/src/main/java/org/apache/airavata/common/utils/ThriftClientPool.java
+++ b/modules/commons/src/main/java/org/apache/airavata/common/utils/ThriftClientPool.java
@@ -19,6 +19,9 @@
  */
 package org.apache.airavata.common.utils;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
 import org.apache.airavata.base.api.BaseAPI;
 import org.apache.commons.pool2.BasePooledObjectFactory;
 import org.apache.commons.pool2.PooledObject;
@@ -40,6 +43,19 @@ public class ThriftClientPool<T extends BaseAPI.Client> implements AutoCloseable
 
     private final GenericObjectPool<T> internalPool;
 
+    /**
+     * StringWriter that flushes to SLF4J logger.
+     */
+    private static class ErrorLoggingStringWriter extends StringWriter {
+
+        @Override
+        public void flush() {
+            logger.error(this.toString());
+            // Reset buffer
+            this.getBuffer().setLength(0);
+        }
+    }
+
     public ThriftClientPool(ClientFactory<T> clientFactory, GenericObjectPoolConfig<T> poolConfig, String host,
             int port) {
         this(clientFactory, new BinaryOverSocketProtocolFactory(host, port), poolConfig);
@@ -54,6 +70,7 @@ public class ThriftClientPool<T extends BaseAPI.Client> implements AutoCloseable
             abandonedConfig.setLogAbandoned(true);
             abandonedConfig.setRemoveAbandonedOnBorrow(true);
             abandonedConfig.setRemoveAbandonedOnMaintenance(true);
+            abandonedConfig.setLogWriter(new PrintWriter(new ErrorLoggingStringWriter()));
         }
         this.internalPool = new GenericObjectPool<T>(new ThriftClientFactory(clientFactory, protocolFactory),
                 poolConfig, abandonedConfig);
diff --git a/modules/commons/src/test/java/org/apache/airavata/common/utils/ThriftClientPoolTest.java b/modules/commons/src/test/java/org/apache/airavata/common/utils/ThriftClientPoolTest.java
index 8bdefe9..5943fdf 100644
--- a/modules/commons/src/test/java/org/apache/airavata/common/utils/ThriftClientPoolTest.java
+++ b/modules/commons/src/test/java/org/apache/airavata/common/utils/ThriftClientPoolTest.java
@@ -1,5 +1,8 @@
 package org.apache.airavata.common.utils;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
 import org.apache.airavata.base.api.BaseAPI;
 import org.apache.airavata.common.exception.ApplicationSettingsException;
 import org.apache.commons.pool2.impl.AbandonedConfig;
@@ -71,6 +74,10 @@ public class ThriftClientPoolTest {
         abandonedConfig.setRemoveAbandonedTimeout(1);
         abandonedConfig.setRemoveAbandonedOnMaintenance(true);
         abandonedConfig.setLogAbandoned(true);
+        StringWriter log = new StringWriter();
+        Assert.assertEquals("Initial length of log is 0", 0, log.toString().length());
+        PrintWriter logWriter = new PrintWriter(log);
+        abandonedConfig.setLogWriter(logWriter);
         ThriftClientPool<BaseAPI.Client> thriftClientPool = new ThriftClientPool<>((protocol) -> mockClient, () -> null,
                 poolConfig, abandonedConfig);
         thriftClientPool.getResource();
@@ -82,6 +89,10 @@ public class ThriftClientPoolTest {
             Assert.fail("sleep interrupted");
         }
 
+        Assert.assertTrue(log.toString().length() > 0);
+        // The stack trace should contain this method's name
+        Assert.assertTrue(log.toString().contains("testWithAbandonConfigAndAbandoned"));
+
         new Verifications() {
             {
                 // Verify client is destroyed when abandoned