From 82b2e3fd55bfcd3c380a35dccd196982db1c098b Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Mon, 28 Aug 2023 18:00:47 +0800 Subject: [PATCH 01/12] =?UTF-8?q?&&=20fix:=20=E4=B9=90=E8=A7=82DNS?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=B8=8B=EF=BC=8C=E4=B8=8D=E5=86=8D=E4=B8=8B?= =?UTF-8?q?=E5=8F=91localdns=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/DnsService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index 61e653f..bbd65b9 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -327,7 +327,7 @@ public static IpSet getAddrsByNamesEnableExpired(final String domain) { @Override public void run() { // 下发解析请求 - getAddrsByName(domain, true, true); + getAddrsByName(domain, false, true); } }); } else { @@ -342,7 +342,7 @@ public void run() { @Override public void run() { DnsLog.d("async look up send"); - getAddrsByName(requestDomain, true, true); + getAddrsByName(requestDomain, false, true); } }); // 缓存过期且不允许使用过期缓存 From 1f5e44c26bdab4a519ef4083904d13f66a9cc68f Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Wed, 30 Aug 2023 16:58:41 +0800 Subject: [PATCH 02/12] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4Cache=E4=B8=BA?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tencent/msdk/dns/core/cache/Cache.java | 19 +++++++++++++++---- .../msdk/dns/core/rest/share/AbsRestDns.java | 2 +- .../msdk/dns/core/rest/share/CacheHelper.java | 9 +++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java index 97e987f..9b7e6f2 100644 --- a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java +++ b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java @@ -18,15 +18,26 @@ public final class Cache implements ICache { - private static final Map mHostnameIpsMap = new ConcurrentHashMap<>(); + private final Map 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 allCache = lookupCacheDao.getAll(); ArrayList expired = new ArrayList<>(); diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index 0108653..b7dadec 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -29,7 +29,7 @@ public abstract class AbsRestDns implements IDns { 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( diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java index d362c50..8e9e6b6 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/CacheHelper.java @@ -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; @@ -46,19 +47,15 @@ public final class CacheHelper { CollectionCompat.>createSet()); private final IDns mDns; - private final ICache mCache; + private final ICache mCache = Cache.getInstance(); private final IpRankHelper mIpRankHelper = new IpRankHelper(); - CacheHelper(IDns dns, ICache cache) { + CacheHelper(IDns 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(); } From 61619457a65412077a0460007ee46fcb7bb9edbb Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Tue, 5 Sep 2023 11:01:25 +0800 Subject: [PATCH 03/12] =?UTF-8?q?feat:=20=E6=B8=85=E9=99=A4=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=9F=9F=E5=90=8D=E7=BC=93=E5=AD=98API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/DnsService.java | 2 +- .../com/tencent/msdk/dns/MSDKDnsResolver.java | 19 +++++++++++++++++++ .../tencent/msdk/dns/core/cache/Cache.java | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index 61e653f..0f900f3 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -93,7 +93,7 @@ public void run() { DnsExecutors.WORK.execute(new Runnable() { @Override public void run() { - Cache.readFromDb(); + Cache.getInstance().readFromDb(); } }); ReportHelper.init(config); diff --git a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java index 46d0694..f60796d 100644 --- a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java +++ b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java @@ -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; @@ -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; @@ -400,6 +403,22 @@ public int getNetworkStack() { return NetworkStack.get(); } + /** + * 清除所有缓存 + */ + public void clearCache() { + clearCache(null); + } + + /** + * 清除指定域名缓存,hostname为null时清除所有域名缓存 + * + * @param hostname 指定域名,多个域名用,分割 + */ + public void clearCache(@Nullable String hostname) { + Cache.getInstance().clearCache(hostname); + } + @SuppressWarnings("unused") public void addLogNode(ILogNode logNode) { DnsLog.addLogNode(logNode); diff --git a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java index 9b7e6f2..4d3e418 100644 --- a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java +++ b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java @@ -53,6 +53,21 @@ public 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)) { From 38151516bc4f81b5ce5151609741d5b18df914b1 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Wed, 6 Sep 2023 16:35:56 +0800 Subject: [PATCH 04/12] =?UTF-8?q?feat:=20=E8=A7=84=E8=8C=83=E5=AF=B9?= =?UTF-8?q?=E5=A4=96API=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java index f60796d..9273119 100644 --- a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java +++ b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java @@ -406,8 +406,8 @@ public int getNetworkStack() { /** * 清除所有缓存 */ - public void clearCache() { - clearCache(null); + public void clearHostCache() { + clearHostCache(null); } /** @@ -415,7 +415,7 @@ public void clearCache() { * * @param hostname 指定域名,多个域名用,分割 */ - public void clearCache(@Nullable String hostname) { + public void clearHostCache(@Nullable String hostname) { Cache.getInstance().clearCache(hostname); } From 7077f7bfb0f7887942f929822dc1d3ad29e183b6 Mon Sep 17 00:00:00 2001 From: lemondark <743855454@qq.com> Date: Wed, 6 Sep 2023 16:45:53 +0800 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20=E8=A7=84=E8=8C=83=E4=BC=A0?= =?UTF-8?q?=E5=8F=82=E5=90=8D=E7=A7=B0=EF=BC=8C=E5=92=8C=E8=A7=A3=E6=9E=90?= =?UTF-8?q?API=E4=BF=9D=E6=8C=81=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java index 9273119..25225ef 100644 --- a/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java +++ b/src/main/java/com/tencent/msdk/dns/MSDKDnsResolver.java @@ -413,10 +413,10 @@ public void clearHostCache() { /** * 清除指定域名缓存,hostname为null时清除所有域名缓存 * - * @param hostname 指定域名,多个域名用,分割 + * @param domain 指定域名,多个域名用,分割 */ - public void clearHostCache(@Nullable String hostname) { - Cache.getInstance().clearCache(hostname); + public void clearHostCache(@Nullable String domain) { + Cache.getInstance().clearCache(domain); } @SuppressWarnings("unused") From fd875c1d09ba4fbe8f965b056eb9670f77a6bbdf Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Fri, 15 Sep 2023 14:16:06 +0800 Subject: [PATCH 06/12] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0localdns?= =?UTF-8?q?=E8=80=97=E6=97=B6=E6=97=B6=E9=97=B4=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/report/AttaHelper.java | 2 ++ .../java/com/tencent/msdk/dns/report/ReportHelper.java | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java index b8bf21b..477363b 100644 --- a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java @@ -33,6 +33,7 @@ public static Runnable report(final String carrier, final long eventTime, final String dnsIp, final long spend, + final long local_spend, final String req_dn, final String req_type, final long req_timeout, @@ -64,6 +65,7 @@ public void run() { + "&systemName=" + SYSTEMNANE + "&systemVersion=" + SYSTEMVERSION + "&spend=" + spend + + "&local_spend=" + local_spend + "&req_dn=" + req_dn + "&req_type=" + req_type + "&req_timeout=" + req_timeout diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index a200836..ac2a62b 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -201,19 +201,19 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR if (statMerge.restDnsStat.errorCode == 0) { // 请求成功后将ErrorCount置为0 backupInfo.setErrorCount(0); - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } else { // ErrorCode==2 (超时)进行容灾处理,https请求存在请求异常超时时间>timeoutMills,此时errCode为1 if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { // 解析失败,仅当达到最大失败次数满足切换IP时候上报 if (backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } // 报错记录+1 backupInfo.incrementErrorCount(); DnsLog.d("dnsip连接失败, 当前失败次数:" + backupInfo.getErrorCount()); } else { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } } } @@ -237,11 +237,11 @@ private static void attaReportStatisticsEvent(Map cacheStatist String reqType = AttaHelper.getReqType(statMerge.curNetStack); if (errCount > 0) { // 为空的缓存统计项上报,解析结果不上报 - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 3, 0, true, errCount, null, null)); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 3, 0, true, errCount, null, null)); } if (curCount > 0) { // 有值的缓存统计项上报,解析结果不上报 - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 0, 0, true, curCount, null, null)); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 0, 0, true, curCount, null, null)); } } } From 4e4740fbd855a58f7024538b34c7e140707e5eec Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Mon, 18 Sep 2023 16:41:58 +0800 Subject: [PATCH 07/12] =?UTF-8?q?feat:=20=E5=9C=A8=E8=A7=A3=E6=9E=90merge?= =?UTF-8?q?=E9=80=9A=E9=81=93=E6=9B=BF=E4=BB=A3=E5=A4=96=E5=B1=82=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=EF=BC=8C=E4=B8=8D=E4=BC=9A=E4=BA=A7=E7=94=9F=E9=98=BB?= =?UTF-8?q?=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/msdk/dns/BackupResolver.java | 3 --- .../java/com/tencent/msdk/dns/DnsService.java | 1 - .../msdk/dns/core/rest/share/AbsRestDns.java | 1 - .../msdk/dns/core/stat/StatisticsMerge.java | 27 ++++++++++--------- .../tencent/msdk/dns/report/ReportHelper.java | 15 +++++------ 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/BackupResolver.java b/src/main/java/com/tencent/msdk/dns/BackupResolver.java index 508faf4..aedc5b3 100644 --- a/src/main/java/com/tencent/msdk/dns/BackupResolver.java +++ b/src/main/java/com/tencent/msdk/dns/BackupResolver.java @@ -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; diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index 71e0dc4..be789bb 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -286,7 +286,6 @@ private static IpSet getAddrsByName(/* @Nullable */String hostname, .enableAsyncLookup(enableAsyncLookup) .customNetStack(sConfig.customNetStack) .build()); - ReportHelper.reportLookupMethodCalledEvent(lookupResult); return lookupResult.ipSet; } if (fallback2Local) { diff --git a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java index b7dadec..4a01be7 100644 --- a/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java +++ b/src/main/java/com/tencent/msdk/dns/core/rest/share/AbsRestDns.java @@ -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; diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 8f615f1..3f897ef 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -2,6 +2,7 @@ import android.content.Context; +import com.tencent.msdk.dns.DnsService; import com.tencent.msdk.dns.base.log.DnsLog; import com.tencent.msdk.dns.base.utils.CommonUtils; import com.tencent.msdk.dns.base.utils.NetworkUtils; @@ -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; @@ -111,6 +114,17 @@ public void merge(IDns dns, Statistics sta } else { restDnsStat = (AbsRestDns.Statistics) stat; } + // 1. 当httpdns和localdns都endLookup时,即costTimeMills不等于初始值0,即为两个线程解析结束,进行上报。 + // 2. 当允许使用过期缓存配置下(httponly),httpdns异步请求完成时,进行上报。 + // 3. 当httpdns解析完成,(非异步请求)命中缓存时,进行上报。 + if ((restDnsStat.costTimeMills > 0 && localDnsStat.costTimeMills > 0) + || DnsService.getDnsConfig().useExpiredIpEnable + || restDnsStat.cached) { + // 上报数据处理,上报仅使用statisticsMerge类。IpSet使用IpSet.EMPTY传参。 + LookupResult lookupResult = new LookupResult(IpSet.EMPTY, this); + DnsLog.d("TESTTTTT------" + lookupResult); + ReportHelper.reportLookupMethodCalledEvent(lookupResult); + } } @Override @@ -174,17 +188,6 @@ public String toJsonResult() { @Override public String toString() { - return super.toString() + "{" + - "netType='" + netType + '\'' + - ", hostname='" + hostname + '\'' + - ", channel='" + channel + '\'' + - ", curNetStack=" + curNetStack + - ", localDnsStat=" + localDnsStat + - ", restDnsStat=" + restDnsStat + - ", ipSet=" + ipSet + - ", lookupSuccess=" + lookupSuccess + - ", lookupGetEmptyResponse=" + lookupFailed + - ", hasBeenMerge=" + hasBeenMerge + - '}'; + return super.toString() + "{" + "netType='" + netType + '\'' + ", hostname='" + hostname + '\'' + ", channel='" + channel + '\'' + ", curNetStack=" + curNetStack + ", localDnsStat=" + localDnsStat + ", restDnsStat=" + restDnsStat + ", ipSet=" + ipSet + ", lookupSuccess=" + lookupSuccess + ", lookupGetEmptyResponse=" + lookupFailed + ", hasBeenMerge=" + hasBeenMerge + '}'; } } diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index ac2a62b..28022ac 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -14,7 +14,6 @@ import com.tencent.msdk.dns.base.report.ReportManager; import com.tencent.msdk.dns.base.utils.CommonUtils; import com.tencent.msdk.dns.core.Const; -import com.tencent.msdk.dns.core.IpSet; import com.tencent.msdk.dns.core.LookupResult; import com.tencent.msdk.dns.core.stat.StatisticsMerge; @@ -58,7 +57,7 @@ public static void reportPreLookupEvent(LookupResult lookupResult) { attaReportLookupEvent(ReportConst.PRE_LOOKUP_EVENT_NAME, lookupResult); // NOTE: 上报字段增减, 记得修改capacity - Map preLookupEventMap = CollectionCompat.createMap(24); + Map preLookupEventMap = CollectionCompat.createMap(16); StatisticsMerge statMerge = (StatisticsMerge) lookupResult.stat; preLookupEventMap.put(ReportConst.CHANNEL_KEY, sDnsConfig.channel); @@ -110,13 +109,13 @@ public static void reportLookupMethodCalledEvent(LookupResult lookupResult) { } // NOTE: 上报字段增减, 记得修改capacity - Map lookupMethodCalledEventMap = CollectionCompat.createMap(29); + Map lookupMethodCalledEventMap = CollectionCompat.createMap(20); - IpSet ipSet = lookupResult.ipSet; - lookupMethodCalledEventMap.put(ReportConst.INET_LOOKUP_IPS_KEY, - CommonUtils.toStringList(ipSet.v4Ips, ReportConst.IP_SPLITTER)); - lookupMethodCalledEventMap.put(ReportConst.INET6_LOOKUP_IPS_KEY, - CommonUtils.toStringList(ipSet.v6Ips, ReportConst.IP_SPLITTER)); +// IpSet ipSet = lookupResult.ipSet; +// lookupMethodCalledEventMap.put(ReportConst.INET_LOOKUP_IPS_KEY, +// CommonUtils.toStringList(ipSet.v4Ips, ReportConst.IP_SPLITTER)); +// lookupMethodCalledEventMap.put(ReportConst.INET6_LOOKUP_IPS_KEY, +// CommonUtils.toStringList(ipSet.v6Ips, ReportConst.IP_SPLITTER)); lookupMethodCalledEventMap.put(ReportConst.CHANNEL_KEY, statMerge.channel); lookupMethodCalledEventMap.put(ReportConst.NETWORK_TYPE_KEY, statMerge.netType); From 108c4ccee497d88c92624926057ab9b968f3e1d8 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Mon, 18 Sep 2023 17:22:44 +0800 Subject: [PATCH 08/12] =?UTF-8?q?feat:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msdk/dns/core/stat/StatisticsMerge.java | 16 ++++------------ .../com/tencent/msdk/dns/report/AttaHelper.java | 4 ++-- .../tencent/msdk/dns/report/ReportHelper.java | 14 +++++++++----- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 3f897ef..71cefd6 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -2,7 +2,6 @@ import android.content.Context; -import com.tencent.msdk.dns.DnsService; import com.tencent.msdk.dns.base.log.DnsLog; import com.tencent.msdk.dns.base.utils.CommonUtils; import com.tencent.msdk.dns.base.utils.NetworkUtils; @@ -114,17 +113,10 @@ public void merge(IDns dns, Statistics sta } else { restDnsStat = (AbsRestDns.Statistics) stat; } - // 1. 当httpdns和localdns都endLookup时,即costTimeMills不等于初始值0,即为两个线程解析结束,进行上报。 - // 2. 当允许使用过期缓存配置下(httponly),httpdns异步请求完成时,进行上报。 - // 3. 当httpdns解析完成,(非异步请求)命中缓存时,进行上报。 - if ((restDnsStat.costTimeMills > 0 && localDnsStat.costTimeMills > 0) - || DnsService.getDnsConfig().useExpiredIpEnable - || restDnsStat.cached) { - // 上报数据处理,上报仅使用statisticsMerge类。IpSet使用IpSet.EMPTY传参。 - LookupResult lookupResult = new LookupResult(IpSet.EMPTY, this); - DnsLog.d("TESTTTTT------" + lookupResult); - ReportHelper.reportLookupMethodCalledEvent(lookupResult); - } + + // 上报数据处理,上报仅使用statisticsMerge类。IpSet使用IpSet.EMPTY传参。 + LookupResult lookupResult = new LookupResult(IpSet.EMPTY, this); + ReportHelper.reportLookupMethodCalledEvent(lookupResult); } @Override diff --git a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java index 477363b..ca4cd4d 100644 --- a/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/AttaHelper.java @@ -33,7 +33,7 @@ public static Runnable report(final String carrier, final long eventTime, final String dnsIp, final long spend, - final long local_spend, + final long ldns_spend, final String req_dn, final String req_type, final long req_timeout, @@ -65,7 +65,7 @@ public void run() { + "&systemName=" + SYSTEMNANE + "&systemVersion=" + SYSTEMVERSION + "&spend=" + spend - + "&local_spend=" + local_spend + + "&ldns_spend=" + ldns_spend + "&req_dn=" + req_dn + "&req_type=" + req_type + "&req_timeout=" + req_timeout diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index 28022ac..f6d1717 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -97,15 +97,19 @@ public static void reportLookupMethodCalledEvent(LookupResult lookupResult) { } StatisticsMerge statMerge = (StatisticsMerge) lookupResult.stat; + // 1. 当httpdns解析完成,(非异步请求)命中缓存时,进行上报。 + // 2. 当允许使用过期缓存配置下(httponly),httpdns异步请求完成时,进行上报。 + // 3. 当httpdns和localdns都endLookup时,即costTimeMills不等于初始值0,即为两个线程解析结束,进行上报。 + // 其他,返回不再打印 if (statMerge.restDnsStat.cached) { // 命中缓存的数据,统计上报 CacheStatisticsReport.add(lookupResult); + } else if (sDnsConfig.useExpiredIpEnable) { + attaReportLookupEvent(ReportConst.EXPIRED_ASYNC_LOOKUP_EVENT_NAME, lookupResult); + } else if (statMerge.restDnsStat.costTimeMills > 0 && statMerge.localDnsStat.costTimeMills > 0) { + attaReportLookupEvent(ReportConst.LOOKUP_METHOD_CALLED_EVENT_NAME, lookupResult); } else { - if (sDnsConfig.useExpiredIpEnable) { - attaReportLookupEvent(ReportConst.EXPIRED_ASYNC_LOOKUP_EVENT_NAME, lookupResult); - } else { - attaReportLookupEvent(ReportConst.LOOKUP_METHOD_CALLED_EVENT_NAME, lookupResult); - } + return; } // NOTE: 上报字段增减, 记得修改capacity From 6c340a5218455d4f0bab1352d1ed29e15e72bdd2 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Mon, 18 Sep 2023 17:33:48 +0800 Subject: [PATCH 09/12] =?UTF-8?q?chore:=20=E6=A0=BC=E5=BC=8F=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tencent/msdk/dns/core/stat/StatisticsMerge.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 71cefd6..0f1cfc8 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -180,6 +180,17 @@ public String toJsonResult() { @Override public String toString() { - return super.toString() + "{" + "netType='" + netType + '\'' + ", hostname='" + hostname + '\'' + ", channel='" + channel + '\'' + ", curNetStack=" + curNetStack + ", localDnsStat=" + localDnsStat + ", restDnsStat=" + restDnsStat + ", ipSet=" + ipSet + ", lookupSuccess=" + lookupSuccess + ", lookupGetEmptyResponse=" + lookupFailed + ", hasBeenMerge=" + hasBeenMerge + '}'; + return super.toString() + "{" + + "netType='" + netType + '\'' + + ", hostname='" + hostname + '\'' + + ", channel='" + channel + '\'' + + ", curNetStack=" + curNetStack + + ", localDnsStat=" + localDnsStat + + ", restDnsStat=" + restDnsStat + + ", ipSet=" + ipSet + + ", lookupSuccess=" + lookupSuccess + + ", lookupGetEmptyResponse=" + lookupFailed + + ", hasBeenMerge=" + hasBeenMerge + + '}'; } } From aea2c683d50de0c35c36c0046ef586472767df60 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Mon, 16 Oct 2023 19:37:45 +0800 Subject: [PATCH 10/12] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=8B?= =?UTF-8?q?=E5=8F=91=E8=A7=A3=E6=9E=90=E8=AF=B7=E6=B1=82=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5hostname=E4=B8=BA=E7=A9=BA=E9=97=AE=E9=A2=98&?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E7=BB=9F=E8=AE=A1=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/core/DnsManager.java | 11 ++++++----- .../tencent/msdk/dns/core/stat/StatisticsMerge.java | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index 9b18f22..731929b 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -13,6 +13,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; @@ -235,8 +236,7 @@ LookupResult lookup(LookupParameters lookupParams List sessions = new ArrayList<>(); lookupContext.sessions(sessions); try { - // NOTE: 当前对外API上, 不支持AAAA记录的解析, 需要保留LocalDns的解析结果作为AAAA解析结果 - // 暂时不忽略LocalDns解析结果(即超时时间内会等待LocalDns解析结果, 无论RestDns是否已经解析成功) + // 暂时不忽略LocalDns解析结果(RestDns解析失败时,超时时间内会等待LocalDns解析结果) if (null != restDnsGroup) { // 先查缓存,有其一即可 LookupResult lookupResultFromCache = getResultFromCache(lookupParams); @@ -245,12 +245,16 @@ LookupResult lookup(LookupParameters lookupParams if (lookupResultFromCache.stat.lookupPartCached()) { // 仅部分命中缓存 lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet); + // 收集命中缓存的数据 + CacheStatisticsReport.add(lookupResultFromCache); } else { lookupResultHolder.mLookupResult = lookupResultFromCache; DnsLog.d("DnsManager lookup getResultFromCache success"); return lookupResultFromCache; } } + // statContext操作在读完缓存之后,下发解析请求前。结果会更新在lookupResult上 + statMerge.statContext(lookupContext); // 打开Selector prepareTasks(restDnsGroup, lookupContext); @@ -355,9 +359,6 @@ LookupResult lookup(LookupParameters 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 diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index 0f1cfc8..e96491c 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -183,6 +183,7 @@ public String toString() { return super.toString() + "{" + "netType='" + netType + '\'' + ", hostname='" + hostname + '\'' + + ", requestHostname='" + requestHostname + '\'' + ", channel='" + channel + '\'' + ", curNetStack=" + curNetStack + ", localDnsStat=" + localDnsStat + From 731af1a390541d3cc9a32cb83d185beb8167db40 Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Tue, 17 Oct 2023 11:24:43 +0800 Subject: [PATCH 11/12] =?UTF-8?q?feat:=20=E7=BC=93=E5=AD=98=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=95=B0=E6=8D=AE=E4=BB=A5domain=E8=AE=A1=E8=B4=B9?= =?UTF-8?q?=E7=BB=B4=E5=BA=A6=E7=BB=9F=E8=AE=A1=E4=B8=8A=E6=8A=A5=EF=BC=8C?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E4=BB=BB=E5=8A=A1=E7=BA=BF=E7=A8=8B=E4=B8=8D?= =?UTF-8?q?=E5=BD=B1=E5=93=8D=E4=B8=BB=E7=BA=BF=E7=A8=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tencent/msdk/dns/DnsService.java | 9 +++- .../com/tencent/msdk/dns/core/DnsManager.java | 11 +++- .../msdk/dns/core/stat/StatisticsMerge.java | 11 +++- .../dns/report/CacheStatisticsReport.java | 54 +++++++++++++------ .../tencent/msdk/dns/report/ReportHelper.java | 16 +++--- 5 files changed, 71 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index be789bb..a4fd710 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -184,7 +184,7 @@ public static void setDnsConfigFromServer(boolean mEnableReport, boolean mEnable public static String getDnsDetail(String hostname) { String dnsIp = BackupResolver.getInstance().getDnsIp(); - LookupResult lookupResult = DnsManager.getResultFromCache(new LookupParameters.Builder() + final LookupResult lookupResult = DnsManager.getResultFromCache(new LookupParameters.Builder() .context(sAppContext) .hostname(hostname) .timeoutMills(sConfig.timeoutMills) @@ -198,7 +198,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(); } diff --git a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java index 731929b..dc0ecd6 100644 --- a/src/main/java/com/tencent/msdk/dns/core/DnsManager.java +++ b/src/main/java/com/tencent/msdk/dns/core/DnsManager.java @@ -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; @@ -239,14 +240,20 @@ LookupResult lookup(LookupParameters lookupParams // 暂时不忽略LocalDns解析结果(RestDns解析失败时,超时时间内会等待LocalDns解析结果) if (null != restDnsGroup) { // 先查缓存,有其一即可 - LookupResult lookupResultFromCache = getResultFromCache(lookupParams); + final LookupResult lookupResultFromCache = getResultFromCache(lookupParams); DnsLog.d("getResultFromCache: " + lookupResultFromCache); if (lookupResultFromCache.stat.lookupSuccess()) { if (lookupResultFromCache.stat.lookupPartCached()) { // 仅部分命中缓存 lookupContext.sorter().putPartCache(lookupResultFromCache.ipSet); // 收集命中缓存的数据 - CacheStatisticsReport.add(lookupResultFromCache); + DnsExecutors.WORK.execute(new Runnable() { + @Override + public void run() { + CacheStatisticsReport.add(lookupResultFromCache); + } + }); + } else { lookupResultHolder.mLookupResult = lookupResultFromCache; DnsLog.d("DnsManager lookup getResultFromCache success"); diff --git a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java index e96491c..b6ccd0d 100644 --- a/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java +++ b/src/main/java/com/tencent/msdk/dns/core/stat/StatisticsMerge.java @@ -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; @@ -115,8 +116,14 @@ public void merge(IDns dns, Statistics sta } // 上报数据处理,上报仅使用statisticsMerge类。IpSet使用IpSet.EMPTY传参。 - LookupResult lookupResult = new LookupResult(IpSet.EMPTY, this); - ReportHelper.reportLookupMethodCalledEvent(lookupResult); + final LookupResult lookupResult = new LookupResult(IpSet.EMPTY, this); + DnsExecutors.WORK.execute(new Runnable() { + @Override + public void run() { + ReportHelper.reportLookupMethodCalledEvent(lookupResult); + } + }); + } @Override diff --git a/src/main/java/com/tencent/msdk/dns/report/CacheStatisticsReport.java b/src/main/java/com/tencent/msdk/dns/report/CacheStatisticsReport.java index 5cf64d7..d30601a 100644 --- a/src/main/java/com/tencent/msdk/dns/report/CacheStatisticsReport.java +++ b/src/main/java/com/tencent/msdk/dns/report/CacheStatisticsReport.java @@ -5,11 +5,14 @@ import com.tencent.msdk.dns.core.LookupResult; import com.tencent.msdk.dns.core.stat.StatisticsMerge; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class CacheStatisticsReport { - private static Map statisticsMap = new HashMap<>(); + private static final Map statisticsMap = new HashMap<>(); public static void add(LookupResult lookupResult) { if (null == lookupResult) { @@ -20,27 +23,48 @@ public static void add(LookupResult lookupResult) { return; } StatisticsMerge statMerge = (StatisticsMerge) lookupResult.stat; + String[] hostnameArr; + + if (statMerge.lookupPartCached()) { + // 对比hostname和requestHostname差值即为部分命中缓存域名数据 + hostnameArr = compare(statMerge.hostname.split(","), statMerge.requestHostname.split(",")); + } else { + hostnameArr = statMerge.hostname.split(","); + } // 命中缓存的数据,统计上报 - String hostname = statMerge.hostname; - if (!statisticsMap.containsKey(hostname)) { - if (lookupResult.stat.lookupSuccess()) { - // Object[costTimeMillsTotal, emptyCount, resultCount, lookupResult] - statisticsMap.put(hostname, new Object[]{statMerge.restDnsStat.costTimeMills, 0, 1, lookupResult}); + for(String hostname: hostnameArr) { + if (!statisticsMap.containsKey(hostname)) { + if (statMerge.lookupSuccess()) { + // Object[costTimeMillsTotal, emptyCount, resultCount] + statisticsMap.put(hostname, new Object[]{statMerge.restDnsStat.costTimeMills, 0, 1}); + } else { + statisticsMap.put(hostname, new Object[]{statMerge.restDnsStat.costTimeMills, 1, 0}); + } + } else { - statisticsMap.put(hostname, new Object[]{statMerge.restDnsStat.costTimeMills, 1, 0, lookupResult}); + Object[] temp = statisticsMap.get(hostname); + assert temp != null; + temp[0] = (Integer) temp[0] + statMerge.restDnsStat.costTimeMills; + if (statMerge.lookupSuccess()) { + temp[2] = (Integer) temp[2] + 1; + } else { + temp[1] = (Integer) temp[1] + 1; + } + statisticsMap.put(hostname, temp); } + } + } - } else { - Object[] temp = statisticsMap.get(hostname); - temp[0] = (Integer) temp[0] + statMerge.restDnsStat.costTimeMills; - if (lookupResult.stat.lookupSuccess()) { - temp[2] = (Integer) temp[2] + 1; - } else { - temp[1] = (Integer) temp[1] + 1; + public static String[] compare(String[] strArr1, String[] strArr2) { + List list = new ArrayList<>(); + List list2 = Arrays.asList(strArr2); + for(String str: strArr1) { + if (!list2.contains(str)) { + list.add(str); } - statisticsMap.put(hostname, temp); } + return list.toArray(new String[list.size()]); } public static Map offerAll() { diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index f6d1717..17c7736 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -25,7 +25,7 @@ public final class ReportHelper { private static DnsConfig sDnsConfig; - private static Runnable sReportAsyncLookupEventTask = new Runnable() { + private static final Runnable sReportAsyncLookupEventTask = new Runnable() { @Override public void run() { // atta上报统计数据 @@ -124,7 +124,7 @@ public static void reportLookupMethodCalledEvent(LookupResult lookupResult) { lookupMethodCalledEventMap.put(ReportConst.CHANNEL_KEY, statMerge.channel); lookupMethodCalledEventMap.put(ReportConst.NETWORK_TYPE_KEY, statMerge.netType); lookupMethodCalledEventMap.put(ReportConst.HOSTNAME_KEY, statMerge.hostname); - if (statMerge.requestHostname != statMerge.hostname) { + if (!statMerge.requestHostname.equals(statMerge.hostname)) { lookupMethodCalledEventMap.put(ReportConst.REQUEST_HOSTNAME_KEY, statMerge.requestHostname); } lookupMethodCalledEventMap.put(ReportConst.NETWORK_STACK_KEY, @@ -204,19 +204,19 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR if (statMerge.restDnsStat.errorCode == 0) { // 请求成功后将ErrorCount置为0 backupInfo.setErrorCount(0); - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } else { // ErrorCode==2 (超时)进行容灾处理,https请求存在请求异常超时时间>timeoutMills,此时errCode为1 if (statMerge.restDnsStat.errorCode == 2 || (Const.HTTPS_CHANNEL.equals(sDnsConfig.channel) && (statMerge.restDnsStat.errorCode == 1))) { // 解析失败,仅当达到最大失败次数满足切换IP时候上报 if (backupInfo.getCanReport(backupInfo.getErrorCount() + 1)) { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } // 报错记录+1 backupInfo.incrementErrorCount(); DnsLog.d("dnsip连接失败, 当前失败次数:" + backupInfo.getErrorCount()); } else { - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.hostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, eventName, System.currentTimeMillis(), dnsIp, statMerge.restDnsStat.costTimeMills, statMerge.localDnsStat.costTimeMills, statMerge.requestHostname, reqType, sDnsConfig.timeoutMills, statMerge.restDnsStat.ttl, statMerge.restDnsStat.errorCode, statMerge.restDnsStat.statusCode, statMerge.restDnsStat.cached, 1, CommonUtils.toStringList(statMerge.localDnsStat.ips, ReportConst.IP_SPLITTER), CommonUtils.toStringList(statMerge.restDnsStat.ips, ReportConst.IP_SPLITTER))); } } } @@ -232,19 +232,17 @@ private static void attaReportStatisticsEvent(Map cacheStatist int errCount = (int) temp[1]; int curCount = (int) temp[2]; int spendAvg = (int) temp[0] / (errCount + curCount); - StatisticsMerge statMerge = (StatisticsMerge) ((LookupResult) temp[3]).stat; // 获取手机卡运营商code String carrierCode = AttaHelper.getSimOperator(DnsService.getAppContext()); // 获取当前dnsip String dnsIp = BackupResolver.getInstance().getDnsIp(); - String reqType = AttaHelper.getReqType(statMerge.curNetStack); if (errCount > 0) { // 为空的缓存统计项上报,解析结果不上报 - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 3, 0, true, errCount, null, null)); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, "", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, item.getKey(), "", sDnsConfig.timeoutMills, 0, 3, 0, true, errCount, null, null)); } if (curCount > 0) { // 有值的缓存统计项上报,解析结果不上报 - DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, statMerge.netType, sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, statMerge.hostname, reqType, sDnsConfig.timeoutMills, 0, 0, 0, true, curCount, null, null)); + DnsExecutors.MAIN.execute(AttaHelper.report(carrierCode, "", sDnsConfig.lookupExtra.bizId, sDnsConfig.appId, sDnsConfig.channel, ReportConst.LOOKUP_FROM_CACHED_EVENT_NAME, System.currentTimeMillis(), dnsIp, spendAvg, 0, item.getKey(), "", sDnsConfig.timeoutMills, 0, 0, 0, true, curCount, null, null)); } } } From b233f9ceb4c99a6a95e057294c4cf5ac1482786c Mon Sep 17 00:00:00 2001 From: sarahzzhang <743855454@qq.com> Date: Tue, 17 Oct 2023 16:05:17 +0800 Subject: [PATCH 12/12] =?UTF-8?q?feat:=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/tencent/msdk/dns/DnsService.java | 5 ++--- .../java/com/tencent/msdk/dns/report/ReportHelper.java | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/tencent/msdk/dns/DnsService.java b/src/main/java/com/tencent/msdk/dns/DnsService.java index a4fd710..7ceb9f2 100644 --- a/src/main/java/com/tencent/msdk/dns/DnsService.java +++ b/src/main/java/com/tencent/msdk/dns/DnsService.java @@ -112,7 +112,6 @@ public void run() { * 设置UserId, 进行数据上报时区分用户, 出现问题时, 依赖该Id进行单用户问题排查 * * @param userId 用户的唯一标识符, 腾讯业务建议直接使用OpenId, 腾讯云客户建议传入长度50位以内, 由字母数字下划线组合而成的字符串 - * @return 是否设置成功, true为设置成功, false为设置失败 * @throws IllegalStateException 没有初始化时抛出 * @throws IllegalArgumentException userId为空时抛出 */ @@ -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) { diff --git a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java index 17c7736..129a65c 100644 --- a/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java +++ b/src/main/java/com/tencent/msdk/dns/report/ReportHelper.java @@ -29,8 +29,7 @@ public final class ReportHelper { @Override public void run() { // atta上报统计数据 - Map cacheStatisticsMap = CacheStatisticsReport.offerAll(); - attaReportStatisticsEvent(cacheStatisticsMap); + attaReportStatisticsEvent(); DnsExecutors.MAIN.cancel(sReportAsyncLookupEventTask); DnsExecutors.MAIN.schedule( sReportAsyncLookupEventTask, REPORT_ASYNC_LOOKUP_EVENT_INTERVAL_MILLS); @@ -223,8 +222,9 @@ private static void attaReportLookupEvent(String eventName, LookupResult lookupR } - private static void attaReportStatisticsEvent(Map cacheStatisticsMap) { - if (!sDnsConfig.enableReport) { + private static void attaReportStatisticsEvent() { + Map cacheStatisticsMap = CacheStatisticsReport.offerAll(); + if (!sDnsConfig.enableReport || cacheStatisticsMap.isEmpty()) { return; } for (Map.Entry item : cacheStatisticsMap.entrySet()) {