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

abhi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a09509d44 RANGER-4943: Error in ElasticSearchAuditDestination 
shutting down RestHighLevelClient client (#397)
0a09509d44 is described below

commit 0a09509d44ebf809aba663e09bf772a52d2fe3ce
Author: Fernando Arribas Jara <[email protected]>
AuthorDate: Wed Jan 22 00:02:42 2025 +0100

    RANGER-4943: Error in ElasticSearchAuditDestination shutting down 
RestHighLevelClient client (#397)
    
    Co-authored-by: Abhishek Kumar <[email protected]>
---
 .../destination/ElasticSearchAuditDestination.java | 43 +++++++++++++---------
 1 file changed, 26 insertions(+), 17 deletions(-)

diff --git 
a/agents-audit/src/main/java/org/apache/ranger/audit/destination/ElasticSearchAuditDestination.java
 
b/agents-audit/src/main/java/org/apache/ranger/audit/destination/ElasticSearchAuditDestination.java
index 5c536ec4ef..12982f4c09 100644
--- 
a/agents-audit/src/main/java/org/apache/ranger/audit/destination/ElasticSearchAuditDestination.java
+++ 
b/agents-audit/src/main/java/org/apache/ranger/audit/destination/ElasticSearchAuditDestination.java
@@ -49,6 +49,7 @@
 import javax.security.auth.kerberos.KerberosTicket;
 
 import java.io.File;
+import java.io.IOException;
 import java.security.PrivilegedActionException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -319,32 +320,30 @@ private String connectionString() {
     }
 
     private RestHighLevelClient newClient() {
+        RestHighLevelClient restHighLevelClient = null;
+
         try {
             if (StringUtils.isNotBlank(user) && 
StringUtils.isNotBlank(password) && password.contains("keytab") && new 
File(password).exists()) {
                 subject = CredentialsProviderUtil.login(user, password);
             }
 
             RestClientBuilder restClientBuilder = getRestClientBuilder(hosts, 
protocol, user, password, port);
+            restHighLevelClient = new RestHighLevelClient(restClientBuilder);
+            boolean exists = false;
 
-            try (RestHighLevelClient restHighLevelClient = new 
RestHighLevelClient(restClientBuilder)) {
-                LOG.debug("Initialized client");
-
-                boolean exists = false;
-
-                try {
-                    exists = restHighLevelClient.indices().open(new 
OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
-                } catch (Exception e) {
-                    LOG.warn("Error validating index {}", this.index);
-                }
-
-                if (exists) {
-                    LOG.debug("Index exists");
-                } else {
-                    LOG.info("Index does not exist");
-                }
+            try {
+                exists = restHighLevelClient.indices().open(new 
OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
+            } catch (Exception e) {
+                LOG.warn("Error validating index {}", this.index);
+            }
 
-                return restHighLevelClient;
+            if (exists) {
+                LOG.debug("Index exists");
+            } else {
+                LOG.info("Index does not exist");
             }
+
+            return restHighLevelClient;
         } catch (Throwable t) {
             lastLoggedAt.updateAndGet(lastLoggedAt -> {
                 long now     = System.currentTimeMillis();
@@ -358,6 +357,16 @@ private RestHighLevelClient newClient() {
                     return lastLoggedAt;
                 }
             });
+
+            if (restHighLevelClient != null) {
+                try {
+                    restHighLevelClient.close();
+                    LOG.debug("Closed RestHighLevelClient after failure");
+                } catch (IOException e) {
+                    LOG.warn("Error closing RestHighLevelClient: {}", 
e.getMessage(), e);
+                }
+            }
+
             return null;
         }
     }

Reply via email to