{"id":10021093,"date":"2025-11-27T14:00:29","date_gmt":"2025-11-27T06:00:29","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021093"},"modified":"2025-11-27T14:00:29","modified_gmt":"2025-11-27T06:00:29","slug":"dragonboat%e7%bb%9f%e4%b8%80%e5%ad%98%e5%82%a8logdb%e5%ae%9e%e7%8e%b0%e5%88%86%e6%9e%90%ef%bd%9c%e5%be%97%e7%89%a9%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021093","title":{"rendered":"Dragonboat\u7edf\u4e00\u5b58\u50a8LogDB\u5b9e\u73b0\u5206\u6790\uff5c\u5f97\u7269\u6280\u672f"},"content":{"rendered":"<h2>\u4e00\u3001\u9879\u76ee\u6982\u89c8<\/h2>\n<p>Dragonboat \u662f\u7eaf Go \u5b9e\u73b0\u7684\uff08multi-group\uff09Raft \u5e93\u3002<\/p>\n<p>\u4e3a\u5e94\u7528\u5c4f\u853d Raft \u590d\u6742\u6027\uff0c\u63d0\u4f9b\u6613\u4e8e\u4f7f\u7528\u7684 NodeHost \u548c\u72b6\u6001\u673a\u63a5\u53e3\u3002\u8be5\u5e93\uff08\u81ea\u79f0\uff09\u6709\u5982\u4e0b\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u9ad8\u541e\u5410\u3001\u6d41\u6c34\u7ebf\u5316\u3001\u6279\u5904\u7406\uff1b<\/li>\n<li>\u63d0\u4f9b\u4e86\u5185\u5b58\/\u78c1\u76d8\u72b6\u6001\u673a\u591a\u79cd\u5b9e\u73b0\uff1b<\/li>\n<li>\u63d0\u4f9b\u4e86 ReadIndex\u3001\u6210\u5458\u53d8\u66f4\u3001Leader\u8f6c\u79fb\u7b49\u7ba1\u7406\u7aefAPI\uff1b<\/li>\n<li>\u9ed8\u8ba4\u4f7f\u7528 Pebble \u4f5c\u4e3a \u5b58\u50a8\u540e\u7aef\u3002<\/li>\n<\/ul>\n<p>\u672c\u6b21\u4ee3\u7801\u4e32\u8bb2\u4ee5V3\u7684\u7a33\u5b9a\u7248\u672c\u4e3a\u57fa\u7840\uff0c\u4e0d\u5305\u62ecGitHub\u4e0av4\u7248\u672c\u5185\u5bb9\u3002<\/p>\n<h2>\u4e8c\u3001\u6574\u4f53\u67b6\u6784<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"671\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbqk\" alt=\"\" title=\"\"><\/p>\n<h2>\u4e09\u3001LogDB \u7edf\u4e00\u5b58\u50a8<\/h2>\n<p>LogDB \u6a21\u5757\u662f Dragonboat \u7684\u6838\u5fc3\u6301\u4e45\u5316\u5b58\u50a8\u5c42\uff0c\u867d\u7136\u6a21\u5757\u540d\u5b57\u6709Log\uff0c\u4f46\u662f\u5b83\u56ca\u62ec\u4e86\u6240\u6709\u548c\u5b58\u50a8\u76f8\u5173\u7684API\uff0c\u8d1f\u8d23\u7ba1\u7406 Raft \u534f\u8bae\u7684\u6240\u6709\u6301\u4e45\u5316\u6570\u636e\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>Raft\u72b6\u6001 (RaftState)<\/strong><\/p>\n<p><strong>Raft\u5185\u90e8\u72b6\u6001\u53d8\u66f4\u7684\u96c6\u5408\u7ed3\u6784<\/strong><\/p>\n<p>\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<ul>\n<li>ClusterID\/NodeID\uff1a \u8282\u70b9ID<\/li>\n<li>RaftState\uff1a Raft\u4efb\u671f\u3001\u6295\u7968\u60c5\u51b5\u3001commit\u8fdb\u5ea6<\/li>\n<li>EntriesToSave\uff1aRaft\u63d0\u6848\u65e5\u5fd7\u6570\u636e<\/li>\n<li>Snapshot\uff1a\u5feb\u7167\u5143\u6570\u636e(\u5305\u62ec\u5feb\u7167\u6587\u4ef6\u8def\u5f84\uff0c\u5feb\u7167\u5927\u5c0f\uff0c\u5feb\u7167\u5bf9\u5e94\u7684\u63d0\u6848Index\uff0c\u5feb\u7167\u5bf9\u5e94\u7684Raft\u4efb\u671f\u7b49\u4fe1\u606f)<\/li>\n<li>Messages\uff1a\u53d1\u7ed9\u5176\u4ed6\u8282\u70b9\u7684Raft\u6d88\u606f<\/li>\n<li>ReadyToReads\uff1aReadIndex\u5c31\u7eea\u7684\u8bf7\u6c42<\/li>\n<\/ul>\n<p><strong>\u5f15\u5bfc\u4fe1\u606f (Bootstrap)<\/strong><\/p>\n<pre><code>type\u00a0Bootstrap\u00a0struct\u00a0{\n\u00a0 \u00a0 Addresses\u00a0map[uint64]string\u00a0\/\/ \u521d\u59cb\u96c6\u7fa4\u6210\u5458\n\u00a0 \u00a0 Join \u00a0 \u00a0 \u00a0bool\n\u00a0 \u00a0 Type \u00a0 \u00a0 \u00a0StateMachineType\n}<\/code><\/pre>\n<p>ILogDB\u7684API\u5982\u4e0b\uff1a<\/p>\n<pre><code>type\u00a0ILogDB\u00a0interface\u00a0{\n\n\n\u00a0 \u00a0 BinaryFormat()\u00a0uint32\u00a0\/\/ \u8fd4\u56de\u652f\u6301\u7684\u4e8c\u8fdb\u5236\u683c\u5f0f\u7248\u672c\u53f7\n\n\n\u00a0 \u00a0 ListNodeInfo() ([]NodeInfo,\u00a0error)\u00a0\/\/ \u5217\u51fa LogDB \u4e2d\u6240\u6709\u53ef\u7528\u7684\u8282\u70b9\u4fe1\u606f\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5b58\u50a8\u96c6\u7fa4\u8282\u70b9\u7684\u521d\u59cb\u5316\u914d\u7f6e\u4fe1\u606f\uff0c\u5305\u62ec\u662f\u5426\u52a0\u5165\u96c6\u7fa4\u3001\u72b6\u6001\u673a\u7c7b\u578b\u7b49\n\u00a0 \u00a0 SaveBootstrapInfo(clusterID\u00a0uint64, nodeID\u00a0uint64, bootstrap pb.Bootstrap)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u83b7\u53d6\u4fdd\u5b58\u7684\u5f15\u5bfc\u4fe1\u606f\n\u00a0 \u00a0 GetBootstrapInfo(clusterID\u00a0uint64, nodeID\u00a0uint64) (pb.Bootstrap,\u00a0error)\n\n\n\u00a0 \u00a0\u00a0\/\/ \u539f\u5b50\u6027\u4fdd\u5b58 Raft \u72b6\u6001\u3001\u65e5\u5fd7\u6761\u76ee\u548c\u5feb\u7167\u5143\u6570\u636e\n\u00a0 \u00a0 SaveRaftState(updates []pb.Update, shardID\u00a0uint64)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u8fed\u4ee3\u8bfb\u53d6\u6307\u5b9a\u8303\u56f4\u5185\u7684\u8fde\u7eed\u65e5\u5fd7\u6761\u76ee\n\u00a0 \u00a0 IterateEntries(ents []pb.Entry, size\u00a0uint64, clusterID\u00a0uint64, nodeID\u00a0uint64,\u00a0\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0low\u00a0uint64, high\u00a0uint64, maxSize\u00a0uint64) ([]pb.Entry,\u00a0uint64,\u00a0error)\n\n\n\u00a0 \u00a0\u00a0\/\/ \u8bfb\u53d6\u6301\u4e45\u5316\u7684 Raft \u72b6\u6001\n\u00a0 \u00a0 ReadRaftState(clusterID\u00a0uint64, nodeID\u00a0uint64, lastIndex\u00a0uint64) (RaftState,\u00a0error)\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5220\u9664\u6307\u5b9a\u7d22\u5f15\u4e4b\u524d\u7684\u6240\u6709\u6761\u76ee, \u65e5\u5fd7\u538b\u7f29\u3001\u5feb\u7167\u540e\u6e05\u7406\u65e7\u65e5\u5fd7\n\u00a0 \u00a0 RemoveEntriesTo(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u56de\u6536\u6307\u5b9a\u7d22\u5f15\u4e4b\u524d\u6761\u76ee\u5360\u7528\u7684\u5b58\u50a8\u7a7a\u95f4\n\u00a0 \u00a0 CompactEntriesTo(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64) (&lt;-chan\u00a0struct{},\u00a0error)\n\n\n\u00a0 \u00a0\u00a0\/\/ \u4fdd\u5b58\u6240\u6709\u5feb\u7167\u5143\u6570\u636e\n\u00a0 \u00a0 SaveSnapshots([]pb.Update)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5220\u9664\u6307\u5b9a\u7684\u5feb\u7167\u5143\u6570\u636e \u6e05\u7406\u8fc7\u65f6\u6216\u65e0\u6548\u7684\u5feb\u7167\n\u00a0 \u00a0 DeleteSnapshot(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5217\u51fa\u6307\u5b9a\u7d22\u5f15\u8303\u56f4\u5185\u7684\u53ef\u7528\u5feb\u7167\n\u00a0 \u00a0 ListSnapshots(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64) ([]pb.Snapshot,\u00a0error)\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5220\u9664\u8282\u70b9\u7684\u6240\u6709\u76f8\u5173\u6570\u636e\n\u00a0 \u00a0 RemoveNodeData(clusterID\u00a0uint64, nodeID\u00a0uint64)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5bfc\u5165\u5feb\u7167\u5e76\u521b\u5efa\u6240\u6709\u5fc5\u9700\u7684\u5143\u6570\u636e\n\u00a0 \u00a0 ImportSnapshot(snapshot pb.Snapshot, nodeID\u00a0uint64)\u00a0error\n}<\/code><\/pre>\n<h3>3.1\u7d22\u5f15\u952e<\/h3>\n<p>\u5b58\u50a8\u7684\u5e95\u5c42\u672c\u8d28\u662f\u4e00\u4e2aKVDB (pebble or rocksdb)\uff0c\u7531\u4e8e\u4e1a\u52a1\u7684\u590d\u6742\u6027\uff0c\u8981\u7edf\u4e00\u5404\u7c7b\u4e1a\u52a1key\u7684\u8bbe\u8ba1\u65b9\u6cd5\uff0c\u800c\u4e14\u8981\u964d\u4f4e\u7a7a\u95f4\u4f7f\u7528\uff0c\u6240\u4ee5\u6709\u4e86\u5982\u4e0b\u7684key\u8bbe\u8ba1\u65b9\u6848\u3002<\/p>\n<p>\u9f99\u821f\u4e2dkey\u5206\u4e3a3\u7c7b\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"141\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbql\" alt=\"\" title=\"\" loading=\"lazy\"><br \/>\u5176\u4e2d\uff0c2\u5b57\u8282\u7684header\u7528\u4e8e\u533a\u5206\u5404\u7c7b\u4e0d\u540c\u4e1a\u52a1\u7684key\u7a7a\u95f4\u3002<\/p>\n<pre><code>entryKeyHeader \u00a0 \u00a0 \u00a0 = [2]byte{0x1,\u00a00x1} \u00a0\/\/ \u666e\u901a\u65e5\u5fd7\u6761\u76ee\npersistentStateKey \u00a0 = [2]byte{0x2,\u00a00x2} \u00a0\/\/ Raft\u72b6\u6001\nmaxIndexKey \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= [2]byte{0x3,\u00a00x3} \u00a0\/\/ \u6700\u5927\u7d22\u5f15\u8bb0\u5f55\nnodeInfoKey \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= [2]byte{0x4,\u00a00x4} \u00a0\/\/ \u8282\u70b9\u5143\u6570\u636e\nbootstrapKey \u00a0 \u00a0 \u00a0 \u00a0 = [2]byte{0x5,\u00a00x5} \u00a0\/\/ \u542f\u52a8\u914d\u7f6e\nsnapshotKey \u00a0 \u00a0 \u00a0 \u00a0 \u00a0= [2]byte{0x6,\u00a00x6} \u00a0\/\/ \u5feb\u7167\u7d22\u5f15\nentryBatchKey \u00a0 \u00a0 \u00a0 \u00a0= [2]byte{0x7,\u00a00x7} \u00a0\/\/ \u6279\u91cf\u65e5\u5fd7<\/code><\/pre>\n<p>\u5728key\u7684\u751f\u6210\u4e2d\uff0c\u91c7\u7528\u4e86useAsXXXKey\u548cSetXXXKey\u7684\u65b9\u5f0f\uff0c\u590d\u7528\u4e86data\u8fd9\u4e2a\u4e8c\u8fdb\u5236\u53d8\u91cf\uff0c\u51cf\u5c11GC\u3002<\/p>\n<pre><code>type\u00a0Key\u00a0struct\u00a0{\n\u00a0 \u00a0 data []byte\u00a0\u00a0\/\/ \u5e95\u5c42\u5b57\u8282\u6570\u7ec4\u590d\u7528\u6c60\n\u00a0 \u00a0 key \u00a0[]byte\u00a0\u00a0\/\/ \u6709\u6548\u6570\u636e\u5207\u7247\n\u00a0 \u00a0 pool *sync.Pool\u00a0\/\/ \u4f3c\u4e4e\u5e76\u6ca1\u6709\u4ec0\u4e48\u7528\n}\n\n\n\n\nfunc\u00a0(k *Key)\u00a0useAsEntryKey() {\n\u00a0 \u00a0 k.key = k.data\n}\n\n\ntype\u00a0IReusableKey\u00a0interface\u00a0{\n\u00a0 \u00a0 SetEntryBatchKey(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64)\n\u00a0 \u00a0\u00a0\/\/ SetEntryKey sets the key to be an entry key for the specified Raft node\n\u00a0 \u00a0\u00a0\/\/ with the specified entry index.\n\u00a0 \u00a0 SetEntryKey(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64)\n\u00a0 \u00a0\u00a0\/\/ SetStateKey sets the key to be an persistent state key suitable\n\u00a0 \u00a0\u00a0\/\/ for the specified Raft cluster node.\n\u00a0 \u00a0 SetStateKey(clusterID\u00a0uint64, nodeID\u00a0uint64)\n\u00a0 \u00a0\u00a0\/\/ SetMaxIndexKey sets the key to be the max possible index key for the\n\u00a0 \u00a0\u00a0\/\/ specified Raft cluster node.\n\u00a0 \u00a0 SetMaxIndexKey(clusterID\u00a0uint64, nodeID\u00a0uint64)\n\u00a0 \u00a0\u00a0\/\/ Key returns the underlying byte slice of the key.\n\u00a0 \u00a0 Key() []byte\n\u00a0 \u00a0\u00a0\/\/ Release releases the key instance so it can be reused in the future.\n\u00a0 \u00a0 Release()\n}\n\n\nfunc\u00a0(k *Key)\u00a0useAsEntryKey() {\n\u00a0 \u00a0 k.key = k.data\n}\n\n\n\/\/ SetEntryKey sets the key value to the specified entry key.\nfunc\u00a0(k *Key)\u00a0SetEntryKey(clusterID\u00a0uint64, nodeID\u00a0uint64, index\u00a0uint64) {\n\u00a0 \u00a0 k.useAsEntryKey()\n\u00a0 \u00a0 k.key[0] = entryKeyHeader[0]\n\u00a0 \u00a0 k.key[1] = entryKeyHeader[1]\n\u00a0 \u00a0 k.key[2] =\u00a00\n\u00a0 \u00a0 k.key[3] =\u00a00\n\u00a0 \u00a0 binary.BigEndian.PutUint64(k.key[4:], clusterID)\n\u00a0 \u00a0\u00a0\/\/ the 8 bytes node ID is actually not required in the key. it is stored as\n\u00a0 \u00a0\u00a0\/\/ an extra safenet - we don't know what we don't know, it is used as extra\n\u00a0 \u00a0\u00a0\/\/ protection between different node instances when things get ugly.\n\u00a0 \u00a0\u00a0\/\/ the wasted 8 bytes per entry is not a big deal - storing the index is\n\u00a0 \u00a0\u00a0\/\/ wasteful as well.\n\u00a0 \u00a0 binary.BigEndian.PutUint64(k.key[12:], nodeID)\n\u00a0 \u00a0 binary.BigEndian.PutUint64(k.key[20:], index)\n}<\/code><\/pre>\n<h3>3.2\u53d8\u91cf\u590d\u7528IContext<\/h3>\n<p>IContext\u7684\u6838\u5fc3\u8bbe\u8ba1\u76ee\u7684\u662f\u5b9e\u73b0<strong>\u5e76\u53d1\u5b89\u5168\u7684\u5185\u5b58\u590d\u7528\u673a\u5236<\/strong>\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u9891\u7e41\u7684\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u4f1a\u9020\u6210\u8f83\u5927\u7684GC\u538b\u529b\uff0c\u901a\u8fc7IContext\u53ef\u4ee5\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><strong>\u952e\u5bf9\u8c61\u590d\u7528<\/strong>\uff1a\u901a\u8fc7GetKey()\u83b7\u53d6\u53ef\u91cd\u7528\u7684IReusableKey<\/li>\n<li><strong>\u7f13\u51b2\u533a\u590d\u7528<\/strong>\uff1a\u901a\u8fc7GetValueBuffer()\u83b7\u53d6\u53ef\u91cd\u7528\u7684\u5b57\u8282\u7f13\u51b2\u533a<\/li>\n<li><strong>\u6279\u91cf\u64cd\u4f5c\u5bf9\u8c61\u590d\u7528<\/strong>\uff1aEntryBatch\u548cWriteBatch\u7684\u590d\u7528<\/li>\n<\/ul>\n<pre><code>\/\/ IContext is the per thread context used in the logdb module.\n\/\/ IContext is expected to contain a list of reusable keys and byte\n\/\/ slices that are owned per thread so they can be safely reused by the\n\/\/ same thread when accessing ILogDB.\ntype\u00a0IContext\u00a0interface\u00a0{\n\u00a0 \u00a0\u00a0\/\/ Destroy destroys the IContext instance.\n\u00a0 \u00a0 Destroy()\n\u00a0 \u00a0\u00a0\/\/ Reset resets the IContext instance, all previous returned keys and\n\u00a0 \u00a0\u00a0\/\/ buffers will be put back to the IContext instance and be ready to\n\u00a0 \u00a0\u00a0\/\/ be used for the next iteration.\n\u00a0 \u00a0 Reset()\n\u00a0 \u00a0\u00a0\/\/ GetKey returns a reusable key.\n\u00a0 \u00a0 GetKey() IReusableKey\u00a0\/\/ \u8fd9\u5c31\u662f\u4e0a\u6587\u4e2d\u7684key\u63a5\u53e3\n\u00a0 \u00a0\u00a0\/\/ GetValueBuffer returns a byte buffer with at least sz bytes in length.\n\u00a0 \u00a0 GetValueBuffer(sz\u00a0uint64) []byte\n\u00a0 \u00a0\u00a0\/\/ GetWriteBatch returns a write batch or transaction instance.\n\u00a0 \u00a0 GetWriteBatch()\u00a0interface{}\n\u00a0 \u00a0\u00a0\/\/ SetWriteBatch adds the write batch to the IContext instance.\n\u00a0 \u00a0 SetWriteBatch(wb\u00a0interface{})\n\u00a0 \u00a0\u00a0\/\/ GetEntryBatch returns an entry batch instance.\n\u00a0 \u00a0 GetEntryBatch() pb.EntryBatch\n\u00a0 \u00a0\u00a0\/\/ GetLastEntryBatch returns an entry batch instance.\n\u00a0 \u00a0 GetLastEntryBatch() pb.EntryBatch\n}\n\n\n\n\n\n\n\n\ntype\u00a0context\u00a0struct\u00a0{\n\u00a0 \u00a0 size \u00a0 \u00a0uint64\n\u00a0 \u00a0 maxSize\u00a0uint64\n\u00a0 \u00a0 eb \u00a0 \u00a0 \u00a0pb.EntryBatch\n\u00a0 \u00a0 lb \u00a0 \u00a0 \u00a0pb.EntryBatch\n\u00a0 \u00a0 key \u00a0 \u00a0 *Key\n\u00a0 \u00a0 val \u00a0 \u00a0 []byte\n\u00a0 \u00a0 wb \u00a0 \u00a0 \u00a0kv.IWriteBatch\n}\n\n\nfunc\u00a0(c *context)\u00a0GetKey() IReusableKey {\n\u00a0 \u00a0\u00a0return\u00a0c.key\n}\n\n\nfunc\u00a0(c *context)\u00a0GetValueBuffer(sz\u00a0uint64) []byte\u00a0{\n\u00a0 \u00a0\u00a0if\u00a0sz &lt;= c.size {\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0c.val\n\u00a0 \u00a0 }\n\u00a0 \u00a0 val :=\u00a0make([]byte, sz)\n\u00a0 \u00a0\u00a0if\u00a0sz &lt; c.maxSize {\n\u00a0 \u00a0 \u00a0 \u00a0 c.size = sz\n\u00a0 \u00a0 \u00a0 \u00a0 c.val = val\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0return\u00a0val\n}\n\n\nfunc\u00a0(c *context)\u00a0GetEntryBatch() pb.EntryBatch {\n\u00a0 \u00a0\u00a0return\u00a0c.eb\n}\n\n\nfunc\u00a0(c *context)\u00a0GetLastEntryBatch() pb.EntryBatch {\n\u00a0 \u00a0\u00a0return\u00a0c.lb\n}\n\n\nfunc\u00a0(c *context)\u00a0GetWriteBatch()\u00a0interface{} {\n\u00a0 \u00a0\u00a0return\u00a0c.wb\n}\n\n\nfunc\u00a0(c *context)\u00a0SetWriteBatch(wb\u00a0interface{}) {\n\u00a0 \u00a0 c.wb = wb.(kv.IWriteBatch)\n}<\/code><\/pre>\n<h3>3.3\u5b58\u50a8\u5f15\u64ce\u5c01\u88c5IKVStore<\/h3>\n<p>IKVStore \u662f Dragonboat \u65e5\u5fd7\u5b58\u50a8\u7cfb\u7edf\u7684\u62bd\u8c61\u63a5\u53e3\uff0c\u5b83\u5b9a\u4e49\u4e86\u5e95\u5c42\u952e\u503c\u5b58\u50a8\u5f15\u64ce\u9700\u8981\u5b9e\u73b0\u7684\u6240\u6709\u57fa\u672c\u64cd\u4f5c\u3002\u8fd9\u4e2a\u63a5\u53e3\u8ba9 Dragonboat \u80fd\u591f\u652f\u6301\u4e0d\u540c\u7684\u5b58\u50a8\u540e\u7aef\uff08\u5982 Pebble\u3001RocksDB \u7b49\uff09\uff0c\u5b9e\u73b0\u4e86\u5b58\u50a8\u5f15\u64ce\u7684\u53ef\u63d2\u62d4\u6027\u3002<\/p>\n<pre><code>type\u00a0IKVStore\u00a0interface\u00a0{\n\u00a0 \u00a0\u00a0\/\/ Name is the IKVStore name.\n\u00a0 \u00a0 Name()\u00a0string\n\u00a0 \u00a0\u00a0\/\/ Close closes the underlying Key-Value store.\n\u00a0 \u00a0 Close()\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u8303\u56f4\u626b\u63cf - \u652f\u6301\u524d\u7f00\u904d\u5386\u7684\u8fed\u4ee3\u5668\n\u00a0 \u00a0 IterateValue(fk []byte,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 lk []byte, inc\u00a0bool, op\u00a0func(key []byte, data []byte)\u00a0(bool,\u00a0error))\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u67e5\u8be2\u64cd\u4f5c - \u57fa\u4e8e\u56de\u8c03\u7684\u5185\u5b58\u9ad8\u6548\u67e5\u8be2\u6a21\u5f0f\n\u00a0 \u00a0 GetValue(key []byte, op\u00a0func([]byte)\u00a0error)\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u5199\u5165\u64cd\u4f5c - \u5355\u6761\u8bb0\u5f55\u7684\u539f\u5b50\u5199\u5165\n\u00a0 \u00a0 SaveValue(key []byte, value []byte)\u00a0error\n\n\n\u00a0 \u00a0\u00a0\/\/ \u5220\u9664\u64cd\u4f5c - \u5355\u6761\u8bb0\u5f55\u7684\u7cbe\u786e\u5220\u9664\n\u00a0 \u00a0 DeleteValue(key []byte)\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u83b7\u53d6\u6279\u91cf\u5199\u5165\u5668\n\u00a0 \u00a0 GetWriteBatch() IWriteBatch\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u539f\u5b50\u63d0\u4ea4\u6279\u91cf\u64cd\u4f5c\n\u00a0 \u00a0 CommitWriteBatch(wb IWriteBatch)\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u6279\u91cf\u5220\u9664\u4e00\u4e2a\u8303\u56f4\u7684\u952e\u503c\u5bf9\n\u00a0 \u00a0 BulkRemoveEntries(firstKey []byte, lastKey []byte)\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u538b\u7f29\u6307\u5b9a\u8303\u56f4\u7684\u5b58\u50a8\u7a7a\u95f4\n\u00a0 \u00a0 CompactEntries(firstKey []byte, lastKey []byte)\u00a0error\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \u5168\u91cf\u538b\u7f29\u6574\u4e2a\u6570\u636e\u5e93\n\u00a0 \u00a0 FullCompaction()\u00a0error\n}\n\n\ntype\u00a0IWriteBatch\u00a0interface\u00a0{\n\u00a0 \u00a0 Destroy() \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6e05\u7406\u8d44\u6e90\uff0c\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\n\u00a0 \u00a0 Put(key, value []byte) \u00a0 \u00a0\/\/ \u6dfb\u52a0\u5199\u5165\u64cd\u4f5c\n\u00a0 \u00a0 Delete(key []byte) \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6dfb\u52a0\u5220\u9664\u64cd\u4f5c\n\u00a0 \u00a0 Clear() \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u6e05\u7a7a\u6279\u5904\u7406\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\n\u00a0 \u00a0 Count()\u00a0int\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u83b7\u53d6\u5f53\u524d\u6279\u5904\u7406\u4e2d\u7684\u64cd\u4f5c\u6570\u91cf\n}<\/code><\/pre>\n<p>openPebbleDB\u662fDragonboat \u4e2d Pebble \u5b58\u50a8\u5f15\u64ce\u7684\u521d\u59cb\u5316\u5165\u53e3\uff0c\u8d1f\u8d23\u6839\u636e\u914d\u7f6e\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u53ef\u7528\u7684\u952e\u503c\u5b58\u50a8\u5b9e\u4f8b\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"501\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbqm\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>\/\/ KV is a pebble based IKVStore type.\ntype\u00a0KV\u00a0struct\u00a0{\n\u00a0 \u00a0 db \u00a0 \u00a0 \u00a0 *pebble.DB\n\u00a0 \u00a0 dbSet \u00a0 \u00a0chan\u00a0struct{}\n\u00a0 \u00a0 opts \u00a0 \u00a0 *pebble.Options\n\u00a0 \u00a0 ro \u00a0 \u00a0 \u00a0 *pebble.IterOptions\n\u00a0 \u00a0 wo \u00a0 \u00a0 \u00a0 *pebble.WriteOptions\n\u00a0 \u00a0 event \u00a0 \u00a0*eventListener\n\u00a0 \u00a0 callback kv.LogDBCallback\n\u00a0 \u00a0 config \u00a0 config.LogDBConfig\n}\n\n\nvar\u00a0_ kv.IKVStore = (*KV)(nil)\n\n\n\n\n\/\/ openPebbleDB\n\/\/ =============\n\/\/ \u5c06 Dragonboat \u7684 LogDBConfig \u2192 Pebble \u5f15\u64ce\u5b9e\u4f8b\nfunc\u00a0openPebbleDB(\n\u00a0 \u00a0 \u00a0 \u00a0 cfg \u00a0config.LogDBConfig,\n\u00a0 \u00a0 \u00a0 \u00a0 cb \u00a0 kv.LogDBCallback, \u00a0 \/\/ =&gt; busy\u901a\u77e5\uff1abusy(true\/false)\n\u00a0 \u00a0 \u00a0 \u00a0 dir \u00a0string, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4e3b\u6570\u636e\u76ee\u5f55\n\u00a0 \u00a0 \u00a0 \u00a0 wal \u00a0string, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ WAL \u72ec\u7acb\u76ee\u5f55\uff08\u53ef\u7a7a\uff09\n\u00a0 \u00a0 \u00a0 \u00a0 fs \u00a0 vfs.IFS, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6587\u4ef6\u7cfb\u7edf\u62bd\u8c61\uff08\u78c1\u76d8\/memfs\uff09\n) (kv.IKVStore,\u00a0error) {\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 2\ufe0f\u20e3 &lt;&lt; \u6838\u5fc3\u8c03\u4f18\u53c2\u6570\u8bfb\u5165\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0 blockSz \u00a0 \u00a0 \u00a0:=\u00a0int(cfg.KVBlockSize) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6570\u636e\u5757(4K\/8K\u2026)\n\u00a0 \u00a0 writeBufSz \u00a0 :=\u00a0int(cfg.KVWriteBufferSize) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5199\u7f13\u51b2\n\u00a0 \u00a0 bufCnt \u00a0 \u00a0 \u00a0 :=\u00a0int(cfg.KVMaxWriteBufferNumber) \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ MemTable\u6570\u91cf\n\u00a0 \u00a0 l0Compact \u00a0 \u00a0:=\u00a0int(cfg.KVLevel0FileNumCompactionTrigger)\u00a0\/\/ L0 \u5c42\u6587\u4ef6\u6570\u91cf\u89e6\u53d1\u538b\u7f29\u7684\u9608\u503c\n\u00a0 \u00a0 l0StopWrites :=\u00a0int(cfg.KVLevel0StopWritesTrigger)\n\u00a0 \u00a0 baseBytes \u00a0 \u00a0:=\u00a0int64(cfg.KVMaxBytesForLevelBase)\n\u00a0 \u00a0 fileBaseSz \u00a0 :=\u00a0int64(cfg.KVTargetFileSizeBase)\n\u00a0 \u00a0 cacheSz \u00a0 \u00a0 \u00a0:=\u00a0int64(cfg.KVLRUCacheSize)\n\u00a0 \u00a0 levelMult \u00a0 \u00a0:=\u00a0int64(cfg.KVTargetFileSizeMultiplier) \u00a0\/\/ \u6bcf\u5c42\u6587\u4ef6\u5927\u5c0f\u500d\u6570\n\u00a0 \u00a0 numLevels \u00a0 \u00a0:=\u00a0int64(cfg.KVNumOfLevels)\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 4\ufe0f\u20e3 \u6784\u5efa LSM-tree \u5c42\u7ea7\u9009\u9879 (\u6bcf\u5c42\u65e0\u538b\u7f29)\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0 levelOpts := []pebble.LevelOptions{}\n\u00a0 \u00a0 sz := fileBaseSz\n\u00a0 \u00a0\u00a0for\u00a0lvl :=\u00a00; lvl &lt;\u00a0int(numLevels); lvl++ {\n\u00a0 \u00a0 \u00a0 \u00a0 levelOpts =\u00a0append(levelOpts, pebble.LevelOptions{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Compression: \u00a0 \u00a0pebble.NoCompression,\u00a0\/\/ \u5199\u6027\u80fd\u4f18\u5148\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 BlockSize: \u00a0 \u00a0 \u00a0blockSz,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 TargetFileSize: sz, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ L0 &lt; L1 &lt; \u2026 \u5448\u6307\u6570\u589e\u957f\n\u00a0 \u00a0 \u00a0 \u00a0 })\n\u00a0 \u00a0 \u00a0 \u00a0 sz *= levelMult\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 5\ufe0f\u20e3 \u521d\u59cb\u5316\u4f9d\u8d56\uff1aLRU Cache + \u8bfb\u5199\u9009\u9879\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0 cache := pebble.NewCache(cacheSz) \u00a0 \u00a0\/\/ block\u7f13\u5b58\n\u00a0 \u00a0 ro \u00a0 \u00a0:= &amp;pebble.IterOptions{} \u00a0 \u00a0 \u00a0\u00a0\/\/ \u8fed\u4ee3\u5668\u9ed8\u8ba4\u914d\u7f6e\n\u00a0 \u00a0 wo \u00a0 \u00a0:= &amp;pebble.WriteOptions{Sync:\u00a0true}\u00a0\/\/ \u2757fsync\u5f3a\u5236\u5237\u76d8\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0 opts := &amp;pebble.Options{\n\u00a0 \u00a0 \u00a0 \u00a0 Levels: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0levelOpts,\n\u00a0 \u00a0 \u00a0 \u00a0 Cache: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 cache,\n\u00a0 \u00a0 \u00a0 \u00a0 MemTableSize: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0writeBufSz,\n\u00a0 \u00a0 \u00a0 \u00a0 MemTableStopWritesThreshold: bufCnt,\n\u00a0 \u00a0 \u00a0 \u00a0 LBaseMaxBytes: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 baseBytes,\n\u00a0 \u00a0 \u00a0 \u00a0 L0CompactionThreshold: \u00a0 \u00a0 \u00a0 l0Compact,\n\u00a0 \u00a0 \u00a0 \u00a0 L0StopWritesThreshold: \u00a0 \u00a0 \u00a0 l0StopWrites,\n\u00a0 \u00a0 \u00a0 \u00a0 Logger: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0PebbleLogger,\n\u00a0 \u00a0 \u00a0 \u00a0 FS: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0vfs.NewPebbleFS(fs),\n\u00a0 \u00a0 \u00a0 \u00a0 MaxManifestFileSize: \u00a0 \u00a0 \u00a0 \u00a0\u00a0128\u00a0*\u00a01024\u00a0*\u00a01024,\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ WAL \u76ee\u5f55\u7a0d\u540e\u6761\u4ef6\u6ce8\u5165\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0 kv := &amp;KV{\n\u00a0 \u00a0 \u00a0 \u00a0 dbSet: \u00a0 \u00a0make(chan\u00a0struct{}), \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5173\u95ed-&gt;\u521d\u59cb\u5316\u5b8c\u6210\u4fe1\u53f7\n\u00a0 \u00a0 \u00a0 \u00a0 callback: cb, \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4e0a\u5c42 raft engine \u56de\u8c03\n\u00a0 \u00a0 \u00a0 \u00a0 config: \u00a0 cfg,\n\u00a0 \u00a0 \u00a0 \u00a0 opts: \u00a0 \u00a0 opts,\n\u00a0 \u00a0 \u00a0 \u00a0 ro: \u00a0 \u00a0 \u00a0 ro,\n\u00a0 \u00a0 \u00a0 \u00a0 wo: \u00a0 \u00a0 \u00a0 wo,\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0 event := &amp;eventListener{\n\u00a0 \u00a0 \u00a0 \u00a0 kv: \u00a0 \u00a0 \u00a0kv,\n\u00a0 \u00a0 \u00a0 \u00a0 stopper: syncutil.NewStopper(),\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ =&gt; \u5173\u952e\u4e8b\u4ef6\u89e6\u53d1\n\u00a0 \u00a0 opts.EventListener = pebble.EventListener{\n\u00a0 \u00a0 \u00a0 \u00a0 WALCreated: \u00a0 \u00a0event.onWALCreated,\n\u00a0 \u00a0 \u00a0 \u00a0 FlushEnd: \u00a0 \u00a0 \u00a0event.onFlushEnd,\n\u00a0 \u00a0 \u00a0 \u00a0 CompactionEnd: event.onCompactionEnd,\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 7\ufe0f\u20e3 \u76ee\u5f55\u51c6\u5907\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0if\u00a0wal !=\u00a0&quot;&quot;\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 fs.MkdirAll(wal) \u00a0 \u00a0 \u00a0 \u00a0\/\/ \ud83d\udcc1 \u4e3a WAL \u5355\u72ec\u78c1\u76d8\u9884\u7559\n\u00a0 \u00a0 \u00a0 \u00a0 opts.WALDir = wal\n\u00a0 \u00a0 }\n\u00a0 \u00a0 fs.MkdirAll(dir) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \ud83d\udcc1 \u4e3b\u6570\u636e\u76ee\u5f55\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 8\ufe0f\u20e3 \u771f\u6b63\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u5316\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0 pdb, err := pebble.Open(dir, opts)\n\u00a0 \u00a0\u00a0if\u00a0err !=\u00a0nil\u00a0{\u00a0return\u00a0nil, err }\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0\u00a0\/\/ 9\ufe0f\u20e3 \ud83e\uddf9 \u8d44\u6e90\u6574\u7406 &amp; \u542f\u52a8\u4e8b\u4ef6\n\u00a0 \u00a0\u00a0\/\/--------------------------------------------------\n\u00a0 \u00a0 cache.Unref() \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u53bb\u9664\u591a\u4f59\u5f15\u7528\uff0c\u9632\u6b62\u6cc4\u9732\n\u00a0 \u00a0 kv.db = pdb\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0\/\/ \ud83d\udd14 \u624b\u52a8\u89e6\u53d1\u4e00\u6b21 WALCreated \u786e\u4fdd\u53cd\u538b\u903b\u8f91\u8fdb\u5165\u9996\u6b21\u8f6e\u8be2\n\u00a0 \u00a0 kv.setEventListener(event) \u00a0\/\/ \u5185\u90e8 close(kv.dbSet)\n\u00a0 \u00a0\u00a0\n\u00a0 \u00a0\u00a0return\u00a0kv,\u00a0nil\n}<\/code><\/pre>\n<p>\u5176\u4e2deventListener\u662f\u5bf9pebble \u5185\u5b58\u7e41\u5fd9\u7684\u56de\u8c03\uff0c\u7e41\u5fd9\u5224\u65ad\u7684\u6761\u4ef6\u6709\u4e24\u4e2a\uff1a<\/p>\n<ul>\n<li>\u5185\u5b58\u8868\u5927\u5c0f\u8d85\u8fc7\u9608\u503c\uff0895%\uff09<\/li>\n<li>L0 \u5c42\u6587\u4ef6\u6570\u91cf\u8d85\u8fc7\u9608\u503c\uff08L0\u5199\u5165\u6700\u5927\u6587\u4ef6\u6570\u91cf-1\uff09<\/li>\n<\/ul>\n<pre><code>\n\nfunc\u00a0(l *eventListener)\u00a0notify() {\n\u00a0 \u00a0 l.stopper.RunWorker(func()\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0select\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0case\u00a0&lt;-l.kv.dbSet:\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0l.kv.callback !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 memSizeThreshold := l.kv.config.KVWriteBufferSize *\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 l.kv.config.KVMaxWriteBufferNumber *\u00a019\u00a0\/\u00a020\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 l0FileNumThreshold := l.kv.config.KVLevel0StopWritesTrigger -\u00a01\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 m := l.kv.db.Metrics()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 busy := m.MemTable.Size &gt;= memSizeThreshold ||\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0uint64(m.Levels[0].NumFiles) &gt;= l0FileNumThreshold\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 l.kv.callback(busy)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0default:\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 })\n}<\/code><\/pre>\n<p><img decoding=\"async\" width=\"723\" height=\"646\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbqn\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>3.4\u65e5\u5fd7\u6761\u76ee\u5b58\u50a8DB<\/h3>\n<p>db\u7ed3\u6784\u4f53\u662fDragonboat\u65e5\u5fd7\u6570\u636e\u5e93\u7684\u6838\u5fc3\u7ba1\u7406\u5668\uff0c\u63d0\u4f9bRaft\u65e5\u5fd7\u3001\u5feb\u7167\u3001\u72b6\u6001\u7b49\u6570\u636e\u7684\u6301\u4e45\u5316\u5b58\u50a8\u63a5\u53e3\u3002\u662f\u6865\u63a5\u4e86\u4e1a\u52a1\u548cpebble\u5b58\u50a8\u7684\u4e2d\u95f4\u5c42\u3002<\/p>\n<pre><code>\/\/ db is the struct used to manage log DB.\ntype\u00a0db\u00a0struct\u00a0{\n\u00a0 \u00a0 cs \u00a0 \u00a0 \u00a0*cache \u00a0 \u00a0 \u00a0\u00a0\/\/ \u8282\u70b9\u4fe1\u606f\u3001Raft\u72b6\u6001\u4fe1\u606f\u7f13\u5b58\n\u00a0 \u00a0 keys \u00a0 \u00a0*keyPool \u00a0 \u00a0\u00a0\/\/ Raft\u65e5\u5fd7\u7d22\u5f15\u952e\u53d8\u91cf\u6c60\n\u00a0 \u00a0 kvs \u00a0 \u00a0 kv.IKVStore \u00a0\/\/ pebble\u7684\u5c01\u88c5\n\u00a0 \u00a0 entries entryManager\u00a0\/\/ \u65e5\u5fd7\u6761\u76ee\u8bfb\u5199\u5c01\u88c5\n}\n\n\n\/\/ \u8fd9\u91cc\u9762\u7684\u4fe1\u606f\u4e0d\u4f1a\u8fc7\u671f\uff0c\u53eb\u5bc4\u5b58\u66f4\u5408\u9002\ntype\u00a0cache\u00a0struct\u00a0{\n\u00a0 \u00a0 nodeInfo \u00a0 \u00a0 \u00a0\u00a0map[raftio.NodeInfo]struct{}\n\u00a0 \u00a0 ps \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0map[raftio.NodeInfo]pb.State\n\u00a0 \u00a0 lastEntryBatch\u00a0map[raftio.NodeInfo]pb.EntryBatch\n\u00a0 \u00a0 maxIndex \u00a0 \u00a0 \u00a0\u00a0map[raftio.NodeInfo]uint64\n\u00a0 \u00a0 mu \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sync.Mutex\n}<\/code><\/pre>\n<ul>\n<li><strong>\u83b7\u53d6\u4e00\u4e2a\u6279\u91cf\u5199\u5bb9\u5668<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>func\u00a0(r *db)\u00a0getWriteBatch(ctx IContext) kv.IWriteBatch {\n\u00a0 \u00a0\u00a0if\u00a0ctx !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 wb := ctx.GetWriteBatch()\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0wb ==\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 wb = r.kvs.GetWriteBatch()\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ctx.SetWriteBatch(wb)\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0wb.(kv.IWriteBatch)\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0return\u00a0r.kvs.GetWriteBatch()\n}<\/code><\/pre>\n<p>\u964d\u4f4eGC\u538b\u529b<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u6240\u6709\u8282\u70b9\u4fe1\u606f<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>func\u00a0(r *db)\u00a0listNodeInfo() ([]raftio.NodeInfo,\u00a0error) {\n\u00a0 \u00a0 fk := newKey(bootstrapKeySize,\u00a0nil)\n\u00a0 \u00a0 lk := newKey(bootstrapKeySize,\u00a0nil)\n\u00a0 \u00a0 fk.setBootstrapKey(0,\u00a00)\n\u00a0 \u00a0 lk.setBootstrapKey(math.MaxUint64, math.MaxUint64)\n\u00a0 \u00a0 ni :=\u00a0make([]raftio.NodeInfo,\u00a00)\n\u00a0 \u00a0 op :=\u00a0func(key []byte, data []byte)\u00a0(bool,\u00a0error) {\n\u00a0 \u00a0 \u00a0 \u00a0 cid, nid := parseNodeInfoKey(key)\n\u00a0 \u00a0 \u00a0 \u00a0 ni =\u00a0append(ni, raftio.GetNodeInfo(cid, nid))\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0true,\u00a0nil\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0if\u00a0err := r.kvs.IterateValue(fk.Key(), lk.Key(),\u00a0true, op); err !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0[]raftio.NodeInfo{}, err\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0return\u00a0ni,\u00a0nil\n}<\/code><\/pre>\n<ul>\n<li><strong>\u4fdd\u5b58\u96c6\u7fa4\u72b6\u6001<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>type\u00a0Update\u00a0struct\u00a0{\n\u00a0 \u00a0 ClusterID\u00a0uint64\u00a0\u00a0\/\/ \u96c6\u7fa4ID\uff0c\u6807\u8bc6\u8282\u70b9\u6240\u5c5e\u7684Raft\u96c6\u7fa4\n\u00a0 \u00a0 NodeID \u00a0 \u00a0uint64\u00a0\u00a0\/\/ \u8282\u70b9ID\uff0c\u6807\u8bc6\u96c6\u7fa4\u4e2d\u7684\u5177\u4f53\u8282\u70b9\n\n\n\u00a0 \u00a0 State \u00a0\/\/ \u5305\u542b\u5f53\u524d\u4efb\u671f\uff08Term\uff09\u3001\u6295\u7968\u8282\u70b9\uff08Vote\uff09\u3001\u63d0\u4ea4\u7d22\u5f15\uff08Commit\uff09\u4e09\u4e2a\u5173\u952e\u6301\u4e45\u5316\u72b6\u6001\n\n\n\u00a0 \u00a0 EntriesToSave []Entry \u00a0 \u00a0\/\/ \u9700\u8981\u6301\u4e45\u5316\u5230\u7a33\u5b9a\u5b58\u50a8\u7684\u65e5\u5fd7\u6761\u76ee\n\u00a0 \u00a0 CommittedEntries []Entry\u00a0\/\/ \u5df2\u63d0\u4ea4\u4f4dapply\u7684\u65e5\u5fd7\u6761\u76ee\n\u00a0 \u00a0 MoreCommittedEntries\u00a0bool\u00a0\u00a0\/\/ \u6307\u793a\u662f\u5426\u8fd8\u6709\u66f4\u591a\u5df2\u63d0\u4ea4\u6761\u76ee\u7b49\u5f85\u5904\u7406\n\n\n\u00a0 \u00a0 Snapshot Snapshot \u00a0\/\/ \u5feb\u7167\u5143\u6570\u636e\uff0c\u5f53\u9700\u8981\u5e94\u7528\u5feb\u7167\u65f6\u8bbe\u7f6e\n\n\n\u00a0 \u00a0 ReadyToReads []ReadyToRead \u00a0\/\/ ReadIndex\u673a\u5236\u5b9e\u73b0\u7684\u7ebf\u6027\u4e00\u81f4\u8bfb\n\n\n\u00a0 \u00a0 Messages []Message \u00a0\/\/ \u9700\u8981\u53d1\u9001\u7ed9\u5176\u4ed6\u8282\u70b9\u7684Raft\u6d88\u606f\n\n\n\u00a0 \u00a0 UpdateCommit\u00a0struct\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 Processed \u00a0 \u00a0 \u00a0 \u00a0\u00a0uint64\u00a0\u00a0\/\/ \u5df2\u63a8\u9001\u7ed9RSM\u5904\u7406\u7684\u6700\u540e\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 LastApplied \u00a0 \u00a0 \u00a0\u00a0uint64\u00a0\u00a0\/\/ RSM\u786e\u8ba4\u5df2\u6267\u884c\u7684\u6700\u540e\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 StableLogTo \u00a0 \u00a0 \u00a0\u00a0uint64\u00a0\u00a0\/\/ \u5df2\u7a33\u5b9a\u5b58\u50a8\u7684\u65e5\u5fd7\u5230\u54ea\u4e2a\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 StableLogTerm \u00a0 \u00a0\u00a0uint64\u00a0\u00a0\/\/ \u5df2\u7a33\u5b9a\u5b58\u50a8\u7684\u65e5\u5fd7\u4efb\u671f\n\u00a0 \u00a0 \u00a0 \u00a0 StableSnapshotTo \u00a0uint64\u00a0\u00a0\/\/ \u5df2\u7a33\u5b9a\u5b58\u50a8\u7684\u5feb\u7167\u5230\u54ea\u4e2a\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 ReadyToRead \u00a0 \u00a0 \u00a0\u00a0uint64\u00a0\u00a0\/\/ \u5df2\u51c6\u5907\u597d\u8bfb\u7684ReadIndex\u8bf7\u6c42\u7d22\u5f15\n\u00a0 \u00a0 }\n}\n\n\n\n\nfunc\u00a0(r *db)\u00a0saveRaftState(updates []pb.Update, ctx IContext)\u00a0error\u00a0{\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u6b65\u9aa41\uff1a\u83b7\u53d6\u5199\u5165\u6279\u6b21\u5bf9\u8c61\uff0c\u7528\u4e8e\u6279\u91cf\u64cd\u4f5c\u63d0\u9ad8\u6027\u80fd\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u4f18\u5148\u4ece\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u5df2\u5b58\u5728\u7684\u6279\u6b21\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa\n\u00a0 \u00a0 \u00a0 wb := r.getWriteBatch(ctx)\n\u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u6b65\u9aa42\uff1a\u904d\u5386\u6240\u6709\u66f4\u65b0\uff0c\u5904\u7406\u6bcf\u4e2a\u8282\u70b9\u7684\u72b6\u6001\u548c\u5feb\u7167\n\u00a0 \u00a0 \u00a0\u00a0for\u00a0_, ud :=\u00a0range\u00a0updates {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4fdd\u5b58 Raft \u7684\u786c\u72b6\u6001\uff08Term\u3001Vote\u3001Commit\uff09\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4f7f\u7528\u7f13\u5b58\u673a\u5236\u907f\u514d\u91cd\u590d\u4fdd\u5b58\u76f8\u540c\u72b6\u6001\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 r.saveState(ud.ClusterID, ud.NodeID, ud.State, wb, ctx)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u68c0\u67e5\u662f\u5426\u6709\u5feb\u7167\u9700\u8981\u4fdd\u5b58\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0!pb.IsEmptySnapshot(ud.Snapshot) {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5feb\u7167\u7d22\u5f15\u4e00\u81f4\u6027\u68c0\u67e5\uff1a\u786e\u4fdd\u5feb\u7167\u7d22\u5f15\u4e0d\u8d85\u8fc7\u6700\u540e\u4e00\u4e2a\u65e5\u5fd7\u6761\u76ee\u7684\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u8fd9\u662f Raft \u534f\u8bae\u7684\u91cd\u8981\u7ea6\u675f\uff0c\u9632\u6b62\u72b6\u6001\u4e0d\u4e00\u81f4\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0len(ud.EntriesToSave) &gt;\u00a00\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 lastIndex := ud.EntriesToSave[len(ud.EntriesToSave)-1].Index\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0ud.Snapshot.Index &gt; lastIndex {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 plog.Panicf(&quot;max index not handled, %d, %d&quot;,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ud.Snapshot.Index, lastIndex)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u4fdd\u5b58\u5feb\u7167\u5143\u6570\u636e\u5230\u6570\u636e\u5e93\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 r.saveSnapshot(wb, ud)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u66f4\u65b0\u8282\u70b9\u7684\u6700\u5927\u65e5\u5fd7\u7d22\u5f15\u4e3a\u5feb\u7167\u7d22\u5f15\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 r.setMaxIndex(wb, ud, ud.Snapshot.Index, ctx)\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u6b65\u9aa43\uff1a\u6279\u91cf\u4fdd\u5b58\u6240\u6709\u65e5\u5fd7\u6761\u76ee\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u8fd9\u91cc\u4f1a\u8c03\u7528 entryManager \u63a5\u53e3\u7684 record \u65b9\u6cd5\uff0c\u6839\u636e\u914d\u7f6e\u9009\u62e9\u6279\u91cf\u6216\u5355\u72ec\u5b58\u50a8\u7b56\u7565\n\u00a0 \u00a0 \u00a0 r.saveEntries(updates, wb, ctx)\n\u00a0 \u00a0 \u00a0\u00a0\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u6b65\u9aa44\uff1a\u63d0\u4ea4\u5199\u5165\u6279\u6b21\u5230\u78c1\u76d8\n\u00a0 \u00a0 \u00a0\u00a0\/\/ \u53ea\u6709\u5728\u6279\u6b21\u4e2d\u6709\u5b9e\u9645\u64cd\u4f5c\u65f6\u624d\u63d0\u4ea4\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u78c1\u76d8 I\/O\n\u00a0 \u00a0 \u00a0\u00a0if\u00a0wb.Count() &gt;\u00a00\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0r.kvs.CommitWriteBatch(wb)\n\u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0\u00a0return\u00a0nil\n\u00a0 }\n\u00a0\u00a0\n\u00a0\u00a0<\/code><\/pre>\n<ul>\n<li><strong>\u4fdd\u5b58\u5f15\u5bfc\u4fe1\u606f<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>func\u00a0(r *db)\u00a0saveBootstrapInfo(clusterID\u00a0uint64,\n\u00a0 \u00a0 nodeID\u00a0uint64, bs pb.Bootstrap)\u00a0error\u00a0{\n\u00a0 \u00a0 wb := r.getWriteBatch(nil)\n\u00a0 \u00a0 r.saveBootstrap(wb, clusterID, nodeID, bs)\n\u00a0 \u00a0\u00a0return\u00a0r.kvs.CommitWriteBatch(wb)\u00a0\/\/ \u63d0\u4ea4\u81f3Pebble\n}\n\n\nfunc\u00a0(r *db)\u00a0saveBootstrap(wb kv.IWriteBatch,\n\u00a0 \u00a0 clusterID\u00a0uint64, nodeID\u00a0uint64, bs pb.Bootstrap) {\n\u00a0 \u00a0 k := newKey(maxKeySize,\u00a0nil)\n\u00a0 \u00a0 k.setBootstrapKey(clusterID, nodeID)\u00a0\/\/ \u5e8f\u5217\u5316\u96c6\u7fa4\u8282\u70b9\u4fe1\u606f\n\u00a0 \u00a0 data, err := bs.Marshal()\n\u00a0 \u00a0\u00a0if\u00a0err !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0panic(err)\n\u00a0 \u00a0 }\n\u00a0 \u00a0 wb.Put(k.Key(), data)\n}<\/code><\/pre>\n<ul>\n<li><strong>\u83b7\u53d6Raft\u72b6\u6001<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>func\u00a0(r *db)\u00a0getState(clusterID\u00a0uint64, nodeID\u00a0uint64) (pb.State,\u00a0error) {\n\u00a0 \u00a0 k := r.keys.get()\n\u00a0 \u00a0\u00a0defer\u00a0k.Release()\n\u00a0 \u00a0 k.SetStateKey(clusterID, nodeID)\n\u00a0 \u00a0 hs := pb.State{}\n\u00a0 \u00a0\u00a0if\u00a0err := r.kvs.GetValue(k.Key(),\u00a0func(data []byte)\u00a0error\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0len(data) ==\u00a00\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0raftio.ErrNoSavedLog\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0err := hs.Unmarshal(data); err !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0panic(err)\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0nil\n\u00a0 \u00a0 }); err !=\u00a0nil\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0pb.State{}, err\n\u00a0 \u00a0 }\n\u00a0 \u00a0\u00a0return\u00a0hs,\u00a0nil\n}<\/code><\/pre>\n<h3>3.5\u5bf9\u5916\u5b58\u50a8API\u5b9e\u73b0<\/h3>\n<p>\u9f99\u821f\u5bf9ILogDB\u63d0\u4f9b\u4e86\u5b9e\u73b0\uff1aShardedDB\uff0c\u4e00\u4e2a\u7ba1\u7406\u4e86\u591a\u4e2apebble bucket\u7684\u5b58\u50a8\u5355\u5143\u3002<\/p>\n<pre><code>var\u00a0_ raftio.ILogDB = (*ShardedDB)(nil)\n\/\/ ShardedDB is a LogDB implementation using sharded pebble instances.\ntype\u00a0ShardedDB\u00a0struct\u00a0{\n\u00a0 \u00a0 completedCompactions\u00a0uint64\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u539f\u5b50\u8ba1\u6570\u5668\uff1a\u5df2\u5b8c\u6210\u538b\u7f29\u64cd\u4f5c\u6570\n\u00a0 \u00a0 config \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 config.LogDBConfig\u00a0\/\/ \u65e5\u5fd7\u5b58\u50a8\u914d\u7f6e\n\u00a0 \u00a0 ctxs \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 []IContext \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u5206\u7247\u4e0a\u4e0b\u6587\u6c60\uff0c\u51cf\u5c11GC\u538b\u529b\n\u00a0 \u00a0 shards \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 []*db \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6838\u5fc3\uff1aPebble\u5b9e\u4f8b\u6570\u7ec4\n\u00a0 \u00a0 partitioner \u00a0 \u00a0 \u00a0 \u00a0 \u00a0server.IPartitioner\u00a0\/\/ \u667a\u80fd\u5206\u7247\u7b56\u7565\u5668\n\u00a0 \u00a0 compactionCh \u00a0 \u00a0 \u00a0 \u00a0\u00a0chan\u00a0struct{} \u00a0 \u00a0 \u00a0\/\/ \u538b\u7f29\u4efb\u52a1\u4fe1\u53f7\u901a\u9053\n\u00a0 \u00a0 compactions \u00a0 \u00a0 \u00a0 \u00a0 \u00a0*compactions \u00a0 \u00a0 \u00a0\u00a0\/\/ \u538b\u7f29\u4efb\u52a1\u7ba1\u7406\u5668\n\u00a0 \u00a0 stopper \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0*syncutil.Stopper \u00a0\/\/ \u4f18\u96c5\u5173\u95ed\u7ba1\u7406\u5668\n}<\/code><\/pre>\n<ul>\n<li><strong>\u521d\u59cb\u5316\u8fc7\u7a0b<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>\/\/ \u5165\u53e3\u51fd\u6570\uff1a\u521b\u5efa\u5e76\u521d\u59cb\u5316\u5206\u7247\u65e5\u5fd7\u6570\u636e\u5e93\nOpenShardedDB(config, cb, dirs, lldirs, batched, check, fs, kvf):\n\n\n\u00a0 \u00a0\u00a0\/\/ ===\u9636\u6bb51\uff1a\u5b89\u5168\u9a8c\u8bc1===\n\u00a0 \u00a0\u00a0if\u00a0\u914d\u7f6e\u4e3a\u7a7a then\u00a0panic\n\u00a0 \u00a0\u00a0if\u00a0check\u548cbatched\u540c\u65f6\u4e3atrue\u00a0then\u00a0panic\n\n\n\u00a0 \u00a0\u00a0\/\/ ===\u9636\u6bb52\uff1a\u9884\u5206\u914d\u8d44\u6e90\u7ba1\u7406\u5668===\n\u00a0 \u00a0 shards := \u7a7a\u6570\u7ec4\n\u00a0 \u00a0 closeAll :=\u00a0func(all []*db)\u00a0{\u00a0\/\/\u51fa\u9519\u6e05\u7406\u5de5\u5177\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0for\u00a0s in all {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 s.close()\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 }\n\n\n\u00a0 \u00a0\u00a0\/\/ ===\u9636\u6bb53\uff1a\u9010\u4e2a\u521b\u5efa\u5206\u7247===\n\u00a0 \u00a0 loop i :=\u00a00\u00a0\u2192 \u5206\u7247\u603b\u6570:\n\u00a0 \u00a0 \u00a0 \u00a0 datadir := pathJoin(dirs[i],\u00a0&quot;logdb-&quot;+i) \u00a0\/\/\u6570\u636e\u76ee\u5f55\n\u00a0 \u00a0 \u00a0 \u00a0 snapdir :=\u00a0&quot;&quot;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\u5feb\u7167\u76ee\u5f55(\u53ef\u9009)\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0lldirs\u975e\u7a7a {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 snapdir = pathJoin(lldirs[i],\u00a0&quot;logdb-&quot;+i)\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\n\n\u00a0 \u00a0 \u00a0 \u00a0 shardCb := {shard:i, callback:cb} \u00a0 \u00a0 \u00a0\/\/\u76d1\u63a7\u56de\u8c03\n\u00a0 \u00a0 \u00a0 \u00a0 db, err := openRDB(...) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\u521b\u5efa\u5b9e\u9645\u6570\u636e\u5e93\u5b9e\u4f8b\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0err !=\u00a0nil\u00a0{ \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/\u521b\u5efa\u5931\u8d25\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 closeAll(shards) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/\u6e05\u7406\u5df2\u521b\u5efa\u7684\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0nil, err\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 shards =\u00a0append(shards, db)\n\n\n\u00a0 \u00a0\u00a0\/\/ ===\u9636\u6bb55\uff1a\u6838\u5fc3\u7ec4\u4ef6\u521d\u59cb\u5316===\n\u00a0 \u00a0 partitioner := \u65b0\u5efa\u5206\u533a\u5668(execShards\u6570\u91cf, logdbShards\u6570\u91cf)\n\u00a0 \u00a0 instance := &amp;ShardedDB{\n\u00a0 \u00a0 \u00a0 \u00a0 shards: \u00a0 \u00a0 \u00a0shards,\n\u00a0 \u00a0 \u00a0 \u00a0 partitioner: partitioner,\n\u00a0 \u00a0 \u00a0 \u00a0 compactions: \u65b0\u5efa\u538b\u7f29\u7ba1\u7406\u5668(),\n\u00a0 \u00a0 \u00a0 \u00a0 compactionCh: \u901a\u9053\u7f13\u51b21,\n\u00a0 \u00a0 \u00a0 \u00a0 ctxs: \u00a0 \u00a0 \u00a0\u00a0make([]IContext, \u6267\u884c\u5206\u7247\u6570),\n\u00a0 \u00a0 \u00a0 \u00a0 stopper: \u00a0 \u00a0\u65b0\u5efa\u505c\u6b62\u5668()\n\u00a0 \u00a0 }\n\n\n\u00a0 \u00a0\u00a0\/\/ ===\u9636\u6bb56\uff1a\u9884\u5206\u914d\u4e0a\u4e0b\u6587&amp;\u542f\u52a8\u540e\u53f0===\n\u00a0 \u00a0\u00a0for\u00a0j :=\u00a00\u00a0\u2192 \u6267\u884c\u5206\u7247\u6570:\n\u00a0 \u00a0 \u00a0 \u00a0 instance.ctxs[j] = \u65b0\u5efaContext(saveBufferSize)\n\n\n\u00a0 \u00a0 instance.stopper.RunWorker(func()\u00a0{ \u00a0 \u00a0 \u00a0 \u00a0\/\/\u540e\u53f0\u538b\u7f29\u534f\u7a0b\n\u00a0 \u00a0 \u00a0 \u00a0 instance.compactionWorkerMain()\n\u00a0 \u00a0 })\n\n\n\u00a0 \u00a0\u00a0return\u00a0instance,\u00a0nil\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/\u6784\u9020\u5b8c\u6210\n\u00a0 \u00a0\u00a0<\/code><\/pre>\n<p><img decoding=\"async\" width=\"723\" height=\"532\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbqo\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li><strong>\u4fdd\u5b58\u96c6\u7fa4\u72b6\u6001<\/strong><\/li>\n<\/ul>\n<p>\u5b9e\u73b0\uff1a<\/p>\n<pre><code>func\u00a0(s *ShardedDB)\u00a0SaveRaftState(updates []pb.Update, shardID\u00a0uint64)\u00a0error\u00a0{\n\u00a0 \u00a0\u00a0if\u00a0shardID-1\u00a0&gt;=\u00a0uint64(len(s.ctxs)) {\n\u00a0 \u00a0 \u00a0 \u00a0 plog.Panicf(&quot;invalid shardID %d, len(s.ctxs): %d&quot;, shardID,\u00a0len(s.ctxs))\n\u00a0 \u00a0 }\n\u00a0 \u00a0 ctx := s.ctxs[shardID-1]\n\u00a0 \u00a0 ctx.Reset()\n\u00a0 \u00a0\u00a0return\u00a0s.SaveRaftStateCtx(updates, ctx)\n}\n\n\nfunc\u00a0(s *ShardedDB)\u00a0SaveRaftStateCtx(updates []pb.Update, ctx IContext)\u00a0error\u00a0{\n\u00a0 \u00a0\u00a0if\u00a0len(updates) ==\u00a00\u00a0{\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0nil\n\u00a0 \u00a0 }\n\u00a0 \u00a0 pid := s.getParititionID(updates)\n\u00a0 \u00a0\u00a0return\u00a0s.shards[pid].saveRaftState(updates, ctx)\n}<\/code><\/pre>\n<p><strong>\u4ee5sylas\u4e3a\u4f8b\u5b50\uff0c\u6211\u4eec\u6bcf\u4e2a\u5206\u7247\u90fd\u662f\u5355\u4e00cluster\uff0c\u6240\u4ee5logdb\u53ea\u4f7f\u7528\u4e86\u4e00\u4e2a\u5206\u7247\uff0c\u9f99\u821f\u8bbe\u8ba1\u521d\u8877\u662f\u4e3a\u4e86\u89e3\u653e\u591acluster\u7684\u541e\u5410\uff0c\u6211\u4eec\u6682\u65f6\u7528\u4e0d\u4e0a\uff0ctindb\u53ef\u4ee5\u8003\u8651<\/strong>\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"388\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnbqp\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>LogDB\u662fDragonboat\u91cd\u8981\u7684\u5b58\u50a8\u5c42\u5b9e\u73b0\uff0c\u4f5c\u8005\u5c06Pebble\u5f15\u64ce\u5305\u88c5\u4e3a\u4e00\u7ec4\u901a\u7528\u7b80\u6d01\u7684API\uff0c\u6781\u5927\u65b9\u4fbf\u4e86\u4e0a\u5c42\u5e94\u7528\u4e0e\u5b58\u50a8\u5f15\u64ce\u7684\u4ea4\u4e92\u6210\u672c\u3002<\/p>\n<p>\u5176\u4e2d\u5305\u542b\u4e86\u5f88\u591aGo\u8bed\u8a00\u7684\u6280\u5de7\uff0c\u4f8b\u5982\u5927\u91cf\u7684\u5185\u5b58\u53d8\u91cf\u590d\u7528\u8bbe\u8ba1\uff0c\u5c55\u793a\u4e86\u8fd9\u4e2a\u5e93\u5bf9\u9ad8\u6027\u80fd\u7684\u6781\u81f4\u8ffd\u6c42\uff0c\u662f\u4e00\u4e2a\u5341\u5206\u503c\u5f97\u5b66\u4e60\u7684\u4f18\u79c0\u5de5\u7a0b\u6848\u4f8b\u3002<\/p>\n<h3>\u5f80\u671f\u56de\u987e<\/h3>\n<p>1.\u00a0\u4ece\u6570\u5b57\u5230\u7248\u9762\uff1a\u5f97\u7269\u6570\u636e\u4ea7\u54c1\u91cc\u6570\u5b57\u683c\u5f0f\u5316\u7684\u90a3\u4e9b\u4e8b<\/p>\n<p>2.\u00a0\u4e00\u6587\u89e3\u6790\u5f97\u7269\u81ea\u5efa Redis \u6700\u65b0\u6280\u672f\u6f14\u8fdb<\/p>\n<p>3.\u00a0Golang HTTP\u8bf7\u6c42\u8d85\u65f6\u4e0e\u91cd\u8bd5\uff1a\u6784\u5efa\u9ad8\u53ef\u9760\u7f51\u7edc\u8bf7\u6c42\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>4.\u00a0RN\u4e0ehawk\u78b0\u649e\u7684\u706b\u82b1\u4e4bC++\u5f02\u5e38\u6355\u83b7\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>5.\u00a0\u5f97\u7269TiDB\u5347\u7ea7\u5b9e\u8df5<\/p>\n<h3>\u6587 \/\u9152\u7c73<\/h3>\n<p>\u5173\u6ce8\u5f97\u7269\u6280\u672f\uff0c\u6bcf\u5468\u66f4\u65b0\u6280\u672f\u5e72\u8d27<\/p>\n<p>\u8981\u662f\u89c9\u5f97\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u6b22\u8fce\u8bc4\u8bba\u8f6c\u53d1\u70b9\u8d5e\uff5e<\/p>\n<p>\u672a\u7ecf\u5f97\u7269\u6280\u672f\u8bb8\u53ef\u4e25\u7981\u8f6c\u8f7d\uff0c\u5426\u5219\u4f9d\u6cd5\u8ffd\u7a76\u6cd5\u5f8b\u8d23\u4efb\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047432161\">https:\/\/segmentfault.com\/a\/1190000047432161<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9879\u76ee\u6982\u89c8 Dragonboat \u662f\u7eaf Go \u5b9e\u73b0\u7684\uff08multi-group\uff09Raft \u5e93\u3002 \u4e3a\u5e94\u7528\u5c4f\u853d R [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021093","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021093"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021093\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}