Skip to content

Inject spurious wakeup and sleep before acquiring db mutex to expose race condition - #10291

Closed
hx235 wants to merge 5 commits into
meta:mainfrom
hx235:inject_sleep_or_yield_in_stress_test
Closed

hx235 wants to merge 5 commits into
meta:mainfrom
hx235:inject_sleep_or_yield_in_stress_test

Conversation

@hx235

@hx235 hx235 commented Jun 30, 2022 •

Copy link
Copy Markdown
Contributor

Context/Summary:
Previous experience with bugs and flaky tests taught us there exist features in RocksDB vulnerable to race condition caused by acquiring db mutex at a particular timing. This PR aggressively exposes those vulnerable features by injecting spurious wakeup and sleep(or alternatively context switch) to cause acquiring db mutex at various timing in order to expose such race condition

Testing:

  • COERCE_CONTEXT_SWITCH=1 make -j56 check / make -j56 db_stress should reveal
    • flaky tests caused by db mutex related race condition
      • Reverted Fix TSAN data race in EventListenerTest.MultiCF #9528
      • A/B testing on COMPILE_WITH_TSAN=1 make -j56 listener_test w/ and w/o COERCE_CONTEXT_SWITCH=1 followed by ./listener_test --gtest_filter=EventListenerTest.MultiCF --gtest_repeat=10
      • COERCE_CONTEXT_SWITCH=1 can cause expected test failure (i.e, expose target TSAN data race error) within 10 run while the other couldn't.
      • This proves our injection can expose flaky tests caused by db mutex related race condition faster.
    • known or new race-condition-type of internal bug by continuously running this PR
  • Performance
    • High ops-threads time: COERCE_CONTEXT_SWITCH=1 regressed by 4 times slower (2:01.16 vs 0:22.10 elapsed ). This PR will be run as a separate CI job so this regression won't affect any existing job.
TEST_TMPDIR=$db /usr/bin/time ./db_stress \
--ops_per_thread=100000 --expected_values_dir=$exp --clear_column_family_one_in=0 \
--write_buffer_size=524288 —target_file_size_base=524288 —ingest_external_file_one_in=100 —compact_files_one_in=1000 —compact_range_one_in=1000
  • Start-up time: COERCE_CONTEXT_SWITCH=1 didn't regress by 25% (0:01.51 vs 0:01.29 elapsed)
TEST_TMPDIR=$db ./db_stress -ops_per_thread=100000000 -expected_values_dir=$exp --clear_column_family_one_in=0 & sleep 120; pkill -9 db_stress

TEST_TMPDIR=$db /usr/bin/time ./db_stress \
--ops_per_thread=1 -reopen=0 --expected_values_dir=$exp --clear_column_family_one_in=0 --destroy_db_initially=0

@hx235 hx235 added the WIP Work in progress label Jun 30, 2022
Comment thread Makefile Resolved
Comment thread db_stress_tool/db_stress_test_base.cc Resolved
@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 0ef68c8 to 0e05d70 Compare September 2, 2022 03:13
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235 hx235 changed the title [Draft] Inject random sleep and context switch to find bad race condition execution sequence Inject sleep, context switch and spurious wakeup before db mutex acquire to expose data race Sep 2, 2022
@hx235 hx235 changed the title Inject sleep, context switch and spurious wakeup before db mutex acquire to expose data race Inject sleep, context switch and spurious wakeup before acquiring db mutex to expose data race Sep 2, 2022
@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 0e05d70 to 9d7ca93 Compare September 2, 2022 03:20
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235 hx235 changed the title Inject sleep, context switch and spurious wakeup before acquiring db mutex to expose data race Inject sleep, context switch and spurious wakeup before acquiring db mutex to expose race condition Sep 2, 2022
@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 9d7ca93 to aeae055 Compare September 2, 2022 03:33
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from aeae055 to ad68317 Compare September 3, 2022 00:42
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

1 similar comment
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from ad68317 to 0386c70 Compare September 3, 2022 02:59
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 9f8be2e to 0386c70 Compare September 3, 2022 04:44
@hx235

hx235 commented Sep 3, 2022 •

Copy link
Copy Markdown
Contributor Author

Update:

  • Fixed tsan data race on the shared instance variable Random in InstrumentedMutex
  • If we inject spurious wakeup in InstrumentedMutex::Unlock(), there will be a read-destroy race on bg_cv_ as below. This is because after the Port::Mutex::Unlock() but before the injected bg_cv_.signalAll(), db destruction can acquire the lock and perform cleanup legitimately. Therefore I decided to remove bg_cv_.signalAll() in InstrumentedMutex::Unlock(). Without spurious wakeup, sleep/context switch does not seem to make a difference (based on make check), therefore I further removed other injection in InstrumentedMutex::Unlock()
