mysql ndb varchar及信号处理

1.概述

以下是对ndb的varchar及其相关设计的理解(基于cluster 7.5版本(mysql5.7)),主要介绍var page及ndb的信号处理机制。由于并没有全面了解(但已经满足了需要)因此有些都是猜测的,若有不正确的欢迎指出。

2. var page介绍

2.1 var page组织方式

把page按剩余可用空间大小分成5个范围,挂在相应的链表下(fragPtr->free_var_page_array,每个fragment都有不同的free_var_page_array)。

void Dbtup::init_list_sizes(void)
{
  c_min_list_size[0]= 200;
  c_max_list_size[0]= 499;

  c_min_list_size[1]= 500;
  c_max_list_size[1]= 999;

  c_min_list_size[2]= 1000;
  c_max_list_size[2]= 4079;

  c_min_list_size[3]= 4080;
  c_max_list_size[3]= 8159;

  c_min_list_size[4]= 0;
  c_max_list_size[4]= 199;
}

var page从一个全局page_pool中分配,大小相同,当free space 变化时会调整page位置,移动到相应范围的链表下(放在头)。
当找不到可用page,重新从page_pool分配。

2.2 var page 结构

ndb把varchar放在var page中,相对应的还有fix page。
var page其格式大致如下图所示:
ndb var page

在var page中space可大致划分为以下几种:used space、free space、delete space、used slot space、empty slot space

Note:

  1. 在ndb中把slot id称为page_idx,在行buffer上用page_no+page_idx来表明varchar data所在位置
  2. var page中data以4byte(称为一个word)对齐。以下的偏移量等变量的单位都为word。

其中:

  • used space用来存储varchar data;
  • free space为未使用空间;
  • delete space为数据删除后不使用的空间,在页重组(数据插入时)后才有可能被使用;
  • used slot space 分配了且在使用的slot,格式如slot 1所示;
  • empty slot space 删除数据后不使用的slot,所有的empty slot 会串成链表以待复用,如图innodb page页中free record list。其内存结构如上图中的slot 3所指结构所示(忽略了其他信息,如free flag没有表示出来)。同时在删数据时有可能调用rebuild index来重构链表。

为了方便管理page,有一些变量用来保存page状态,下面介绍其中一些:

  • insert_pos 下次插入起始位置,uint32
  • high_index 下一次需新分配的slot的id,uint32
  • next_free_index 下一次可使用的slot的id,uint32
  • free_space 总空闲空间,uint32
  • DATA_WORDS 页空间大小,uint32
2.2.1 分配与释放空间

设置slot内存,及各变量

Uint32
Tup_varsize_page::alloc_record(Uint32 alloc_size,                                   Tup_varsize_page* temp, Uint32 chain)
{
  assert(free_space >= alloc_size);
  Uint32 largest_size= DATA_WORDS - (insert_pos + high_index);
  if (alloc_size >= largest_size) {
    /*
      We can't fit this segment between the insert position and the end of
      the index entries. We will pack the page so that all free space
      exists between the insert position and the end of the index entries.
     若不可直接分配,则需要对页进行重组
    */
    reorg(temp);
    largest_size= DATA_WORDS - (insert_pos + high_index);
  }
  assert(largest_size > alloc_size);

  Uint32 page_idx;
  if (next_free_index == END_OF_FREE_LIST) {
    /*
      We are out of free index slots. We will extend the array of free
      slots
      没有empty slot,需新分配
    */
    page_idx= high_index++;
    free_space--;
  } else {
    // Pick an empty slot among the index entries 
    // 使用empty slot
    page_idx= next_free_index;
    assert((get_index_word(page_idx) & FREE) == FREE);
    assert(((get_index_word(page_idx) & PREV_MASK) >> PREV_SHIFT) == END_OF_FREE_LIST);
    Uint32 next = (get_index_word(page_idx) & NEXT_MASK) >> NEXT_SHIFT;
    next_free_index = next;
    assert(next_free_index);
    if (next_free_index != END_OF_FREE_LIST)
    {
      Uint32 *ptr = get_index_ptr(next_free_index);
      Uint32 word = *ptr;
      * ptr = (word & ~PREV_MASK) | (END_OF_FREE_LIST << PREV_SHIFT);
    }
  }

  assert(chain == 0 || chain == CHAIN);
  * get_index_ptr(page_idx) = insert_pos + chain + (alloc_size << LEN_SHIFT);

  insert_pos += alloc_size;
  free_space -= alloc_size;
  //ndbout_c("%p->alloc_record(%d%s) -> %d", this,alloc_size, (chain ? " CHAIN" : ""),page_idx);
  return page_idx;
}

