stat.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "stat.h"
  2. #include <util/generic/hash.h>
  3. #include <util/generic/singleton.h>
  4. #include <util/system/spinlock.h>
  5. #include <util/system/tls.h>
  6. using namespace NNeh;
  7. volatile TAtomic NNeh::TServiceStat::MaxContinuousErrors_ = 0; //by default disabled
  8. volatile TAtomic NNeh::TServiceStat::ReSendValidatorPeriod_ = 100;
  9. NNeh::TServiceStat::EStatus NNeh::TServiceStat::GetStatus() {
  10. if (!AtomicGet(MaxContinuousErrors_) || AtomicGet(LastContinuousErrors_) < AtomicGet(MaxContinuousErrors_)) {
  11. return Ok;
  12. }
  13. if (RequestsInProcess_.Val() != 0)
  14. return Fail;
  15. if (AtomicIncrement(SendValidatorCounter_) != AtomicGet(ReSendValidatorPeriod_)) {
  16. return Fail;
  17. }
  18. //time for refresh service status (send validation request)
  19. AtomicSet(SendValidatorCounter_, 0);
  20. return ReTry;
  21. }
  22. void NNeh::TServiceStat::DbgOut(IOutputStream& out) const {
  23. out << "----------------------------------------------------" << '\n';;
  24. out << "RequestsInProcess: " << RequestsInProcess_.Val() << '\n';
  25. out << "LastContinuousErrors: " << AtomicGet(LastContinuousErrors_) << '\n';
  26. out << "SendValidatorCounter: " << AtomicGet(SendValidatorCounter_) << '\n';
  27. out << "ReSendValidatorPeriod: " << AtomicGet(ReSendValidatorPeriod_) << Endl;
  28. }
  29. void NNeh::TServiceStat::OnBegin() {
  30. RequestsInProcess_.Inc();
  31. }
  32. void NNeh::TServiceStat::OnSuccess() {
  33. RequestsInProcess_.Dec();
  34. AtomicSet(LastContinuousErrors_, 0);
  35. }
  36. void NNeh::TServiceStat::OnCancel() {
  37. RequestsInProcess_.Dec();
  38. }
  39. void NNeh::TServiceStat::OnFail() {
  40. RequestsInProcess_.Dec();
  41. if (AtomicIncrement(LastContinuousErrors_) == AtomicGet(MaxContinuousErrors_)) {
  42. AtomicSet(SendValidatorCounter_, 0);
  43. }
  44. }
  45. namespace {
  46. class TGlobalServicesStat {
  47. public:
  48. inline TServiceStatRef ServiceStat(const TStringBuf addr) noexcept {
  49. const auto guard = Guard(Lock_);
  50. TServiceStatRef& ss = SS_[addr];
  51. if (!ss) {
  52. TServiceStatRef tmp(new TServiceStat());
  53. ss.Swap(tmp);
  54. }
  55. return ss;
  56. }
  57. protected:
  58. TAdaptiveLock Lock_;
  59. THashMap<TString, TServiceStatRef> SS_;
  60. };
  61. class TServicesStat {
  62. public:
  63. inline TServiceStatRef ServiceStat(const TStringBuf addr) noexcept {
  64. TServiceStatRef& ss = SS_[addr];
  65. if (!ss) {
  66. TServiceStatRef tmp(Singleton<TGlobalServicesStat>()->ServiceStat(addr));
  67. ss.Swap(tmp);
  68. }
  69. return ss;
  70. }
  71. protected:
  72. THashMap<TString, TServiceStatRef> SS_;
  73. };
  74. inline TServicesStat* ThrServiceStat() {
  75. Y_POD_STATIC_THREAD(TServicesStat*)
  76. ss;
  77. if (!ss) {
  78. Y_STATIC_THREAD(TServicesStat)
  79. tss;
  80. ss = &(TServicesStat&)tss;
  81. }
  82. return ss;
  83. }
  84. }
  85. TServiceStatRef NNeh::GetServiceStat(const TStringBuf addr) {
  86. return ThrServiceStat()->ServiceStat(addr);
  87. }