branch-2.1: [fix](cluster)fix fe host may be contains Scope Identifier #52076 (#52398)

Cherry-picked from #52076

Co-authored-by: zhangdong <zhangdong@selectdb.com>
This commit is contained in:
github-actions[bot]
2025-06-28 10:09:22 +08:00
committed by GitHub
parent 74a409d74e
commit d7184af32b
2 changed files with 65 additions and 1 deletions

View File

@ -183,7 +183,20 @@ public class FrontendOptions {
// so we call localAddr.getCanonicalHostName() at here
return localAddr.getCanonicalHostName();
}
return InetAddresses.toAddrString(localAddr);
return getIpByLocalAddr(localAddr);
}
/**
* get ip from addr
* @param addr InetAddress
* @return ip
*/
public static String getIpByLocalAddr(InetAddress addr) {
String addrString = InetAddresses.toAddrString(addr);
if (addrString.contains("%")) {
addrString = addrString.split("%")[0];
}
return addrString;
}
private static void analyzePriorityCidrs() {

View File

@ -0,0 +1,51 @@
// 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.service;
import org.apache.doris.common.AnalysisException;
import com.google.common.net.InetAddresses;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
public class FrontendOptionsTest {
@Mocked
private InetAddresses inetAddresses;
@Before
public void setUp() throws NoSuchMethodException, SecurityException, AnalysisException {
new Expectations() {
{
inetAddresses.toAddrString((InetAddress) any);
minTimes = 0;
result = "2408:400a:5a:ea00:2fb5:112e:39dd:9bba%eth0";
}
};
}
@Test
public void testGetIpByLocalAddr() {
String ip = FrontendOptions.getIpByLocalAddr(null);
Assert.assertEquals("2408:400a:5a:ea00:2fb5:112e:39dd:9bba", ip);
}
}