transaction_util.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <string>
  7. #include <unordered_map>
  8. #include "db/dbformat.h"
  9. #include "db/read_callback.h"
  10. #include "rocksdb/db.h"
  11. #include "rocksdb/slice.h"
  12. #include "rocksdb/status.h"
  13. #include "rocksdb/types.h"
  14. #include "utilities/transactions/lock/lock_tracker.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. class DBImpl;
  17. struct SuperVersion;
  18. class WriteBatchWithIndex;
  19. class TransactionUtil {
  20. public:
  21. // Verifies there have been no commits to this key in the db since this
  22. // sequence number. If user-defined timestamp is enabled, then also check
  23. // no commits to this key in the db since the given ts.
  24. //
  25. // If cache_only is true, then this function will not attempt to read any
  26. // SST files. This will make it more likely this function will
  27. // return an error if it is unable to determine if there are any conflicts.
  28. //
  29. // See comment of CheckKey() for explanation of `snap_seq`, `ts`,
  30. // `snap_checker` and `min_uncommitted`.
  31. //
  32. // Returns OK on success, BUSY if there is a conflicting write, or other error
  33. // status for any unexpected errors.
  34. static Status CheckKeyForConflicts(
  35. DBImpl* db_impl, ColumnFamilyHandle* column_family,
  36. const std::string& key, SequenceNumber snap_seq,
  37. const std::string* const ts, bool cache_only,
  38. ReadCallback* snap_checker = nullptr,
  39. SequenceNumber min_uncommitted = kMaxSequenceNumber,
  40. bool enable_udt_validation = true);
  41. // For each key,SequenceNumber pair tracked by the LockTracker, this function
  42. // will verify there have been no writes to the key in the db since that
  43. // sequence number.
  44. //
  45. // Returns OK on success, BUSY if there is a conflicting write, or other error
  46. // status for any unexpected errors.
  47. //
  48. // REQUIRED:
  49. // This function should only be called on the write thread or if the
  50. // mutex is held.
  51. // tracker must support point lock.
  52. static Status CheckKeysForConflicts(DBImpl* db_impl,
  53. const LockTracker& tracker,
  54. bool cache_only);
  55. private:
  56. // If `snap_checker` == nullptr, writes are always commited in sequence number
  57. // order. All sequence number <= `snap_seq` will not conflict with any
  58. // write, and all keys > `snap_seq` of `key` will trigger conflict.
  59. // If `snap_checker` != nullptr, writes may not commit in sequence number
  60. // order. In this case `min_uncommitted` is a lower bound.
  61. // seq < `min_uncommitted`: no conflict
  62. // seq > `snap_seq`: applicable to conflict
  63. // `min_uncommitted` <= seq <= `snap_seq`: call `snap_checker` to determine.
  64. //
  65. // If user-defined timestamp is enabled and `enable_udt_validation` is set to
  66. // true, a write conflict is detected if an operation for `key` with timestamp
  67. // greater than `ts` exists.
  68. static Status CheckKey(DBImpl* db_impl, SuperVersion* sv,
  69. SequenceNumber earliest_seq, SequenceNumber snap_seq,
  70. const std::string& key, const std::string* const ts,
  71. bool cache_only, ReadCallback* snap_checker = nullptr,
  72. SequenceNumber min_uncommitted = kMaxSequenceNumber,
  73. bool enable_udt_validation = true);
  74. };
  75. } // namespace ROCKSDB_NAMESPACE