Skip to content

Commit 23fca5a

Browse files
authored
Merge pull request #404 from YangSen-qn/dns_local_cache
Dns local cache
2 parents b1ee780 + 868b494 commit 23fca5a

File tree

8 files changed

+171
-123
lines changed

8 files changed

+171
-123
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#Changelog
2+
# 7.6.5
3+
* 优化Dns local cache
24

35
# 7.6.4
46
* 修复io异常

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ https://github.com/qiniudemo/qiniu-lab-android
2525
| 7.0.7 | Android 2.2+ | android-async-http 1.4.8 |
2626

2727
### 注意
28-
* 推荐使用最新版:7.6.4
29-
* 7.4.6是在7.6.4版本上降低okhttp版本,其他功能不变,AndroidNetwork.getMobileDbm()可以获取手机信号强度,需要如下权限(API>=18时生效)
28+
* 推荐使用最新版:7.6.5
29+
* AndroidNetwork.getMobileDbm()可以获取手机信号强度,需要如下权限(API>=18时生效)
3030
```
3131
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
3232
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.qiniu.android;
2+
3+
import android.test.AndroidTestCase;
4+
import android.util.Log;
5+
6+
import com.qiniu.android.http.ResponseInfo;
7+
import com.qiniu.android.storage.Configuration;
8+
import com.qiniu.android.storage.UpCompletionHandler;
9+
import com.qiniu.android.storage.UploadManager;
10+
11+
import junit.framework.Assert;
12+
13+
import org.json.JSONObject;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.util.concurrent.CountDownLatch;
18+
import java.util.concurrent.TimeUnit;
19+
20+
public class ComplexUploadSceneTest extends AndroidTestCase {
21+
22+
private final CountDownLatch signal = new CountDownLatch(1);
23+
24+
public void testMutiUpload(){
25+
26+
final int maxCount = 10;
27+
28+
final TestParam param = new TestParam();
29+
param.completeCount = 0;
30+
param.successCount = 0;
31+
32+
for (int i = 0; i < maxCount; i++) {
33+
template((i + 1) * 100, new CompleteHandler() {
34+
@Override
35+
public void complete(boolean isSuccess) {
36+
37+
synchronized (param){
38+
param.completeCount += 1;
39+
if (isSuccess){
40+
param.successCount += 1;
41+
}
42+
if (param.completeCount == maxCount){
43+
signal.countDown();
44+
}
45+
}
46+
}
47+
});
48+
}
49+
50+
try {
51+
signal.await(); // wait for callback
52+
} catch (InterruptedException e) {
53+
}
54+
55+
Log.d("ComplexUploadSceneTest", "complex_upload successCount: " + param.successCount);
56+
assertTrue("Pass", param.successCount == param.completeCount);
57+
}
58+
59+
private void template(int size, final CompleteHandler completeHandler){
60+
61+
final String keyUp = "android_complex_upload_" + size + "k";
62+
File file = null;
63+
try {
64+
file = TempFile.createFile(size);
65+
} catch (IOException e) {
66+
completeHandler.complete(false);
67+
return;
68+
}
69+
70+
Configuration configuration = new Configuration.Builder()
71+
.useHttps(true)
72+
.build();
73+
UploadManager manager = new UploadManager(configuration);
74+
75+
manager.put(file, keyUp, TestConfig.token_na0, new UpCompletionHandler() {
76+
@Override
77+
public void complete(String key, ResponseInfo info, JSONObject response) {
78+
if (info.isOK() && info.reqId != null && keyUp.equals(key)){
79+
completeHandler.complete(true);
80+
} else {
81+
completeHandler.complete(false);
82+
}
83+
}
84+
}, null);
85+
}
86+
87+
private interface CompleteHandler{
88+
void complete(boolean isSuccess);
89+
}
90+
91+
92+
private class TestParam{
93+
int successCount = 0;
94+
int completeCount = 0;
95+
}
96+
97+
}

library/src/androidTest/java/com/qiniu/android/DnsApiTest.java

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,22 @@
22

33

44
import android.test.AndroidTestCase;
5-
import android.test.InstrumentationTestCase;
65
import android.util.Log;
76

87
import com.qiniu.android.collect.Config;
98
import com.qiniu.android.common.ZoneInfo;
109
import com.qiniu.android.http.DnsPrefetcher;
11-
import com.qiniu.android.http.ResponseInfo;
12-
import com.qiniu.android.http.custom.DnsCacheKey;
10+
import com.qiniu.android.http.custom.DnsCacheInfo;
1311
import com.qiniu.android.storage.Configuration;
1412
import com.qiniu.android.storage.Recorder;
15-
import com.qiniu.android.storage.UpCompletionHandler;
16-
import com.qiniu.android.storage.UpProgressHandler;
17-
import com.qiniu.android.storage.UploadManager;
18-
import com.qiniu.android.storage.UploadOptions;
1913
import com.qiniu.android.storage.persistent.DnsCacheFile;
2014
import com.qiniu.android.utils.AndroidNetwork;
2115
import com.qiniu.android.utils.StringUtils;
2216

23-
import org.json.JSONObject;
24-
25-
import java.io.File;
2617
import java.io.IOException;
2718
import java.net.InetAddress;
2819
import java.net.UnknownHostException;
2920
import java.util.ArrayList;
30-
import java.util.HashMap;
3121
import java.util.List;
3222
import java.util.concurrent.ConcurrentHashMap;
3323

@@ -133,24 +123,8 @@ public void testLocalIp() {
133123
public void testRecoverCache() {
134124

135125
Recorder recorder = null;
136-
try {
137-
recorder = new DnsCacheFile(Config.dnscacheDir);
138-
} catch (IOException e) {
139-
e.printStackTrace();
140-
return;
141-
}
142-
String fileName = recorder.getFileName();
143-
if (fileName == null) {
144-
Log.e("qiniutest: ", "recover file is null ");
145-
return;
146-
}
147-
byte[] data = recorder.get(recorder.getFileName());
148-
if (data == null) {
149-
Log.e("qiniutest: ", "recover data is null ");
150-
return;
151-
}
152-
DnsPrefetcher.recoverDnsCache(data);
153126

127+
DnsPrefetcher.recoverCache(configuration);
154128

155129
ConcurrentHashMap<String, List<InetAddress>> map1 = DnsPrefetcher.getDnsPrefetcher().getConcurrentHashMap();
156130
if (map1.size() <= 0)
@@ -165,20 +139,24 @@ public void testRecoverCache() {
165139
}
166140
}
167141

168-
int time = 0;
169-
final Object lock = new Object();
170-
142+
public void testSerializable() throws UnknownHostException {
143+
InetAddress address = InetAddress.getByName("127.0.0.1");
144+
List<InetAddress> addressList = new ArrayList<>();
145+
addressList.add(address);
146+
ConcurrentHashMap<String, List<InetAddress>> info = new ConcurrentHashMap<>();
147+
info.put("localhost", addressList);
171148

172-
public void testSerializable() {
173-
DnsCacheKey key = new DnsCacheKey("12321", "127.0.0.1", "akscope");
174-
Log.e("qiniutest", key.toString());
175-
DnsCacheKey key1 = DnsCacheKey.toCacheKey(key.toString());
176-
if (key1 == null) {
177-
return;
178-
}
179-
Log.e("qiniutest", key1.getCurrentTime() + ":" + key1.getLocalIp() + ":" + key1.getAkScope());
149+
DnsCacheInfo cacheInfo = new DnsCacheInfo("12321", "192.168.1.1", "akScope", info);
150+
Log.e("qiniutest", cacheInfo.toString());
180151

181-
}
152+
byte[] data = StringUtils.toByteArray(cacheInfo);
182153

154+
DnsCacheInfo cacheInfoSer = (DnsCacheInfo)StringUtils.toObject(data);
183155

156+
assertTrue(cacheInfoSer != null);
157+
assertTrue(cacheInfoSer.localIp != null);
158+
assertTrue(cacheInfoSer.info != null);
159+
assertTrue(cacheInfoSer.info.get("localhost") != null);
160+
assertTrue(cacheInfoSer.info.get("localhost").get(0).getHostAddress().equals("127.0.0.1"));
161+
}
184162
}

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.6.4";
5+
public static final String VERSION = "7.6.5";
66

77
public static final String UTF_8 = "utf-8";
88
}

library/src/main/java/com/qiniu/android/http/DnsPrefetcher.java

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.qiniu.android.common.Constants;
88
import com.qiniu.android.common.FixedZone;
99
import com.qiniu.android.common.ZoneInfo;
10-
import com.qiniu.android.http.custom.DnsCacheKey;
10+
import com.qiniu.android.http.custom.DnsCacheInfo;
1111
import com.qiniu.android.storage.Configuration;
1212
import com.qiniu.android.storage.Recorder;
1313
import com.qiniu.android.storage.persistent.DnsCacheFile;
@@ -28,6 +28,8 @@
2828
import java.util.concurrent.ConcurrentHashMap;
2929
import java.util.concurrent.atomic.AtomicReference;
3030

31+
import okhttp3.internal.Util;
32+
3133
/**
3234
* <p>
3335
* Created by jemy on 2019/8/20.
@@ -323,11 +325,11 @@ public static boolean checkRePrefetchDns(String token, Configuration config) {
323325

324326
if (currentTime == null || localip == null || akScope == null)
325327
return true;
326-
DnsCacheKey dnsCacheKey = (DnsCacheKey) mDnsCacheKey.get();
327-
if (dnsCacheKey == null || dnsCacheKey.getCurrentTime() == null)
328+
DnsCacheInfo dnsCacheInfo = (DnsCacheInfo) mDnsCacheKey.get();
329+
if (dnsCacheInfo == null || dnsCacheInfo.getCurrentTime() == null)
328330
return true;
329-
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(dnsCacheKey.getCurrentTime())) / 1000;
330-
if (!localip.equals(dnsCacheKey.getLocalIp()) || cacheTime > config.dnsCacheTimeMs || !akScope.equals(dnsCacheKey.getAkScope())) {
331+
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(dnsCacheInfo.getCurrentTime())) / 1000;
332+
if (!localip.equals(dnsCacheInfo.getLocalIp()) || cacheTime > config.dnsCacheTimeMs || !akScope.equals(dnsCacheInfo.getAkScope())) {
331333
return true;
332334
}
333335

@@ -348,29 +350,29 @@ public static boolean recoverCache(Configuration config) {
348350
e.printStackTrace();
349351
return true;
350352
}
351-
String dnscache = recorder.getFileName();
352-
if (dnscache == null)
353-
return true;
354353

355-
byte[] data = recorder.get(dnscache);
354+
String localIP = AndroidNetwork.getHostIP();
355+
356+
byte[] data = recorder.get(localIP);
356357
if (data == null)
357358
return true;
358359

359-
DnsCacheKey cacheKey = DnsCacheKey.toCacheKey(dnscache);
360-
if (cacheKey == null)
360+
DnsCacheInfo cacheInfo = (DnsCacheInfo) StringUtils.toObject(data);
361+
if (cacheInfo == null)
361362
return true;
362363

363364
String currentTime = String.valueOf(System.currentTimeMillis());
364-
String localip = AndroidNetwork.getHostIP();
365365

366-
if (currentTime == null || localip == null)
366+
if (currentTime == null) {
367367
return true;
368-
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(cacheKey.getCurrentTime())) / 1000;
369-
if (!cacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs) {
368+
}
369+
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(cacheInfo.getCurrentTime())) / 1000;
370+
if (cacheTime > config.dnsCacheTimeMs) {
370371
return true;
371372
}
372-
mDnsCacheKey.set(cacheKey);
373-
return recoverDnsCache(data);
373+
mDnsCacheKey.set(cacheInfo);
374+
DnsPrefetcher.getDnsPrefetcher().setConcurrentHashMap(cacheInfo.info);
375+
return true;
374376
}
375377

376378
/**
@@ -380,20 +382,26 @@ public static boolean recoverCache(Configuration config) {
380382
*/
381383
public static void startPrefetchDns(String token, Configuration config) {
382384
String currentTime = String.valueOf(System.currentTimeMillis());
383-
String localip = AndroidNetwork.getHostIP();
385+
String localIP = AndroidNetwork.getHostIP();
384386
String akScope = StringUtils.getAkAndScope(token);
385-
if (currentTime == null || localip == null || akScope == null)
387+
if (currentTime == null || localIP == null || akScope == null) {
386388
return;
387-
DnsCacheKey dnsCacheKey = new DnsCacheKey(currentTime, localip, akScope);
388-
String cacheKey = dnsCacheKey.toString();
389+
}
390+
391+
DnsCacheInfo dnsCacheInfo = (DnsCacheInfo)mDnsCacheKey.get();
392+
if (dnsCacheInfo == null || !dnsCacheInfo.localIp.equals(localIP)){
393+
dnsCacheInfo = new DnsCacheInfo(currentTime, localIP, akScope, mConcurrentHashMap);
394+
}
395+
396+
String cacheKey = dnsCacheInfo.cacheKey();
389397

390398
Recorder recorder = null;
391399
DnsPrefetcher dnsPrefetcher = null;
392400
try {
393401
recorder = new DnsCacheFile(Config.dnscacheDir);
394402
dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher().init(token, config);
395403
//确认预取结束后,需要更新缓存mDnsCacheKey
396-
mDnsCacheKey.set(dnsCacheKey);
404+
mDnsCacheKey.set(dnsCacheInfo);
397405
} catch (IOException e) {
398406
e.printStackTrace();
399407
return;
@@ -409,22 +417,4 @@ public static void startPrefetchDns(String token, Configuration config) {
409417
recorder.set(cacheKey, dnscache);
410418
}
411419
}
412-
413-
/**
414-
* @param data
415-
* @return
416-
*/
417-
public static boolean recoverDnsCache(byte[] data) {
418-
ConcurrentHashMap<String, List<InetAddress>> concurrentHashMap = null;
419-
try {
420-
concurrentHashMap = (ConcurrentHashMap<String, List<InetAddress>>) StringUtils.toObject(data);
421-
}catch (Exception e){
422-
return true;
423-
}
424-
if (concurrentHashMap == null) {
425-
return true;
426-
}
427-
DnsPrefetcher.getDnsPrefetcher().setConcurrentHashMap(concurrentHashMap);
428-
return false;
429-
}
430420
}

0 commit comments

Comments
 (0)