5.2 数据的写入流程

  1. Client先访问 zookeeper,从 meta 表读取 region 的位置,然后读取 meta 表中的数据。meta 中存储了用户表的 region 信息;

  2. 根据namespace、表名和 rowkey 在 meta 表中找到对应的 region 信息;

  3. 找到这个 region 对应的 regionserver;

  4. Client 向 HregionServer 发送写(put)请求;

  5. HregionServer 将数据写到 HLog(write ahead log)。为了数据的持久化和恢复;

  6. HregionServer 将数据写到内存(MemStore);

  7. 将 WAL 数据落盘(同步到HDFS);

  8. 如果在同步 WAL 到 HDFS 过程中发生异常,则MemStore中的数据会删除(rollBack);

  9. 反馈Client写成功。

      // ------------------------------------
      // STEP 1. Try to acquire as many locks as we can, and ensure
      // we acquire at least one.
      // ----------------------------------

      // ------------------------------------
      // STEP 2. Update any LATEST_TIMESTAMP timestamps
      // ----------------------------------

      // ------------------------------------
      // STEP 2. Update any LATEST_TIMESTAMP timestamps
      // ----------------------------------

      // -------------------------
      // STEP 4. Append the final edit to WAL. Do not sync wal.
      // -------------------------

      // ------------------------------------
      // STEP 5. Write back to memstore
      // Write to memstore. It is ok to write to memstore
      // first without syncing the WAL because we do not roll
      // forward the memstore MVCC. The MVCC will be moved up when
      // the complete operation is done. These changes are not yet
      // visible to scanners till we update the MVCC. The MVCC is
      // moved only when the sync is complete.
      // ----------------------------------

      // -------------------------------
      // STEP 6. Release row locks, etc.
      // -------------------------------

      // -------------------------
      // STEP 7. Sync wal.
      // -------------------------

      // ------------------------------------------------------------------
      // STEP 8. Advance mvcc. This will make this put visible to scanners and getters.
      // ------------------------------------------------------------------

      // ------------------------------------------------------------------
      // STEP 8. Advance mvcc. This will make this put visible to scanners and getters.
      // ------------------------------------------------------------------

MVCC(Multi-Version Concurrency Control) 多版本并发控制,MVCC 是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问;在编程语言中实现事务内存。


关于 WAL

HBase 中,WAL 的实现类为 HLog,每个 Region Server 一个 HLog 日志,所有region 的写入都是写到同一个HLog。下图表示同一个Region Server中的3个 region 共享一个HLog。当数据写入时,是将数据对<HLogKey,WALEdit>按照顺序追加到 HLog 中,以获取最好的写入性能。

上图中 HLogKey 主要存储了log sequence number,更新时间 write timeregion name,表名table name以及cluster ids

region name和table name分别表征该段日志属于哪个region以及哪张表;cluster ids用于将日志复制到集群中其他机器上。

Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2019-03-26 08:52:09

results matching ""

    No results matching ""