This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support resolving meta host through DNS (#60)
- Loading branch information
1 parent
6f38c0b
commit 278525a
Showing
8 changed files
with
453 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/xiaomi/infra/pegasus/rpc/async/HostNameResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2019, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
package com.xiaomi.infra.pegasus.rpc.async; | ||
|
||
import com.xiaomi.infra.pegasus.base.rpc_address; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
/* | ||
* Resolves host:port into a set of ip addresses. | ||
* The intention of this class is to mock DNS. | ||
*/ | ||
public class HostNameResolver { | ||
|
||
public rpc_address[] resolve(String hostPort) throws IllegalArgumentException { | ||
String[] pairs = hostPort.split(":"); | ||
if (pairs.length != 2) { | ||
throw new IllegalArgumentException("Meta server host name format error!"); | ||
} | ||
|
||
try { | ||
Integer port = Integer.valueOf(pairs[1]); | ||
InetAddress[] resolvedAddresses = InetAddress.getAllByName(pairs[0]); | ||
rpc_address[] results = new rpc_address[resolvedAddresses.length]; | ||
int size = 0; | ||
for (InetAddress addr : resolvedAddresses) { | ||
rpc_address rpcAddr = new rpc_address(); | ||
int ip = ByteBuffer.wrap(addr.getAddress()).order(ByteOrder.BIG_ENDIAN).getInt(); | ||
rpcAddr.address = ((long) ip << 32) + ((long) port << 16) + 1; | ||
results[size++] = rpcAddr; | ||
} | ||
return results; | ||
} catch (UnknownHostException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/test/java/com/xiaomi/infra/pegasus/base/TestRpcAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2019, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
package com.xiaomi.infra.pegasus.base; | ||
|
||
import com.xiaomi.infra.pegasus.rpc.async.HostNameResolver; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TestRpcAddress { | ||
@Test | ||
public void testResolveFromHostPort() throws Exception { | ||
HostNameResolver hostNameResolver = new HostNameResolver(); | ||
rpc_address[] addrs = hostNameResolver.resolve("127.0.0.1:34601"); | ||
|
||
Assert.assertNotNull(addrs); | ||
Assert.assertEquals(addrs.length, 1); | ||
Assert.assertEquals(addrs[0].get_ip(), "127.0.0.1"); | ||
Assert.assertEquals(addrs[0].get_port(), 34601); | ||
|
||
addrs = hostNameResolver.resolve("www.baidu.com:80"); | ||
Assert.assertNotNull(addrs); | ||
Assert.assertTrue(addrs.length >= 1); | ||
|
||
addrs = hostNameResolver.resolve("abcabcabcabc:34601"); | ||
Assert.assertNull(addrs); | ||
|
||
try { | ||
addrs = hostNameResolver.resolve("localhost"); | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
Assert.assertNull(addrs); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFromString() throws Exception { | ||
rpc_address addr = new rpc_address(); | ||
Assert.assertTrue(addr.fromString("127.0.0.1:34601")); | ||
Assert.assertEquals(addr.get_ip(), "127.0.0.1"); | ||
Assert.assertEquals(addr.get_port(), 34601); | ||
} | ||
} |
Oops, something went wrong.