標籤:
mysql_declare_plugin(innobase){ MYSQL_STORAGE_ENGINE_PLUGIN, &innobase_storage_engine, innobase_hton_name, plugin_author, "Supports transactions, row-level locking, and foreign keys", PLUGIN_LICENSE_GPL, innobase_init, /* Plugin Init */ NULL, /* Plugin Deinit */ INNODB_VERSION_SHORT, innodb_status_variables_export,/* status variables */ innobase_system_variables, /* system variables */ NULL, /* reserved */ 0, /* flags */},i_s_innodb_trx,i_s_innodb_locks,i_s_innodb_lock_waits,i_s_innodb_cmp,i_s_innodb_cmp_reset,i_s_innodb_cmpmem,i_s_innodb_cmpmem_resetmysql_declare_plugin_end;
- 我曾經看了一本《MySQL技術內幕:innodb儲存引擎》,個人感覺這本書儘管名字像是講底層的。事實上還是面向營運的。書中基本上沒有剖析代碼,都是用是文字和範例來解說。我簡略地看了一遍。大概懂了一點東西,可是明顯認為不夠爽。由於心中非常多的疑惑沒法解釋。
《深入理解MySQL》裡面的demo儲存引擎倒是有比較具體的解釋,可是感覺和innodb這種“高大上”還是沒法比的。
沒辦法了,僅僅能硬著頭皮上代碼了。我是帶著問題來看代碼的。寫的東西肯定不是系統的。
- 我心中最大的疑惑是。innodb為什麼能夠是多線程的?《深入》中的引擎、csv什麼的都根本看不到了多線程的影子。在外掛程式式的架構中,innodb事實上就是一個動態庫。
這個動態庫內部實現了多線程架構,總覺的不太踏實。假設這動態庫被別的MySQL執行個體載入了。究竟會不會有問題?(當然這個問題感覺我短時間回答不了)。
- 回到正題,這個innodb的儲存外掛程式聲明中有一個innobase_init欄位。
- innobase_init是一個函數,它被賦值給誰了呢?看plugin.h中的程式碼片段。
struct st_mysql_plugin{ int type; /* the plugin type (a MYSQL_XXX_PLUGIN value) */ void *info; /* pointer to type-specific plugin descriptor */ const char *name; /* plugin name */ const char *author; /* plugin author (for I_S.PLUGINS) */ const char *descr; /* general descriptive text (for I_S.PLUGINS) */ int license; /* the plugin license (PLUGIN_LICENSE_XXX) */ int (*init)(void *); /* the function to invoke when plugin is loaded */ int (*deinit)(void *);/* the function to invoke when plugin is unloaded */ unsigned int version; /* plugin version (for I_S.PLUGINS) */ struct st_mysql_show_var *status_vars; struct st_mysql_sys_var **system_vars; void * __reserved1; /* reserved for dependency checking */ unsigned long flags; /* flags for plugin */};
for (i = 0; i < srv_n_file_io_threads; i++) { n[i] = i; os_thread_create(io_handler_thread, n + i, thread_ids + i); } /***** *****/ /* Create the thread which watches the timeouts for lock waits */ os_thread_create(&srv_lock_timeout_thread, NULL, thread_ids + 2 + SRV_MAX_N_IO_THREADS); /* Create the thread which warns of long semaphore waits */ os_thread_create(&srv_error_monitor_thread, NULL, thread_ids + 3 + SRV_MAX_N_IO_THREADS); /* Create the thread which prints InnoDB monitor info */ os_thread_create(&srv_monitor_thread, NULL, thread_ids + 4 + SRV_MAX_N_IO_THREADS); /***** *****/ os_thread_create(&srv_master_thread, NULL, thread_ids + (1 + SRV_MAX_N_IO_THREADS));
除了上面的幾個線程,另一個可選的purge線程。
普通情況下,innodb儲存引擎會有大概八個線程。這個結論和《MySQL技術內幕》的還是非常接近的,我們看的innodb版本號碼應該不一樣。這大概就是innodb引擎多線程架構的初始化過程,進一步研究innodb的架構就應該去看各個線程的工作函數,以及它們之間的同步及通訊機制了。
至於我那個“動態庫被別的MySQL執行個體載入的問題”。
我認為在這個情形下,動態庫的機制並非為了代碼反覆利用,而是基於模組化的軟體設計思想。這裡的引擎不應該被別的MySQL執行個體載入。
MySQL中innodb引擎分析(初始化)