当前位置:Gxlcms > 数据库问题 > LevelDB源码之一SkipList

LevelDB源码之一SkipList

时间:2021-07-01 10:21:17 帮助过:3人阅读

为SkipList的关键参数,与性能直接相关。

程序中修改MaxHeight时,在数值变小时,性能上有明显下降,但当数值增大时,甚至增大到10000时,和默认的MaxHeight=12相比仍旧无明显差异,内存使用上也是如此。

看如下代码:

	template<typename Key, class Comparator>
	int SkipList<Key, Comparator>::RandomHeight() {
		// Increase height with probability 1 in kBranching
		static const unsigned int kBranching = 4;
		int height = 1;
		
		while (height < kMaxHeight && ((rnd_.Next() % kBranching) == 0)) {
			height++;
		}
		assert(height > 0);
		assert(height <= kMaxHeight);
		return height;
	}

其中的关键在于粗体的kBranching及(rnd_.Next() % kBranching。这使得上层节点的数量约为下层的1/4。那么,当设定MaxHeight=12时,根节点为1时,约可均匀容纳Key的数量为4^11=4194304(约为400W)。

当单独增大MaxHeight时,并不会使得SkipList的层级提升。MaxHeight=12为经验值,在百万数据规模时,尤为适用。

2. 读写并发

读值本身并不会改变SkipList的结构,因此多个读之间不存在并发问题。

而当读、写同时存在时,SkipList通过AtomicPointer(原子指针)及结构调整上的小技巧达到“无锁”并发。

SkipList<Key, Comparator>::Node

首先,节点一旦被添加到SkipList中,其层级结构将不再发生变化,Node中的唯一成员:port::AtomicPointer next_[1] 大小不会再发生改变。

port::AtomicPointer next_[1];用于站位,实际的数组大小和本节点的Height一致,Node创建代码如下:

1     template<typename Key, class Comparator>
2     typename SkipList<Key, Comparator>::Node*
3         SkipList<Key, Comparator>::NewNode(const Key& key, int height) {
4         char* mem = arena_->AllocateAligned(
5             sizeof(Node) + sizeof(port::AtomicPointer) * (height - 1));
6         return new (mem) Node(key);
7     }

其中,Line4根据height创建真正大小的Node,Line6显示调用构造函数,完成Node创建(这种用法并不常见)。

再来看Node的四个成员函数:

1         // Accessors/mutators for links.  Wrapped in methods so we can
2         // add the appropriate barriers as necessary.
3         Node* Next(int n);
4         void SetNext(int n, Node* x) ;
5 
6         // No-barrier variants that can be safely used in a few locations.
7         Node* NoBarrier_Next(int n);
8         void NoBarrier_SetNext(int n, Node* x);

上面两组为线程安全访问操作,下面两组为非线程安全访问操作。后两组函数是作者追求极致性能时,降低了对封装的要求。

template<typename Key, class Comparator> class SkipList 

读操作时的并发处理主要体现在:使用Next成员函数执行原子的下一条查找动作。

写操作的并发处理稍复杂,下面为Insert代码:

 1     template<typename Key, class Comparator>
 2     void SkipList<Key, Comparator>::Insert(const Key& key) {
 3         // TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()
 4         // here since Insert() is externally synchronized.
 5         Node* prev[kMaxHeight];
 6         Node* x = FindGreaterOrEqual(key, prev);
 7 
 8         // Our data structure does not allow duplicate insertion
 9         assert(x == NULL || !Equal(key, x->key));
10 
11         int height = RandomHeight();
12         if (height > GetMaxHeight()) {
13             for (int i = GetMaxHeight(); i < height; i++) {
14                 prev[i] = head_;
15             }
16             //fprintf(stderr, "Change height from %d to %d\n", max_height_, height);
17 
18             // It is ok to mutate max_height_ without any synchronization
19             // with concurrent readers.  A concurrent reader that observes
20             // the new value of max_height_ will see either the old value of
21             // new level pointers from head_ (NULL), or a new value set in
22             // the loop below.  In the former case the reader will
23             // immediately drop to the next level since NULL sorts after all
24             // keys.  In the latter case the reader will use the new node.
25             max_height_.NoBarrier_Store(reinterpret_cast<void*>(height));
26         }
27 
28         x = NewNode(key, height);
29         for (int i = 0; i < height; i++) {
30             // NoBarrier_SetNext() suffices since we will add a barrier when
31             // we publish a pointer to "x" in prev[i].
32             x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));    //为性能及并发考虑的深度优化,这里的两个NoBarrier
33             prev[i]->SetNext(i, x);
34         }
35     }

15行之前用于查找插入的位置,25行执行了第一个状态变更:设置当前的max_height_。

作者的注释指明了并发读时可能存在的两种情况,但完整描述应该如下:

1. 读到旧的max_height_,而后写线程更新了max_height_并正在进行或完成节点插入

2. 读到新的max_height_,而写线程正在进行或完成节点插入

对于上述两种(其实是多种,这里为细分)情况,作者说明并不存在并发问题,为何呢?

关键在于28-34行插入方式:

28         x = NewNode(key, height);
29         for (int i = 0; i < height; i++) {
30             // NoBarrier_SetNext() suffices since we will add a barrier when
31             // we publish a pointer to "x" in prev[i].
32             x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));    //为性能及并发考虑的深度优化,这里的两个NoBarrier
33             prev[i]->SetNext(i, x);
34         }
关键在哪里?两点:29行的for循环顺序及33行的SetNext.
1. 由最下层向上插入可以保证当前层一旦插入后,其下层状态已经更新。
2. SetNext为原子操作,保证读线程在调用Next查找节点时不存在并发问题
额外需注意的是,32行中,作者为了保证性能最优在x的SetNext及prev的Next均采用了非线程安全的方式。

当然,多个写之间的并发SkipList时非线程安全的,在LevelDB的MemTable中采用了另外的技巧来处理写并发问题。

template<typename Key, class Comparator> class SkipList<Key, Comparator>::Iterator 

SkipList的迭代器,支持双向遍历,其实现本身并无特别之处,只不过是SkipList的一个封装,略。

    Insert:         1252072    Contains:       1296074

LevelDB源码之一SkipList

标签:

人气教程排行