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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8aab196a50d [fix](fe) Close BDB JE group administrators after use 
(#70210)
8aab196a50d is described below

commit 8aab196a50dd58b39b0d625fedee2188e5b5b260
Author: Gavin Chou <[email protected]>
AuthorDate: Tue Nov 24 18:43:46 2026 +0800

    [fix](fe) Close BDB JE group administrators after use (#70210)
    
    ### What problem does this PR solve?
    
    Related PR: https://github.com/apache/doris-thirdparty/pull/380
    
    `BDBHA` creates a fresh `ReplicationGroupAdmin` for every HA operation
    but leaves it open. An SSL-backed administrator owns a
    certificate-monitor executor, so repeated metadata queries and HA
    operations can retain its monitor threads. JE `18.3.18-doris-SNAPSHOT`,
    already used by master, exposes `close()` to release these resources.
    
    Close administrators in `finally` blocks at all seven `BDBHA` call
    sites. Preserve existing return values and exception handling, and cover
    nested conflict-node removal as well as errors. The default non-SSL path
    retains its behavior.
    
    Add `BdbHaTest` with real JE administrator construction, SSL factories,
    certificate monitors and resource shutdown. Mockito spies replace only
    network RPCs. The test explicitly supplies the SSL configuration, so it
    exercises the ownership contract independently of the production
    administrator's default network configuration. Reuse the existing
    regression certificate and its matching client key converted to PKCS#8.
    
    The PKCS#8 fixture is the same public regression key as the existing
    `client-key.pem`: OpenSSL confirms both encodings and `client-cert.pem`
    have identical public keys. Add an exact-path allowlist for this fixture
    to the existing Gitleaks `private-key` rule, matching the convention
    already used for the MySQL TLS fixture.
    
    ### Release note
    
    Release BDB JE group administrators after frontend metadata and HA
    operations, preventing certificate-monitor thread retention when using
    an SSL-backed administrator.
---
 .gitleaks.toml                                     |   4 +
 .../src/main/java/org/apache/doris/ha/BDBHA.java   |  70 ++++---
 .../test/java/org/apache/doris/ha/BdbHaTest.java   | 225 +++++++++++++++++++++
 .../ssl_default_certificate/client-key-pkcs8.pem   |  28 +++
 4 files changed, 303 insertions(+), 24 deletions(-)

diff --git a/.gitleaks.toml b/.gitleaks.toml
index 6de6d3834bb..1d47f34d878 100644
--- a/.gitleaks.toml
+++ b/.gitleaks.toml
@@ -99,3 +99,7 @@ id = "private-key"
 [[rules.allowlists]]
 description = "Ignore test-only TLS private key used by MySQL docker-compose 
fixtures"
 paths = ['''^docker/thirdparties/docker-compose/mysql/certs/server\.key$''']
+
+[[rules.allowlists]]
+description = "Ignore the PKCS#8 form of the existing regression client TLS 
test key"
+paths = ['''^regression-test/ssl_default_certificate/client-key-pkcs8\.pem$''']
diff --git a/fe/fe-core/src/main/java/org/apache/doris/ha/BDBHA.java 
b/fe/fe-core/src/main/java/org/apache/doris/ha/BDBHA.java
index 5270bf19c54..a1b5f84313f 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/ha/BDBHA.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/ha/BDBHA.java
@@ -119,6 +119,8 @@ public class BDBHA implements HAProtocol {
         } catch (UnknownMasterException e) {
             LOG.warn("Catch UnknownMasterException when calling 
getObserverNodes.", e);
             return Lists.newArrayList();
+        } finally {
+            replicationGroupAdmin.close();
         }
         return ret;
     }
@@ -146,6 +148,8 @@ public class BDBHA implements HAProtocol {
         } catch (UnknownMasterException e) {
             LOG.warn("Catch UnknownMasterException when calling 
getElectableNodes.", e);
             return Lists.newArrayList();
+        } finally {
+            replicationGroupAdmin.close();
         }
         return ret;
     }
@@ -153,10 +157,16 @@ public class BDBHA implements HAProtocol {
     @Override
     public InetSocketAddress getLeader() {
         ReplicationGroupAdmin replicationGroupAdmin = 
environment.getReplicationGroupAdmin();
-        String leaderName = replicationGroupAdmin.getMasterNodeName();
-        ReplicationGroup rg = replicationGroupAdmin.getGroup();
-        ReplicationNode rn = rg.getMember(leaderName);
-        return rn.getSocketAddress();
+        try {
+            String leaderName = replicationGroupAdmin.getMasterNodeName();
+            ReplicationGroup rg = replicationGroupAdmin.getGroup();
+            ReplicationNode rn = rg.getMember(leaderName);
+            return rn.getSocketAddress();
+        } finally {
+            if (replicationGroupAdmin != null) {
+                replicationGroupAdmin.close();
+            }
+        }
     }
 
     @Override
@@ -174,6 +184,8 @@ public class BDBHA implements HAProtocol {
         } catch (Exception e) {
             LOG.error("remove electable node {} meeting unkown exception:", 
nodeName, e);
             System.exit(-1);
+        } finally {
+            replicationGroupAdmin.close();
         }
         return true;
     }
@@ -192,6 +204,8 @@ public class BDBHA implements HAProtocol {
         } catch (MasterStateException e) {
             LOG.error("the updating electable node is master {}", nodeName, e);
             return false;
+        } finally {
+            replicationGroupAdmin.close();
         }
         return true;
     }
@@ -202,16 +216,20 @@ public class BDBHA implements HAProtocol {
             return;
         }
 
-        List<String> conflictNodes = Lists.newArrayList();
-        Set<ReplicationNode> replicationNodes = 
replicationGroupAdmin.getGroup().getElectableNodes();
-        for (ReplicationNode replicationNode : replicationNodes) {
-            if (replicationNode.getHostName().equals(host) && 
replicationNode.getPort() == port) {
-                conflictNodes.add(replicationNode.getName());
+        try {
+            List<String> conflictNodes = Lists.newArrayList();
+            Set<ReplicationNode> replicationNodes = 
replicationGroupAdmin.getGroup().getElectableNodes();
+            for (ReplicationNode replicationNode : replicationNodes) {
+                if (replicationNode.getHostName().equals(host) && 
replicationNode.getPort() == port) {
+                    conflictNodes.add(replicationNode.getName());
+                }
             }
-        }
 
-        for (String conflictNode : conflictNodes) {
-            removeElectableNode(conflictNode);
+            for (String conflictNode : conflictNodes) {
+                removeElectableNode(conflictNode);
+            }
+        } finally {
+            replicationGroupAdmin.close();
         }
     }
 
@@ -253,20 +271,24 @@ public class BDBHA implements HAProtocol {
         if (replicationGroupAdmin == null) {
             return;
         }
-        Set<ReplicationNode> replicationNodes = 
replicationGroupAdmin.getGroup().getElectableNodes();
-        LOG.debug("removedFrontends:{}", removedFrontends);
-        for (ReplicationNode replicationNode : replicationNodes) {
-            LOG.debug("node:{}", replicationNode.toString());
-            if (removedFrontends.contains(replicationNode.getName())) {
-                try {
-                    
replicationGroupAdmin.removeMember(replicationNode.getName());
-                } catch (MemberNotFoundException e) {
-                    LOG.warn("the electable node is not found {}", 
replicationNode.getName());
-                } catch (Exception e) {
-                    LOG.error("remove electable node {} meeting unknown 
exception:", replicationNode.getName(), e);
-                    System.exit(-1);
+        try {
+            Set<ReplicationNode> replicationNodes = 
replicationGroupAdmin.getGroup().getElectableNodes();
+            LOG.debug("removedFrontends:{}", removedFrontends);
+            for (ReplicationNode replicationNode : replicationNodes) {
+                LOG.debug("node:{}", replicationNode.toString());
+                if (removedFrontends.contains(replicationNode.getName())) {
+                    try {
+                        
replicationGroupAdmin.removeMember(replicationNode.getName());
+                    } catch (MemberNotFoundException e) {
+                        LOG.warn("the electable node is not found {}", 
replicationNode.getName());
+                    } catch (Exception e) {
+                        LOG.error("remove electable node {} meeting unknown 
exception:", replicationNode.getName(), e);
+                        System.exit(-1);
+                    }
                 }
             }
+        } finally {
+            replicationGroupAdmin.close();
         }
     }
 }
diff --git a/fe/fe-core/src/test/java/org/apache/doris/ha/BdbHaTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/ha/BdbHaTest.java
new file mode 100644
index 00000000000..d0bfb21b0fe
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/ha/BdbHaTest.java
@@ -0,0 +1,225 @@
+// 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.
+
+package org.apache.doris.ha;
+
+import org.apache.doris.common.jmockit.Deencapsulation;
+import org.apache.doris.journal.bdbje.BDBEnvironment;
+
+import com.sleepycat.je.rep.MemberNotFoundException;
+import com.sleepycat.je.rep.ReplicationGroup;
+import com.sleepycat.je.rep.ReplicationNode;
+import com.sleepycat.je.rep.ReplicationSSLConfig;
+import com.sleepycat.je.rep.UnknownMasterException;
+import com.sleepycat.je.rep.util.ReplicationGroupAdmin;
+import com.sleepycat.je.rep.utilint.net.SSLChannelFactory;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.mockito.Mockito;
+
+import java.net.InetSocketAddress;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ScheduledExecutorService;
+
+public class BdbHaTest {
+    private static final String MASTER = "test-master";
+    private static final InetSocketAddress ADDRESS = new 
InetSocketAddress("127.0.0.1", 9010);
+
+    private final List<ReplicationGroupAdmin> admins = new ArrayList<>();
+    private final List<ScheduledExecutorService> monitors = new ArrayList<>();
+    private BDBHA ha;
+    private boolean fail;
+    private String keyStore;
+
+    enum Operation {
+        GET_OBSERVER_NODES,
+        GET_ELECTABLE_NODES,
+        GET_ELECTABLE_NODES_WITHOUT_LEADER,
+        GET_LEADER,
+        REMOVE_ELECTABLE_NODE,
+        UPDATE_NODE_ADDRESS,
+        REMOVE_CONFLICT_NODE,
+        REMOVE_DROPPED_MEMBER
+    }
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        keyStore = System.getProperty("javax.net.ssl.keyStore");
+        System.clearProperty("javax.net.ssl.keyStore");
+        Path certificates = 
Paths.get(System.getenv().getOrDefault("DORIS_HOME", "../.."),
+                "regression-test", "ssl_default_certificate");
+        ReplicationSSLConfig config = new ReplicationSSLConfig();
+        config.setSSLPemCaCertFile(certificates.resolve("ca.pem").toString());
+        
config.setSSLPemCertFile(certificates.resolve("client-cert.pem").toString());
+        // PKCS#8 form of the existing regression-test client key, as required 
by JE.
+        
config.setSSLPemKeyFile(certificates.resolve("client-key-pkcs8.pem").toString());
+        config.setSSLCertRefreshIntervalSeconds(3600);
+
+        ReplicationNode node = Mockito.mock(ReplicationNode.class);
+        Mockito.when(node.getName()).thenReturn(MASTER);
+        Mockito.when(node.getHostName()).thenReturn(ADDRESS.getHostString());
+        Mockito.when(node.getPort()).thenReturn(ADDRESS.getPort());
+        Mockito.when(node.getSocketAddress()).thenReturn(ADDRESS);
+        ReplicationGroup group = Mockito.mock(ReplicationGroup.class);
+        Mockito.when(group.getMember(MASTER)).thenReturn(node);
+        
Mockito.when(group.getElectableNodes()).thenReturn(Collections.singleton(node));
+        
Mockito.when(group.getSecondaryNodes()).thenReturn(Collections.singleton(node));
+
+        BDBEnvironment environment = Mockito.mock(BDBEnvironment.class);
+        
Mockito.when(environment.getReplicationGroupAdmin()).thenAnswer(invocation -> {
+            // Keep real JE construction, SSL factories, schedulers and 
close().
+            // Only replace RPCs, so these tests need no running replication 
group.
+            ReplicationGroupAdmin admin = Mockito.spy(new 
ReplicationGroupAdmin("test-group",
+                    Collections.singleton(ADDRESS), config));
+            Mockito.doAnswer(call -> {
+                if (fail) {
+                    throw new UnknownMasterException("test failure");
+                }
+                return group;
+            }).when(admin).getGroup();
+            Mockito.doReturn(MASTER).when(admin).getMasterNodeName();
+            Mockito.doAnswer(call -> {
+                if (fail) {
+                    throw new MemberNotFoundException("test failure");
+                }
+                return null;
+            }).when(admin).removeMember(Mockito.anyString());
+            Mockito.doAnswer(call -> {
+                if (fail) {
+                    throw new MemberNotFoundException("test failure");
+                }
+                return null;
+            }).when(admin).updateAddress(Mockito.anyString(), 
Mockito.anyString(), Mockito.anyInt());
+            admins.add(admin);
+            SSLChannelFactory factory = Deencapsulation.getField(admin, 
"channelFactory");
+            ScheduledExecutorService monitor = 
Deencapsulation.getField(factory, "certificateCheckExecutor");
+            Assertions.assertNotNull(monitor, "The test must enable real PEM 
certificate monitoring");
+            monitors.add(monitor);
+            return admin;
+        });
+        ha = new BDBHA(environment, MASTER);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        // Also clean up when run against the unfixed implementation.
+        for (ReplicationGroupAdmin admin : admins) {
+            admin.close();
+        }
+        if (keyStore == null) {
+            System.clearProperty("javax.net.ssl.keyStore");
+        } else {
+            System.setProperty("javax.net.ssl.keyStore", keyStore);
+        }
+    }
+
+    @ParameterizedTest
+    @EnumSource(Operation.class)
+    public void testReleasesCertificateMonitor(Operation operation) {
+        invoke(operation);
+        assertMonitorsReleased();
+    }
+
+    @ParameterizedTest
+    @EnumSource(Operation.class)
+    public void testReleasesCertificateMonitorOnFailure(Operation operation) {
+        fail = true;
+        switch (operation) {
+            case GET_LEADER:
+            case REMOVE_CONFLICT_NODE:
+            case REMOVE_DROPPED_MEMBER:
+                Assertions.assertThrows(UnknownMasterException.class, () -> 
invoke(operation));
+                break;
+            default:
+                invoke(operation);
+        }
+        assertMonitorsReleased();
+    }
+
+    @Test
+    public void testRepeatedFrontendQueriesDoNotAccumulateMonitors() {
+        for (int i = 0; i < 40; i++) {
+            ha.getLeader();
+            ha.getElectableNodes(true);
+            ha.getObserverNodes();
+        }
+        Assertions.assertEquals(120, monitors.size());
+        assertMonitorsReleased();
+    }
+
+    @Test
+    public void testNoAvailableAdmin() {
+        BDBHA empty = new BDBHA(Mockito.mock(BDBEnvironment.class), MASTER);
+        Assertions.assertTrue(empty.getObserverNodes().isEmpty());
+        Assertions.assertTrue(empty.getElectableNodes(true).isEmpty());
+        Assertions.assertThrows(NullPointerException.class, empty::getLeader);
+        Assertions.assertFalse(empty.removeElectableNode(MASTER));
+        Assertions.assertFalse(empty.updateNodeAddress(MASTER, "127.0.0.2", 
9010));
+        empty.removeConflictNodeIfExist(ADDRESS.getHostString(), 
ADDRESS.getPort());
+        empty.removeDroppedMember(new ConcurrentLinkedQueue<>());
+        Assertions.assertTrue(admins.isEmpty());
+    }
+
+    private void assertMonitorsReleased() {
+        Assertions.assertFalse(monitors.isEmpty());
+        for (ScheduledExecutorService monitor : monitors) {
+            Assertions.assertTrue(monitor.isTerminated(), "BDB SSL certificate 
monitor was not released");
+        }
+    }
+
+    private void invoke(Operation operation) {
+        switch (operation) {
+            case GET_OBSERVER_NODES:
+                Assertions.assertEquals(fail ? Collections.emptyList() : 
Collections.singletonList(ADDRESS),
+                        ha.getObserverNodes());
+                break;
+            case GET_ELECTABLE_NODES:
+                Assertions.assertEquals(fail ? Collections.emptyList() : 
Collections.singletonList(ADDRESS),
+                        ha.getElectableNodes(true));
+                break;
+            case GET_ELECTABLE_NODES_WITHOUT_LEADER:
+                Assertions.assertTrue(ha.getElectableNodes(false).isEmpty());
+                break;
+            case GET_LEADER:
+                Assertions.assertEquals(ADDRESS, ha.getLeader());
+                break;
+            case REMOVE_ELECTABLE_NODE:
+                Assertions.assertEquals(!fail, ha.removeElectableNode(MASTER));
+                break;
+            case UPDATE_NODE_ADDRESS:
+                Assertions.assertEquals(!fail, ha.updateNodeAddress(MASTER, 
"127.0.0.2", 9010));
+                break;
+            case REMOVE_CONFLICT_NODE:
+                ha.removeConflictNodeIfExist(ADDRESS.getHostString(), 
ADDRESS.getPort());
+                break;
+            case REMOVE_DROPPED_MEMBER:
+                ha.removeDroppedMember(new 
ConcurrentLinkedQueue<>(Collections.singleton(MASTER)));
+                break;
+            default:
+                throw new AssertionError(operation);
+        }
+    }
+}
diff --git a/regression-test/ssl_default_certificate/client-key-pkcs8.pem 
b/regression-test/ssl_default_certificate/client-key-pkcs8.pem
new file mode 100644
index 00000000000..4dad2d1b2e0
--- /dev/null
+++ b/regression-test/ssl_default_certificate/client-key-pkcs8.pem
@@ -0,0 +1,28 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtZmgvKcVuRNNe
+vLetGg5PTIj5VYTxCz9x0qyFPx5w2taCSgxsG0YSg9qG0L4G4x/Iex01LHojLLGB
+57Z/jUyqF+9z2yxkVur3c08Tuttvy/4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/2h
+d78al8/ISfUiwiu1gQQrTJWpmaqm7QDmtFy7KZIL1qvdjfLvDibFHlDLReUCgYAx
+incy+D03tivqijPXf+/ydmOZ2jIUy45zWHv9l7JskXeQf0BxDobItldQ4wa7UVpl
+bacf07dWA9ubKMQ3KzM/E3iAcXpD3qNQ3LZiPLhd8VE0zWxIPmm6kbsVdE84U9/P
+70rd6fxaXL4Jmkt0tJAqQ7rIjsHnch95kNjAFEMD2QKBgCfN4ib5zxqFDoc1Z1G2
+4x094//awfIlX22/9lOyLZACB93iuqYYSEzbR4mnhFDMHrzAgMxlyhI/zGkAJlBj
+AflFm9DzOjndJeuBaBvpdiejyVFzRzGHmgsgu2AIDG3/bQ9ykXHkiCHThMg6xhjY
+qynnpxskvq7u7IGcrDBrhekK
+-----END PRIVATE KEY-----


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to