-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eureka Client doesn't respect DNS server change at runtime #1157
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,15 +33,10 @@ public final class DnsResolver { | |
private static final String CNAME_RECORD_TYPE = "CNAME"; | ||
private static final String TXT_RECORD_TYPE = "TXT"; | ||
|
||
static final DirContext dirContext = getDirContext(); | ||
|
||
private DnsResolver() { | ||
} | ||
|
||
/** | ||
* Load up the DNS JNDI context provider. | ||
*/ | ||
public static DirContext getDirContext() { | ||
private DirContext getDirContext() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this is now called N+1 times, where N is number of zones. Are we okay with this? Isn't it time-consuming? (IMHO even if it is, it's not critical, but maybe somebody has another opinion). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not an expensive request, we don't make a network call, just initialize context where it re-check dns server on the local machine. Here the implementation of dns server resolution for Linux on AdoptOpenJDK 11 (a first link i can found) https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/unix/classes/sun/net/dns/ResolverConfigurationImpl.java |
||
Hashtable<String, String> env = new Hashtable<String, String>(); | ||
env.put(JAVA_NAMING_FACTORY_INITIAL, DNS_NAMING_FACTORY); | ||
env.put(JAVA_NAMING_PROVIDER_URL, DNS_PROVIDER_URL); | ||
|
@@ -58,15 +53,15 @@ public static DirContext getDirContext() { | |
* | ||
* @return resolved host name | ||
*/ | ||
public static String resolve(String originalHost) { | ||
public String resolve(String originalHost) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DnsResolver was written as a utility class with only static methods. This PR changes an implementation detail of this class so there's no need to make it possible to instantiate DnsResolver directly. Also, I'm concerned about making such breaking changes to public methods. The PR can be greatly simplified if the static change were to be reverted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elandau thanks for looking into this. |
||
String currentHost = originalHost; | ||
if (isLocalOrIp(currentHost)) { | ||
return originalHost; | ||
} | ||
try { | ||
String targetHost = null; | ||
do { | ||
Attributes attrs = dirContext.getAttributes(currentHost, new String[]{A_RECORD_TYPE, CNAME_RECORD_TYPE}); | ||
Attributes attrs = getDirContext().getAttributes(currentHost, new String[]{A_RECORD_TYPE, CNAME_RECORD_TYPE}); | ||
Attribute attr = attrs.get(A_RECORD_TYPE); | ||
if (attr != null) { | ||
targetHost = attr.get().toString(); | ||
|
@@ -92,12 +87,12 @@ public static String resolve(String originalHost) { | |
* @return resolved IP addresses or null if no A-record was present | ||
*/ | ||
@Nullable | ||
public static List<String> resolveARecord(String rootDomainName) { | ||
public List<String> resolveARecord(String rootDomainName) { | ||
if (isLocalOrIp(rootDomainName)) { | ||
return null; | ||
} | ||
try { | ||
Attributes attrs = dirContext.getAttributes(rootDomainName, new String[]{A_RECORD_TYPE, CNAME_RECORD_TYPE}); | ||
Attributes attrs = getDirContext().getAttributes(rootDomainName, new String[]{A_RECORD_TYPE, CNAME_RECORD_TYPE}); | ||
Attribute aRecord = attrs.get(A_RECORD_TYPE); | ||
Attribute cRecord = attrs.get(CNAME_RECORD_TYPE); | ||
if (aRecord != null && cRecord == null) { | ||
|
@@ -128,8 +123,8 @@ private static boolean isLocalOrIp(String currentHost) { | |
/** | ||
* Looks up the DNS name provided in the JNDI context. | ||
*/ | ||
public static Set<String> getCNamesFromTxtRecord(String discoveryDnsName) throws NamingException { | ||
Attributes attrs = dirContext.getAttributes(discoveryDnsName, new String[]{TXT_RECORD_TYPE}); | ||
public Set<String> getCNamesFromTxtRecord(String discoveryDnsName) throws NamingException { | ||
Attributes attrs = getDirContext().getAttributes(discoveryDnsName, new String[]{TXT_RECORD_TYPE}); | ||
Attribute attr = attrs.get(TXT_RECORD_TYPE); | ||
String txtRecord = null; | ||
if (attr != null) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can see that this method can
throw new RuntimeException
. It's okay to throw this when application starts: this is a critical problem which has rights to prevent application startup. But did you check what are the consequences of this happening at some point in time when application is already operating and just wants to refresh list of Eureka nodes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I have changed all invocations and ensure that this RuntimeException properly caught and logged.