rpc.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "rpc.h"
  2. #include "rq.h"
  3. #include "multi.h"
  4. #include "location.h"
  5. #include <library/cpp/threading/thread_local/thread_local.h>
  6. #include <util/generic/hash.h>
  7. #include <util/thread/factory.h>
  8. #include <util/system/spinlock.h>
  9. using namespace NNeh;
  10. namespace {
  11. typedef std::pair<TString, IServiceRef> TServiceDescr;
  12. typedef TVector<TServiceDescr> TServicesBase;
  13. class TServices: public TServicesBase, public TThrRefBase, public IOnRequest {
  14. typedef THashMap<TStringBuf, IServiceRef> TSrvs;
  15. struct TVersionedServiceMap {
  16. TSrvs Srvs;
  17. i64 Version = 0;
  18. };
  19. struct TFunc: public IThreadFactory::IThreadAble {
  20. inline TFunc(TServices* parent)
  21. : Parent(parent)
  22. {
  23. }
  24. void DoExecute() override {
  25. TThread::SetCurrentThreadName("NehTFunc");
  26. TVersionedServiceMap mp;
  27. while (true) {
  28. IRequestRef req = Parent->RQ_->Next();
  29. if (!req) {
  30. break;
  31. }
  32. Parent->ServeRequest(mp, req);
  33. }
  34. Parent->RQ_->Schedule(nullptr);
  35. }
  36. TServices* Parent;
  37. };
  38. public:
  39. inline TServices()
  40. : RQ_(CreateRequestQueue())
  41. {
  42. }
  43. inline TServices(TCheck check)
  44. : RQ_(CreateRequestQueue())
  45. , C_(check)
  46. {
  47. }
  48. inline ~TServices() override {
  49. LF_.Destroy();
  50. }
  51. inline void Add(const TString& service, IServiceRef srv) {
  52. TGuard<TSpinLock> guard(L_);
  53. push_back(std::make_pair(service, srv));
  54. AtomicIncrement(SelfVersion_);
  55. }
  56. inline void Listen() {
  57. Y_ENSURE(!HasLoop_ || !*HasLoop_);
  58. HasLoop_ = false;
  59. RR_ = MultiRequester(ListenAddrs(), this);
  60. }
  61. inline void Loop(size_t threads) {
  62. Y_ENSURE(!HasLoop_ || *HasLoop_);
  63. HasLoop_ = true;
  64. TIntrusivePtr<TServices> self(this);
  65. IRequesterRef rr = MultiRequester(ListenAddrs(), this);
  66. TFunc func(this);
  67. typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
  68. TVector<IThreadRef> thrs;
  69. for (size_t i = 1; i < threads; ++i) {
  70. thrs.push_back(SystemThreadFactory()->Run(&func));
  71. }
  72. func.Execute();
  73. for (size_t i = 0; i < thrs.size(); ++i) {
  74. thrs[i]->Join();
  75. }
  76. RQ_->Clear();
  77. }
  78. inline void ForkLoop(size_t threads) {
  79. Y_ENSURE(!HasLoop_ || *HasLoop_);
  80. HasLoop_ = true;
  81. //here we can have trouble with binding port(s), so expect exceptions
  82. IRequesterRef rr = MultiRequester(ListenAddrs(), this);
  83. LF_.Reset(new TLoopFunc(this, threads, rr));
  84. }
  85. inline void Stop() {
  86. RQ_->Schedule(nullptr);
  87. }
  88. inline void SyncStopFork() {
  89. Stop();
  90. if (LF_) {
  91. LF_->SyncStop();
  92. }
  93. RQ_->Clear();
  94. LF_.Destroy();
  95. }
  96. void OnRequest(IRequestRef req) override {
  97. if (C_) {
  98. if (auto error = C_(req)) {
  99. req->SendError(*error);
  100. return;
  101. }
  102. }
  103. if (!*HasLoop_) {
  104. ServeRequest(LocalMap_.GetRef(), req);
  105. } else {
  106. RQ_->Schedule(req);
  107. }
  108. }
  109. private:
  110. class TLoopFunc: public TFunc {
  111. public:
  112. TLoopFunc(TServices* parent, size_t threads, IRequesterRef& rr)
  113. : TFunc(parent)
  114. , RR_(rr)
  115. {
  116. T_.reserve(threads);
  117. try {
  118. for (size_t i = 0; i < threads; ++i) {
  119. T_.push_back(SystemThreadFactory()->Run(this));
  120. }
  121. } catch (...) {
  122. //paranoid mode on
  123. SyncStop();
  124. throw;
  125. }
  126. }
  127. ~TLoopFunc() override {
  128. try {
  129. SyncStop();
  130. } catch (...) {
  131. Cdbg << TStringBuf("neh rpc ~loop_func: ") << CurrentExceptionMessage() << Endl;
  132. }
  133. }
  134. void SyncStop() {
  135. if (!T_) {
  136. return;
  137. }
  138. Parent->Stop();
  139. for (size_t i = 0; i < T_.size(); ++i) {
  140. T_[i]->Join();
  141. }
  142. T_.clear();
  143. }
  144. private:
  145. typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
  146. TVector<IThreadRef> T_;
  147. IRequesterRef RR_;
  148. };
  149. inline void ServeRequest(TVersionedServiceMap& mp, IRequestRef req) {
  150. if (!req) {
  151. return;
  152. }
  153. const TStringBuf name = req->Service();
  154. TSrvs::const_iterator it = mp.Srvs.find(name);
  155. if (Y_UNLIKELY(it == mp.Srvs.end())) {
  156. if (UpdateServices(mp.Srvs, mp.Version)) {
  157. it = mp.Srvs.find(name);
  158. }
  159. }
  160. if (Y_UNLIKELY(it == mp.Srvs.end())) {
  161. it = mp.Srvs.find(TStringBuf("*"));
  162. }
  163. if (Y_UNLIKELY(it == mp.Srvs.end())) {
  164. req->SendError(IRequest::NotExistService);
  165. } else {
  166. try {
  167. it->second->ServeRequest(req);
  168. } catch (...) {
  169. Cdbg << CurrentExceptionMessage() << Endl;
  170. }
  171. }
  172. }
  173. inline bool UpdateServices(TSrvs& srvs, i64& version) const {
  174. if (AtomicGet(SelfVersion_) == version) {
  175. return false;
  176. }
  177. srvs.clear();
  178. TGuard<TSpinLock> guard(L_);
  179. for (const auto& it : *this) {
  180. srvs[TParsedLocation(it.first).Service] = it.second;
  181. }
  182. version = AtomicGet(SelfVersion_);
  183. return true;
  184. }
  185. inline TListenAddrs ListenAddrs() const {
  186. TListenAddrs addrs;
  187. {
  188. TGuard<TSpinLock> guard(L_);
  189. for (const auto& it : *this) {
  190. addrs.push_back(it.first);
  191. }
  192. }
  193. return addrs;
  194. }
  195. TSpinLock L_;
  196. IRequestQueueRef RQ_;
  197. THolder<TLoopFunc> LF_;
  198. TAtomic SelfVersion_ = 1;
  199. TCheck C_;
  200. NThreading::TThreadLocalValue<TVersionedServiceMap> LocalMap_;
  201. IRequesterRef RR_;
  202. TMaybe<bool> HasLoop_;
  203. };
  204. class TServicesFace: public IServices {
  205. public:
  206. inline TServicesFace()
  207. : S_(new TServices())
  208. {
  209. }
  210. inline TServicesFace(TCheck check)
  211. : S_(new TServices(check))
  212. {
  213. }
  214. void DoAdd(const TString& service, IServiceRef srv) override {
  215. S_->Add(service, srv);
  216. }
  217. void Loop(size_t threads) override {
  218. S_->Loop(threads);
  219. }
  220. void ForkLoop(size_t threads) override {
  221. S_->ForkLoop(threads);
  222. }
  223. void SyncStopFork() override {
  224. S_->SyncStopFork();
  225. }
  226. void Stop() override {
  227. S_->Stop();
  228. }
  229. void Listen() override {
  230. S_->Listen();
  231. }
  232. private:
  233. TIntrusivePtr<TServices> S_;
  234. };
  235. }
  236. IServiceRef NNeh::Wrap(const TServiceFunction& func) {
  237. struct TWrapper: public IService {
  238. inline TWrapper(const TServiceFunction& f)
  239. : F(f)
  240. {
  241. }
  242. void ServeRequest(const IRequestRef& request) override {
  243. F(request);
  244. }
  245. TServiceFunction F;
  246. };
  247. return new TWrapper(func);
  248. }
  249. IServicesRef NNeh::CreateLoop() {
  250. return new TServicesFace();
  251. }
  252. IServicesRef NNeh::CreateLoop(TCheck check) {
  253. return new TServicesFace(check);
  254. }