Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#Changelog
# 7.6.5
* 优化Dns local cache

# 7.6.4
* 修复io异常
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ https://github.com/qiniudemo/qiniu-lab-android
| 7.0.7 | Android 2.2+ | android-async-http 1.4.8 |

### 注意
* 推荐使用最新版:7.6.4
* 7.4.6是在7.6.4版本上降低okhttp版本,其他功能不变,AndroidNetwork.getMobileDbm()可以获取手机信号强度,需要如下权限(API>=18时生效)
* 推荐使用最新版:7.6.5
* AndroidNetwork.getMobileDbm()可以获取手机信号强度,需要如下权限(API>=18时生效)
```
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.qiniu.android;

import android.test.AndroidTestCase;
import android.util.Log;

import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import junit.framework.Assert;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ComplexUploadSceneTest extends AndroidTestCase {

private final CountDownLatch signal = new CountDownLatch(1);

public void testMutiUpload(){

final int maxCount = 10;

final TestParam param = new TestParam();
param.completeCount = 0;
param.successCount = 0;

for (int i = 0; i < maxCount; i++) {
template((i + 1) * 100, new CompleteHandler() {
@Override
public void complete(boolean isSuccess) {

synchronized (param){
param.completeCount += 1;
if (isSuccess){
param.successCount += 1;
}
if (param.completeCount == maxCount){
signal.countDown();
}
}
}
});
}

try {
signal.await(); // wait for callback
} catch (InterruptedException e) {
}

Log.d("ComplexUploadSceneTest", "complex_upload successCount: " + param.successCount);
assertTrue("Pass", param.successCount == param.completeCount);
}

private void template(int size, final CompleteHandler completeHandler){

final String keyUp = "android_complex_upload_" + size + "k";
File file = null;
try {
file = TempFile.createFile(size);
} catch (IOException e) {
completeHandler.complete(false);
return;
}

Configuration configuration = new Configuration.Builder()
.useHttps(true)
.build();
UploadManager manager = new UploadManager(configuration);

manager.put(file, keyUp, TestConfig.token_na0, new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject response) {
if (info.isOK() && info.reqId != null && keyUp.equals(key)){
completeHandler.complete(true);
} else {
completeHandler.complete(false);
}
}
}, null);
}

private interface CompleteHandler{
void complete(boolean isSuccess);
}


private class TestParam{
int successCount = 0;
int completeCount = 0;
}

}
58 changes: 18 additions & 40 deletions library/src/androidTest/java/com/qiniu/android/DnsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,22 @@


import android.test.AndroidTestCase;
import android.test.InstrumentationTestCase;
import android.util.Log;

import com.qiniu.android.collect.Config;
import com.qiniu.android.common.ZoneInfo;
import com.qiniu.android.http.DnsPrefetcher;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.http.custom.DnsCacheKey;
import com.qiniu.android.http.custom.DnsCacheInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.Recorder;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.storage.persistent.DnsCacheFile;
import com.qiniu.android.utils.AndroidNetwork;
import com.qiniu.android.utils.StringUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -133,24 +123,8 @@ public void testLocalIp() {
public void testRecoverCache() {

Recorder recorder = null;
try {
recorder = new DnsCacheFile(Config.dnscacheDir);
} catch (IOException e) {
e.printStackTrace();
return;
}
String fileName = recorder.getFileName();
if (fileName == null) {
Log.e("qiniutest: ", "recover file is null ");
return;
}
byte[] data = recorder.get(recorder.getFileName());
if (data == null) {
Log.e("qiniutest: ", "recover data is null ");
return;
}
DnsPrefetcher.recoverDnsCache(data);

DnsPrefetcher.recoverCache(configuration);

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

int time = 0;
final Object lock = new Object();

public void testSerializable() throws UnknownHostException {
InetAddress address = InetAddress.getByName("127.0.0.1");
List<InetAddress> addressList = new ArrayList<>();
addressList.add(address);
ConcurrentHashMap<String, List<InetAddress>> info = new ConcurrentHashMap<>();
info.put("localhost", addressList);

public void testSerializable() {
DnsCacheKey key = new DnsCacheKey("12321", "127.0.0.1", "akscope");
Log.e("qiniutest", key.toString());
DnsCacheKey key1 = DnsCacheKey.toCacheKey(key.toString());
if (key1 == null) {
return;
}
Log.e("qiniutest", key1.getCurrentTime() + ":" + key1.getLocalIp() + ":" + key1.getAkScope());
DnsCacheInfo cacheInfo = new DnsCacheInfo("12321", "192.168.1.1", "akScope", info);
Log.e("qiniutest", cacheInfo.toString());

}
byte[] data = StringUtils.toByteArray(cacheInfo);

DnsCacheInfo cacheInfoSer = (DnsCacheInfo)StringUtils.toObject(data);

assertTrue(cacheInfoSer != null);
assertTrue(cacheInfoSer.localIp != null);
assertTrue(cacheInfoSer.info != null);
assertTrue(cacheInfoSer.info.get("localhost") != null);
assertTrue(cacheInfoSer.info.get("localhost").get(0).getHostAddress().equals("127.0.0.1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.6.4";
public static final String VERSION = "7.6.5";

public static final String UTF_8 = "utf-8";
}
70 changes: 30 additions & 40 deletions library/src/main/java/com/qiniu/android/http/DnsPrefetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.qiniu.android.common.Constants;
import com.qiniu.android.common.FixedZone;
import com.qiniu.android.common.ZoneInfo;
import com.qiniu.android.http.custom.DnsCacheKey;
import com.qiniu.android.http.custom.DnsCacheInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.Recorder;
import com.qiniu.android.storage.persistent.DnsCacheFile;
Expand All @@ -28,6 +28,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

import okhttp3.internal.Util;

/**
* <p>
* Created by jemy on 2019/8/20.
Expand Down Expand Up @@ -323,11 +325,11 @@ public static boolean checkRePrefetchDns(String token, Configuration config) {

if (currentTime == null || localip == null || akScope == null)
return true;
DnsCacheKey dnsCacheKey = (DnsCacheKey) mDnsCacheKey.get();
if (dnsCacheKey == null || dnsCacheKey.getCurrentTime() == null)
DnsCacheInfo dnsCacheInfo = (DnsCacheInfo) mDnsCacheKey.get();
if (dnsCacheInfo == null || dnsCacheInfo.getCurrentTime() == null)
return true;
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(dnsCacheKey.getCurrentTime())) / 1000;
if (!localip.equals(dnsCacheKey.getLocalIp()) || cacheTime > config.dnsCacheTimeMs || !akScope.equals(dnsCacheKey.getAkScope())) {
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(dnsCacheInfo.getCurrentTime())) / 1000;
if (!localip.equals(dnsCacheInfo.getLocalIp()) || cacheTime > config.dnsCacheTimeMs || !akScope.equals(dnsCacheInfo.getAkScope())) {
return true;
}

Expand All @@ -348,29 +350,29 @@ public static boolean recoverCache(Configuration config) {
e.printStackTrace();
return true;
}
String dnscache = recorder.getFileName();
if (dnscache == null)
return true;

byte[] data = recorder.get(dnscache);
String localIP = AndroidNetwork.getHostIP();

byte[] data = recorder.get(localIP);
if (data == null)
return true;

DnsCacheKey cacheKey = DnsCacheKey.toCacheKey(dnscache);
if (cacheKey == null)
DnsCacheInfo cacheInfo = (DnsCacheInfo) StringUtils.toObject(data);
if (cacheInfo == null)
return true;

String currentTime = String.valueOf(System.currentTimeMillis());
String localip = AndroidNetwork.getHostIP();

if (currentTime == null || localip == null)
if (currentTime == null) {
return true;
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(cacheKey.getCurrentTime())) / 1000;
if (!cacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs) {
}
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(cacheInfo.getCurrentTime())) / 1000;
if (cacheTime > config.dnsCacheTimeMs) {
return true;
}
mDnsCacheKey.set(cacheKey);
return recoverDnsCache(data);
mDnsCacheKey.set(cacheInfo);
DnsPrefetcher.getDnsPrefetcher().setConcurrentHashMap(cacheInfo.info);
return true;
}

/**
Expand All @@ -380,20 +382,26 @@ public static boolean recoverCache(Configuration config) {
*/
public static void startPrefetchDns(String token, Configuration config) {
String currentTime = String.valueOf(System.currentTimeMillis());
String localip = AndroidNetwork.getHostIP();
String localIP = AndroidNetwork.getHostIP();
String akScope = StringUtils.getAkAndScope(token);
if (currentTime == null || localip == null || akScope == null)
if (currentTime == null || localIP == null || akScope == null) {
return;
DnsCacheKey dnsCacheKey = new DnsCacheKey(currentTime, localip, akScope);
String cacheKey = dnsCacheKey.toString();
}

DnsCacheInfo dnsCacheInfo = (DnsCacheInfo)mDnsCacheKey.get();
if (dnsCacheInfo == null || !dnsCacheInfo.localIp.equals(localIP)){
dnsCacheInfo = new DnsCacheInfo(currentTime, localIP, akScope, mConcurrentHashMap);
}

String cacheKey = dnsCacheInfo.cacheKey();

Recorder recorder = null;
DnsPrefetcher dnsPrefetcher = null;
try {
recorder = new DnsCacheFile(Config.dnscacheDir);
dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher().init(token, config);
//确认预取结束后,需要更新缓存mDnsCacheKey
mDnsCacheKey.set(dnsCacheKey);
mDnsCacheKey.set(dnsCacheInfo);
} catch (IOException e) {
e.printStackTrace();
return;
Expand All @@ -409,22 +417,4 @@ public static void startPrefetchDns(String token, Configuration config) {
recorder.set(cacheKey, dnscache);
}
}

/**
* @param data
* @return
*/
public static boolean recoverDnsCache(byte[] data) {
ConcurrentHashMap<String, List<InetAddress>> concurrentHashMap = null;
try {
concurrentHashMap = (ConcurrentHashMap<String, List<InetAddress>>) StringUtils.toObject(data);
}catch (Exception e){
return true;
}
if (concurrentHashMap == null) {
return true;
}
DnsPrefetcher.getDnsPrefetcher().setConcurrentHashMap(concurrentHashMap);
return false;
}
}
Loading