Skip to content

Commit 62b0451

Browse files
lilinjun1884风雪夜归人
authored andcommitted
update version to 5.16.2
1 parent 42c4998 commit 62b0451

File tree

5 files changed

+196
-3
lines changed

5 files changed

+196
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.aliyun.openservices</groupId>
55
<artifactId>tablestore</artifactId>
6-
<version>5.16.1</version>
6+
<version>5.16.2</version>
77
<packaging>jar</packaging>
88
<name>AliCloud TableStore SDK for Java</name>
99
<url>http://www.aliyun.com</url>

src/main/java/com/alicloud/openservices/tablestore/ClientConfiguration.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class ClientConfiguration {
4141
*/
4242
private boolean enableRequestTracer = false;
4343

44+
/**
45+
* DNS缓存相关配置 默认不开启
46+
*/
47+
private boolean enableDnsCache = false;
48+
private int dnsCacheMaxSize = 100;
49+
private int dnsCacheExpireAfterWriteSec = 600;
50+
private int dnsCacheRefreshAfterWriteSec = 300;
51+
4452

4553
/**
4654
* 链路追踪系统接口
@@ -463,6 +471,60 @@ public void setEnableRequestTracer(boolean enableRequestTracer) {
463471
}
464472

465473

474+
/**
475+
* 获取DNS缓存开启状态
476+
*/
477+
public boolean isEnableDnsCache() {
478+
return enableDnsCache;
479+
}
480+
481+
/**
482+
* 设置是否开启DNS缓存
483+
* @param enableDnsCache 控制是否打开DNS缓存的配置
484+
*/
485+
public void setEnableDnsCache(boolean enableDnsCache) {
486+
this.enableDnsCache = enableDnsCache;
487+
}
488+
489+
/**
490+
* 获取DNS缓存的条目数
491+
*/
492+
public int getDnsCacheMaxSize() {
493+
return dnsCacheMaxSize;
494+
}
495+
496+
/**
497+
* 获取DNS缓存的过期时间,单位秒
498+
*/
499+
public int getDnsCacheExpireAfterWriteSec() {
500+
return dnsCacheExpireAfterWriteSec;
501+
}
502+
503+
/**
504+
* 设置DNS缓存的过期时间,单位秒
505+
*/
506+
public void setDnsCacheExpireAfterWriteSec(int dnsCacheExpireAfterWriteSec) {
507+
Preconditions.checkArgument(dnsCacheExpireAfterWriteSec > 0, "The dns cache expire after write seconds must be greater than 0.");
508+
Preconditions.checkArgument(dnsCacheExpireAfterWriteSec <= 600, "The dns cache expire after write seconds must be less than or equal to 600s.");
509+
this.dnsCacheExpireAfterWriteSec = dnsCacheExpireAfterWriteSec;
510+
}
511+
512+
/**
513+
* 获取DNS缓存的刷新频率,单位秒
514+
*/
515+
public int getDnsCacheRefreshAfterWriteSec() {
516+
return dnsCacheRefreshAfterWriteSec;
517+
}
518+
519+
/**
520+
* 设置DNS缓存的刷新频率,单位秒
521+
*/
522+
public void setDnsCacheRefreshAfterWriteSec(int dnsCacheRefreshAfterWriteSec) {
523+
Preconditions.checkArgument(dnsCacheRefreshAfterWriteSec > 0, "The dns cache refresh after write seconds must be greater than 0.");
524+
Preconditions.checkArgument(dnsCacheRefreshAfterWriteSec < this.dnsCacheExpireAfterWriteSec, "The dns cache refresh after write seconds must be less than dnsCacheExpireAfterWriteSec.");
525+
this.dnsCacheRefreshAfterWriteSec = dnsCacheRefreshAfterWriteSec;
526+
}
527+
466528
/**
467529
* 设置接入链路追踪系统接口
468530
*

src/main/java/com/alicloud/openservices/tablestore/core/http/AsyncServiceClient.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77

88
import com.alicloud.openservices.tablestore.RequestTracer;
99
import org.apache.http.HttpHost;
10-
import org.apache.http.HttpRequest;
1110
import org.apache.http.concurrent.FutureCallback;
11+
import org.apache.http.config.Registry;
12+
import org.apache.http.config.RegistryBuilder;
13+
import org.apache.http.conn.DnsResolver;
1214
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
1315
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
1416
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
1517
import org.apache.http.impl.nio.reactor.IOReactorConfig;
1618
import org.apache.http.nio.conn.NHttpClientConnectionManager;
19+
import org.apache.http.nio.conn.NoopIOSessionStrategy;
20+
import org.apache.http.nio.conn.SchemeIOSessionStrategy;
21+
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
1722
import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
1823
import org.apache.http.nio.reactor.ConnectingIOReactor;
1924
import org.apache.http.nio.reactor.IOReactorException;
@@ -37,8 +42,16 @@ public AsyncServiceClient(ClientConfiguration config) {
3742
.setIoThreadCount(config.getIoThreadCount()).build();
3843
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(
3944
ioReactorConfig);
45+
Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
46+
.register("http", NoopIOSessionStrategy.INSTANCE)
47+
.register("https", SSLIOSessionStrategy.getDefaultStrategy())
48+
.build();
49+
DnsResolver dnsResolver = null;
50+
if (config.isEnableDnsCache()) {
51+
dnsResolver = new CachedDnsResolver(config.getDnsCacheMaxSize(), config.getDnsCacheExpireAfterWriteSec(), config.getDnsCacheRefreshAfterWriteSec());
52+
}
4053
PoolingNHttpClientConnectionManager cm =
41-
new PoolingNHttpClientConnectionManager(ioReactor);
54+
new PoolingNHttpClientConnectionManager(ioReactor, null, iosessionFactoryRegistry, dnsResolver);
4255
cm.setMaxTotal(config.getMaxConnections());
4356
cm.setDefaultMaxPerRoute(config.getMaxConnections());
4457
httpClient = HttpFactory.createHttpAsyncClient(config, cm);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.alicloud.openservices.tablestore.core.http;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import com.google.common.cache.Cache;
8+
import com.google.common.cache.CacheBuilder;
9+
import com.google.common.cache.CacheLoader;
10+
import com.google.common.cache.CacheStats;
11+
import com.google.common.cache.RemovalListener;
12+
import com.google.common.cache.RemovalNotification;
13+
import com.google.common.util.concurrent.ListenableFuture;
14+
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
public class CachedDnsResolver extends SystemDefaultDnsResolver {
19+
private final Logger logger = LoggerFactory.getLogger(CachedDnsResolver.class);
20+
private final Cache<String, InetAddress[]> dnsCache;
21+
22+
public CachedDnsResolver(int maxSize, int expireAfterWriteSec, int refreshAfterWriteSec) {
23+
RemovalListener<String, InetAddress[]> rmListener = new RemovalListener<String, InetAddress[]>() {
24+
@Override
25+
public void onRemoval(RemovalNotification<String, InetAddress[]> notify) {
26+
if (logger.isDebugEnabled()) {
27+
logger.debug("dns cache remove host key: {}, reason: {}", notify.getKey(), notify.getCause().name());
28+
}
29+
}
30+
};
31+
32+
dnsCache = CacheBuilder.newBuilder()
33+
.maximumSize(maxSize)
34+
.weakKeys()
35+
.expireAfterWrite(expireAfterWriteSec, TimeUnit.SECONDS)
36+
.refreshAfterWrite(refreshAfterWriteSec, TimeUnit.SECONDS)
37+
.removalListener(rmListener)
38+
.build(new CacheLoader<String, InetAddress[]>() {
39+
@Override
40+
public InetAddress[] load(String host) throws Exception {
41+
if (logger.isDebugEnabled()) {
42+
logger.debug("dns cache load, host: {}", host);
43+
}
44+
return CachedDnsResolver.super.resolve(host);
45+
}
46+
47+
@Override
48+
public ListenableFuture<InetAddress[]> reload(String host, InetAddress[] oldValue) throws Exception {
49+
if (logger.isDebugEnabled()) {
50+
logger.debug("dns cache reload, host: {}", host);
51+
}
52+
return super.reload(host, oldValue);
53+
}
54+
});
55+
}
56+
57+
public long cacheSize() {
58+
return dnsCache.size();
59+
}
60+
61+
public CacheStats cacheStat() {
62+
return dnsCache.stats();
63+
}
64+
65+
@Override
66+
public InetAddress[] resolve(String host) throws UnknownHostException {
67+
// 需要 getIfPresent and put. get(key, Callable) 会覆盖cache对象上的load和reload定义
68+
InetAddress[] cached = dnsCache.getIfPresent(host);
69+
if (logger.isDebugEnabled()) {
70+
logger.debug("dns resolve, host: {}, has cached: {}", host, cached != null);
71+
}
72+
if (cached == null) {
73+
InetAddress[] realtime = super.resolve(host);
74+
if (realtime != null) {
75+
dnsCache.put(host, realtime);
76+
}
77+
return realtime;
78+
}
79+
return cached;
80+
}
81+
82+
public void clear() {
83+
dnsCache.cleanUp();
84+
}
85+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.alicloud.openservices.tablestore.core.http;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
6+
import junit.framework.TestCase;
7+
import org.junit.Assert;
8+
9+
10+
public class CachedDnsResolverTest extends TestCase {
11+
public void testResolve() throws InterruptedException, UnknownHostException {
12+
CachedDnsResolver dnsResolver = new CachedDnsResolver(100, 10, 2);
13+
try {
14+
String domain = "taobao.com";
15+
InetAddress[] resolvedAddresses = dnsResolver.resolve(domain);
16+
assertEquals(1, dnsResolver.cacheSize());
17+
18+
for (InetAddress address : resolvedAddresses) {
19+
Assert.assertNotNull(address.getHostAddress());
20+
}
21+
22+
//Thread.sleep(13000);
23+
//CacheStats cacheStats = dnsResolver.cacheStat();
24+
//assertEquals(1, cacheStats.missCount());
25+
//dnsResolver.resolve(domain);
26+
//assertEquals(1, dnsResolver.cacheSize());
27+
//Thread.sleep(3000);
28+
//assertEquals(0, dnsResolver.cacheSize());
29+
} finally {
30+
dnsResolver.clear();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)