1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
|
#include "threaded_updater.h"
#include <library/cpp/tvmauth/client/misc/disk_cache.h>
#include <library/cpp/tvmauth/client/misc/utils.h>
#include <library/cpp/tvmauth/client/misc/retry_settings/v1/settings.pb.h>
#include <library/cpp/tvmauth/client/logger.h>
#include <library/cpp/json/json_reader.h>
#include <util/stream/str.h>
#include <util/string/builder.h>
#include <util/string/cast.h>
#include <util/system/thread.h>
namespace NTvmAuth::NTvmApi {
static TString CreatePublicKeysUrl(const TClientSettings& settings,
const NUtils::TProcInfo& procInfo) {
TStringStream s;
s << "/2/keys";
s << "?";
procInfo.AddToRequest(s);
s << "&get_retry_settings=yes";
if (settings.GetSelfTvmId() != 0) {
s << "&src=" << settings.GetSelfTvmId();
}
if (settings.IsUserTicketCheckingRequired()) {
s << "&env=" << static_cast<int>(settings.GetEnvForUserTickets());
}
return s.Str();
}
TAsyncUpdaterPtr TThreadedUpdater::Create(const TClientSettings& settings, TLoggerPtr logger) {
Y_ENSURE_EX(logger, TNonRetriableException() << "Logger is required");
THolder<TThreadedUpdater> p(new TThreadedUpdater(settings, std::move(logger)));
p->Init();
p->StartWorker();
return p.Release();
}
TThreadedUpdater::~TThreadedUpdater() {
ExpBackoff_.SetEnabled(false);
ExpBackoff_.Interrupt();
StopWorker(); // Required here to avoid using of deleted members
}
TClientStatus TThreadedUpdater::GetStatus() const {
const TClientStatus::ECode state = GetState();
return TClientStatus(state, GetLastError(state == TClientStatus::Ok || state == TClientStatus::IncompleteTicketsSet));
}
NRoles::TRolesPtr TThreadedUpdater::GetRoles() const {
Y_ENSURE_EX(RolesFetcher_,
TBrokenTvmClientSettings() << "Roles were not configured in settings");
return RolesFetcher_->GetCurrentRoles();
}
TClientStatus::ECode TThreadedUpdater::GetState() const {
const TInstant now = TInstant::Now();
if (Settings_.IsServiceTicketFetchingRequired()) {
if (AreServiceTicketsInvalid(now)) {
return TClientStatus::Error;
}
auto tickets = GetCachedServiceTickets();
if (!tickets) {
return TClientStatus::Error;
}
if (tickets->TicketsById.size() < Destinations_.size()) {
if (Settings_.IsIncompleteTicketsSetAnError) {
return TClientStatus::Error;
} else {
return TClientStatus::IncompleteTicketsSet;
}
}
}
if ((Settings_.IsServiceTicketCheckingRequired() || Settings_.IsUserTicketCheckingRequired()) && ArePublicKeysInvalid(now)) {
return TClientStatus::Error;
}
const TDuration sincePublicKeysUpdate = now - GetUpdateTimeOfPublicKeys();
const TDuration sinceServiceTicketsUpdate = now - GetUpdateTimeOfServiceTickets();
const TDuration sinceRolesUpdate = now - GetUpdateTimeOfRoles();
if (Settings_.IsServiceTicketFetchingRequired() && sinceServiceTicketsUpdate > ServiceTicketsDurations_.Expiring) {
return TClientStatus::Warning;
}
if ((Settings_.IsServiceTicketCheckingRequired() || Settings_.IsUserTicketCheckingRequired()) &&
sincePublicKeysUpdate > PublicKeysDurations_.Expiring)
{
return TClientStatus::Warning;
}
if (RolesFetcher_ && TRolesFetcher::ShouldWarn(RetrySettings_, sinceRolesUpdate)) {
return TClientStatus::Warning;
}
return TClientStatus::Ok;
}
TThreadedUpdater::TThreadedUpdater(const TClientSettings& settings, TLoggerPtr logger)
: TThreadedUpdaterBase(
TRetrySettings{}.WorkerAwakingPeriod,
std::move(logger),
settings.GetTvmHost(),
settings.GetTvmPort(),
settings.TvmSocketTimeout,
settings.TvmConnectTimeout)
, ExpBackoff_(RetrySettings_.BackoffSettings)
, Settings_(settings.CloneNormalized())
, ProcInfo_(NUtils::TProcInfo::Create(Settings_.GetLibVersionPrefix()))
, PublicKeysUrl_(CreatePublicKeysUrl(Settings_, ProcInfo_))
, DstAliases_(MakeAliasMap(Settings_))
, Headers_({{"Content-Type", "application/x-www-form-urlencoded"}})
, Random_(TInstant::Now().MicroSeconds())
{
if (Settings_.IsServiceTicketFetchingRequired()) {
SigningContext_ = TServiceContext::SigningFactory(Settings_.GetSelfSecret());
}
if (Settings_.IsServiceTicketFetchingRequired()) {
Destinations_ = {Settings_.GetDestinations().begin(), Settings_.GetDestinations().end()};
}
PublicKeysDurations_.RefreshPeriod = TDuration::Days(1);
ServiceTicketsDurations_.RefreshPeriod = TDuration::Hours(1);
if (Settings_.IsUserTicketCheckingRequired()) {
SetBbEnv(Settings_.GetEnvForUserTickets());
}
if (Settings_.IsRolesFetchingEnabled()) {
RolesFetcher_ = std::make_unique<TRolesFetcher>(
TRolesFetcherSettings{
Settings_.GetTiroleHost(),
Settings_.GetTirolePort(),
Settings_.GetDiskCacheDir(),
ProcInfo_,
Settings_.GetSelfTvmId(),
Settings_.GetIdmSystemSlug(),
},
Logger_);
}
if (Settings_.IsDiskCacheUsed()) {
TString path = Settings_.GetDiskCacheDir();
if (path.back() != '/') {
path.push_back('/');
}
if (Settings_.IsServiceTicketFetchingRequired()) {
ServiceTicketsFilepath_ = path;
ServiceTicketsFilepath_.append("service_tickets");
}
if (Settings_.IsServiceTicketCheckingRequired() || Settings_.IsUserTicketCheckingRequired()) {
PublicKeysFilepath_ = path;
PublicKeysFilepath_.append("public_keys");
}
RetrySettingsFilepath_ = path + "retry_settings";
} else {
LogInfo("Disk cache disabled. Please set disk cache directory in settings for best reliability");
}
}
void TThreadedUpdater::Init() {
ReadStateFromDisk();
ClearErrors();
ExpBackoff_.SetEnabled(false);
// First of all try to get tickets: there are a lot of reasons to fail this request.
// As far as disk cache usually disabled, client will not fetch keys before fail on every ctor call.
UpdateServiceTickets();
if (!AreServicesTicketsOk()) {
ThrowLastError();
}
UpdatePublicKeys();
if (!IsServiceContextOk() || !IsUserContextOk()) {
ThrowLastError();
}
UpdateRoles();
if (RolesFetcher_ && !RolesFetcher_->AreRolesOk()) {
ThrowLastError();
}
Inited_ = true;
ExpBackoff_.SetEnabled(true);
}
void TThreadedUpdater::UpdateServiceTickets() {
if (!Settings_.IsServiceTicketFetchingRequired()) {
return;
}
TInstant stut = GetUpdateTimeOfServiceTickets();
try {
if (IsTimeToUpdateServiceTickets(stut)) {
UpdateAllServiceTickets();
NeedFetchMissingServiceTickets_ = false;
} else if (NeedFetchMissingServiceTickets_ && GetCachedServiceTickets()->TicketsById.size() < Destinations_.size()) {
UpdateMissingServiceTickets(Destinations_);
NeedFetchMissingServiceTickets_ = false;
}
if (AreServicesTicketsOk()) {
ClearError(EScope::ServiceTickets);
}
} catch (const std::exception& e) {
ProcessError(EType::Retriable, EScope::ServiceTickets, e.what());
LogWarning(TStringBuilder() << "Failed to update service tickets: " << e.what());
if (TInstant::Now() - stut > ServiceTicketsDurations_.Expiring) {
LogError("Service tickets have not been refreshed for too long period");
}
}
}
void TThreadedUpdater::UpdateAllServiceTickets() {
THttpResult st = GetServiceTicketsFromHttp(Destinations_, RetrySettings_.DstsLimit);
auto oldCache = GetCachedServiceTickets();
if (oldCache) {
for (const auto& pair : oldCache->ErrorsById) {
st.TicketsWithErrors.Errors.insert(pair);
}
}
UpdateServiceTicketsCache(std::move(st.TicketsWithErrors), TInstant::Now());
if (ServiceTicketsFilepath_) {
DiskCacheServiceTickets_ = CreateJsonArray(st.Responses);
TDiskWriter w(ServiceTicketsFilepath_, Logger_.Get());
w.Write(PrepareTicketsForDisk(DiskCacheServiceTickets_, Settings_.GetSelfTvmId()));
}
}
TServiceTicketsPtr TThreadedUpdater::UpdateMissingServiceTickets(const TDstSet& required) {
TServiceTicketsPtr cache = GetCachedServiceTickets();
TClientSettings::TDstVector dsts = FindMissingDsts(cache, required);
if (dsts.empty()) {
return cache;
}
THttpResult st = GetServiceTicketsFromHttp(dsts, RetrySettings_.DstsLimit);
size_t gotTickets = st.TicketsWithErrors.Tickets.size();
for (const auto& pair : cache->TicketsById) {
st.TicketsWithErrors.Tickets.insert(pair);
}
for (const auto& pair : cache->ErrorsById) {
st.TicketsWithErrors.Errors.insert(pair);
}
for (const auto& pair : st.TicketsWithErrors.Tickets) {
st.TicketsWithErrors.Errors.erase(pair.first);
}
TServiceTicketsPtr c = UpdateServiceTicketsCachePartly(
std::move(st.TicketsWithErrors),
gotTickets);
if (!c) {
LogWarning("UpdateMissingServiceTickets: new cache is NULL. BUG?");
c = cache;
}
if (!ServiceTicketsFilepath_) {
return c;
}
DiskCacheServiceTickets_ = AppendToJsonArray(DiskCacheServiceTickets_, st.Responses);
TDiskWriter w(ServiceTicketsFilepath_, Logger_.Get());
w.Write(PrepareTicketsForDisk(DiskCacheServiceTickets_, Settings_.GetSelfTvmId()));
return c;
}
void TThreadedUpdater::UpdatePublicKeys() {
if (!Settings_.IsServiceTicketCheckingRequired() && !Settings_.IsUserTicketCheckingRequired()) {
return;
}
TInstant pkut = GetUpdateTimeOfPublicKeys();
if (!IsTimeToUpdatePublicKeys(pkut)) {
return;
}
try {
TString publicKeys = GetPublicKeysFromHttp();
UpdatePublicKeysCache(publicKeys, TInstant::Now());
if (PublicKeysFilepath_) {
TDiskWriter w(PublicKeysFilepath_, Logger_.Get());
w.Write(publicKeys);
}
if (IsServiceContextOk() && IsUserContextOk()) {
ClearError(EScope::PublicKeys);
}
} catch (const std::exception& e) {
ProcessError(EType::Retriable, EScope::PublicKeys, e.what());
LogWarning(TStringBuilder() << "Failed to update public keys: " << e.what());
if (TInstant::Now() - pkut > PublicKeysDurations_.Expiring) {
LogError("Public keys have not been refreshed for too long period");
}
}
}
void TThreadedUpdater::UpdateRoles() {
if (!RolesFetcher_) {
return;
}
TInstant rut = GetUpdateTimeOfRoles();
if (!TRolesFetcher::IsTimeToUpdate(RetrySettings_, TInstant::Now() - rut)) {
return;
}
struct TCloser {
TRolesFetcher* Fetcher;
~TCloser() {
Fetcher->ResetConnection();
}
} closer{RolesFetcher_.get()};
try {
TServiceTicketsPtr st = GetCachedServiceTickets();
Y_ENSURE(st, "No one service ticket in memory: how it possible?");
auto it = st->TicketsById.find(Settings_.GetTiroleTvmId());
Y_ENSURE(it != st->TicketsById.end(),
"Missing tvmid for tirole in cache: " << Settings_.GetTiroleTvmId());
RolesFetcher_->Update(
FetchWithRetries(
[&]() { return RolesFetcher_->FetchActualRoles(it->second); },
EScope::Roles));
SetUpdateTimeOfRoles(TInstant::Now());
if (RolesFetcher_->AreRolesOk()) {
ClearError(EScope::Roles);
}
} catch (const std::exception& e) {
ProcessError(EType::Retriable, EScope::Roles, e.what());
LogWarning(TStringBuilder() << "Failed to update roles: " << e.what());
if (TRolesFetcher::ShouldWarn(RetrySettings_, TInstant::Now() - rut)) {
LogError("Roles have not been refreshed for too long period");
}
}
}
TServiceTicketsPtr TThreadedUpdater::UpdateServiceTicketsCachePartly(
TAsyncUpdaterBase::TPairTicketsErrors&& tickets,
size_t got) {
size_t count = tickets.Tickets.size();
TServiceTicketsPtr c = MakeIntrusiveConst<TServiceTickets>(std::move(tickets.Tickets),
std::move(tickets.Errors),
DstAliases_);
SetServiceTickets(c);
LogInfo(TStringBuilder()
<< "Cache was partly updated with " << got
<< " service ticket(s). total: " << count);
return c;
}
void TThreadedUpdater::UpdateServiceTicketsCache(TPairTicketsErrors&& tickets, TInstant time) {
size_t count = tickets.Tickets.size();
SetServiceTickets(MakeIntrusiveConst<TServiceTickets>(std::move(tickets.Tickets),
std::move(tickets.Errors),
DstAliases_));
SetUpdateTimeOfServiceTickets(time);
if (count > 0) {
LogInfo(TStringBuilder() << "Cache was updated with " << count << " service ticket(s): " << time);
}
}
void TThreadedUpdater::UpdatePublicKeysCache(const TString& publicKeys, TInstant time) {
if (publicKeys.empty()) {
return;
}
if (Settings_.IsServiceTicketCheckingRequired()) {
SetServiceContext(MakeIntrusiveConst<TServiceContext>(
TServiceContext::CheckingFactory(Settings_.GetSelfTvmId(),
publicKeys)));
}
if (Settings_.IsUserTicketCheckingRequired()) {
SetUserContext(publicKeys);
}
SetUpdateTimeOfPublicKeys(time);
LogInfo(TStringBuilder() << "Cache was updated with public keys: " << time);
}
void TThreadedUpdater::ReadStateFromDisk() {
try {
TServiceTicketsFromDisk st = ReadServiceTicketsFromDisk();
UpdateServiceTicketsCache(std::move(st.TicketsWithErrors), st.BornDate);
DiskCacheServiceTickets_ = st.FileBody;
} catch (const std::exception& e) {
LogWarning(TStringBuilder() << "Failed to read service tickets from disk: " << e.what());
}
try {
std::pair<TString, TInstant> pk = ReadPublicKeysFromDisk();
UpdatePublicKeysCache(pk.first, pk.second);
} catch (const std::exception& e) {
LogWarning(TStringBuilder() << "Failed to read public keys from disk: " << e.what());
}
try {
TString rs = ReadRetrySettingsFromDisk();
UpdateRetrySettings(rs);
} catch (const std::exception& e) {
LogWarning(TStringBuilder() << "Failed to read retry settings from disk: " << e.what());
}
try {
if (RolesFetcher_) {
SetUpdateTimeOfRoles(RolesFetcher_->ReadFromDisk());
}
} catch (const std::exception& e) {
LogWarning(TStringBuilder() << "Failed to read roles from disk: " << e.what());
}
}
TThreadedUpdater::TServiceTicketsFromDisk TThreadedUpdater::ReadServiceTicketsFromDisk() const {
if (!ServiceTicketsFilepath_) {
return {};
}
TDiskReader r(ServiceTicketsFilepath_, Logger_.Get());
if (!r.Read()) {
return {};
}
std::pair<TStringBuf, TTvmId> data = ParseTicketsFromDisk(r.Data());
if (data.second != Settings_.GetSelfTvmId()) {
TStringStream s;
s << "Disk cache is for another tvmId (" << data.second << "). ";
s << "Self=" << Settings_.GetSelfTvmId();
LogWarning(s.Str());
return {};
}
TPairTicketsErrors res;
ParseTicketsFromResponse(data.first, Destinations_, res);
if (IsInvalid(TServiceTickets::GetInvalidationTime(res.Tickets), TInstant::Now())) {
LogWarning("Disk cache (service tickets) is too old");
return {};
}
LogInfo(TStringBuilder() << "Got " << res.Tickets.size() << " service ticket(s) from disk");
return {std::move(res), r.Time(), TString(data.first)};
}
std::pair<TString, TInstant> TThreadedUpdater::ReadPublicKeysFromDisk() const {
if (!PublicKeysFilepath_) {
return {};
}
TDiskReader r(PublicKeysFilepath_, Logger_.Get());
if (!r.Read()) {
return {};
}
if (TInstant::Now() - r.Time() > PublicKeysDurations_.Invalid) {
LogWarning("Disk cache (public keys) is too old");
return {};
}
return {r.Data(), r.Time()};
}
TString TThreadedUpdater::ReadRetrySettingsFromDisk() const {
if (!RetrySettingsFilepath_) {
return {};
}
TDiskReader r(RetrySettingsFilepath_, Logger_.Get());
if (!r.Read()) {
return {};
}
return r.Data();
}
template <class Dsts>
TThreadedUpdater::THttpResult TThreadedUpdater::GetServiceTicketsFromHttp(const Dsts& dsts, const size_t dstLimit) const {
Y_ENSURE(SigningContext_, "Internal error");
TClientSettings::TDstVector part;
part.reserve(dstLimit);
THttpResult res;
res.TicketsWithErrors.Tickets.reserve(dsts.size());
res.Responses.reserve(dsts.size() / dstLimit + 1);
for (auto it = dsts.begin(); it != dsts.end();) {
part.clear();
for (size_t count = 0; it != dsts.end() && count < dstLimit; ++count, ++it) {
part.push_back(*it);
}
TString response =
FetchWithRetries(
[this, &part]() {
// create request here to keep 'ts' actual
return FetchServiceTicketsFromHttp(PrepareRequestForServiceTickets(
Settings_.GetSelfTvmId(),
*SigningContext_,
part,
ProcInfo_));
},
EScope::ServiceTickets)
.Response;
ParseTicketsFromResponse(response, part, res.TicketsWithErrors);
LogDebug(TStringBuilder()
<< "Response with service tickets for " << part.size()
<< " destination(s) was successfully fetched from " << TvmUrl_);
res.Responses.push_back(response);
}
LogDebug(TStringBuilder()
<< "Got responses with service tickets with " << res.Responses.size() << " pages for "
<< dsts.size() << " destination(s)");
for (const auto& p : res.TicketsWithErrors.Errors) {
LogError(TStringBuilder()
<< "Failed to get service ticket for dst=" << p.first << ": " << p.second);
}
return res;
}
TString TThreadedUpdater::GetPublicKeysFromHttp() const {
TString publicKeys =
FetchWithRetries(
[this]() { return FetchPublicKeysFromHttp(); },
EScope::PublicKeys)
.Response;
LogDebug("Public keys were successfully fetched from " + TvmUrl_);
return publicKeys;
}
NUtils::TFetchResult TThreadedUpdater::FetchServiceTicketsFromHttp(const TString& body) const {
TStringStream s;
THttpHeaders outHeaders;
TKeepAliveHttpClient::THttpCode code = GetClient().DoPost("/2/ticket", body, &s, Headers_, &outHeaders);
const THttpInputHeader* settings = outHeaders.FindHeader("X-Ya-Retry-Settings");
return {code, {}, "/2/ticket", s.Str(), settings ? settings->Value() : ""};
}
NUtils::TFetchResult TThreadedUpdater::FetchPublicKeysFromHttp() const {
TStringStream s;
THttpHeaders outHeaders;
TKeepAliveHttpClient::THttpCode code = GetClient().DoGet(PublicKeysUrl_, &s, {}, &outHeaders);
const THttpInputHeader* settings = outHeaders.FindHeader("X-Ya-Retry-Settings");
return {code, {}, "/2/keys", s.Str(), settings ? settings->Value() : ""};
}
bool TThreadedUpdater::UpdateRetrySettings(const TString& header) const {
if (header.empty()) {
// Probably it is some kind of test?
return false;
}
try {
TString raw = NUtils::Base64url2bin(header);
Y_ENSURE(raw, "Invalid base64url in settings");
retry_settings::v1::Settings proto;
Y_ENSURE(proto.ParseFromString(raw), "Invalid proto");
// This ugly hack helps to process these settings in any case
TThreadedUpdater& this_ = *const_cast<TThreadedUpdater*>(this);
TRetrySettings& res = this_.RetrySettings_;
TStringStream diff;
auto update = [&diff](auto& l, const auto& r, TStringBuf desc) {
if (l != r) {
diff << desc << ":" << l << "->" << r << ";";
l = r;
}
};
if (proto.has_exponential_backoff_min_sec()) {
update(res.BackoffSettings.Min,
TDuration::Seconds(proto.exponential_backoff_min_sec()),
"exponential_backoff_min");
}
if (proto.has_exponential_backoff_max_sec()) {
update(res.BackoffSettings.Max,
TDuration::Seconds(proto.exponential_backoff_max_sec()),
"exponential_backoff_max");
}
if (proto.has_exponential_backoff_factor()) {
update(res.BackoffSettings.Factor,
proto.exponential_backoff_factor(),
"exponential_backoff_factor");
}
if (proto.has_exponential_backoff_jitter()) {
update(res.BackoffSettings.Jitter,
proto.exponential_backoff_jitter(),
"exponential_backoff_jitter");
}
this_.ExpBackoff_.UpdateSettings(res.BackoffSettings);
if (proto.has_max_random_sleep_default()) {
update(res.MaxRandomSleepDefault,
TDuration::MilliSeconds(proto.max_random_sleep_default()),
"max_random_sleep_default");
}
if (proto.has_max_random_sleep_when_ok()) {
update(res.MaxRandomSleepWhenOk,
TDuration::MilliSeconds(proto.max_random_sleep_when_ok()),
"max_random_sleep_when_ok");
}
if (proto.has_retries_on_start()) {
Y_ENSURE(proto.retries_on_start(), "retries_on_start==0");
update(res.RetriesOnStart,
proto.retries_on_start(),
"retries_on_start");
}
if (proto.has_retries_in_background()) {
Y_ENSURE(proto.retries_in_background(), "retries_in_background==0");
update(res.RetriesInBackground,
proto.retries_in_background(),
"retries_in_background");
}
if (proto.has_worker_awaking_period_sec()) {
update(res.WorkerAwakingPeriod,
TDuration::Seconds(proto.worker_awaking_period_sec()),
"worker_awaking_period");
this_.WorkerAwakingPeriod_ = res.WorkerAwakingPeriod;
}
if (proto.has_dsts_limit()) {
Y_ENSURE(proto.dsts_limit(), "dsts_limit==0");
update(res.DstsLimit,
proto.dsts_limit(),
"dsts_limit");
}
if (proto.has_roles_update_period_sec()) {
Y_ENSURE(proto.roles_update_period_sec(), "roles_update_period==0");
update(res.RolesUpdatePeriod,
TDuration::Seconds(proto.roles_update_period_sec()),
"roles_update_period_sec");
}
if (proto.has_roles_warn_period_sec()) {
Y_ENSURE(proto.roles_warn_period_sec(), "roles_warn_period_sec==0");
update(res.RolesWarnPeriod,
TDuration::Seconds(proto.roles_warn_period_sec()),
"roles_warn_period_sec");
}
if (diff.empty()) {
return false;
}
LogDebug("Retry settings were updated: " + diff.Str());
return true;
} catch (const std::exception& e) {
LogWarning(TStringBuilder()
<< "Failed to update retry settings from server, header '"
<< header << "': "
<< e.what());
}
return false;
}
template <typename Func>
NUtils::TFetchResult TThreadedUpdater::FetchWithRetries(Func func, EScope scope) const {
const ui32 tries = Inited_ ? RetrySettings_.RetriesInBackground
: RetrySettings_.RetriesOnStart;
for (size_t idx = 1;; ++idx) {
RandomSleep();
try {
NUtils::TFetchResult result = func();
if (UpdateRetrySettings(result.RetrySettings) && RetrySettingsFilepath_) {
TDiskWriter w(RetrySettingsFilepath_, Logger_.Get());
w.Write(result.RetrySettings);
}
if (400 <= result.Code && result.Code <= 499) {
throw TNonRetriableException() << ProcessHttpError(scope, result.Path, result.Code, result.Response);
}
if (result.Code < 200 || result.Code >= 399) {
throw yexception() << ProcessHttpError(scope, result.Path, result.Code, result.Response);
}
ExpBackoff_.Decrease();
return result;
} catch (const TNonRetriableException& e) {
LogWarning(TStringBuilder() << "Failed to get " << scope << ": " << e.what());
ExpBackoff_.Increase();
throw;
} catch (const std::exception& e) {
LogWarning(TStringBuilder() << "Failed to get " << scope << ": " << e.what());
ExpBackoff_.Increase();
if (idx >= tries) {
throw;
}
}
}
throw yexception() << "unreachable";
}
void TThreadedUpdater::RandomSleep() const {
const TDuration maxSleep = TClientStatus::ECode::Ok == GetState()
? RetrySettings_.MaxRandomSleepWhenOk
: RetrySettings_.MaxRandomSleepDefault;
if (maxSleep) {
ui32 toSleep = Random_.GenRand() % maxSleep.MilliSeconds();
ExpBackoff_.Sleep(TDuration::MilliSeconds(toSleep));
}
}
TString TThreadedUpdater::PrepareRequestForServiceTickets(TTvmId src,
const TServiceContext& ctx,
const TClientSettings::TDstVector& dsts,
const NUtils::TProcInfo& procInfo,
time_t now) {
TStringStream s;
const TString ts = IntToString<10>(now);
TStringStream dst;
dst.Reserve(10 * dsts.size());
for (const TClientSettings::TDst& d : dsts) {
if (dst.Str()) {
dst << ',';
}
dst << d.Id;
}
s << "grant_type=client_credentials";
s << "&src=" << src;
s << "&dst=" << dst.Str();
s << "&ts=" << ts;
s << "&sign=" << ctx.SignCgiParamsForTvm(ts, dst.Str());
s << "&get_retry_settings=yes";
s << "&";
procInfo.AddToRequest(s);
return s.Str();
}
template <class Dsts>
void TThreadedUpdater::ParseTicketsFromResponse(TStringBuf resp,
const Dsts& dsts,
TPairTicketsErrors& out) const {
NJson::TJsonValue doc;
Y_ENSURE(NJson::ReadJsonTree(resp, &doc), "Invalid json from tvm-api: " << resp);
const NJson::TJsonValue* currentResp = doc.IsMap() ? &doc : nullptr;
auto find = [¤tResp, &doc](TTvmId id, NJson::TJsonValue& obj) -> bool {
const TString idStr = IntToString<10>(id);
if (currentResp && currentResp->GetValue(idStr, &obj)) {
return true;
}
for (const NJson::TJsonValue& val : doc.GetArray()) {
currentResp = &val;
if (currentResp->GetValue(idStr, &obj)) {
return true;
}
}
return false;
};
for (const TClientSettings::TDst& d : dsts) {
NJson::TJsonValue obj;
NJson::TJsonValue val;
if (!find(d.Id, obj) || !obj.GetValue("ticket", &val)) {
TString err;
if (obj.GetValue("error", &val)) {
err = val.GetString();
} else {
err = "Missing tvm_id in response, should never happend: " + IntToString<10>(d.Id);
}
TStringStream s;
s << "Failed to get ServiceTicket for " << d.Id << ": " << err;
ProcessError(EType::NonRetriable, EScope::ServiceTickets, s.Str());
out.Errors.insert({d.Id, std::move(err)});
continue;
}
out.Tickets.insert({d.Id, val.GetString()});
}
}
static const char DELIMETER = '\t';
TString TThreadedUpdater::PrepareTicketsForDisk(TStringBuf tvmResponse, TTvmId selfId) {
TStringStream s;
s << tvmResponse << DELIMETER << selfId;
return s.Str();
}
std::pair<TStringBuf, TTvmId> TThreadedUpdater::ParseTicketsFromDisk(TStringBuf data) {
TStringBuf tvmId = data.RNextTok(DELIMETER);
return {data, IntFromString<TTvmId, 10>(tvmId)};
}
const TDstSet& TThreadedUpdater::GetDsts() const {
return Destinations_;
}
void TThreadedUpdater::AddDstToSettings(const TClientSettings::TDst& dst) {
Destinations_.insert(dst);
}
bool TThreadedUpdater::IsTimeToUpdateServiceTickets(TInstant lastUpdate) const {
return TInstant::Now() - lastUpdate > ServiceTicketsDurations_.RefreshPeriod;
}
bool TThreadedUpdater::IsTimeToUpdatePublicKeys(TInstant lastUpdate) const {
return TInstant::Now() - lastUpdate > PublicKeysDurations_.RefreshPeriod;
}
bool TThreadedUpdater::AreServicesTicketsOk() const {
if (!Settings_.IsServiceTicketFetchingRequired()) {
return true;
}
auto c = GetCachedServiceTickets();
return c && (!Settings_.IsIncompleteTicketsSetAnError || c->TicketsById.size() == Destinations_.size());
}
bool TThreadedUpdater::IsServiceContextOk() const {
if (!Settings_.IsServiceTicketCheckingRequired()) {
return true;
}
return bool(GetCachedServiceContext());
}
bool TThreadedUpdater::IsUserContextOk() const {
if (!Settings_.IsUserTicketCheckingRequired()) {
return true;
}
return bool(GetCachedUserContext());
}
void TThreadedUpdater::Worker() {
UpdateServiceTickets();
UpdatePublicKeys();
UpdateRoles();
}
TServiceTickets::TMapAliasId TThreadedUpdater::MakeAliasMap(const TClientSettings& settings) {
TServiceTickets::TMapAliasId res;
if (settings.HasDstAliases()) {
for (const auto& pair : settings.GetDstAliases()) {
res.insert({pair.first, pair.second.Id});
}
}
return res;
}
TClientSettings::TDstVector TThreadedUpdater::FindMissingDsts(TServiceTicketsPtr available, const TDstSet& required) {
Y_ENSURE(available);
TDstSet set;
// available->TicketsById is not sorted
for (const auto& pair : available->TicketsById) {
set.insert(pair.first);
}
return FindMissingDsts(set, required);
}
TClientSettings::TDstVector TThreadedUpdater::FindMissingDsts(const TDstSet& available, const TDstSet& required) {
TClientSettings::TDstVector res;
std::set_difference(required.begin(), required.end(),
available.begin(), available.end(),
std::inserter(res, res.begin()));
return res;
}
TString TThreadedUpdater::CreateJsonArray(const TSmallVec<TString>& responses) {
if (responses.empty()) {
return "[]";
}
size_t size = 0;
for (const TString& r : responses) {
size += r.size() + 1;
}
TString res;
res.reserve(size + 2);
res.push_back('[');
for (const TString& r : responses) {
res.append(r).push_back(',');
}
res.back() = ']';
return res;
}
TString TThreadedUpdater::AppendToJsonArray(const TString& json, const TSmallVec<TString>& responses) {
Y_ENSURE(json, "previous body required");
size_t size = 0;
for (const TString& r : responses) {
size += r.size() + 1;
}
TString res;
res.reserve(size + 2 + json.size());
res.push_back('[');
if (json.StartsWith('[')) {
Y_ENSURE(json.EndsWith(']'), "array is broken:" << json);
res.append(TStringBuf(json).Chop(1).Skip(1));
} else {
res.append(json);
}
res.push_back(',');
for (const TString& r : responses) {
res.append(r).push_back(',');
}
res.back() = ']';
return res;
}
}
|