transaction.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "abortable_registry.h"
  3. #include <yt/cpp/mapreduce/http/requests.h>
  4. #include <yt/cpp/mapreduce/http/retry_request.h>
  5. #include <util/datetime/base.h>
  6. #include <util/generic/maybe.h>
  7. #include <util/generic/ptr.h>
  8. #include <util/system/thread.h>
  9. namespace NYT {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. class TPingableTransaction
  12. {
  13. public:
  14. //
  15. // Start a new transaction.
  16. TPingableTransaction(
  17. const IClientRetryPolicyPtr& retryPolicy,
  18. const TClientContext& context,
  19. const TTransactionId& parentId,
  20. ITransactionPingerPtr transactionPinger,
  21. const TStartTransactionOptions& options);
  22. //
  23. // Attach to an existing transaction.
  24. TPingableTransaction(
  25. const IClientRetryPolicyPtr& retryPolicy,
  26. const TClientContext& context,
  27. const TTransactionId& transactionId,
  28. ITransactionPingerPtr transactionPinger,
  29. const TAttachTransactionOptions& options);
  30. ~TPingableTransaction();
  31. const TTransactionId GetId() const;
  32. const std::pair<TDuration, TDuration> GetPingInterval() const;
  33. const TClientContext GetContext() const;
  34. void Commit();
  35. void Abort();
  36. void Detach();
  37. private:
  38. enum class EStopAction
  39. {
  40. Detach,
  41. Abort,
  42. Commit,
  43. };
  44. private:
  45. IClientRetryPolicyPtr ClientRetryPolicy_;
  46. TClientContext Context_;
  47. TTransactionId TransactionId_;
  48. TDuration MinPingInterval_;
  49. TDuration MaxPingInterval_;
  50. // We have to own an IntrusivePtr to registry to prevent use-after-free.
  51. ::TIntrusivePtr<NDetail::TAbortableRegistry> AbortableRegistry_;
  52. bool AbortOnTermination_;
  53. bool AutoPingable_;
  54. bool Finalized_ = false;
  55. ITransactionPingerPtr Pinger_;
  56. private:
  57. void Init(
  58. const TClientContext& context,
  59. const TTransactionId& transactionId,
  60. TDuration timeout);
  61. void Stop(EStopAction action);
  62. };
  63. ////////////////////////////////////////////////////////////////////////////////
  64. TYPath Snapshot(
  65. const IClientRetryPolicyPtr& clientRetryPolicy,
  66. const TClientContext& context,
  67. const TTransactionId& transactionId,
  68. const TYPath& path);
  69. ////////////////////////////////////////////////////////////////////////////////
  70. } // namespace NYT