- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
WiredTiger实现探秘
展开查看详情
1 .WiredTiger 实现探秘 Presented by 许鹏
2 .引擎特点 最小化线程间的竞争 无锁算法 如 hazard pinters 减少并发访问中的互斥操作 充分挖掘硬件能力 多核 大内存 缓存和文件压缩
3 .整体架构
4 .整体架构 (2)
5 .内存管理 – 页面管理 BTree
6 .内存管理 - 叶子结点
7 .初始化写入 – 数据没有落入磁盘 $97 = { u = { intl = { parent_ref = 0x0, split_gen = 0, __index = 0x0 }, row = 0x0, fix_bitf = 0x0, col_var = { col_var = 0x0, repeats = 0x0 } }, entries = 0, type = 7 a, flags_atomic = 0 000, unused = "
8 .
9 .页面释放 – Hazard Pointer 建立一个全局数组,数组容量为线程数目,每个线程只能修改自己的数组元素,而不允许修改其他的数组元素,但可以读别的数组元素 。 当线程尝试访问一个关键数据节点时,先把该节点指针赋给自己的数组元素(即不要释放这个节点) 。 每个线程自己维护一个私有链表,当线程准备释放掉某个节点时,将该节点放入到链表中。当链表内的数目达到一个设定的数目后,遍历该链表用于释放链表内所有节点 。 当释放节点时,需要检查全局数组,确定没有任何一个线程的数组元素与当前指针相同时,就释放该节点。否则仍然滞留在自己的链表中。
10 .数据持久化 Checkpoint Occur at an interval of 60 seconds Journal log Sync to disk every 50 milliseconds
11 .文件压缩 默认使用 Snappy 压缩算法 缓存中是解压后的格式 , 占用内存空间远大于磁盘
12 .并发访问控制 -- Ticket Lock struct __ wt_rwlock { /* Read/write lock */ volatile union { uint64_t v; /* Full 64-bit value */ struct { uint8_t current; /* Current ticket */ uint8_t next; /* Next available ticket */ uint8_t reader; /* Read queue ticket */ uint8_t readers_queued ; /* Count of queued readers */ uint32_t readers_active ; /* Count of active readers */ } s; } u; int16_t stat_read_count_off ; /* read acquisitions offset */ int16_t stat_write_count_off ; /* write acquisitions offset */ int16_t stat_app_usecs_off ; /* waiting application threads offset */ int16_t stat_int_usecs_off ; /* waiting server threads offset */ WT_CONDVAR * cond_readers ; /* Blocking readers */ WT_CONDVAR * cond_writers ; /* Blocking writers */ }; db.serverStatus().wiredTiger.concurrentTransactions
13 .性能监控指标 db.server.status (). wiredtiger db.coll.stats () db.currentop ()
14 .参数设置 db.createCollection ( "users", { storageEngine : { wiredTiger : { configString : " leaf_page_max =64kb, leaf_value_max =64MB" } } } )
15 .Demo Application #include " wiredtiger.h “ #include < stdio.h > int main( int argc , char** argv ) { char* home = "/ tmp / wt "; WT_CONNECTION *conn; WT_SESSION *session; WT_CURSOR *cursor; const char* key, *value; int ret; wiredtiger_open (home , NULL, "create", &conn); conn-> open_session (conn, NULL, NULL, &session); // create a table session->create(session, " table:demo ", " key_format =S, value_format =S"); // open the table session-> open_cursor (session, " table:demo ", NULL, NULL, &cursor); cursor- > set_key (cursor, "name"); cursor-> set_value (cursor, " peng "); cursor->insert(cursor); cursor-> set_key (cursor, "iris"); cursor-> set_value (cursor, "teacher"); cursor->insert(cursor); cursor->reset(cursor); while ( (ret = cursor->next(cursor) ) == 0 ) { cursor-> get_key (cursor, &key); cursor-> get_value (cursor, &value); printf ("Got Record: %s, %s
16 .Demo Application – 编译和运行 gcc -g -I. -o / tmp / wt_demo.o -c ~/working/ wt_demo.c gcc -L .libs/ $PWD/.libs/libwiredtiger-3.1.0.so / tmp / wt_demo.o -o / tmp / wt_demo LD_LIBRARY_PATH =$PWD/.libs / tmp / wt_demo 编译 调试 set print pretty br wt_demo.c:37 r set scheduler-locking on
17 .Q & A