Skip to content

Commit

Permalink
Merge pull request #26 from lemondark/release/v4.7.0
Browse files Browse the repository at this point in the history
release/v4.7.0
  • Loading branch information
ilovetochangetheworld authored Oct 27, 2023
2 parents 9d1be27 + b233f9c commit 71e8cfc
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 71 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/tencent/msdk/dns/BackupResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.utils.DebounceTask;
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.DnsDescription;
import com.tencent.msdk.dns.core.DnsManager;
import com.tencent.msdk.dns.core.IDns;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.rest.deshttp.DesHttpDns;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;
import com.tencent.msdk.dns.report.ReportHelper;

Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/tencent/msdk/dns/DnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void run() {
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
Cache.readFromDb();
Cache.getInstance().readFromDb();
}
});
ReportHelper.init(config);
Expand All @@ -112,7 +112,6 @@ public void run() {
* 设置UserId, 进行数据上报时区分用户, 出现问题时, 依赖该Id进行单用户问题排查
*
* @param userId 用户的唯一标识符, 腾讯业务建议直接使用OpenId, 腾讯云客户建议传入长度50位以内, 由字母数字下划线组合而成的字符串
* @return 是否设置成功, true为设置成功, false为设置失败
* @throws IllegalStateException 没有初始化时抛出
* @throws IllegalArgumentException userId为空时抛出
*/
Expand Down Expand Up @@ -171,8 +170,8 @@ public static synchronized void setCachedIpEnable(boolean mCachedIpEnable) {
/**
* 设置是否上报,是否启用域名服务(获取底层配置)
*
* @param mEnableReport
* @param mEnableDomainServer
* @param mEnableReport 启用日志上报
* @param mEnableDomainServer 启用域名服务
*/
public static void setDnsConfigFromServer(boolean mEnableReport, boolean mEnableDomainServer) {
if (!sInited) {
Expand All @@ -184,7 +183,7 @@ public static void setDnsConfigFromServer(boolean mEnableReport, boolean mEnable

public static String getDnsDetail(String hostname) {
String dnsIp = BackupResolver.getInstance().getDnsIp();
LookupResult<IStatisticsMerge> lookupResult = DnsManager.getResultFromCache(new LookupParameters.Builder<LookupExtra>()
final LookupResult<IStatisticsMerge> lookupResult = DnsManager.getResultFromCache(new LookupParameters.Builder<LookupExtra>()
.context(sAppContext)
.hostname(hostname)
.timeoutMills(sConfig.timeoutMills)
Expand All @@ -198,7 +197,12 @@ public static String getDnsDetail(String hostname) {
.build());

// 收集命中缓存的数据
CacheStatisticsReport.add(lookupResult);
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
CacheStatisticsReport.add(lookupResult);
}
});
StatisticsMerge statMerge = (StatisticsMerge) lookupResult.stat;
return statMerge.toJsonResult();
}
Expand Down Expand Up @@ -286,7 +290,6 @@ private static IpSet getAddrsByName(/* @Nullable */String hostname,
.enableAsyncLookup(enableAsyncLookup)
.customNetStack(sConfig.customNetStack)
.build());
ReportHelper.reportLookupMethodCalledEvent(lookupResult);
return lookupResult.ipSet;
}
if (fallback2Local) {
Expand Down Expand Up @@ -327,7 +330,7 @@ public static IpSet getAddrsByNamesEnableExpired(final String domain) {
@Override
public void run() {
// 下发解析请求
getAddrsByName(domain, true, true);
getAddrsByName(domain, false, true);
}
});
} else {
Expand All @@ -342,7 +345,7 @@ public void run() {
@Override
public void run() {
DnsLog.d("async look up send");
getAddrsByName(requestDomain, true, true);
getAddrsByName(requestDomain, false, true);
}
});
// 缓存过期且不允许使用过期缓存
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.util.Log;

import androidx.annotation.Nullable;

import com.tencent.msdk.dns.base.executor.DnsExecutors;
import com.tencent.msdk.dns.base.jni.JniWrapper;
import com.tencent.msdk.dns.base.log.DnsLog;
Expand All @@ -11,6 +13,7 @@
import com.tencent.msdk.dns.base.utils.NetworkStack;
import com.tencent.msdk.dns.core.Const;
import com.tencent.msdk.dns.core.IpSet;
import com.tencent.msdk.dns.core.cache.Cache;

public class MSDKDnsResolver {
public static final String DES_HTTP_CHANNEL = Const.DES_HTTP_CHANNEL;
Expand Down Expand Up @@ -400,6 +403,22 @@ public int getNetworkStack() {
return NetworkStack.get();
}

/**
* 清除所有缓存
*/
public void clearHostCache() {
clearHostCache(null);
}

/**
* 清除指定域名缓存,hostname为null时清除所有域名缓存
*
* @param domain 指定域名,多个域名用,分割
*/
public void clearHostCache(@Nullable String domain) {
Cache.getInstance().clearCache(domain);
}

@SuppressWarnings("unused")
public void addLogNode(ILogNode logNode) {
DnsLog.addLogNode(logNode);
Expand Down
20 changes: 14 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 @@ -3,6 +3,7 @@
import android.os.SystemClock;

import com.tencent.msdk.dns.base.compat.CollectionCompat;
import com.tencent.msdk.dns.base.executor.DnsExecutors;
import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.utils.NetworkStack;
import com.tencent.msdk.dns.core.local.LocalDns;
Expand All @@ -13,6 +14,7 @@
import com.tencent.msdk.dns.core.sorter.Sorter;
import com.tencent.msdk.dns.core.stat.StatisticsMerge;
import com.tencent.msdk.dns.core.stat.StatisticsMergeFactory;
import com.tencent.msdk.dns.report.CacheStatisticsReport;

import java.io.IOException;
import java.nio.channels.Selector;
Expand Down Expand Up @@ -235,22 +237,31 @@ LookupResult<IStatisticsMerge> lookup(LookupParameters<LookupExtra> lookupParams
List<IDns.ISession> sessions = new ArrayList<>();
lookupContext.sessions(sessions);
try {
// NOTE: 当前对外API上, 不支持AAAA记录的解析, 需要保留LocalDns的解析结果作为AAAA解析结果
// 暂时不忽略LocalDns解析结果(即超时时间内会等待LocalDns解析结果, 无论RestDns是否已经解析成功)
// 暂时不忽略LocalDns解析结果(RestDns解析失败时,超时时间内会等待LocalDns解析结果)
if (null != restDnsGroup) {
// 先查缓存,有其一即可
LookupResult<IStatisticsMerge> lookupResultFromCache = getResultFromCache(lookupParams);
final LookupResult<IStatisticsMerge> lookupResultFromCache = getResultFromCache(lookupParams);
DnsLog.d("getResultFromCache: " + lookupResultFromCache);
if (lookupResultFromCache.stat.lookupSuccess()) {
if (lookupResultFromCache.stat.lookupPartCached()) {
// 仅部分命中缓存
lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet);
// 收集命中缓存的数据
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
CacheStatisticsReport.add(lookupResultFromCache);
}
});

} else {
lookupResultHolder.mLookupResult = lookupResultFromCache;
DnsLog.d("DnsManager lookup getResultFromCache success");
return lookupResultFromCache;
}
}
// statContext操作在读完缓存之后,下发解析请求前。结果会更新在lookupResult上
statMerge.statContext(lookupContext);

// 打开Selector
prepareTasks(restDnsGroup, lookupContext);
Expand Down Expand Up @@ -355,9 +366,6 @@ LookupResult<IStatisticsMerge> lookup(LookupParameters<LookupExtra> lookupParams
endSessions(lookupContext);
lookupLatch.countDown();
RUNNING_LOOKUP_LATCH_MAP.remove(lookupParams);
// NOTE: statContext在前, 因为后续操作会清理lookupContext
// NOTE: 这里应该会在创建LookupResult实例之后执行, 但statMerge实例上的更新会更新到LookupResult上
statMerge.statContext(lookupContext);
DnsLog.d("FINALLY statMerge: %s", statMerge.toJsonResult());

// 解析完成, 清理lookupContext
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/com/tencent/msdk/dns/core/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@

public final class Cache implements ICache {

private static final Map<String, LookupResult> mHostnameIpsMap = new ConcurrentHashMap<>();
private final Map<String, LookupResult> mHostnameIpsMap = new ConcurrentHashMap<>();

private static final LookupCacheDao lookupCacheDao = LookupCacheDatabase.getInstance(DnsService.getAppContext()).lookupCacheDao();
private final LookupCacheDao lookupCacheDao = LookupCacheDatabase.getInstance(DnsService.getAppContext()).lookupCacheDao();

private static boolean getCachedIpEnable() {
private boolean getCachedIpEnable() {
return DnsService.getDnsConfig().cachedIpEnable;
}

public static void readFromDb() {
private Cache() {
}

private static final class CacheHolder {
static final Cache instance = new Cache();
}

public static Cache getInstance() {
return CacheHolder.instance;
}

public void readFromDb() {
if (getCachedIpEnable()) {
List<LookupCache> allCache = lookupCacheDao.getAll();
ArrayList<LookupCache> expired = new ArrayList<>();
Expand All @@ -42,6 +53,21 @@ public static void readFromDb() {
}
}

public void clearCache(String hostname) {
if (TextUtils.isEmpty(hostname)) {
CacheHolder.instance.clear();
} else {
String[] hostnameArr = hostname.split(",");
if(hostnameArr.length > 1) {
for(String tempHostname: hostnameArr) {
CacheHolder.instance.delete(tempHostname);
}
} else {
CacheHolder.instance.delete(hostname);
}
}
}

@Override
public LookupResult get(String hostname) {
if (TextUtils.isEmpty(hostname)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.tencent.msdk.dns.core.LookupContext;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.cache.Cache;
import com.tencent.msdk.dns.core.rest.share.rsp.Response;
import com.tencent.msdk.dns.core.stat.AbsStatistics;

Expand All @@ -29,7 +28,7 @@ public abstract class AbsRestDns implements IDns<LookupExtra> {
protected static final int TCP_CONTINUOUS_RCV_BUF_SIZE = 1024;
protected static final int RCV_ZERO_MAX = 128;

protected final CacheHelper mCacheHelper = new CacheHelper(this, new Cache());
protected final CacheHelper mCacheHelper = new CacheHelper(this);

// NOTE: stat: 结果参数
protected boolean tryGetResultFromCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.tencent.msdk.dns.core.IDns;
import com.tencent.msdk.dns.core.LookupParameters;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.cache.Cache;
import com.tencent.msdk.dns.core.ipRank.IpRankCallback;
import com.tencent.msdk.dns.core.ipRank.IpRankHelper;
import com.tencent.msdk.dns.core.rest.share.rsp.Response;
Expand Down Expand Up @@ -46,19 +47,15 @@ public final class CacheHelper {
CollectionCompat.<LookupParameters<LookupExtra>>createSet());

private final IDns<LookupExtra> mDns;
private final ICache mCache;
private final ICache mCache = Cache.getInstance();
private final IpRankHelper mIpRankHelper = new IpRankHelper();

CacheHelper(IDns<LookupExtra> dns, ICache cache) {
CacheHelper(IDns<LookupExtra> dns) {
if (null == dns) {
throw new IllegalArgumentException("dns".concat(Const.NULL_POINTER_TIPS));
}
if (null == cache) {
throw new IllegalArgumentException("cache".concat(Const.NULL_POINTER_TIPS));
}

mDns = dns;
mCache = cache;
listenNetworkChange();
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import com.tencent.msdk.dns.base.executor.DnsExecutors;
import com.tencent.msdk.dns.base.log.DnsLog;
import com.tencent.msdk.dns.base.utils.CommonUtils;
import com.tencent.msdk.dns.base.utils.NetworkUtils;
Expand All @@ -11,9 +12,11 @@
import com.tencent.msdk.dns.core.IStatisticsMerge;
import com.tencent.msdk.dns.core.IpSet;
import com.tencent.msdk.dns.core.LookupContext;
import com.tencent.msdk.dns.core.LookupResult;
import com.tencent.msdk.dns.core.local.LocalDns;
import com.tencent.msdk.dns.core.rest.share.AbsRestDns;
import com.tencent.msdk.dns.core.rest.share.LookupExtra;
import com.tencent.msdk.dns.report.ReportHelper;

import org.json.JSONObject;

Expand Down Expand Up @@ -111,6 +114,16 @@ public <Statistics extends IDns.IStatistics> void merge(IDns dns, Statistics sta
} else {
restDnsStat = (AbsRestDns.Statistics) stat;
}

// 上报数据处理,上报仅使用statisticsMerge类。IpSet使用IpSet.EMPTY传参。
final LookupResult<IStatisticsMerge> lookupResult = new LookupResult<IStatisticsMerge>(IpSet.EMPTY, this);
DnsExecutors.WORK.execute(new Runnable() {
@Override
public void run() {
ReportHelper.reportLookupMethodCalledEvent(lookupResult);
}
});

}

@Override
Expand Down Expand Up @@ -177,6 +190,7 @@ public String toString() {
return super.toString() + "{" +
"netType='" + netType + '\'' +
", hostname='" + hostname + '\'' +
", requestHostname='" + requestHostname + '\'' +
", channel='" + channel + '\'' +
", curNetStack=" + curNetStack +
", localDnsStat=" + localDnsStat +
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/tencent/msdk/dns/report/AttaHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static Runnable report(final String carrier,
final long eventTime,
final String dnsIp,
final long spend,
final long ldns_spend,
final String req_dn,
final String req_type,
final long req_timeout,
Expand Down Expand Up @@ -64,6 +65,7 @@ public void run() {
+ "&systemName=" + SYSTEMNANE
+ "&systemVersion=" + SYSTEMVERSION
+ "&spend=" + spend
+ "&ldns_spend=" + ldns_spend
+ "&req_dn=" + req_dn
+ "&req_type=" + req_type
+ "&req_timeout=" + req_timeout
Expand Down
Loading

0 comments on commit 71e8cfc

Please sign in to comment.