forked from yugabyte/yugabyte-db
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[yugabyte#16380] Use thread safety analysis annotations to enforce th…
…e reactor thread requirement Summary: Some functions in Connection, Reactor, Messenger, OutboundCall, and other classes of the RPC framework have to run only on a particular reactor thread. Also, some fields of those classes are not protected by a mutex but are only accessed in the reactor thread. We can use a "thread role" and Clang thread safety analysis annotations to further document and enforce these requirements during compilation time. Functions can now be annotated with REQUIRES(ReactorThreadRole::kReactor), with a convenience macro ON_REACTOR_THREAD. Functions that should not be run on the reactor thread can be annotated with EXCLUDES(ReactorThreadRole::kReactor), or EXCLUDES_REACTOR_THREAD. Also removing some redundant checks that the current thread is the correct reactor thread. Functions that are executed as callbacks on the reactor thread should use the following construct. This will check for the current thread (can be turned off by setting the reactor_check_current_thread flag to false), and return a "guard" object: auto guard = reactor->CheckCurrentThread(); The guard object, acting as a lock being held, will ensure that the compiler allows calling functions that are marked with ON_REACTOR_THREAD from that point on, and those functions do not need to check that they are running on the reactor thread. Concurrency issues fixed: - Access to Reactor::cur_time_ was not being synchronized. It is now an atomic. - Connection::last_activity_time_ had unclear locking semantics. Made it an atomic. - Connection::shutdown_status_ was being assigned while holding outbound_data_queue_lock_. However, it was being read without that mutex. Now it is fully guarded by that mutex (which was renamed to outbound_data_queue_mtx_). - Now we are only modifying Reactor::state_ while holding pending_tasks_mtx_, to match the documented behavior, even though state_ is an atomic. - Access to Reactor::waiting_conns_ was synchronized in an unclear way. We were erasing connections from that set in a ListenIdle callback that could be called either from the reactor thread or from some other thread after the reactor has started closing, but only in a single-threaded fashion. Instead of this, introduced a special mutex to protect that data structure and got rid of the IsCurrentThreadOrStartedClosing logic. Some other details of the changes: - Reorganized fields in Reactor, DelayedTask, and Connection. Clearly split fields into categories: - Immutable fields initialized in the constructor - Fields protected by a particular mutex - Fields only accessed on the reactor thread - Atomic fields - Renamed Reactor::Shutdown to StartShutdown, because it only initiates shutdown and does not wait for it to complete. - Reactor::StartShutdown had a compare-and-set loop, which did not make much sense, as it usually did exactly two iterations. Replaced that with a single compare-exchange-strong operation. - Removed Reactor::stop_start_time_. It was assigned but never used. - Got rid of the Reactor::DrainTaskQueueAndCheckIfClosing method. It was only called from one place, and it looked like a random combination of things clumped together. Simply moved this code where it was being used. - Moved various reactor task classes to reactor_task.{h,cc}. - Moved DelayedTask to delayed_task.{h,cc}. - Made Connection::rpc_metrics_ a reference instead of a const pointer that can never be null anyway. Same for Reactor::messenger_. - Now consistently handling failures to queue a reactor task by issuing a DFATAL log message. - Simplified the method Messenger::ScheduleOnReactor. It used to take a Messenger* argument, but it is already a method of Messenger, which was confusing. That argument could be nullptr in Reactor tests. Got rid of that argument and fixed the tests. - In Reactor::QueueOutboundCall, if we failed to schedule the call due to the reactor being in the wrong state, do not simply log a warning, but call the Transferred method on the OutboundCall with the appropriate status instead. General improvements of the codebase: - Removed the UNIQUE_LOCK macro and replaced it with direct use of the UniqueLock wrapper class, utilizing CTAD (class template argument deduction). Build infrastructure improvements: - We still have some old macOS build worker nodes with Clang 13 installed. In those cases, use custom-built Clang 14 by specifying compiler type as `clang14`. - In activate_virtualenv, detect target architecture first so that we don't create a virtualenv for the wrong target architecture (Rosetta 2 emulated x86_64 on an arm64 Apple machine). - Made Java build output less verbose (ignoring messages about resolving dependencies, Maven plugins, etc.) - Updated the is_clang function to return true for `clang<version>` values of YB_COMPILER_TYPE. - We run a "process supervisor" script to look for stray processes after test termination. Temporarily ignore process_supervisor failures if they happen on a CentOS 7 machine with an absent psutil Python module, to work around an issue in a recently updated CentOS 7 test runner VM. - Set PYTHONPATH at the top level of the run-test.sh test runner script. Test Plan: Jenkins: run all tests Reviewers: timur, sergei, dmitry Reviewed By: sergei, dmitry Subscribers: bogdan, ybase Differential Revision: https://phabricator.dev.yugabyte.com/D23468
- Loading branch information
Showing
61 changed files
with
1,658 additions
and
1,148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.