summaryrefslogtreecommitdiff
path: root/sql/table.h
diff options
context:
space:
mode:
authorSergey Vojtovich <svoj@mariadb.org>2019-10-16 18:19:59 +0400
committerSergey Vojtovich <svoj@mariadb.org>2020-05-29 21:53:54 +0400
commit609a0d3db3fecc12c2d63b52bec3e3a305e6c702 (patch)
tree1a56b4c5ac6595ef6eaf802ff1b448d540971c5a /sql/table.h
parent1055a7f4fc0a095e6ab3a6a138d64688b764a78b (diff)
downloadmariadb-git-609a0d3db3fecc12c2d63b52bec3e3a305e6c702.tar.gz
Thread safe statistics loading
Previously multiple threads were allowed to load statistics concurrently. There were no known problems caused by this. But given amount of data races in this code, it'd happen sooner or later. To avoid scalability bottleneck, statistics loading is protected by per-TABLE_SHARE atomic variable. Whenever statistics were loaded by preceding statement (hot-path), a scalable load-acquire check is performed. Whenever statistics have to be loaded anew, mutual exclusion for loaders is established by atomic variable. If statistics are being loaded concurrently, statement waits until load is completed. TABLE_STATISTICS_CB::stats_can_be_read and TABLE_STATISTICS_CB::stats_is_read are replaced with a tri state atomic variable. Part of MDEV-19061 - table_share used for reading statistical tables is not protected
Diffstat (limited to 'sql/table.h')
-rw-r--r--sql/table.h75
1 files changed, 71 insertions, 4 deletions
diff --git a/sql/table.h b/sql/table.h
index 29b4cfdbcf3..85ba7b29ce2 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -594,15 +594,82 @@ enum open_frm_error {
from persistent statistical tables
*/
-struct TABLE_STATISTICS_CB
+class TABLE_STATISTICS_CB
{
+ class Statistics_state
+ {
+ enum state_codes
+ {
+ EMPTY, /** data is not loaded */
+ LOADING, /** data is being loaded in some connection */
+ READY /** data is loaded and available for use */
+ };
+ int32 state;
+
+ public:
+ /** No state copy */
+ Statistics_state &operator=(const Statistics_state &) { return *this; }
+
+ /** Checks if data loading have been completed */
+ bool is_ready() const
+ {
+ return my_atomic_load32_explicit(const_cast<int32*>(&state),
+ MY_MEMORY_ORDER_ACQUIRE) == READY;
+ }
+
+ /**
+ Sets mutual exclusion for data loading
+
+ If stats are in LOADING state, waits until state change.
+
+ @return
+ @retval true atomic EMPTY -> LOADING transfer completed, ok to load
+ @retval false stats are in READY state, no need to load
+ */
+ bool start_load()
+ {
+ for (;;)
+ {
+ int32 expected= EMPTY;
+ if (my_atomic_cas32_weak_explicit(&state, &expected, LOADING,
+ MY_MEMORY_ORDER_RELAXED,
+ MY_MEMORY_ORDER_RELAXED))
+ return true;
+ if (expected == READY)
+ return false;
+ (void) LF_BACKOFF;
+ }
+ }
+
+ /** Marks data available for subsequent use */
+ void end_load()
+ {
+ DBUG_ASSERT(my_atomic_load32_explicit(&state, MY_MEMORY_ORDER_RELAXED) ==
+ LOADING);
+ my_atomic_store32_explicit(&state, READY, MY_MEMORY_ORDER_RELEASE);
+ }
+
+ /** Restores empty state on error (e.g. OOM) */
+ void abort_load()
+ {
+ DBUG_ASSERT(my_atomic_load32_explicit(&state, MY_MEMORY_ORDER_RELAXED) ==
+ LOADING);
+ my_atomic_store32_explicit(&state, EMPTY, MY_MEMORY_ORDER_RELAXED);
+ }
+ };
+
+ class Statistics_state stats_state;
+
+public:
MEM_ROOT mem_root; /* MEM_ROOT to allocate statistical data for the table */
Table_statistics *table_stats; /* Structure to access the statistical data */
- bool stats_can_be_read; /* Memory for statistical data is allocated */
- bool stats_is_read; /* Statistical data for table has been read
- from statistical tables */
bool histograms_can_be_read;
bool histograms_are_read;
+
+ bool stats_are_ready() const { return stats_state.is_ready(); }
+ bool start_stats_load() { return stats_state.start_load(); }
+ void end_stats_load() { stats_state.end_load(); }
+ void abort_stats_load() { stats_state.abort_load(); }
};