stderr has error message:
***  Write of size 8 at 0x7b8c00769ab0 by thread T52 (mutexes: write M822047616802873896):***
stderr has error message:
***    #0 pthread_cond_destroy /home/engshare/third-party2/gcc/9.x/src/gcc-10.x/libsanitizer/tsan/tsan_interceptors.cc:1171 (libtsan.so.0+0x598e5)***
stderr has error message:
***    #1 rocksdb::port::CondVar::~CondVar() port/port_posix.cc:116 (db_stress+0x9e6420)***
stderr has error message:
***    #2 rocksdb::InstrumentedCondVar::~InstrumentedCondVar() monitoring/instrumented_mutex.h:114 (db_stress+0x5e028e)***
stderr has error message:
***    #3 rocksdb::DBImpl::~DBImpl() db/db_impl/db_impl.cc:740 (db_stress+0x5e028e)***
stderr has error message:
***    #4 rocksdb::DBImpl::~DBImpl() db/db_impl/db_impl.cc:758 (db_stress+0x5e13ad)***
stderr has error message:
***    #5 rocksdb::StressTest::TestBackupRestore(rocksdb::ThreadState*, std::vector<int, std::allocator<int> > const&, std::vector<long, std::allocator<long> > const&) db_stress_tool/db_stress_test_base.cc:1640 (db_stress+0x52972b)***
stderr has error message:
***    #6 rocksdb::StressTest::OperateDb(rocksdb::ThreadState*) db_stress_tool/db_stress_test_base.cc:843 (db_stress+0x53a913)***
stderr has error message:
***    #7 rocksdb::ThreadBody(void*) db_stress_tool/db_stress_driver.cc:33 (db_stress+0x505102)***
stderr has error message:
***    #8 StartThreadWrapper env/env_posix.cc:461 (db_stress+0x8b332f)***
stderr has error message:
***  Previous read of size 8 at 0x7b8c00769ab0 by thread T7:***
stderr has error message:
***    #0 pthread_cond_broadcast /home/engshare/third-party2/gcc/9.x/src/gcc-10.x/libsanitizer/tsan/tsan_interceptors.cc:1164 (libtsan.so.0+0x54862)***
stderr has error message:
***    #1 rocksdb::port::CondVar::SignalAll() port/port_posix.cc:154 (db_stress+0x9e67a1)***
stderr has error message:
***    #2 rocksdb::InstrumentedCondVar::SignalAll() monitoring/instrumented_mutex.h:131 (db_stress+0x95763c)***
stderr has error message:
***    #3 rocksdb::InstrumentedMutex::Unlock() monitoring/instrumented_mutex.cc:76 (db_stress+0x95763c)***
stderr has error message:
***    #4 rocksdb::InstrumentedMutexLock::~InstrumentedMutexLock() monitoring/instrumented_mutex.h:89 (db_stress+0x63fc3c)***
stderr has error message:
***    #5 rocksdb::DBImpl::BackgroundCallCompaction(rocksdb::DBImpl::PrepickedCompaction*, rocksdb::Env::Priority) db/db_impl/db_impl_compaction_flush.cc:2935 (db_stress+0x63fc3c)***
stderr has error message:
***    #6 rocksdb::DBImpl::BGWorkCompaction(void*) db/db_impl/db_impl_compaction_flush.cc:2693 (db_stress+0x6404f2)***
stderr has error message:
***    #7 void std::__invoke_impl<void, void (*&)(void*), void*&>(std::__invoke_other, void (*&)(void*), void*&) /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/bits/invoke.h:60 (db_stress+0xb47468)***
stderr has error message:
***    #8 std::__invoke_result<void (*&)(void*), void*&>::type std::__invoke<void (*&)(void*), void*&>(void (*&)(void*), void*&) /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/bits/invoke.h:95 (db_stress+0xb47468)***
stderr has error message:
***    #9 void std::_Bind<void (*(void*))(void*)>::__call<void, , 0ul>(std::tuple<>&&, std::_Index_tuple<0ul>) /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/functional:400 (db_stress+0xb47468)***
stderr has error message:
***    #10 void std::_Bind<void (*(void*))(void*)>::operator()<, void>() /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/functional:482 (db_stress+0xb47468)***
stderr has error message:
***    #11 std::_Function_handler<void (), std::_Bind<void (*(void*))(void*)> >::_M_invoke(std::_Any_data const&) /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/bits/std_function.h:300 (db_stress+0xb47468)***
stderr has error message:
***    #12 std::function<void ()>::operator()() const /mnt/gvfs/third-party2/libgcc/4959b39cfbe5965a37c861c4c327fa7c5c759b87/9.x/platform009/9202ce7/include/c++/9.3.0/bits/std_function.h:688 (db_stress+0xb44dd5)***
stderr has error message:
***    #13 rocksdb::ThreadPoolImpl::Impl::BGThread(unsigned long) util/threadpool_imp.cc:319 (db_stress+0xb44dd5)***
stderr has error message:
***    #14 rocksdb::ThreadPoolImpl::Impl::BGThreadWrapper(void*) util/threadpool_imp.cc:360 (db_stress+0xb4549f)***
stderr has error message:

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 50b4c06 to 27e59d5 Compare September 6, 2022 18:57
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

