Skip to content

Commit

Permalink
Merge pull request #28 from lemondark/release/v4.8.1
Browse files Browse the repository at this point in the history
release v4.8.1
  • Loading branch information
ilovetochangetheworld authored Jan 24, 2024
2 parents 2e3624a + b352d43 commit a8d41f6
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/tencent/msdk/dns/DnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public final class DnsConfig {

private DnsConfig(int logLevel, String appId, String userId, boolean initBuiltInReporters, String dnsId,
String dnsKey, String token, int timeoutMills, Set<WildcardDomain> protectedDomains,
Set<String> preLookupDomains, boolean enablePersistentCache, Set<String> persistentCacheDomains
, Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
Set<String> preLookupDomains, boolean enablePersistentCache, Set<String> persistentCacheDomains,
Set<IpRankItem> ipRankItems, String channel, boolean enableReport, boolean blockFirst,
int customNetStack, DnsExecutors.ExecutorSupplier executorSupplier,
ILookedUpListener lookedUpListener, List<ILogNode> logNodes, List<IReporter> reporters,
boolean useExpiredIpEnable, boolean cachedIpEnable, String routeIp) {
Expand Down Expand Up @@ -745,8 +745,8 @@ public DnsConfig build() {
return new DnsConfig(mLogLevel, mAppId, mUserId, mInitBuiltInReporters, mDnsId, mDnsKey, mToken,
mTimeoutMills, mProtectedDomains, mPreLookupDomains, mEnablePersistentCache,
mPersistentCacheDomains, mIpRankItems, mChannel, mEnableReport, mBlockFirst, mCustomNetStack,
mExecutorSupplier, mLookedUpListener, mLogNodes, mReporters, mUseExpiredIpEnable, mCachedIpEnable
, mRouteIp);
mExecutorSupplier, mLookedUpListener, mLogNodes, mReporters, mUseExpiredIpEnable, mCachedIpEnable,
mRouteIp);
}
}
}
3 changes: 0 additions & 3 deletions src/main/java/com/tencent/msdk/dns/core/CountDownManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ public void run() {
} catch (Exception ignored) {
DnsLog.e("exception: %s", ignored);
}
if (!mRealTask.mIgnorable) {
mCountDownLatch.countDown();
}
}
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/tencent/msdk/dns/core/DnsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ LookupResult<IStatisticsMerge> getResultForSelector(CountDownLatch countDownLatc
long startTimeMills) {
Selector selector = lookupContext.selector();
if (selector == null) {
return getResultForNullSelector(countDownLatch, lookupContext, lookupParams, lookupResultHolder,
return getResultForNullSelector(countDownLatch, lookupContext, lookupParams, lookupResultHolder,
startTimeMills);
}
IRetry retry = sRetry;
Expand Down Expand Up @@ -484,14 +484,12 @@ private static <LookupExtraT extends IDns.ILookupExtra> void tryLookup(LookupCon
if (token.isReadable()) {
DnsLog.d("%s event readable", session.getDns().getDescription());
String[] ips = session.receiveResponse();
if (session.getStatistics().lookupSuccess() || session.getStatistics().lookupFailed()) {
IDns.IStatistics statistics = session.getStatistics();
if (statistics.lookupSuccess() || statistics.lookupFailed()) {
IDns dns = session.getDns();
sessionIterator.remove();
lookupContext.dnses().remove(dns);
if (session.getStatistics().lookupSuccess()) {
lookupContext.sorter().put(dns, ips);
}
lookupContext.statisticsMerge().merge(dns, session.getStatistics());
LookupHelper.lookupFinished(lookupContext, dns, statistics, ips);
continue;
}
} else if (token.isWritable()) {
Expand Down
57 changes: 43 additions & 14 deletions src/main/java/com/tencent/msdk/dns/core/LookupHelper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.tencent.msdk.dns.core;

import static com.tencent.msdk.dns.base.utils.CommonUtils.isEmpty;

import com.tencent.msdk.dns.base.log.DnsLog;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

public final class LookupHelper {

Expand All @@ -23,17 +27,14 @@ void prepareNonBlockLookupTask(IDns.ISession session, LookupContext<LookupExtraT
if (session.getToken().isReadable()) {
DnsLog.d("prepareNonBlockLookupTask start receive");
String[] ips = session.receiveResponse();
if (session.getStatistics().lookupSuccess() || session.getStatistics().lookupFailed()) {
IDns.IStatistics statistics = session.getStatistics();
if (statistics.lookupSuccess() || statistics.lookupFailed()) {
IDns dns = session.getDns();
if (!forRetry) {
lookupContext.sessions().remove(session);
}
lookupContext.dnses().remove(dns);
if (session.getStatistics().lookupSuccess()) {
lookupContext.sorter().put(dns, ips);
}
lookupContext.statisticsMerge()
.merge(dns, session.getStatistics());
lookupFinished(lookupContext, dns, statistics, ips);
}
} else if (!forRetry) {
lookupContext.sessions().add(session);
Expand Down Expand Up @@ -61,22 +62,50 @@ public void run() {
LookupResult lookupResult = dns.lookup(lookupContext.asLookupParameters());
if (lookupResult.stat.lookupSuccess() || lookupResult.stat.lookupFailed()) {
dnses.remove(dns);
if (lookupResult.stat.lookupSuccess()) {
lookupContext.sorter().put(dns, lookupResult.ipSet.ips);
}
// 不论是否成功都将stat进行合并,让正确的errorCode可以传出
lookupContext.statisticsMerge()
.merge(dns, lookupResult.stat);
lookupFinished(lookupContext, dns, lookupResult.stat, lookupResult.ipSet.ips);
}

}
};

// 只有非localDNS进入countDownLatch阻塞任务
if (!Const.LOCAL_CHANNEL.equals(dns.getDescription().channel)) {
// 只有localDNS进入countDownLatch阻塞任务
if (Const.LOCAL_CHANNEL.equals(dns.getDescription().channel)) {
lookupContext.transaction().addTask(blockLookupTask);
} else {
lookupContext.transaction().addTask(blockLookupTask, true);
}
}

/**
* 解析结束处理:解析结果处理,流程是否阻塞。
*
* @param lookupContext lookupContext
* @param dns 当前dns服务
* @param stat 统计数据
* @param ips 解析结果
*/
public static <LookupExtraT extends IDns.ILookupExtra>
void lookupFinished(LookupContext<LookupExtraT> lookupContext, IDns<LookupExtraT> dns,
IDns.IStatistics stat, String[] ips) {
Set<IDns> dnses = lookupContext.dnses();
List<IDns.ISession> sessions = lookupContext.sessions();
CountDownLatch countDownLatch = lookupContext.countDownLatch();
if (stat.lookupSuccess() && !isEmpty(ips)) {
lookupContext.sorter().put(dns, ips);
// httpdns只要成功就清除定时器,此时localdns不阻塞
if (!Const.LOCAL_CHANNEL.equals(dns.getDescription().channel)) {
countDownLatch.countDown();
}
}
// httpdns & localdns解析均已完成,httpdns未解析成功时,清空countDownLatch。localdns兜底
if ((dnses.isEmpty() && sessions.isEmpty()) && countDownLatch.getCount() > 0) {
countDownLatch.countDown();
}

// 不论是否成功都将stat进行合并,让正确的errorCode可以传出
lookupContext.statisticsMerge()
.merge(dns, stat);


}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tencent.msdk.dns.core.cache.database;

import android.os.SystemClock;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
Expand Down Expand Up @@ -28,7 +30,7 @@ public LookupCache() {
public boolean isExpired() {
AbsRestDns.Statistics stat = (AbsRestDns.Statistics) lookupResult.stat;
if (stat != null) {
return System.currentTimeMillis() > stat.expiredTime;
return SystemClock.elapsedRealtime() > stat.expiredTime;
}
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/tencent/msdk/dns/core/rank/IpRankTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tencent.msdk.dns.core.rank;

import android.os.SystemClock;
import android.util.Pair;

import com.tencent.msdk.dns.core.Const;
Expand Down Expand Up @@ -73,12 +74,12 @@ public int compare(Pair<String, Integer> o1, Pair<String, Integer> o2) {
*/
private int ipSpeedTask(String ip, int port) {
Socket socket = new Socket();
long start = System.currentTimeMillis();
long start = SystemClock.elapsedRealtime();
long end = start + timeouts;
SocketAddress remoteAddress = new InetSocketAddress(ip, port);
try {
socket.connect(remoteAddress, timeouts);
end = System.currentTimeMillis();
end = SystemClock.elapsedRealtime();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tencent.msdk.dns.core.rest.share;

import android.os.SystemClock;
import android.text.TextUtils;

import com.tencent.msdk.dns.DnsService;
Expand Down Expand Up @@ -96,7 +97,7 @@ private Map<String, Object> handleHostnameCached(String[] hostnameArr) {
Statistics cachedStat = (Statistics) lookupResult.stat;

if (DnsService.getDnsConfig().useExpiredIpEnable
&& cachedStat.expiredTime < System.currentTimeMillis()) {
&& cachedStat.expiredTime < SystemClock.elapsedRealtime()) {
requestHostname.append(hostname).append(',');
}
} else {
Expand Down Expand Up @@ -465,7 +466,7 @@ public long getExpiredTime(Map<String, Integer> ttl) {
min = Math.min(value, min);
}
}
return System.currentTimeMillis() + min * 1000L;
return SystemClock.elapsedRealtime() + min * 1000L;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tencent.msdk.dns.core.rest.share;

import android.os.SystemClock;
import android.text.TextUtils;

import com.tencent.msdk.dns.BackupResolver;
Expand Down Expand Up @@ -68,7 +69,7 @@ public LookupResult get(String hostname) {
AbsRestDns.Statistics cachedStat = (AbsRestDns.Statistics) lookupResult.stat;
final boolean useExpiredIpEnable = DnsService.getDnsConfig().useExpiredIpEnable;
// 乐观DNS或者未过期
if (useExpiredIpEnable || cachedStat.expiredTime > System.currentTimeMillis()) {
if (useExpiredIpEnable || cachedStat.expiredTime > SystemClock.elapsedRealtime()) {
return lookupResult;
}
DnsLog.d("Cache of %s(%d) expired", hostname, mDns.getDescription().family);
Expand Down
69 changes: 46 additions & 23 deletions src/main/java/com/tencent/msdk/dns/core/sorter/Sorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public final class Sorter implements ISorter {

private final int mCurNetStack;
// 适配批量更新的情况,IPFromLocal更改为数组
// private String mV4IpFromLocal = null;
// private String mV6IpFromLocal = null;
private List<String> mV4IpFromLocal = Collections.emptyList();
private List<String> mV6IpFromLocal = Collections.emptyList();
private List<String> mV4IpsFromRest = Collections.emptyList();
Expand Down Expand Up @@ -68,39 +66,64 @@ public synchronized void put(IDns dns, String[] ips) {
public synchronized void putPartCache(IpSet ipSet) {
String[] v4Ips = ipSet.v4Ips;
String[] v6Ips = ipSet.v6Ips;
if (v4Ips.length > 0) {
if (v4Ips != null && v4Ips.length > 0) {
mV4IpsFromCache = Arrays.asList(v4Ips);
}
if (v6Ips.length > 0) {
if (v6Ips != null && v6Ips.length > 0) {
mV6IpsFromCache = Arrays.asList(v6Ips);
}
}

@Override
public IpSet sort() {
List<String> v4IpSet = new ArrayList<>();
String[] v4Ips = Const.EMPTY_IPS;
if (0 != (mCurNetStack & NetworkStack.IPV4_ONLY)) {
if (!mV4IpsFromCache.isEmpty()) {
v4IpSet.addAll(mV4IpsFromCache);
}
if (!mV4IpsFromRest.isEmpty()) {
v4IpSet.addAll(mV4IpsFromRest);
} else if (null != mV4IpFromLocal) {
v4IpSet.addAll(mV4IpFromLocal);
}
v4Ips = combineIps("ipv4");
}
List<String> v6IpSet = new ArrayList<>();
String[] v6Ips = Const.EMPTY_IPS;
if (0 != (mCurNetStack & NetworkStack.IPV6_ONLY)) {
if (!mV6IpsFromCache.isEmpty()) {
v6IpSet.addAll(mV6IpsFromCache);
}
if (!mV6IpsFromRest.isEmpty()) {
v6IpSet.addAll(mV6IpsFromRest);
} else if (null != mV6IpFromLocal) {
v6IpSet.addAll(mV6IpFromLocal);
v6Ips = combineIps("ipv6");
}
return new IpSet(v4Ips, v6Ips);
}

private String[] combineIps(String type) {
List<String> ipsFromCache = Objects.equals(type, "ipv6") ? mV6IpsFromCache : mV4IpsFromCache;
List<String> ipsFromLocal = Objects.equals(type, "ipv6") ? mV6IpFromLocal : mV4IpFromLocal;
List<String> ipsFromRest = Objects.equals(type, "ipv6") ? mV6IpsFromRest : mV4IpsFromRest;
List<String> ipSet = new ArrayList<>();
if (!ipsFromCache.isEmpty()) {
ipSet.addAll(ipsFromCache);
}

if (!ipsFromRest.isEmpty()) {
ipSet.addAll(ipsFromRest);
if (!ipsFromLocal.isEmpty()) {
// 对比httpdns和local,对httpdns返回部分域名为空的情况使用localdns兜底
List<String> restHosts = new ArrayList<>();
for (String restItem : ipsFromRest) {
if (restItem.contains(":")) {
String hostname = restItem.split(":")[0];
if (!restHosts.contains(hostname)) {
restHosts.add(hostname);
}
}
}
for (String item : ipsFromLocal) {
if (item.contains(":")) {
String hostname = item.split(":")[0];
if (!restHosts.contains(hostname)) {
DnsLog.d("%s's %s result is from localDns", hostname, type);
ipSet.add(item);
}
}
}
}
} else if (!ipsFromLocal.isEmpty()) {
DnsLog.d("%s result all from localDns", type);
ipSet.addAll(ipsFromLocal);
}
return new IpSet(v4IpSet.toArray(Const.EMPTY_IPS), v6IpSet.toArray(Const.EMPTY_IPS));
return ipSet.toArray(Const.EMPTY_IPS);
}

private List<String> addIp(List<String> ipList, String ip) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tencent.msdk.dns.core.stat;

import android.os.SystemClock;

import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.IDns;

Expand Down Expand Up @@ -27,11 +29,11 @@ public abstract class AbsStatistics implements IDns.IStatistics {
public long startLookupTimeMills = 0L;

public void startLookup() {
startLookupTimeMills = System.currentTimeMillis();
startLookupTimeMills = SystemClock.elapsedRealtime();
}

public void endLookup() {
costTimeMills = (int) (System.currentTimeMillis() - startLookupTimeMills);
costTimeMills = (int) (SystemClock.elapsedRealtime() - startLookupTimeMills);
}

public boolean lookupPartCached() {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/tencent/msdk/dns/report/AttaHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AttaHelper {
private static final String ATTA_TOKEN = BuildConfig.ATTA_TOKEN;
private static final String SDKVERSION = BuildConfig.VERSION_NAME;
private static final String DEVICEMODEL = getSystemModel();
private static final String SYSTEMNANE = "Andriod";
private static final String SYSTEMNANE = "Android";
private static final String SYSTEMVERSION = getSystemVersion();
private static final String SESSIONID = Session.getSessionId();

Expand Down Expand Up @@ -70,15 +70,17 @@ public void run() {
+ "&req_dn=" + reqDn
+ "&req_type=" + reqType
+ "&req_timeout=" + reqTimeout
+ "&req_query="
// todo: ttl需处理,先上报0
+ "&req_ttl= 0"
+ "&req_ttl=0"
+ "&errorCode=" + errorCode
+ "&statusCode=" + statusCode
+ "&sessionId=" + SESSIONID
+ "&isCache=" + (isCache ? 1 : 0)
+ "&count=" + count
+ "&ldns=" + ldns
+ "&hdns=" + hdns
+ "&_dc=" + Math.random()
);
DnsLog.d("开始Atta上报:" + url);
connection = (HttpURLConnection) url.openConnection();
Expand Down
Loading

0 comments on commit a8d41f6

Please sign in to comment.