-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra_readings.html
394 lines (251 loc) · 16.2 KB
/
cassandra_readings.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
<!DOCTYPE HTML>
<html lang="en-US" >
<head>
<meta charset="UTF-8">
<title>Cassandra启动 | Cassandra</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 1.0.3">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="./cassandra/README.html" />
<link rel="prev" href="./index.html" />
</head>
<body>
<link rel="stylesheet" href="gitbook/style.css">
<div class="book" data-level="1" data-basepath="." data-revision="1417758529365">
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Cassandra Readins
</a>
</li>
<li class="chapter active" data-level="1" data-path="cassandra_readings.html">
<a href="./cassandra_readings.html">
<i class="fa fa-check"></i>
<b>1.</b>
Cassandra启动
</a>
</li>
<li class="chapter " data-level="2" data-path="cassandra/README.html">
<a href="./cassandra/README.html">
<i class="fa fa-check"></i>
<b>2.</b>
Cassandra查询过程
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="cassandra/columnfamilystore.html">
<a href="./cassandra/columnfamilystore.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
创建ColumnFamilyStore
</a>
</li>
<li class="chapter " data-level="2.2" data-path="cassandra/bloom_filter.html">
<a href="./cassandra/bloom_filter.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Bloom Filter实现
</a>
</li>
<li class="chapter " data-level="2.3" data-path="cassandra/cassandra_cache.html">
<a href="./cassandra/cassandra_cache.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Cassandra Cache
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="cassandra/README">
<a href="./cassandra/README">
<i class="fa fa-check"></i>
<b>3.</b>
Cassandra写入过程
</a>
</li>
<li class="divider"></li>
<li>
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
</li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">Twitter</button>
<button type="button" data-sharing="google-plus" class="button">Google</button>
<button type="button" data-sharing="facebook" class="button">Facebook</button>
<button type="button" data-sharing="weibo" class="button">Weibo</button>
<button type="button" data-sharing="instapaper" class="button">Instapaper</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Cassandra</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1">
<div class="page-inner">
<section class="normal" id="section-gitbook_4762">
<h1 id="cassandra-">Cassandra 启动</h1>
<h2 id="databasedescritor">DatabaseDescritor初始化</h2>
<p>读取cassandra.yaml配置文件, 存储到Config类, 从Config类中读取配置参数, 来初始化部分配置类</p>
<p>重点谈论下DatabaseDescriptor对SystemKeyspace的初始化, System Keyspace是写死在KSMetaData中</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">applyConfig</span><span class="hljs-params">(Config config)</span> <span class="hljs-keyword">throws</span> ConfigurationException</span>{
List<KSMetaData> systemKeyspaces = Arrays.asList(KSMetaData.systemKeyspace());
<span class="hljs-keyword">assert</span> systemKeyspaces.size() == Schema.systemKeyspaceNames.size();
<span class="hljs-keyword">for</span> (KSMetaData ksmd : systemKeyspaces)
<span class="hljs-comment">//将每个KSMetaData下面的CFMetaData的UUID存入cfIdMap, KSMetaData存入keyspaces</span>
Schema.instance.load(ksmd);
}
</code></pre>
<p>Schema类结构</p>
<pre><code class="lang-java">NonBlockingHashMap<String, KSMetaData> keyspaces; <span class="hljs-comment">//ksName --> KSMataData</span>
NonBlockingHashMap<String, Keyspace> keyspaceInstances; <span class="hljs-comment">//ksName --> Keyspace</span>
ConcurrentBiMap<Pair<String, String>, UUID> cfIdMap; <span class="hljs-comment">//Pair<ksName, cfName> --> cfId</span>
</code></pre>
<h2 id="load-keyspaces--udfs">Load keyspaces 和 UDFs</h2>
<p>所有的keyspaces存储在system.schema_keyspaces中,ColumnFamily都存储在system.schema_columnfamilies,UTMetaData存储在system.schema_usertypes, UDFs存储在system.schema_functions,在CassandraDaemon.setup中触发。</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">setup</span><span class="hljs-params">()</span></span>{
DatabaseDescriptor.loadSchemas();
Functions.loadUDFFromSchema();
}
<span class="hljs-comment">//DatabaseDescriptor.loadSchemas</span>
<span class="hljs-comment">// load kSMetaData, 同时也初始化Keyspace, 存入Schema.keyspaceInstances</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">loadSchemas</span><span class="hljs-params">()</span></span>{
Schema.instance.load(DefsTables.loadFromKeyspace());
Schema.instance.updateVersion();
}
</code></pre>
<p>DefsTables类中全是静态方法,在loadFromKeyspace中主要关注</p>
<ul>
<li><p>ColumnFamilyStore创建及查询,查询具体细节参考查询一章</p>
<p>由于创建ColumnFamilyStore是在Keyspace类中执行,需要先初始化Keyspace, 再对每个ColumnFamily创建相应的ColumnFamilyStore</p>
<p>Keyspace类结构</p>
<pre><code class="lang-java"><span class="hljs-comment">//包含当前节点的token/identifier. token将会在节点之间gossip传递, 还维持其他节点负载信息柱状统计图</span>
KSMetaData metadata;
OpOrder writeOrder;
ConcurrentHashMap<UUID, ColumnFamilyStore> columnFamilyStores; <span class="hljs-comment">//cfId --> ColumnFamilyStore</span>
AbstractReplicationStrategy replicationStrategy;
</code></pre>
<p>初始化</p>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Keyspace</span><span class="hljs-params">(String keyspaceName, <span class="hljs-keyword">boolean</span> loadSSTables)</span></span>{
metadata = Schema.instance.getKSMetaData(keyspaceName);<span class="hljs-comment">//从schema取出KSMetaData</span>
createReplicationStrategy(metadata);<span class="hljs-comment">//创建ReplicationStrategy</span>
<span class="hljs-keyword">this</span>.metric = <span class="hljs-keyword">new</span> KeyspaceMetrics(<span class="hljs-keyword">this</span>);
<span class="hljs-keyword">for</span> (CFMetaData cfm : <span class="hljs-keyword">new</span> ArrayList<CFMetaData>(metadata.cfMetaData().values()))
{
initCf(cfm.cfId, cfm.cfName, loadSSTables);
}
}
<span class="hljs-comment">//在initCF中创建ColumnFamilyStore</span>
columnFamilyStores.putIfAbsent(cfId, ColumnFamilyStore.createColumnFamilyStore(<span class="hljs-keyword">this</span>, cfName, loadSSTables));
<span class="hljs-comment">//在Keyspace.open中对RowCache初始化</span>
<span class="hljs-keyword">for</span> (ColumnFamilyStore cfs : keyspaceInstance.getColumnFamilyStores())
cfs.initRowCache();
</code></pre>
</li>
<li>对结果的组装</li>
</ul>
<pre><code class="lang-java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Collection<KSMetaData> <span class="hljs-title">loadFromKeyspace</span><span class="hljs-params">()</span></span>{
<span class="hljs-comment">//执行查询</span>
List<Row> serializedSchema = SystemKeyspace.serializedSchema(SystemKeyspace.SCHEMA_KEYSPACES_CF);
List<KSMetaData> keyspaces = <span class="hljs-keyword">new</span> ArrayList<>(serializedSchema.size());
<span class="hljs-keyword">for</span> (Row row : serializedSchema){
<span class="hljs-keyword">if</span> (Schema.invalidSchemaRow(row) || Schema.ignoredSchemaRow(row))
<span class="hljs-keyword">continue</span>;
<span class="hljs-comment">//还需要对ColumnFamily, UserType进行查询</span>
keyspaces.add(KSMetaData.fromSchema(row, serializedColumnFamilies(row.key), serializedUserTypes(row.key)));
}
<span class="hljs-keyword">return</span> keyspaces;
}
<span class="hljs-comment">//在KSMetaData中对Row执行反序列化</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> KSMetaData <span class="hljs-title">fromSchema</span><span class="hljs-params">(Row serializedKs, Row serializedCFs, Row serializedUserTypes)</span></span>{
Map<String, CFMetaData> cfs = deserializeColumnFamilies(serializedCFs);
UTMetaData userTypes = <span class="hljs-keyword">new</span> UTMetaData(UTMetaData.fromSchema(serializedUserTypes));
<span class="hljs-function"><span class="hljs-keyword">return</span> <span class="hljs-title">fromSchema</span><span class="hljs-params">(serializedKs, cfs.values()</span>, userTypes)</span>;
}
<span class="hljs-comment">//KSMataData结构</span>
String name;
Class<? extends AbstractReplicationStrategy> strategyClass;
Map<String, String> strategyOptions;
Map<String, CFMetaData> cfMetaData; <span class="hljs-comment">// cfName --> CFMetaData</span>
<span class="hljs-keyword">boolean</span> durableWrites; <span class="hljs-comment">//是否写入Commit log</span>
UTMetaData userTypes;
<span class="hljs-comment">//CFMetaData结构, 主要参数</span>
UUID cfId; <span class="hljs-comment">//内部id, 不会暴露给用户</span>
String ksName;
String cfName;
ColumnFamilyType cfType; <span class="hljs-comment">//standard or super</span>
CellNameType comparator; <span class="hljs-comment">// bytes, long, timeuuid, utf8, etc.</span>
</code></pre>
</section>
</div>
</div>
</div>
<a href="./index.html" class="navigation navigation-prev " aria-label="Previous page: Cassandra Readins"><i class="fa fa-angle-left"></i></a>
<a href="./cassandra/README.html" class="navigation navigation-next " aria-label="Next page: Cassandra查询过程"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>