释放空间与之类似:

Tup_varsize_page::free_record(Uint32 page_idx, Uint32 chain)
....
  if (page_idx + 1 == high_index) {
    /*
      We are removing the last in the entry list. We could potentially
      have several free entries also before this. To take that into account
      we will rebuild the free list and thus compress it and update the
      free space accordingly.
      如果释放的slot为最后的slot,则rebuild_index
    */
    rebuild_index(index_ptr);
  } else { //修改slot值及其他变量
    if (next_free_index != END_OF_FREE_LIST)
    {
      Uint32 *ptr = get_index_ptr(next_free_index);
      Uint32 word = *ptr;
      assert(((word & PREV_MASK) >> PREV_SHIFT) == END_OF_FREE_LIST);
      * ptr = (word & ~PREV_MASK) | (page_idx << PREV_SHIFT);
    }
    * index_ptr= FREE | next_free_index | (END_OF_FREE_LIST << PREV_SHIFT);
    next_free_index= page_idx;
    assert(next_free_index);
  }

2.3 重组

把page数据copy另一个tmp_page,page reset,再从tmp_page的data一条条拷到page(从头开始),同时修改slot值。

2.4 rebuild index

从大到小遍历slot移动next_free_index至id最大且id+1不为empty slot的slot,然后继续遍历重新串链表(从大到小)。

3. Ndb 信号处理机制

对于查询并不遵循下列机制,具体如何实现尚不清楚。

3.1 Ndbd

ndbd是单线程模式,对于信号单线程处理。

3.2 Ndbmtd

ndbmtd is a multi-threaded version of ndbd

通过设置MaxNoOfExecutionThreads 来使其执行多线程模式。其值与线程的对于关系可参看下表(从官方文档https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-ndbd-definition.html#ndbparam-ndbmtd-maxnoofexecutionthreads截取):
MaxNoOfExecutionThreads values and the corresponding number of threads by thread type (LQH, TC, Send, Receive).

ndbmtd的多线程主要体现在LDM线程(local query handler - LQH),每个LDM线程都对应着一个分片(fragment),其多线程原理就是在单节点上进行分片。

The number of LDM threads also determines the number of partitions used by an NDB table that is not explicitly partitioned; this is the number of LDM threads times the number of data nodes in the cluster.
These subdirectories each contain a number of files whose names follow the pattern TNFM.Data, where N is a table ID and M is a fragment number. Each data node typically has one primary fragment and one backup fragment. This means that, for an NDB Cluster having 2 data nodes, and with NoOfReplicas equal to 2, M is either 0 to 1. For a 4-node cluster with NoOfReplicas equal to 2, M is either 0 or 2 on node group 1, and either 1 or 3 on node group 2.
When using ndbmtd there may be more than one primary fragment per node. In this case, M is a number in the range of 0 to the number of LQH worker threads in the entire cluster, less 1. The number of fragments on each data node is equal to the number of LQH on that node times NoOfReplicas.

在执行时会通过行记录计算hash值,获取相应的fragment、block、thread,然后把信号插入相应线程的信号队列(各线程从队列读取信号进行处理)。因此同一page的操作在同一线程处理,不需加锁。

从thr_job_queue(thr_jb_read_state)中取出对应信号数据,根据theReceiversBlockNumber获取block实例,处理signal,
在把信号数据插入thr_job_queue时,会根据block info(block,instance)获取thread id,把此信号插入对应thread的thr_job_queue。

处理信号
mt_job_thread_main
    run_job_buffers
        execute_signals


插入信号
Dbtc::tckeyreq050Lab
    EXECUTE_DIRECT(DBDIH, GSN_DIGETNODESREQ, signal,
                 DiGetNodesReq::SignalLength, 0);//获取block等信息
sendSignal
    sendlocal
        block2ThreadId

4.参考

  1. https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-ndbd-definition.html#ndbparam-ndbmtd-maxnoofexecutionthreads
  2. https://dev.mysql.com/doc/refman/5.7/en/mysql-cluster-programs-ndbmtd.html
  3. https://dev.mysql.com/doc/ndb-internals/en/ndb-internals-ndbd-filesystemdir-files.html
  4. src: block/dbtup等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫无之紫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值