Skip to content

Commit 967c41e

Browse files
lilinjun1884风雪夜归人
authored andcommitted
update version to 5.16.3
1 parent 62b0451 commit 967c41e

File tree

4 files changed

+111
-13
lines changed

4 files changed

+111
-13
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.2</version>
6+
<version>5.16.3</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/core/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class Constants {
66
// ALL HTTP HEADERS SHOULD BE DEFINED IN LOWERCASE
77
// request headers
8-
public static final String USER_AGENT = "ots-java-sdk 5.16.1";
8+
public static final String USER_AGENT = "ots-java-sdk 5.16.3";
99
public static final String OTS_HEADER_API_VERSION = "x-ots-apiversion";
1010
public static final String OTS_HEADER_ACCESS_KEY_ID = "x-ots-accesskeyid";
1111
public static final String OTS_HEADER_OTS_CONTENT_MD5 = "x-ots-contentmd5";

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.net.InetAddress;
44
import java.net.UnknownHostException;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ScheduledExecutorService;
7+
import java.util.concurrent.ThreadFactory;
58
import java.util.concurrent.TimeUnit;
69

710
import com.google.common.cache.Cache;
@@ -17,6 +20,7 @@
1720

1821
public class CachedDnsResolver extends SystemDefaultDnsResolver {
1922
private final Logger logger = LoggerFactory.getLogger(CachedDnsResolver.class);
23+
private static final int SCHEDULED_CORE_POOL_SIZE = 2;
2024
private final Cache<String, InetAddress[]> dnsCache;
2125

2226
public CachedDnsResolver(int maxSize, int expireAfterWriteSec, int refreshAfterWriteSec) {
@@ -29,29 +33,29 @@ public void onRemoval(RemovalNotification<String, InetAddress[]> notify) {
2933
}
3034
};
3135

36+
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(SCHEDULED_CORE_POOL_SIZE, new ThreadFactory() {
37+
@Override
38+
public Thread newThread(Runnable r) {
39+
Thread thread = new Thread(r);
40+
thread.setName("refresh-dns-cache-" + thread.getId());
41+
return thread;
42+
}
43+
});
3244
dnsCache = CacheBuilder.newBuilder()
3345
.maximumSize(maxSize)
3446
.weakKeys()
3547
.expireAfterWrite(expireAfterWriteSec, TimeUnit.SECONDS)
3648
.refreshAfterWrite(refreshAfterWriteSec, TimeUnit.SECONDS)
3749
.removalListener(rmListener)
38-
.build(new CacheLoader<String, InetAddress[]>() {
50+
.build(CacheLoader.asyncReloading(new CacheLoader<String, InetAddress[]>() {
3951
@Override
4052
public InetAddress[] load(String host) throws Exception {
4153
if (logger.isDebugEnabled()) {
4254
logger.debug("dns cache load, host: {}", host);
4355
}
4456
return CachedDnsResolver.super.resolve(host);
4557
}
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-
});
58+
}, scheduler));
5559
}
5660

5761
public long cacheSize() {

src/test/java/com/alicloud/openservices/tablestore/core/http/CachedDnsResolverTest.java

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22

33
import java.net.InetAddress;
44
import java.net.UnknownHostException;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ScheduledExecutorService;
7+
import java.util.concurrent.ThreadFactory;
8+
import java.util.concurrent.TimeUnit;
59

10+
import com.google.common.cache.Cache;
11+
import com.google.common.cache.CacheBuilder;
12+
import com.google.common.cache.CacheLoader;
13+
import com.google.common.cache.RemovalListener;
14+
import com.google.common.cache.RemovalNotification;
615
import junit.framework.TestCase;
16+
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
717
import org.junit.Assert;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
820

921

1022
public class CachedDnsResolverTest extends TestCase {
@@ -30,4 +42,86 @@ public void testResolve() throws InterruptedException, UnknownHostException {
3042
dnsResolver.clear();
3143
}
3244
}
33-
}
45+
46+
public void testAsyncResolve() throws UnknownHostException, InterruptedException {
47+
TestCachedDnsResolver resolver = new TestCachedDnsResolver(
48+
100, 20, 3);
49+
50+
for (int i = 0; i < 10; ++i) {
51+
long start = System.currentTimeMillis();
52+
InetAddress[] resolve = resolver.resolve("taobao.com");
53+
long end = System.currentTimeMillis();
54+
System.out.println(end - start);
55+
56+
Thread.sleep(2000);
57+
}
58+
}
59+
}
60+
61+
class BaseResolver extends SystemDefaultDnsResolver {
62+
63+
@Override
64+
public InetAddress[] resolve(String host) throws UnknownHostException {
65+
try {
66+
Thread.sleep(5000);
67+
} catch (InterruptedException e) {
68+
throw new RuntimeException(e);
69+
}
70+
return super.resolve(host);
71+
}
72+
}
73+
74+
class TestCachedDnsResolver extends BaseResolver {
75+
private final Logger logger = LoggerFactory.getLogger(CachedDnsResolverTest.class);
76+
private final Cache<String, InetAddress[]> dnsCache;
77+
78+
public TestCachedDnsResolver(int maxSize, int expireAfterWriteSec, int refreshAfterWriteSec) {
79+
RemovalListener<String, InetAddress[]> rmListener = new RemovalListener<String, InetAddress[]>() {
80+
@Override
81+
public void onRemoval(RemovalNotification<String, InetAddress[]> notify) {
82+
logger.info("dns cache remove host key: {}, reason: {}", notify.getKey(), notify.getCause().name());
83+
}
84+
};
85+
86+
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2, new ThreadFactory() {
87+
@Override
88+
public Thread newThread(Runnable r) {
89+
Thread thread = new Thread(r);
90+
thread.setName("refresh-dns-cache-" + thread.getId());
91+
return thread;
92+
}
93+
});
94+
dnsCache = CacheBuilder.newBuilder()
95+
.maximumSize(maxSize)
96+
.weakKeys()
97+
.expireAfterWrite(expireAfterWriteSec, TimeUnit.SECONDS)
98+
.refreshAfterWrite(refreshAfterWriteSec, TimeUnit.SECONDS)
99+
.removalListener(rmListener)
100+
.build(CacheLoader.asyncReloading(new CacheLoader<String, InetAddress[]>() {
101+
@Override
102+
public InetAddress[] load(String host) throws Exception {
103+
logger.info("dns cache load, host: {}", host);
104+
return TestCachedDnsResolver.super.resolve(host);
105+
}
106+
}, scheduler));
107+
}
108+
109+
110+
@Override
111+
public InetAddress[] resolve(String host) throws UnknownHostException {
112+
InetAddress[] cached = dnsCache.getIfPresent(host);
113+
logger.info("dns resolve, host: {}, has cached: {}", host, cached!=null);
114+
if (cached==null) {
115+
InetAddress[] realtime = super.resolve(host);
116+
if (realtime!=null) {
117+
dnsCache.put(host, realtime);
118+
}
119+
return realtime;
120+
}
121+
return cached;
122+
}
123+
124+
public void clear() {
125+
dnsCache.cleanUp();
126+
}
127+
}

0 commit comments

Comments
 (0)