1 similar comment
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from d759be8 to 155e55d Compare September 9, 2022 02:00
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 155e55d to a116e99 Compare September 9, 2022 02:05
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235 hx235 changed the title Inject sleep, context switch and spurious wakeup before acquiring db mutex to expose race condition Inject spurious wakeup and sleep before acquiring db mutex to expose race condition Sep 9, 2022
@hx235

hx235 commented Sep 9, 2022

Copy link
Copy Markdown
Contributor Author

Update:

  • Address comment
  • TODO: see PR summary

Comment thread monitoring/instrumented_mutex.h Resolved
Comment thread db/db_impl/db_impl.cc Resolved
@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from a116e99 to 971f891 Compare September 9, 2022 02:24
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@ajkr ajkr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates LGTM

Comment thread monitoring/instrumented_mutex.h Resolved
@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 971f891 to be25290 Compare September 9, 2022 02:33
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hx235

hx235 commented Sep 9, 2022

Copy link
Copy Markdown
Contributor Author

Update:

  • Mixed in sched_yield with sleep
  • Updated performance data
  • Will land after signals are clear

@ajkr

ajkr commented Sep 9, 2022

Copy link
Copy Markdown
Contributor

I might try adding a mode that saves the last DB, last expected state+trace, and all WALs.

@hx235 it's in #10659. Using that my command to produce failures is:

$ make DEBUG_LEVEL=0 COERCE_CONTEXT_SWITCH=1 -j24 db_stress
...
$ DEBUG_LEVEL=0 TEST_TMPDIR=/dev/shm python3 tools/db_crashtest.py blackbox --simple --value_size_mult=33 --write_buffer_size=524288 --target_file_size_base=524288 --ingest_external_file_one_in=100 --compact_files_one_in=1000 --compact_range_one_in=1000 --read_fault_one_in=0 --readpercent=0 --writepercent=80 --interval=10 --max_key=100000 --preserve_unverified_changes=1

Then upon verification failure you'll find all the files leading up to the crash in "/dev/shm/rocksdb_crashtest_blackbox/unverified" and "/dev/shm/rocksdb_crashtest_expected/unverified". Or if it's a failure in the middle (like a force_consistency_checks), you'll find all the files in "/dev/shm/rocksdb_crashtest_blackbox" and "/dev/shm/rocksdb_crashtest_expected"

@hx235
hx235 force-pushed the inject_sleep_or_yield_in_stress_test branch from 3da7cef to 120d758 Compare September 12, 2022 18:11
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@hx235 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Tim-Reed pushed a commit to Tim-Reed/rocksdb that referenced this pull request Oct 1, 2026
…race condition (meta#10291)

Summary:
**Context/Summary:**
Previous experience with bugs and flaky tests taught us there exist features in RocksDB vulnerable to race condition caused by acquiring db mutex at a particular timing. This PR aggressively exposes those vulnerable features by injecting spurious wakeup and sleep to cause acquiring db mutex at various timing in order to expose such race condition

**Testing:**
- `COERCE_CONTEXT_SWITCH=1 make -j56 check / make -j56 db_stress` should reveal
    - flaky tests caused by db mutex related race condition
       - Reverted meta#9528
       - A/B testing on `COMPILE_WITH_TSAN=1 make -j56 listener_test` w/ and w/o `COERCE_CONTEXT_SWITCH=1` followed by `./listener_test --gtest_filter=EventListenerTest.MultiCF --gtest_repeat=10`
       - `COERCE_CONTEXT_SWITCH=1` can cause expected test failure (i.e, expose target TSAN data race error) within 10 run while the other couldn't.
       - This proves our injection can expose flaky tests caused by db mutex related race condition faster.
    -  known or new race-condition-type of internal bug by continuously running this PR
- Performance
   - High ops-threads time: COERCE_CONTEXT_SWITCH=1 regressed by 4 times slower (2:01.16 vs 0:22.10 elapsed ). This PR will be run as a separate CI job so this regression won't affect any existing job.
```
TEST_TMPDIR=$db /usr/bin/time ./db_stress \
--ops_per_thread=100000 --expected_values_dir=$exp --clear_column_family_one_in=0 \
--write_buffer_size=524288 —target_file_size_base=524288 —ingest_external_file_one_in=100 —compact_files_one_in=1000 —compact_range_one_in=1000
```
  - Start-up time:  COERCE_CONTEXT_SWITCH=1 didn't regress by 25% (0:01.51 vs 0:01.29 elapsed)
```
TEST_TMPDIR=$db ./db_stress -ops_per_thread=100000000 -expected_values_dir=$exp --clear_column_family_one_in=0 & sleep 120; pkill -9 db_stress

TEST_TMPDIR=$db /usr/bin/time ./db_stress \
--ops_per_thread=1 -reopen=0 --expected_values_dir=$exp --clear_column_family_one_in=0 --destroy_db_initially=0
```

Pull Request resolved: meta#10291

Reviewed By: ajkr

Differential Revision: D39231182

Pulled By: hx235

fbshipit-source-id: 7ab6695430460e0826727fd8c66679b32b3e44b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed WIP Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants