-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply version range change and enhance DNS resolver
- Loading branch information
Showing
7 changed files
with
111 additions
and
66 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
58 changes: 58 additions & 0 deletions
58
clickhouse-client/src/main/java/com/clickhouse/client/naming/SrvResolver.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,58 @@ | ||
package com.clickhouse.client.naming; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import com.clickhouse.client.ClickHouseDnsResolver; | ||
import com.clickhouse.client.ClickHouseProtocol; | ||
import com.clickhouse.client.logging.Logger; | ||
import com.clickhouse.client.logging.LoggerFactory; | ||
|
||
import org.xbill.DNS.Lookup; | ||
import org.xbill.DNS.Record; | ||
import org.xbill.DNS.SRVRecord; | ||
import org.xbill.DNS.TextParseException; | ||
import org.xbill.DNS.Type; | ||
|
||
public class SrvResolver extends ClickHouseDnsResolver { | ||
private static final Logger log = LoggerFactory.getLogger(SrvResolver.class); | ||
|
||
protected SRVRecord lookup(String srvDns, boolean basedOnWeight) { | ||
Record[] records = null; | ||
try { | ||
records = new Lookup(srvDns, Type.SRV).run(); | ||
} catch (TextParseException e) { | ||
// fallback to a cached entry? | ||
log.warn("Not able to resolve given DNS query: [%s]", srvDns, e); | ||
} | ||
|
||
SRVRecord record = null; | ||
if (records != null) { | ||
if (basedOnWeight) { | ||
for (int i = 0; i < records.length; i++) { | ||
SRVRecord rec = (SRVRecord) records[i]; | ||
if (record == null || record.getWeight() > rec.getWeight()) { | ||
record = rec; | ||
} | ||
} | ||
} else { | ||
record = (SRVRecord) records[0]; | ||
} | ||
} | ||
|
||
return record; | ||
} | ||
|
||
@Override | ||
public InetSocketAddress resolve(ClickHouseProtocol protocol, String host, int port) { | ||
if (protocol == null || host == null) { | ||
throw new IllegalArgumentException("Non-null protocol and host are required"); | ||
} | ||
|
||
SRVRecord r = lookup(host, false); | ||
if (r != null) { | ||
host = r.getName().canonicalize().toString(true); | ||
port = r.getPort(); | ||
} | ||
return new InetSocketAddress(host, port); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
clickhouse-client/src/test/java/com/clickhouse/client/naming/SrvResolverTest.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,27 @@ | ||
package com.clickhouse.client.naming; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import com.clickhouse.client.ClickHouseProtocol; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class SrvResolverTest { | ||
@Test(groups = { "integration" }) | ||
public void testLookup() { | ||
String dns = "_sip._udp.sip.voice.google.com"; | ||
Assert.assertNotNull(new SrvResolver().lookup(dns, true)); | ||
Assert.assertNotNull(new SrvResolver().lookup(dns, false)); | ||
} | ||
|
||
@Test(groups = { "integration" }) | ||
public void testResolv() throws Exception { | ||
String host = "_sip._udp.sip.voice.google.com"; | ||
int port = 5060; | ||
|
||
String dns = "_sip._udp.sip.voice.google.com"; | ||
Assert.assertEquals(new SrvResolver().resolve(ClickHouseProtocol.ANY, host, 0), | ||
new InetSocketAddress(host, port)); | ||
} | ||
} |
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