expected_value.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2021-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. #ifdef GFLAGS
  6. #include "db_stress_tool/expected_value.h"
  7. #include <atomic>
  8. namespace ROCKSDB_NAMESPACE {
  9. void ExpectedValue::Put(bool pending) {
  10. if (pending) {
  11. SetPendingWrite();
  12. } else {
  13. SetValueBase(NextValueBase());
  14. ClearDeleted();
  15. ClearPendingWrite();
  16. }
  17. }
  18. bool ExpectedValue::Delete(bool pending) {
  19. if (pending && !Exists()) {
  20. return false;
  21. }
  22. if (pending) {
  23. SetPendingDel();
  24. } else {
  25. SetDelCounter(NextDelCounter());
  26. SetDeleted();
  27. ClearPendingDel();
  28. }
  29. return true;
  30. }
  31. void ExpectedValue::SyncPut(uint32_t value_base) {
  32. assert(ExpectedValue::IsValueBaseValid(value_base));
  33. SetValueBase(value_base);
  34. ClearDeleted();
  35. ClearPendingWrite();
  36. // This is needed in case crash happens during a pending delete of the key
  37. // assocated with this expected value
  38. ClearPendingDel();
  39. }
  40. void ExpectedValue::SyncPendingPut() { Put(true /* pending */); }
  41. void ExpectedValue::SyncDelete() {
  42. Delete(false /* pending */);
  43. // This is needed in case crash happens during a pending write of the key
  44. // assocated with this expected value
  45. ClearPendingWrite();
  46. }
  47. uint32_t ExpectedValue::GetFinalValueBase() const {
  48. return PendingWrite() ? NextValueBase() : GetValueBase();
  49. }
  50. uint32_t ExpectedValue::GetFinalDelCounter() const {
  51. return PendingDelete() ? NextDelCounter() : GetDelCounter();
  52. }
  53. bool ExpectedValueHelper::MustHaveNotExisted(
  54. ExpectedValue pre_read_expected_value,
  55. ExpectedValue post_read_expected_value) {
  56. const bool pre_read_expected_deleted = pre_read_expected_value.IsDeleted();
  57. const uint32_t pre_read_expected_value_base =
  58. pre_read_expected_value.GetValueBase();
  59. const uint32_t post_read_expected_final_value_base =
  60. post_read_expected_value.GetFinalValueBase();
  61. const bool during_read_no_write_happened =
  62. (pre_read_expected_value_base == post_read_expected_final_value_base);
  63. return pre_read_expected_deleted && during_read_no_write_happened;
  64. }
  65. bool ExpectedValueHelper::MustHaveExisted(
  66. ExpectedValue pre_read_expected_value,
  67. ExpectedValue post_read_expected_value) {
  68. const bool pre_read_expected_not_deleted =
  69. !pre_read_expected_value.IsDeleted();
  70. const uint32_t pre_read_expected_del_counter =
  71. pre_read_expected_value.GetDelCounter();
  72. const uint32_t post_read_expected_final_del_counter =
  73. post_read_expected_value.GetFinalDelCounter();
  74. const bool during_read_no_delete_happened =
  75. (pre_read_expected_del_counter == post_read_expected_final_del_counter);
  76. return pre_read_expected_not_deleted && during_read_no_delete_happened;
  77. }
  78. bool ExpectedValueHelper::InExpectedValueBaseRange(
  79. uint32_t value_base, ExpectedValue pre_read_expected_value,
  80. ExpectedValue post_read_expected_value) {
  81. assert(ExpectedValue::IsValueBaseValid(value_base));
  82. const uint32_t pre_read_expected_value_base =
  83. pre_read_expected_value.GetValueBase();
  84. const uint32_t post_read_expected_final_value_base =
  85. post_read_expected_value.GetFinalValueBase();
  86. if (pre_read_expected_value_base <= post_read_expected_final_value_base) {
  87. const uint32_t lower_bound = pre_read_expected_value_base;
  88. const uint32_t upper_bound = post_read_expected_final_value_base;
  89. return lower_bound <= value_base && value_base <= upper_bound;
  90. } else {
  91. const uint32_t upper_bound_1 = post_read_expected_final_value_base;
  92. const uint32_t lower_bound_2 = pre_read_expected_value_base;
  93. const uint32_t upper_bound_2 = ExpectedValue::GetValueBaseMask();
  94. return (value_base <= upper_bound_1) ||
  95. (lower_bound_2 <= value_base && value_base <= upper_bound_2);
  96. }
  97. }
  98. } // namespace ROCKSDB_NAMESPACE
  99. #endif // GFLAGS