-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1260 lines (459 loc) · 49.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta property="og:type" content="website">
<meta property="og:title" content="Jake's Blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Jake's Blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Jake's Blog">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title>Jake's Blog</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Jake's Blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/19/golang-一致性hash算法/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jake's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/19/golang-一致性hash算法/" itemprop="url">golang-一致性hash算法</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-19T12:56:55+08:00">
2018-08-19
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/18/03-源码解析/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jake's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/18/03-源码解析/" itemprop="url">03-源码解析</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-18T18:54:00+08:00">
2018-08-18
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/etcd/" itemprop="url" rel="index">
<span itemprop="name">etcd</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Raft模块介绍"><a href="#Raft模块介绍" class="headerlink" title="Raft模块介绍"></a>Raft模块介绍</h2><h3 id="Raft结构体介绍"><a href="#Raft结构体介绍" class="headerlink" title="Raft结构体介绍"></a>Raft结构体介绍</h3><p>raft结构体封装了当前节点的所有核心数据</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> raft <span class="keyword">struct</span> {</span><br><span class="line"> id <span class="keyword">uint64</span> <span class="comment">//当前节点在集群中的ID</span></span><br><span class="line"></span><br><span class="line"> Term <span class="keyword">uint64</span> <span class="comment">// 当前任期号</span></span><br><span class="line"> Vote <span class="keyword">uint64</span> <span class="comment">// 当前任期投票给了哪个节点</span></span><br><span class="line"> <span class="comment">// 与只读有关,后面详细介绍</span></span><br><span class="line"> readStates []ReadState</span><br><span class="line"></span><br><span class="line"> raftLog *raftLog <span class="comment">//存放本地log</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 对于当前节点,发送出去的消息,但未收到响应的消息个数上线.</span></span><br><span class="line"> <span class="comment">//如果超数量超过当前阈值,则进制当前节点发送消息.</span></span><br><span class="line"> maxInflight <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 单条消息的最大字节数.</span></span><br><span class="line"> maxMsgSize <span class="keyword">uint64</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// Leader节点记录集群其他节点的日志复制情况.</span></span><br><span class="line"> prs <span class="keyword">map</span>[<span class="keyword">uint64</span>]*Progress</span><br><span class="line"> learnerPrs <span class="keyword">map</span>[<span class="keyword">uint64</span>]*Progress</span><br><span class="line"> <span class="comment">// 当前节点在集群中的状态.可分为4中,分别为StateFollower,</span></span><br><span class="line"> <span class="comment">// StateCandidate,StateLeader,StatePreCandidate</span></span><br><span class="line"> state StateType</span><br><span class="line"> <span class="comment">// 是不是Leader</span></span><br><span class="line"> isLearner <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 在选举过程中,收到节点的投票,如果收到某一个节点的投票,</span></span><br><span class="line"> <span class="comment">//则对应的vote则为true.用来投票数是否过半.</span></span><br><span class="line"> votes <span class="keyword">map</span>[<span class="keyword">uint64</span>]<span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 缓存当前节点等待发送的消息.</span></span><br><span class="line"> msgs []pb.Message</span><br><span class="line"> <span class="comment">// 当前集群中leader节点的ID</span></span><br><span class="line"> lead <span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 用于集群中Leader节点的转移.</span></span><br><span class="line"> leadTransferee <span class="keyword">uint64</span></span><br><span class="line"></span><br><span class="line"> pendingConfIndex <span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 与只读有关,后面详细介绍</span></span><br><span class="line"> readOnly *readOnly</span><br><span class="line"> <span class="comment">// 选举计时器的指针</span></span><br><span class="line"> electionElapsed <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 心跳计时指针</span></span><br><span class="line"> heartbeatElapsed <span class="keyword">int</span></span><br><span class="line"> <span class="comment">//每隔一段时间Leader会发送心跳.如果发现自己没有收到</span></span><br><span class="line"> <span class="comment">//足够的心跳响应.则会切换为Follower</span></span><br><span class="line"> checkQuorum <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 选举之前会询问所有Follower是否进行选举,如果一半以上的</span></span><br><span class="line"> <span class="comment">// Follower能够正常的收到Leader的心跳,则会拒绝参选.反之</span></span><br><span class="line"> <span class="comment">// 则会举行新的选举.</span></span><br><span class="line"> preVote <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 心跳超时时间</span></span><br><span class="line"> heartbeatTimeout <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 选举超时时间</span></span><br><span class="line"> electionTimeout <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 该值是electionTimeout~2 * electionTimeout -1之间的随机值.</span></span><br><span class="line"> <span class="comment">// 也是选举计时器的上限,如果electionElapased超过该值,则超时.</span></span><br><span class="line"> randomizedElectionTimeout <span class="keyword">int</span></span><br><span class="line"> disableProposalForwarding <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 当前节点推进逻辑时钟的函数</span></span><br><span class="line"> tick <span class="function"><span class="keyword">func</span><span class="params">()</span></span></span><br><span class="line"><span class="function"> // 当前节点收到消息时的处理函数</span></span><br><span class="line"><span class="function"> <span class="title">step</span> <span class="title">stepFunc</span></span></span><br><span class="line"><span class="function"> <span class="title">logger</span> <span class="title">Logger</span></span></span><br><span class="line"><span class="function">}</span></span><br></pre></td></tr></table></figure>
<p>目前先理解各个组件,后面介绍raft结构体的绑定方法.</p>
<h3 id="Config结构体介绍"><a href="#Config结构体介绍" class="headerlink" title="Config结构体介绍"></a>Config结构体介绍</h3><p>config结构体主要用于配置参数的传递,在创建raft实例时所需的参数会通过Config实例进行传递.<br><figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Config <span class="keyword">struct</span> {</span><br><span class="line"> <span class="comment">// 当前节点ID</span></span><br><span class="line"> ID <span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 记录集群中所有节点的ID</span></span><br><span class="line"> peers []<span class="keyword">uint64</span></span><br><span class="line"></span><br><span class="line"> learners []<span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 用于初始化raft.electionTimeout,就是逻辑时间钟进多少次,</span></span><br><span class="line"> <span class="comment">// 会触发新的Leader选举.</span></span><br><span class="line"> ElectionTick <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 用于初始化raft.headtbeatTime, 就是逻辑时钟推进多少次,</span></span><br><span class="line"> <span class="comment">// 就可以收到Leader心跳.</span></span><br><span class="line"> HeartbeatTick <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 当前节点保存raft日志使用的存储.</span></span><br><span class="line"> Storage Storage</span><br><span class="line"> <span class="comment">// 当前已经应用的记录位置.该值在节点启动时需要设置,</span></span><br><span class="line"> <span class="comment">// 否则会重新应用已用过的Entry记录</span></span><br><span class="line"> Applied <span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 用于初始化raft.maxMsgSize,如果是math.MaxUint64则没</span></span><br><span class="line"> <span class="comment">// 有上限,如果为0,则每一条消息只能带一个Entry</span></span><br><span class="line"> MaxSizePerMsg <span class="keyword">uint64</span></span><br><span class="line"> <span class="comment">// 用于初始化raft.maxInflighyMsgs,已经发送出去的消息,</span></span><br><span class="line"> <span class="comment">// 单位收到响应的最大值</span></span><br><span class="line"> MaxInflightMsgs <span class="keyword">int</span></span><br><span class="line"> <span class="comment">// 初始化raft.CheckQuorum模式</span></span><br><span class="line"> CheckQuorum <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 初始化rafy.PreVote字段</span></span><br><span class="line"> PreVote <span class="keyword">bool</span></span><br><span class="line"> <span class="comment">// 与只读有关,后面详细介绍</span></span><br><span class="line"> ReadOnlyOption ReadOnlyOption</span><br><span class="line"></span><br><span class="line"> Logger Logger</span><br><span class="line"></span><br><span class="line"> DisableProposalForwarding <span class="keyword">bool</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>没有方法可以介绍</p>
<h3 id="Storage接口及其实现"><a href="#Storage接口及其实现" class="headerlink" title="Storage接口及其实现"></a>Storage接口及其实现</h3><p>在raft模块中,定义了Storage接口以及其实现,目的就是存储当前节点收到的Entry记录.<br><figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// HardState结构</span></span><br><span class="line"><span class="keyword">type</span> HardState <span class="keyword">struct</span> {</span><br><span class="line"> Term <span class="keyword">uint64</span> <span class="string">`protobuf:"varint,1,opt,name=term" json:"term"`</span></span><br><span class="line"> Vote <span class="keyword">uint64</span> <span class="string">`protobuf:"varint,2,opt,name=vote" json:"vote"`</span></span><br><span class="line"> Commit <span class="keyword">uint64</span> <span class="string">`protobuf:"varint,3,opt,name=commit" json:"commit"`</span></span><br><span class="line"> XXX_unrecognized []<span class="keyword">byte</span> <span class="string">`json:"-"`</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// ConfState结构</span></span><br><span class="line"><span class="keyword">type</span> ConfState <span class="keyword">struct</span> {</span><br><span class="line"> Nodes []<span class="keyword">uint64</span> <span class="string">`protobuf:"varint,1,rep,name=nodes" json:"nodes,omitempty"`</span></span><br><span class="line"> Learners []<span class="keyword">uint64</span> <span class="string">`protobuf:"varint,2,rep,name=learners" json:"learners,omitempty"`</span></span><br><span class="line"> XXX_unrecognized []<span class="keyword">byte</span> <span class="string">`json:"-"`</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> Storage <span class="keyword">interface</span> {</span><br><span class="line"> <span class="comment">// 集群中的每个几点都应该保存一些基本主要信息</span></span><br><span class="line"> <span class="comment">// HardState存储以上主要信息</span></span><br><span class="line"> <span class="comment">// ConfState存储集群所有ID(及Nodes)</span></span><br><span class="line"> InitialState() (pb.HardState, pb.ConfState, error)</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Entries返回指定范围([lo, hi])的entry记录</span></span><br><span class="line"> <span class="comment">// maxSize限制返回Entry集合的字数上线</span></span><br><span class="line"> Entries(lo, hi, maxSize <span class="keyword">uint64</span>) ([]pb.Entry, error)</span><br><span class="line"></span><br><span class="line"> <span class="comment">// FirstIndex 返回SnapShot(快照)后的第一个Entry的索引值</span></span><br><span class="line"> FirstIndex() (<span class="keyword">uint64</span>, error)</span><br><span class="line"></span><br><span class="line"> <span class="comment">// LastIndex返回记录中最后一条Entry记录的索引值</span></span><br><span class="line"> LastIndex() (<span class="keyword">uint64</span>, error)</span><br><span class="line"> <span class="comment">// 返回最后一次快照的数据</span></span><br><span class="line"> Snapshot() (pb.Snapshot, error)</span><br><span class="line"> <span class="comment">// 查询Index对应的Term值</span></span><br><span class="line"> Term(i <span class="keyword">uint64</span>) (<span class="keyword">uint64</span>, error)</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h4 id="MemoryStorage"><a href="#MemoryStorage" class="headerlink" title="MemoryStorage"></a>MemoryStorage</h4><p>MemoryStorage是在内存中维护上述状态比如状态信息(hardState信息),数据快照(snapshot字段),以及所有的entries.<br><figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> MemoryStorage <span class="keyword">struct</span> {</span><br><span class="line"> sync.Mutex</span><br><span class="line"></span><br><span class="line"> hardState pb.HardState</span><br><span class="line"> snapshot pb.Snapshot</span><br><span class="line"></span><br><span class="line"> ents []pb.Entry</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/18/02-Raft简介/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jake's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/18/02-Raft简介/" itemprop="url">02-Raft简介</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-18T17:32:07+08:00">
2018-08-18
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/etcd/" itemprop="url" rel="index">
<span itemprop="name">etcd</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Raft协议"><a href="#Raft协议" class="headerlink" title="Raft协议"></a>Raft协议</h2><p>raft协议是解决分布式一致性问题的协议.</p>
<h3 id="工作状态简介"><a href="#工作状态简介" class="headerlink" title="工作状态简介"></a>工作状态简介</h3><p>Raft协议的工作模式是一个Leader节点和多个Follower节点的模式.在Raft协议中,每个节点都维护了一个状态机,该状态机有三个状态.</p>
<ul>
<li>Leader: Leader节点负责所有的客户端请求,当收到客户端的写入请求后,Leader节点会在本地写入一条相应的日志.然后将其封装成Message发送给其他的Follower,Follower收到后会给Leader一个消息回执,当Leader收到一半以上的成功的消息回执后,Leader则认为该条Message已提交.Leader还会处理客户端的读请求.还会定时向Folloer发送健康心跳(主要防止Follower进行下一个term选举).</li>
<li>Candidate: 是Follower长时间未收到Leader的心跳,会自动的从Follower状态变为Candidate状态.并且进入下一个任期(Trem),进行新的选举Leader.</li>
<li>Follower: Follower只是响应一个Leader,Candidate的请求.当Follower收到客户端的请求后,会转发给Leader.</li>
</ul>
<p>Term介绍: Term是任期的意思.可以理解为下一届的意思.当Follower切换状态为Candidate时,Term进行加一.</p>
<h3 id="Leader选举时间"><a href="#Leader选举时间" class="headerlink" title="Leader选举时间"></a>Leader选举时间</h3><p>在Raft协议中两个时间控制Leader的选举.一个选举超时时间(election time),另一个为心跳超时时间.</p>
<ul>
<li>选举超时时间: 每个Follower收不到Leader的心跳后,不会立刻进行选举,而是等待一段时间后才切换为Candidate进行选举(哪个节点醒来的早,则哪个节点变为Candidate),这段时间称之为选举超时时间.election time默认的范围是150~300ms之间.</li>
<li>心跳超时时间: 也就是Leader向所有Follower发送心跳的时间.心跳超时时间远小于选举超时时间.</li>
</ul>
<p>准确来说选举超时时间控制着Leader的选举.当获取投票数量大于总结点数量一半时会从candidate状态变为Leader状态.</p>
<p></p>
<h3 id="选举Leader时可能出现的情况"><a href="#选举Leader时可能出现的情况" class="headerlink" title="选举Leader时可能出现的情况"></a>选举Leader时可能出现的情况</h3><h4 id="两个Follower同时苏醒-及选举超时时间相同"><a href="#两个Follower同时苏醒-及选举超时时间相同" class="headerlink" title="两个Follower同时苏醒(及选举超时时间相同)"></a>两个Follower同时苏醒(及选举超时时间相同)</h4><p>比如说当前有4个节点,进入了新的Term,当两个Follower同时进入Candidate状态时,首先他们都会给自己先投一票,然后向其他节点发送一个request_vote请求,让其他的Follower对他们进行投票.获取的票数一样.则当前Term失效,进入下一次选举.</p>
<h4 id="Candidate的Term小于其他节点时"><a href="#Candidate的Term小于其他节点时" class="headerlink" title="Candidate的Term小于其他节点时"></a>Candidate的Term小于其他节点时</h4><p>这种情况也是存在的,比如说当前有A,B,C,D四个节点,当在新的一个选举的Term中,B变为Candidate,C也是Candidate,B,C都有投票,以及让别人投票的权利,如果B收到C的投票请求后,发现C的Term大于自己的Term,则会自动切换为Follower状态.</p>
<h4 id="脑裂情况"><a href="#脑裂情况" class="headerlink" title="脑裂情况"></a>脑裂情况</h4><p>当Leader发生宕机的情况时,会有一个Follower的选举超时时间最先超时,他会切换状态为Candidate,并且自身Term加一,向自己进行投票,随后发送request_vote,请求其他Follower向他进行投票,如果获取票数大于总节点数一般以上,它会变为当界的Leader.</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/16/01-etcd简介/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jake's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/16/01-etcd简介/" itemprop="url">01-etcd简介</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-16T02:09:04+08:00">
2018-08-16
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/etcd/" itemprop="url" rel="index">
<span itemprop="name">etcd</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p>etcd 是一个可靠的分布式KV存储系统, 底层实现Raft协议, 主要用于共享配置和服务发现.</p>
<h3 id="zookeeper比较"><a href="#zookeeper比较" class="headerlink" title="zookeeper比较"></a>zookeeper比较</h3><p>etcd是唯一一个可以和zookeeper媲美的组件,下列列出etcd比zoopkeeper的一些优点:</p>
<ul>
<li>一致性协议: etcd是Raft一致性协议来实现分布式一致性.zookeeper是ZAB协议,ZAB协议是一种类Paxos协议.目前公认Raft协议比Paxos协议易于理解,易于工作施展.</li>
<li>API接口: etcd的V2版本提供了HTTP+Json的调用方式.在etcd的V3中的客户端则使用GRPC进行远程调用.</li>
<li>性能: 官方提供的测试基准数据,etcd每秒的写入量可达1000+次,优于zoopkeeper.</li>
<li>安全性:etcd提供TLS访问, 而zookeeper在权限访问方面略显粗糙.</li>
</ul>
<h3 id="源码模块的主要功能说明"><a href="#源码模块的主要功能说明" class="headerlink" title="源码模块的主要功能说明"></a>源码模块的主要功能说明</h3><ul>
<li>raft: Raft协议的核心实现.其中只实现了基本的Raft协议.并未提供实现网络层相关内容.</li>
<li>raft-http: Raft协议中各个节点发送消息时使用的网络层协议.该模块与raft模块紧密相关.</li>
<li>wal 和 snap: WAL日志和快照存储相关的实现.</li>
<li>store: etcd中的v2版本存储实现.v2版本的存储是完全的内存实现.</li>
<li>mvcc: etcd中的v3版本存储实现.v3版本底层使用BoltDB实现持久换存储.</li>
<li>lease: 租约相关实现.</li>
<li>auth 和 alarm: 权限和警报相关实现.</li>
<li>etcdserver: etcd服务端相关实现.它会基于上述模块提供的功能,对外提供一个etcd节点的完整功能.</li>
<li>client: v2版本客户端的实现. etcd的V2版本提供了HTTP+Json的调用方式与服务端进行交互.</li>
<li>clientv3: v3版本的客户端实现. etcd的V3的客户端则通过GRPC的方式与服务端进行交互.</li>
</ul>
<h3 id="下载etcd"><a href="#下载etcd" class="headerlink" title="下载etcd"></a>下载etcd</h3><p>go get github.com/coreos/etcd/cmd/etcd</p>
<h3 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h3><p>etcd提供了单机模式和集群模式.默认配置运行时, etcd会监听本地的2379<br>和2380两个端口(2379主要与客户端进行交互,2380主要与etcd内部节点进行交互).</p>
<p>下面在一台机器上搭建3个etcd节点,使每个节点监听不同的端口,形成伪分布式集群.<br>以下启动的方式,属于静态方式.这种方式会预先将集群的各个节点的配置信息设置好,然后逐个启动,形成集群.</p>
<p>etcd –name infra0 –initial-adverties-peer-urls <a href="http://127.0.0.1:2380" target="_blank" rel="noopener">http://127.0.0.1:2380</a> \<br>–listen-peer-urls <a href="http://127.0.0.1:2380" target="_blank" rel="noopener">http://127.0.0.1:2380</a> \<br>–listen-client-urls <a href="http://127.0.0.1:2379" target="_blank" rel="noopener">http://127.0.0.1:2379</a> \<br>–advertise-client-urls <a href="http://127.0.0.1:2379" target="_blank" rel="noopener">http://127.0.0.1:2379</a> \<br>–initial-cluster-token etcd-cluster-1 \<br>–initial-cluster infra0=<a href="http://127.0.0.1:2380" target="_blank" rel="noopener">http://127.0.0.1:2380</a>, infra1=<a href="http://127.0.0.1:2382,infar2=http://127.0.0.1:2384" target="_blank" rel="noopener">http://127.0.0.1:2382,infar2=http://127.0.0.1:2384</a> \<br>–initial-cluster-state new</p>
<p>etcd –name infra1 –initial-adverties-peer-urls <a href="http://127.0.0.1:2382" target="_blank" rel="noopener">http://127.0.0.1:2382</a> \<br>–listen-peer-urls <a href="http://127.0.0.1:2382" target="_blank" rel="noopener">http://127.0.0.1:2382</a> \<br>–listen-client-urls <a href="http://127.0.0.1:2381" target="_blank" rel="noopener">http://127.0.0.1:2381</a> \<br>–advertise-client-urls <a href="http://127.0.0.1:2381" target="_blank" rel="noopener">http://127.0.0.1:2381</a> \<br>–initial-cluster-token etcd-cluster-1 \<br>–initial-cluster infra0=<a href="http://127.0.0.1:2380" target="_blank" rel="noopener">http://127.0.0.1:2380</a>, infra1=<a href="http://127.0.0.1:2382,infar2=http://127.0.0.1:2384" target="_blank" rel="noopener">http://127.0.0.1:2382,infar2=http://127.0.0.1:2384</a> \<br>–initial-cluster-state new</p>
<p>etcd –name infra2 –initial-adverties-peer-urls <a href="http://127.0.0.1:2384" target="_blank" rel="noopener">http://127.0.0.1:2384</a> \<br>–listen-peer-urls <a href="http://127.0.0.1:2384" target="_blank" rel="noopener">http://127.0.0.1:2384</a> \<br>–listen-client-urls <a href="http://127.0.0.1:2383" target="_blank" rel="noopener">http://127.0.0.1:2383</a> \<br>–advertise-client-urls <a href="http://127.0.0.1:2383" target="_blank" rel="noopener">http://127.0.0.1:2383</a> \<br>–initial-cluster-token etcd-cluster-1 \<br>–initial-cluster infra0=<a href="http://127.0.0.1:2380" target="_blank" rel="noopener">http://127.0.0.1:2380</a>, infra1=<a href="http://127.0.0.1:2382,infar2=http://127.0.0.1:2384" target="_blank" rel="noopener">http://127.0.0.1:2382,infar2=http://127.0.0.1:2384</a> \<br>–initial-cluster-state new</p>
<h3 id="命令参数简介"><a href="#命令参数简介" class="headerlink" title="命令参数简介"></a>命令参数简介</h3><ul>
<li>–name: etcd集群中的节点名称设置, 在同一个集群内必须是唯一的.</li>
<li>–listen-peer-urls: 用于集群内部各个节点之间的通信的url地址.每个节点可以监听多个url地址.集群内部将通过这些URL地址进行交互,例如:Leader选举,Message传输.</li>
<li>–initial-advertise-peer-urls: 建议用于集群内部节点之间交互的URL地址,节点以该值进行通信.</li>
<li>–listen-client-urls: 用于当前节点与客户端交互的URL.每个节点同样可以有提供多个URL.</li>
<li>–advertise-client-urls: 建议客户端使用的URL地址,该值用于etcd代理或者etcd成员与etcd节通信.</li>
<li>–initial-cluster-token etcd-culster-1:集群唯一标识.</li>
<li>–initial-cluster: 集群中所有的initial-advertise-peers-urls的合集.</li>
<li>–initial-cluster-state new: 新建集群的标识.</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/16/golang-search-algorithm/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jake's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/16/golang-search-algorithm/" itemprop="url">golang-search-algorithm</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-16T02:03:52+08:00">
2018-08-16
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/golang/" itemprop="url" rel="index">
<span itemprop="name">golang</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h3><p>二分查找,也成折半搜索,对数搜索, 是在有序数组查找某一个特定的值</p>
<!--noindex-->
<div class="post-button text-center">
<a class="btn" href="/2018/08/16/golang-search-algorithm/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
</section>
<nav class="pagination">
<span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
</nav>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>