transaction.h 2.3 KB

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