Skip to content

Commit

Permalink
FEAT: Add check DNS cache ttl method.
Browse files Browse the repository at this point in the history
  • Loading branch information
brido4125 committed Mar 21, 2024
1 parent d388cee commit a2aeaf4
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
<exclude>**/RedistributeFailureModeTest.java</exclude>
<exclude>**/LoggingTest.java</exclude>
</excludes>
<argLine>--illegal-access=permit</argLine>
</configuration>
</plugin>
<plugin>
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/net/spy/memcached/ArcusClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.URL;
import java.security.Security;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -201,6 +202,10 @@ public class ArcusClient extends FrontCacheMemcachedClient implements ArcusClien

private static final int SHUTDOWN_TIMEOUT_MILLISECONDS = 2000;

private static final String DNS_CACHE_TTL_PROP = "networkaddress.cache.ttl";
private static final String SUN_NET_POLICY_PROP = "sun.net.InetAddressCachePolicy";
private static final int MAX_DNS_CACHE_TTL = 300;

private CacheManager cacheManager;

public void setCacheManager(CacheManager cacheManager) {
Expand Down Expand Up @@ -313,6 +318,12 @@ protected static ArcusClient getInstance(ConnectionFactory cf,
public ArcusClient(ConnectionFactory cf, String name, List<InetSocketAddress> addrs)
throws IOException {
super(cf, name, addrs);

if (cf.getDnsCacheTtlCheck() && !isValidDnsCacheTtl()) {
getLogger().warn("The minimum value for DNS cache TTL is 0 and the maximum value is " + MAX_DNS_CACHE_TTL +
"Invoke ConnectionFactoryBuilder.setDnsCacheTtlCheck(false) to avoid this validation.");
throw new RuntimeException("The value of the DNS cache TTL is less than 0 or greater than" + MAX_DNS_CACHE_TTL);
}
collectionTranscoder = new CollectionTranscoder();
smgetKeyChunkSize = cf.getDefaultMaxSMGetKeyChunkSize();
registerMbean();
Expand All @@ -327,10 +338,47 @@ public ArcusClient(ConnectionFactory cf, String name, List<InetSocketAddress> ad
*/
public ArcusClient(ConnectionFactory cf, List<InetSocketAddress> addrs)
throws IOException {
super(cf, DEFAULT_ARCUS_CLIENT_NAME, addrs);
collectionTranscoder = new CollectionTranscoder();
smgetKeyChunkSize = cf.getDefaultMaxSMGetKeyChunkSize();
registerMbean();
this(cf, DEFAULT_ARCUS_CLIENT_NAME, addrs);
}

private static boolean isValidDnsCacheTtl() {
Integer ttl = null;
try {
ttl = (Integer) Class.forName(SUN_NET_POLICY_PROP)
.getMethod("get")
.invoke(null);
} catch (Throwable t) {
arcusLogger.error("Can not access to " + SUN_NET_POLICY_PROP);
}
if (ttl != null) {
if (ttl < 0 || ttl > MAX_DNS_CACHE_TTL) {
return false;
}
}

String security = null;
String system = null;
// Get DNS cache TTL from System property.
try {
security = Security.getProperty(DNS_CACHE_TTL_PROP);
system = System.getProperty(DNS_CACHE_TTL_PROP);
} catch (NumberFormatException e) {
arcusLogger.error("Can not get DNS cache TTL value by {}", e.getCause());
}

if (security != null) {
int securityTtl = Integer.parseInt(security);
if (securityTtl < 0 || securityTtl > MAX_DNS_CACHE_TTL) {
return false;
}
}
if (system != null) {
int systemTtl = Integer.parseInt(system);
if (systemTtl < 0 || systemTtl > MAX_DNS_CACHE_TTL) {
return false;
}
}
return System.getSecurityManager() == null;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ MemcachedNode createMemcachedNode(String name,
*/
boolean getKeepAlive();

/**
* If true, check DNS cache TTL when invoking ArcusClient constructor.
*/
boolean getDnsCacheTtlCheck();

/**
* Observers that should be established at the time of connection
* instantiation.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/spy/memcached/ConnectionFactoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class ConnectionFactoryBuilder {
private boolean shouldOptimize = false;
private boolean useNagle = false;
private boolean keepAlive = false;
private boolean dnsCacheTtlCheck = true;
//private long maxReconnectDelay =
//DefaultConnectionFactory.DEFAULT_MAX_RECONNECT_DELAY;
private long maxReconnectDelay = 1;
Expand Down Expand Up @@ -448,6 +449,12 @@ public ConnectionFactoryBuilder setKeepAlive(boolean on) {
keepAlive = on;
return this;
}

public ConnectionFactoryBuilder setDnsCacheTtlCheck(boolean dnsCacheTtlCheck) {
this.dnsCacheTtlCheck = dnsCacheTtlCheck;
return this;
}

/**
* Get the ConnectionFactory set up with the provided parameters.
*/
Expand Down Expand Up @@ -577,6 +584,11 @@ public boolean getKeepAlive() {
return keepAlive;
}

@Override
public boolean getDnsCacheTtlCheck() {
return dnsCacheTtlCheck;
}

@Override
public long getMaxReconnectDelay() {
return maxReconnectDelay;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ public boolean useNagleAlgorithm() {
public boolean getKeepAlive() {
return false;
}

@Override
public boolean getDnsCacheTtlCheck() {
return true;
}

public boolean shouldOptimize() {
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/net/spy/memcached/ClientBaseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public boolean getKeepAlive() {
return inner.getKeepAlive();
}

@Override
public boolean getDnsCacheTtlCheck() {
return inner.getDnsCacheTtlCheck();
}

@Override
public Collection<ConnectionObserver> getInitialObservers() {
return observers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void testDefaults() throws Exception {
assertFalse(f.shouldOptimize());
assertFalse(f.useNagleAlgorithm());
assertFalse(f.getKeepAlive());
assertTrue(f.getDnsCacheTtlCheck());
assertEquals(f.getOpQueueMaxBlockTime(),
DefaultConnectionFactory.DEFAULT_OP_QUEUE_MAX_BLOCK_TIME);
}
Expand Down Expand Up @@ -132,6 +133,7 @@ public void connectionEstablished(SocketAddress sa, int reconnectCount) {
.setTranscoder(new WhalinTranscoder())
.setUseNagleAlgorithm(true)
.setKeepAlive(true)
.setDnsCacheTtlCheck(false)
.setLocatorType(Locator.CONSISTENT)
.setOpQueueMaxBlockTime(19)
.setAuthDescriptor(anAuthDescriptor)
Expand All @@ -152,6 +154,7 @@ public void connectionEstablished(SocketAddress sa, int reconnectCount) {
assertFalse(f.shouldOptimize());
assertTrue(f.useNagleAlgorithm());
assertTrue(f.getKeepAlive());
assertFalse(f.getDnsCacheTtlCheck());
assertEquals(f.getOpQueueMaxBlockTime(), 19);
assertSame(anAuthDescriptor, f.getAuthDescriptor());

Expand Down

0 comments on commit a2aeaf4

Please sign in to comment.