Skip to content

Commit

Permalink
Site updated: 2023-02-24 15:51:47
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonm3n committed Feb 24, 2023
1 parent 2560a6c commit c4f5ff1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 8 additions & 4 deletions 2022/02/03/tinykv-project2-RaftKV-PartB/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,27 @@
<p>TinyKV Project2 Part B, 利用 Raft 模块构建容错的 KV 存储服务.</p>
<span id="more"></span>

<h2 id="Basic"><a href="#Basic" class="headerlink" title="Basic"></a>Basic</h2><p>在 Project2 Part A 中我们实现了 Raft 算法的 Leader election 和 Log replication 部分. 也实现了 Ready 相关的几个函数, 能够抽取 Raft 层的变更信息. 整个 2A 的主要工作都是在 Raft 层. 在 Project2 Part B 中, 我们把 KV 存储引擎用作 Raft 中的状态机, 将 Client 的请求作为日志, 扩展出一个容错的 KV 存储服务.</p>
<h2 id="Basic"><a href="#Basic" class="headerlink" title="Basic"></a>Basic</h2><p>在 Project2A 中, 我们实现了 Raft 算法的两个重要部分: Leader election 和 Log replication. 此外,我们还实现了几个处理 Ready 相关的函数,以提取 Raft 层的变更信息. 总体来说, 2A 的主要工作都是在 Raft 层.<br>在 Project2B 中, 我们将 KV 存储引擎作为 Raft 中的状态机, 将 Client 的请求作为日志, 扩展出一个基于 Raft 的容错 KV 存储服务.</p>
<h2 id="TinyKV-架构"><a href="#TinyKV-架构" class="headerlink" title="TinyKV 架构"></a>TinyKV 架构</h2><p>在开始 Project2B 之前, 我们需要了解一下 TinyKV 的架构, 以便后续更好地进行代码实现.</p>
<p>首先需要了解几个基本的概念:</p>
<ul>
<li>Store: 一个 tinykv-server.</li>
<li>Peer: tinykv-server 中运行的一个 Raft node, 一个 Store 上可能运行有多个 Peer, 每个 Peer 所属于不同的 Region, 在 2B 中, 只会涉及到一个 Region.</li>
<li>Region: Raft group.</li>
<li>Region: 一个 Raft group.</li>
</ul>
<p><img src="https://moonm3n-img.oss-cn-chengdu.aliyuncs.com/img/Xnip2023-02-05_16-56-22.jpg" alt="TinyKV 2B 集群架构"></p>
<p>细化到代码层面 Raftstore</p>
<p>raftWorker 不断地从 raftCh 中 poll 消息, 下发到 Raft 模块, 再从 raft 模块中 获取 ready 并进行消息发送、状态持久化、将日志应用到状态机等. 最后返回 response.</p>
<h2 id="Implement-peer-storage"><a href="#Implement-peer-storage" class="headerlink" title="Implement peer storage"></a>Implement peer storage</h2><p>在这一步中, 我们需要将 Raft 层的状态持久化.</p>
<p>Raft 论文中提到的需要持久化的状态有:</p>
<ol>
<li>currentTerm: 节点当前的任期, 既 HardState 中的 Term.</li>
<li>votedFor: 节点给谁投了票, 既 HardState 中的 Vote. 如果不持久化 Vote, 节点投票后重启会产生一个节点在一个 Term 中投出两张票的现象.</li>
<li>log[]: 节点当前的日志.</li>
</ol>
<p>与 Raft 论文不同, tinykv 中还需要将 Commit 和 Region 信息持久化. tinykv 中的绝大多数 request 都是幂等的.</p>
<p>与 Raft 论文不同, TinyKV 中还需要将 Commit 和 Region 信息持久化. TinyKV 中的绝大多数 request 都是幂等的.</p>
<p>具体到代码中, 我们需要实现 SaveReadyState() 与 Append() 两个函数, 2B 中并不涉及快照, ApplySnapshot() 函数暂时不需要实现.</p>
<h2 id="Implement-Raft-ready-process"><a href="#Implement-Raft-ready-process" class="headerlink" title="Implement Raft ready process"></a>Implement Raft ready process</h2><p>HandleRaftReady</p>
<h3 id="SaveReadyState"><a href="#SaveReadyState" class="headerlink" title="SaveReadyState()"></a>SaveReadyState()</h3><h2 id="Implement-Raft-ready-process"><a href="#Implement-Raft-ready-process" class="headerlink" title="Implement Raft ready process"></a>Implement Raft ready process</h2><p>4<br>HandleRaftReady</p>
<ol>
<li>get the ready from Raft module.</li>
<li>persisting log entries.</li>
Expand Down
Loading

0 comments on commit c4f5ff1

Please sign in to comment.