xxubai commented on code in PR #3920: URL: https://github.com/apache/amoro/pull/3920#discussion_r2887969126
########## amoro-ams/src/test/java/org/apache/amoro/server/ha/TestZkHighAvailabilityContainer.java: ########## @@ -0,0 +1,566 @@ +/* + * 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.amoro.server.ha; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.amoro.client.AmsServerInfo; +import org.apache.amoro.config.Configurations; +import org.apache.amoro.properties.AmsHAProperties; +import org.apache.amoro.server.AmoroManagementConf; +import org.apache.amoro.shade.zookeeper3.org.apache.curator.framework.CuratorFramework; +import org.apache.amoro.shade.zookeeper3.org.apache.curator.framework.recipes.leader.LeaderLatch; +import org.apache.amoro.shade.zookeeper3.org.apache.zookeeper.CreateMode; +import org.apache.amoro.shade.zookeeper3.org.apache.zookeeper.KeeperException; +import org.apache.amoro.shade.zookeeper3.org.apache.zookeeper.data.Stat; +import org.apache.amoro.utils.JacksonUtil; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +/** Test for HighAvailabilityContainer using mocked ZK to avoid connection issues. */ +public class TestZkHighAvailabilityContainer { + + private Configurations serviceConfig; + private HighAvailabilityContainer haContainer; + private MockZkState mockZkState; + private CuratorFramework mockZkClient; + private LeaderLatch mockLeaderLatch; + + @Before + public void setUp() throws Exception { + mockZkState = new MockZkState(); + mockZkClient = createMockZkClient(); + mockLeaderLatch = createMockLeaderLatch(); + + // Create test configuration + serviceConfig = new Configurations(); + serviceConfig.setString(AmoroManagementConf.SERVER_EXPOSE_HOST, "127.0.0.1"); + serviceConfig.setInteger(AmoroManagementConf.TABLE_SERVICE_THRIFT_BIND_PORT, 1260); + serviceConfig.setInteger(AmoroManagementConf.OPTIMIZING_SERVICE_THRIFT_BIND_PORT, 1261); + serviceConfig.setInteger(AmoroManagementConf.HTTP_SERVER_PORT, 1630); + serviceConfig.setBoolean(AmoroManagementConf.HA_ENABLE, true); + serviceConfig.setString(AmoroManagementConf.HA_ZOOKEEPER_ADDRESS, "127.0.0.1:2181"); + serviceConfig.setString(AmoroManagementConf.HA_CLUSTER_NAME, "test-cluster"); + } + + @After + public void tearDown() throws Exception { + if (haContainer != null) { + haContainer.close(); + } + mockZkState.clear(); + } + + @Test + public void testRegisterAndElectWithoutMasterSlaveMode() throws Exception { + // Test that node registration is skipped when master-slave mode is disabled + serviceConfig.setBoolean(AmoroManagementConf.USE_MASTER_SLAVE_MODE, false); + haContainer = createContainerWithMockZk(); + + // Should not throw exception and should not register node + haContainer.registerAndElect(); + + // Verify no node was registered + String nodesPath = AmsHAProperties.getNodesPath("test-cluster"); + List<String> children = mockZkState.getChildren(nodesPath); + Assert.assertEquals( + "No nodes should be registered when master-slave mode is disabled", 0, children.size()); + } + + @Test + public void testRegisterAndElectWithMasterSlaveMode() throws Exception { + // Test that node registration works when master-slave mode is enabled + serviceConfig.setBoolean(AmoroManagementConf.USE_MASTER_SLAVE_MODE, true); + haContainer = createContainerWithMockZk(); + + // Register node + haContainer.registerAndElect(); + + // Verify node was registered + String nodesPath = AmsHAProperties.getNodesPath("test-cluster"); + List<String> children = mockZkState.getChildren(nodesPath); + Assert.assertEquals("One node should be registered", 1, children.size()); + + // Verify node data + String nodePath = nodesPath + "/" + children.get(0); + byte[] data = mockZkState.getData(nodePath); + Assert.assertNotNull("Node data should not be null", data); + Assert.assertTrue("Node data should not be empty", data.length > 0); + + // Verify node info + String nodeInfoJson = new String(data, StandardCharsets.UTF_8); + AmsServerInfo nodeInfo = JacksonUtil.parseObject(nodeInfoJson, AmsServerInfo.class); + Assert.assertEquals("Host should match", "127.0.0.1", nodeInfo.getHost()); + Assert.assertEquals( + "Thrift port should match", Integer.valueOf(1260), nodeInfo.getThriftBindPort()); Review Comment: ```suggestion "Thrift port should match", Integer.valueOf(1261), nodeInfo.getThriftBindPort()); ``` -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
