diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/dns | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/dns')
-rw-r--r-- | library/cpp/dns/cache.cpp | 160 | ||||
-rw-r--r-- | library/cpp/dns/cache.h | 48 | ||||
-rw-r--r-- | library/cpp/dns/magic.cpp | 50 | ||||
-rw-r--r-- | library/cpp/dns/magic.h | 30 | ||||
-rw-r--r-- | library/cpp/dns/thread.cpp | 178 | ||||
-rw-r--r-- | library/cpp/dns/ut/dns_ut.cpp | 14 | ||||
-rw-r--r-- | library/cpp/dns/ut/ya.make | 18 |
7 files changed, 249 insertions, 249 deletions
diff --git a/library/cpp/dns/cache.cpp b/library/cpp/dns/cache.cpp index 05c14e82fc..6d2dbe9ec9 100644 --- a/library/cpp/dns/cache.cpp +++ b/library/cpp/dns/cache.cpp @@ -1,121 +1,121 @@ #include "cache.h" - + #include "thread.h" #include <util/system/tls.h> #include <util/system/info.h> #include <util/system/rwlock.h> -#include <util/thread/singleton.h> +#include <util/thread/singleton.h> #include <util/generic/singleton.h> -#include <util/generic/hash.h> - +#include <util/generic/hash.h> + using namespace NDns; - -namespace { + +namespace { struct TResolveTask { enum EMethod { - Normal, - Threaded + Normal, + Threaded }; - + inline TResolveTask(const TResolveInfo& info, EMethod method) : Info(info) , Method(method) { } - + const TResolveInfo& Info; const EMethod Method; }; - + class IDns { public: virtual ~IDns() = default; virtual const TResolvedHost* Resolve(const TResolveTask&) = 0; }; - typedef TAtomicSharedPtr<TResolvedHost> TResolvedHostPtr; - + typedef TAtomicSharedPtr<TResolvedHost> TResolvedHostPtr; + struct THashResolveInfo { inline size_t operator()(const TResolveInfo& ri) const { return ComputeHash(ri.Host) ^ ri.Port; } }; - + struct TCompareResolveInfo { inline bool operator()(const NDns::TResolveInfo& x, const NDns::TResolveInfo& y) const { return x.Host == y.Host && x.Port == y.Port; } }; - + class TGlobalCachedDns: public IDns, public TNonCopyable { public: const TResolvedHost* Resolve(const TResolveTask& rt) override { //2. search host in cache { TReadGuard guard(L_); - + TCache::const_iterator it = C_.find(rt.Info); - + if (it != C_.end()) { return it->second.Get(); - } - } - + } + } + TResolvedHostPtr res = ResolveA(rt); - + //update cache { TWriteGuard guard(L_); - + std::pair<TCache::iterator, bool> updateResult = C_.insert(std::make_pair(TResolveInfo(res->Host, rt.Info.Port), res)); - TResolvedHost* rh = updateResult.first->second.Get(); - + TResolvedHost* rh = updateResult.first->second.Get(); + if (updateResult.second) { //fresh resolved host, set cache record id for it rh->Id = C_.size() - 1; } - + return rh; - } + } } - + void AddAlias(const TString& host, const TString& alias) noexcept { TWriteGuard guard(LA_); - + A_[host] = alias; - } - + } + static inline TGlobalCachedDns* Instance() { return SingletonWithPriority<TGlobalCachedDns, 65530>(); - } - - private: + } + + private: inline TResolvedHostPtr ResolveA(const TResolveTask& rt) { TString originalHost(rt.Info.Host); TString host(originalHost); - + //3. replace host to alias, if exist if (A_.size()) { TReadGuard guard(LA_); TStringBuf names[] = {"*", host}; - - for (const auto& name : names) { + + for (const auto& name : names) { TAliases::const_iterator it = A_.find(name); - + if (it != A_.end()) { host = it->second; } - } - } - + } + } + if (host.length() > 2 && host[0] == '[') { TString unbracedIpV6(host.data() + 1, host.size() - 2); host.swap(unbracedIpV6); } TAutoPtr<TNetworkAddress> na; - + //4. getaddrinfo (direct or in separate thread) if (rt.Method == TResolveTask::Normal) { na.Reset(new TNetworkAddress(host, rt.Info.Port)); @@ -125,60 +125,60 @@ namespace { Y_ASSERT(0); throw yexception() << TStringBuf("invalid resolve method"); } - + return new TResolvedHost(originalHost, *na); - } - + } + typedef THashMap<TResolveInfo, TResolvedHostPtr, THashResolveInfo, TCompareResolveInfo> TCache; TCache C_; TRWMutex L_; typedef THashMap<TString, TString> TAliases; TAliases A_; TRWMutex LA_; - }; - - class TCachedDns: public IDns { - public: - inline TCachedDns(IDns* slave) - : S_(slave) - { - } - + }; + + class TCachedDns: public IDns { + public: + inline TCachedDns(IDns* slave) + : S_(slave) + { + } + const TResolvedHost* Resolve(const TResolveTask& rt) override { //1. search in local thread cache - { + { TCache::const_iterator it = C_.find(rt.Info); - - if (it != C_.end()) { - return it->second; - } - } - + + if (it != C_.end()) { + return it->second; + } + } + const TResolvedHost* res = S_->Resolve(rt); - + C_[TResolveInfo(res->Host, rt.Info.Port)] = res; - - return res; - } - - private: + + return res; + } + + private: typedef THashMap<TResolveInfo, const TResolvedHost*, THashResolveInfo, TCompareResolveInfo> TCache; - TCache C_; - IDns* S_; - }; - + TCache C_; + IDns* S_; + }; + struct TThreadedDns: public TCachedDns { - inline TThreadedDns() + inline TThreadedDns() : TCachedDns(TGlobalCachedDns::Instance()) - { - } - }; - + { + } + }; + inline IDns* ThrDns() { - return FastTlsSingleton<TThreadedDns>(); - } -} - + return FastTlsSingleton<TThreadedDns>(); + } +} + namespace NDns { const TResolvedHost* CachedResolve(const TResolveInfo& ri) { TResolveTask rt(ri, TResolveTask::Normal); @@ -195,4 +195,4 @@ namespace NDns { void AddHostAlias(const TString& host, const TString& alias) { TGlobalCachedDns::Instance()->AddAlias(host, alias); } -} +} diff --git a/library/cpp/dns/cache.h b/library/cpp/dns/cache.h index eda5dc4070..a49ffb972f 100644 --- a/library/cpp/dns/cache.h +++ b/library/cpp/dns/cache.h @@ -1,34 +1,34 @@ -#pragma once - -#include <util/network/socket.h> -#include <util/generic/strbuf.h> +#pragma once + +#include <util/network/socket.h> +#include <util/generic/strbuf.h> #include <util/generic/string.h> - + namespace NDns { - struct TResolveInfo { - inline TResolveInfo(const TStringBuf& host, ui16 port) - : Host(host) - , Port(port) - { - } - - TStringBuf Host; - ui16 Port; - }; - - struct TResolvedHost { + struct TResolveInfo { + inline TResolveInfo(const TStringBuf& host, ui16 port) + : Host(host) + , Port(port) + { + } + + TStringBuf Host; + ui16 Port; + }; + + struct TResolvedHost { inline TResolvedHost(const TString& host, const TNetworkAddress& addr) noexcept : Host(host) , Addr(addr) , Id(0) - { - } - + { + } + TString Host; //resolved hostname (from TResolveInfo, - before aliasing) - TNetworkAddress Addr; + TNetworkAddress Addr; size_t Id; //cache record id - }; - + }; + // Resolving order: // 1. check local thread cache, return if found // 2. check global cache, return if found @@ -42,4 +42,4 @@ namespace NDns { //create alias for host, which can be used for static resolving (when alias is ip address) void AddHostAlias(const TString& host, const TString& alias); -} +} diff --git a/library/cpp/dns/magic.cpp b/library/cpp/dns/magic.cpp index b93792146f..239c33fb75 100644 --- a/library/cpp/dns/magic.cpp +++ b/library/cpp/dns/magic.cpp @@ -1,28 +1,28 @@ -#include "magic.h" - -#include <util/generic/yexception.h> - +#include "magic.h" + +#include <util/generic/yexception.h> + using namespace NDns; - -namespace { - namespace NX { - struct TError: public IError { - inline TError() - : E_(std::current_exception()) - { - } - + +namespace { + namespace NX { + struct TError: public IError { + inline TError() + : E_(std::current_exception()) + { + } + void Raise() override { - std::rethrow_exception(E_); - } - - std::exception_ptr E_; - }; - } -} - + std::rethrow_exception(E_); + } + + std::exception_ptr E_; + }; + } +} + IErrorRef NDns::SaveError() { - using namespace NX; - - return new NX::TError(); -} + using namespace NX; + + return new NX::TError(); +} diff --git a/library/cpp/dns/magic.h b/library/cpp/dns/magic.h index d52cde0a6c..94673444cd 100644 --- a/library/cpp/dns/magic.h +++ b/library/cpp/dns/magic.h @@ -1,17 +1,17 @@ -#pragma once - -#include <util/generic/yexception.h> -#include <util/generic/ptr.h> - +#pragma once + +#include <util/generic/yexception.h> +#include <util/generic/ptr.h> + namespace NDns { - class IError { - public: + class IError { + public: virtual ~IError() = default; - - virtual void Raise() = 0; - }; - - typedef TAutoPtr<IError> IErrorRef; - - IErrorRef SaveError(); -} + + virtual void Raise() = 0; + }; + + typedef TAutoPtr<IError> IErrorRef; + + IErrorRef SaveError(); +} diff --git a/library/cpp/dns/thread.cpp b/library/cpp/dns/thread.cpp index 8b27d2d527..ca9c1ff8a1 100644 --- a/library/cpp/dns/thread.cpp +++ b/library/cpp/dns/thread.cpp @@ -1,133 +1,133 @@ #include "thread.h" -#include "magic.h" - +#include "magic.h" + #include <util/network/socket.h> #include <util/thread/factory.h> #include <util/thread/lfqueue.h> #include <util/system/event.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> #include <util/generic/singleton.h> - + using namespace NDns; - -namespace { + +namespace { class TThreadedResolver: public IThreadFactory::IThreadAble, public TNonCopyable { - struct TResolveRequest { + struct TResolveRequest { inline TResolveRequest(const TString& host, ui16 port) : Host(host) , Port(port) - { - } - + { + } + inline TNetworkAddressPtr Wait() { E.Wait(); - - if (!Error) { - if (!Result) { + + if (!Error) { + if (!Result) { ythrow TNetworkResolutionError(EAI_AGAIN) << TStringBuf(": resolver down"); - } - - return Result; - } - - Error->Raise(); - + } + + return Result; + } + + Error->Raise(); + ythrow TNetworkResolutionError(EAI_FAIL) << TStringBuf(": shit happen"); - } - + } + inline void Resolve() noexcept { - try { + try { Result = new TNetworkAddress(Host, Port); - } catch (...) { - Error = SaveError(); - } - - Wake(); - } - + } catch (...) { + Error = SaveError(); + } + + Wake(); + } + inline void Wake() noexcept { E.Signal(); - } - + } + TString Host; ui16 Port; TManualEvent E; TNetworkAddressPtr Result; - IErrorRef Error; - }; - - public: + IErrorRef Error; + }; + + public: inline TThreadedResolver() : E_(TSystemEvent::rAuto) { T_.push_back(SystemThreadFactory()->Run(this)); - } - + } + inline ~TThreadedResolver() override { Schedule(nullptr); - + for (size_t i = 0; i < T_.size(); ++i) { - T_[i]->Join(); - } - - { + T_[i]->Join(); + } + + { TResolveRequest* rr = nullptr; - - while (Q_.Dequeue(&rr)) { - if (rr) { - rr->Wake(); - } - } - } - } - - static inline TThreadedResolver* Instance() { - return Singleton<TThreadedResolver>(); - } - + + while (Q_.Dequeue(&rr)) { + if (rr) { + rr->Wake(); + } + } + } + } + + static inline TThreadedResolver* Instance() { + return Singleton<TThreadedResolver>(); + } + inline TNetworkAddressPtr Resolve(const TString& host, ui16 port) { TResolveRequest rr(host, port); - - Schedule(&rr); - - return rr.Wait(); - } - - private: - inline void Schedule(TResolveRequest* rr) { - Q_.Enqueue(rr); - E_.Signal(); - } - + + Schedule(&rr); + + return rr.Wait(); + } + + private: + inline void Schedule(TResolveRequest* rr) { + Q_.Enqueue(rr); + E_.Signal(); + } + void DoExecute() override { - while (true) { + while (true) { TResolveRequest* rr = nullptr; - - while (!Q_.Dequeue(&rr)) { - E_.Wait(); - } - - if (rr) { - rr->Resolve(); - } else { - break; - } - } - + + while (!Q_.Dequeue(&rr)) { + E_.Wait(); + } + + if (rr) { + rr->Resolve(); + } else { + break; + } + } + Schedule(nullptr); - } - - private: - TLockFreeQueue<TResolveRequest*> Q_; + } + + private: + TLockFreeQueue<TResolveRequest*> Q_; TSystemEvent E_; typedef TAutoPtr<IThreadFactory::IThread> IThreadRef; TVector<IThreadRef> T_; - }; + }; } - + namespace NDns { TNetworkAddressPtr ThreadedResolve(const TString& host, ui16 port) { return TThreadedResolver::Instance()->Resolve(host, port); - } -} + } +} diff --git a/library/cpp/dns/ut/dns_ut.cpp b/library/cpp/dns/ut/dns_ut.cpp index aae05a742c..5b99ee2a8a 100644 --- a/library/cpp/dns/ut/dns_ut.cpp +++ b/library/cpp/dns/ut/dns_ut.cpp @@ -1,20 +1,20 @@ #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/dns/cache.h> #include <util/network/address.h> - + Y_UNIT_TEST_SUITE(TestDNS) { - using namespace NDns; - + using namespace NDns; + Y_UNIT_TEST(TestMagic) { - UNIT_ASSERT_EXCEPTION(CachedThrResolve(TResolveInfo("?", 80)), yexception); - } + UNIT_ASSERT_EXCEPTION(CachedThrResolve(TResolveInfo("?", 80)), yexception); + } Y_UNIT_TEST(TestAsteriskAlias) { AddHostAlias("*", "localhost"); const TResolvedHost* rh = CachedThrResolve(TResolveInfo("yandex.ru", 80)); UNIT_ASSERT(rh != nullptr); - const TNetworkAddress& addr = rh->Addr; + const TNetworkAddress& addr = rh->Addr; for (TNetworkAddress::TIterator ai = addr.Begin(); ai != addr.End(); ai++) { if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6) { NAddr::TAddrInfo info(&*ai); @@ -22,4 +22,4 @@ Y_UNIT_TEST_SUITE(TestDNS) { } } } -} +} diff --git a/library/cpp/dns/ut/ya.make b/library/cpp/dns/ut/ya.make index 7cfd0c4c32..df0c944219 100644 --- a/library/cpp/dns/ut/ya.make +++ b/library/cpp/dns/ut/ya.make @@ -1,16 +1,16 @@ UNITTEST() - + OWNER( and42 pg ) -PEERDIR( +PEERDIR( library/cpp/dns -) - -SRCS( - dns_ut.cpp -) - -END() +) + +SRCS( + dns_ut.cpp +) + +END() |