Permalink
Newer
Older
100644 1311 lines (1076 sloc) 62.7 KB
1
# About
2
This document is an updated version of the original design documents
3
by Spencer Kimball from early 2014.
4
5
# Overview
6
7
CockroachDB is a distributed SQL database. The primary design goals
8
are **scalability**, **strong consistency** and **survivability**
9
(hence the name). CockroachDB aims to tolerate disk, machine, rack, and
10
even **datacenter failures** with minimal latency disruption and **no
11
manual intervention**. CockroachDB nodes are symmetric; a design goal is
12
**homogeneous deployment** (one binary) with minimal configuration and
13
no required external dependencies.
14
15
The entry point for database clients is the SQL interface. Every node
16
in a CockroachDB cluster can act as a client SQL gateway. A SQL
17
gateway transforms and executes client SQL statements to key-value
18
(KV) operations, which the gateway distributes across the cluster as
19
necessary and returns results to the client. CockroachDB implements a
20
**single, monolithic sorted map** from key to value where both keys
21
and values are byte strings.
22
23
The KV map is logically composed of smaller segments of the keyspace
24
called ranges. Each range is backed by data stored in a local KV
25
storage engine (we use [RocksDB](http://rocksdb.org/), a variant of
26
LevelDB). Range data is replicated to a configurable number of
27
additional CockroachDB nodes. Ranges are merged and split to maintain
28
a target size, by default `64M`. The relatively small size facilitates
29
quick repair and rebalancing to address node failures, new capacity
30
and even read/write load. However, the size must be balanced against
31
the pressure on the system from having more ranges to manage.
32
33
CockroachDB achieves horizontally scalability:
34
- adding more nodes increases the capacity of the cluster by the
35
amount of storage on each node (divided by a configurable
36
replication factor), theoretically up to 4 exabytes (4E) of logical
37
data;
38
- client queries can be sent to any node in the cluster, and queries
39
can operate independently (w/o conflicts), meaning that overall
40
throughput is a linear factor of the number of nodes in the cluster.
41
- queries are distributed (ref: distributed SQL) so that the overall
42
throughput of single queries can be increased by adding more nodes.
44
CockroachDB achieves strong consistency:
45
- uses a distributed consensus protocol for synchronous replication of
46
data in each key value range. We’ve chosen to use the [Raft
47
consensus algorithm](https://raftconsensus.github.io); all consensus
48
state is stored in RocksDB.
49
- single or batched mutations to a single range are mediated via the
50
range's Raft instance. Raft guarantees ACID semantics.
51
- logical mutations which affect multiple ranges employ distributed
52
transactions for ACID semantics. CockroachDB uses an efficient
53
**non-locking distributed commit** protocol.
54
55
CockroachDB achieves survivability:
56
- range replicas can be co-located within a single datacenter for low
57
latency replication and survive disk or machine failures. They can
58
be distributed across racks to survive some network switch failures.
59
- range replicas can be located in datacenters spanning increasingly
60
disparate geographies to survive ever-greater failure scenarios from
61
datacenter power or networking loss to regional power failures
62
(e.g. `{ US-East-1a, US-East-1b, US-East-1c }, `{ US-East, US-West,
63
Japan }`, `{ Ireland, US-East, US-West}`, `{ Ireland, US-East,
64
US-West, Japan, Australia }`).
66
CockroachDB provides [snapshot isolation](http://en.wikipedia.org/wiki/Snapshot_isolation) (SI) and
67
serializable snapshot isolation (SSI) semantics, allowing **externally
68
consistent, lock-free reads and writes**--both from a historical
69
snapshot timestamp and from the current wall clock time. SI provides
70
lock-free reads and writes but still allows write skew. SSI eliminates
71
write skew, but introduces a performance hit in the case of a
72
contentious system. SSI is the default isolation; clients must
73
consciously decide to trade correctness for performance. CockroachDB
74
implements [a limited form of linearizability](#linearizability),
75
providing ordering for any observer or chain of observers.
76
77
Similar to
78
[Spanner](http://static.googleusercontent.com/media/research.google.com/en/us/archive/spanner-osdi2012.pdf)
79
directories, CockroachDB allows configuration of arbitrary zones of data.
80
This allows replication factor, storage device type, and/or datacenter
81
location to be chosen to optimize performance and/or availability.
82
Unlike Spanner, zones are monolithic and don’t allow movement of fine
83
grained data on the level of entity groups.
84
85
# Architecture
86
87
CockroachDB implements a layered architecture. The highest level of
88
abstraction is the SQL layer (currently unspecified in this document).
89
It depends directly on the [*SQL layer*](#sql),
90
which provides familiar relational concepts
91
such as schemas, tables, columns, and indexes. The SQL layer
92
in turn depends on the [distributed key value store](#key-value-api),
93
which handles the details of range addressing to provide the abstraction
94
of a single, monolithic key value store. The distributed KV store
95
communicates with any number of physical cockroach nodes. Each node
96
contains one or more stores, one per physical device.
97
98
![Architecture](media/architecture.png)
99
100
Each store contains potentially many ranges, the lowest-level unit of
101
key-value data. Ranges are replicated using the Raft consensus protocol.
102
The diagram below is a blown up version of stores from four of the five
103
nodes in the previous diagram. Each range is replicated three ways using
104
raft. The color coding shows associated range replicas.
105
106
![Ranges](media/ranges.png)
107
108
Each physical node exports a RoachNode service. Each RoachNode exports
109
one or more key ranges. RoachNodes are symmetric. Each has the same
110
binary and assumes identical roles.
111
112
Nodes and the ranges they provide access to can be arranged with various
113
physical network topologies to make trade offs between reliability and
114
performance. For example, a triplicated (3-way replica) range could have
115
each replica located on different:
116
117
- disks within a server to tolerate disk failures.
118
- servers within a rack to tolerate server failures.
119
- servers on different racks within a datacenter to tolerate rack power/network failures.
120
- servers in different datacenters to tolerate large scale network or power outages.
121
122
Up to `F` failures can be tolerated, where the total number of replicas `N = 2F + 1` (e.g. with 3x replication, one failure can be tolerated; with 5x replication, two failures, and so on).
123
124
# Cockroach Client
125
126
In order to support diverse client usage, Cockroach clients connect to
127
any node via HTTPS using protocol buffers or JSON. The connected node
128
proxies involved client work including key lookups and write buffering.
129
130
# Keys
131
132
Cockroach keys are arbitrary byte arrays. If textual data is used in
133
keys, utf8 encoding is recommended (this helps for cleaner display of
134
values in debugging tools). User-supplied keys are encoded using an
135
ordered code. System keys are either prefixed with null characters (`\0`
136
or `\0\0`) for system tables, or take the form of
137
`<user-key><system-suffix>` to sort user-key-range specific system
138
keys immediately after the user keys they refer to. Null characters are
139
used in system key prefixes to guarantee that they sort first.
140
141
# Versioned Values
142
143
Cockroach maintains historical versions of values by storing them with
144
associated commit timestamps. Reads and scans can specify a snapshot
145
time to return the most recent writes prior to the snapshot timestamp.
146
Older versions of values are garbage collected by the system during
147
compaction according to a user-specified expiration interval. In order
148
to support long-running scans (e.g. for MapReduce), all versions have a
149
minimum expiration.
150
151
Versioned values are supported via modifications to RocksDB to record
152
commit timestamps and GC expirations per key.
153
154
# Lock-Free Distributed Transactions
155
156
Cockroach provides distributed transactions without locks. Cockroach
157
transactions support two isolation levels:
158
159
- snapshot isolation (SI) and
160
- *serializable* snapshot isolation (SSI).
161
162
*SI* is simple to implement, highly performant, and correct for all but a
163
handful of anomalous conditions (e.g. write skew). *SSI* requires just a touch
164
more complexity, is still highly performant (less so with contention), and has
165
no anomalous conditions. Cockroach’s SSI implementation is based on ideas from
166
the literature and some possibly novel insights.
167
168
SSI is the default level, with SI provided for application developers
169
who are certain enough of their need for performance and the absence of
170
write skew conditions to consciously elect to use it. In a lightly
171
contended system, our implementation of SSI is just as performant as SI,
172
requiring no locking or additional writes. With contention, our
173
implementation of SSI still requires no locking, but will end up
174
aborting more transactions. Cockroach’s SI and SSI implementations
175
prevent starvation scenarios even for arbitrarily long transactions.
176
177
See the [Cahill paper](https://drive.google.com/file/d/0B9GCVTp_FHJIcEVyZVdDWEpYYXVVbFVDWElrYUV0NHFhU2Fv/edit?usp=sharing)
178
for one possible implementation of SSI. This is another [great paper](http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf).
179
For a discussion of SSI implemented by preventing read-write conflicts
180
(in contrast to detecting them, called write-snapshot isolation), see
181
the [Yabandeh paper](https://drive.google.com/file/d/0B9GCVTp_FHJIMjJ2U2t6aGpHLTFUVHFnMTRUbnBwc2pLa1RN/edit?usp=sharing),
182
which is the source of much inspiration for Cockroach’s SSI.
183
184
Both SI and SSI require that the outcome of reads must be preserved, i.e.
185
a write of a key at a lower timestamp than a previous read must not succeed. To
186
this end, each range maintains a bounded *in-memory* cache from key range to
187
the latest timestamp at which it was read.
188
189
Most updates to this *timestamp cache* correspond to keys being read, though
190
the timestamp cache also protects the outcome of some writes (notably range
191
deletions) which consequently must also populate the cache. The cache’s entries
192
are evicted oldest timestamp first, updating the low water mark of the cache
193
appropriately.
194
195
Each Cockroach transaction is assigned a random priority and a
196
"candidate timestamp" at start. The candidate timestamp is the
197
provisional timestamp at which the transaction will commit, and is
198
chosen as the current clock time of the node coordinating the
199
transaction. This means that a transaction without conflicts will
200
usually commit with a timestamp that, in absolute time, precedes the
201
actual work done by that transaction.
202
May 22, 2015
203
In the course of coordinating a transaction between one or more
204
distributed nodes, the candidate timestamp may be increased, but will
205
never be decreased. The core difference between the two isolation levels
206
SI and SSI is that the former allows the transaction's candidate
207
timestamp to increase and the latter does not.
209
**Hybrid Logical Clock**
210
211
Each cockroach node maintains a hybrid logical clock (HLC) as discussed
212
in the [Hybrid Logical Clock paper](http://www.cse.buffalo.edu/tech-reports/2014-04.pdf).
213
HLC time uses timestamps which are composed of a physical component (thought of
214
as and always close to local wall time) and a logical component (used to
215
distinguish between events with the same physical component). It allows us to
216
track causality for related events similar to vector clocks, but with less
217
overhead. In practice, it works much like other logical clocks: When events
218
are received by a node, it informs the local HLC about the timestamp supplied
219
with the event by the sender, and when events are sent a timestamp generated by
220
the local HLC is attached.
221
222
For a more in depth description of HLC please read the paper. Our
223
implementation is [here](https://github.com/cockroachdb/cockroach/blob/master/util/hlc/hlc.go).
224
225
Cockroach picks a Timestamp for a transaction using HLC time. Throughout this
226
document, *timestamp* always refers to the HLC time which is a singleton
227
on each node. The HLC is updated by every read/write event on the node, and
228
the HLC time >= wall time. A read/write timestamp received in a cockroach request
229
from another node is not only used to version the operation, but also updates
230
the HLC on the node. This is useful in guaranteeing that all data read/written
231
on a node is at a timestamp < next HLC time.
233
**Transaction execution flow**
234
235
Transactions are executed in two phases:
237
1. Start the transaction by selecting a range which is likely to be
238
heavily involved in the transaction and writing a new transaction
239
record to a reserved area of that range with state "PENDING". In
240
parallel write an "intent" value for each datum being written as part
241
of the transaction. These are normal MVCC values, with the addition of
242
a special flag (i.e. “intent”) indicating that the value may be
243
committed after the transaction itself commits. In addition,
244
the transaction id (unique and chosen at tx start time by client)
245
is stored with intent values. The txn id is used to refer to the
246
transaction record when there are conflicts and to make
247
tie-breaking decisions on ordering between identical timestamps.
248
Each node returns the timestamp used for the write (which is the
249
original candidate timestamp in the absence of read/write conflicts);
250
the client selects the maximum from amongst all write timestamps as the
251
final commit timestamp.
253
2. Commit the transaction by updating its transaction record. The value
254
of the commit entry contains the candidate timestamp (increased as
255
necessary to accommodate any latest read timestamps). Note that the
256
transaction is considered fully committed at this point and control
257
may be returned to the client.
258
259
In the case of an SI transaction, a commit timestamp which was
260
increased to accommodate concurrent readers is perfectly
261
acceptable and the commit may continue. For SSI transactions,
262
however, a gap between candidate and commit timestamps
263
necessitates transaction restart (note: restart is different than
264
abort--see below).
265
266
After the transaction is committed, all written intents are upgraded
267
in parallel by removing the “intent” flag. The transaction is
268
considered fully committed before this step and does not wait for
269
it to return control to the transaction coordinator.
270
271
In the absence of conflicts, this is the end. Nothing else is necessary
272
to ensure the correctness of the system.
273
274
**Conflict Resolution**
275
276
Things get more interesting when a reader or writer encounters an intent
277
record or newly-committed value in a location that it needs to read or
278
write. This is a conflict, usually causing either of the transactions to
279
abort or restart depending on the type of conflict.
280
281
***Transaction restart:***
282
283
This is the usual (and more efficient) type of behaviour and is used
284
except when the transaction was aborted (for instance by another
285
transaction).
286
In effect, that reduces to two cases; the first being the one outlined
287
above: An SSI transaction that finds upon attempting to commit that
288
its commit timestamp has been pushed. The second case involves a transaction
289
actively encountering a conflict, that is, one of its readers or writers
290
encounter data that necessitate conflict resolution
291
(see transaction interactions below).
292
293
When a transaction restarts, it changes its priority and/or moves its
294
timestamp forward depending on data tied to the conflict, and
295
begins anew reusing the same txn id. The prior run of the transaction might
296
have written some write intents, which need to be deleted before the
297
transaction commits, so as to not be included as part of the transaction.
298
These stale write intent deletions are done during the reexecution of the
299
transaction, either implicitly, through writing new intents to
300
the same keys as part of the reexecution of the transaction, or explicitly,
301
by cleaning up stale intents that are not part of the reexecution of the
302
transaction. Since most transactions will end up writing to the same keys,
303
the explicit cleanup run just before committing the transaction is usually
304
a NOOP.
305
306
***Transaction abort:***
307
308
This is the case in which a transaction, upon reading its transaction
309
record, finds that it has been aborted. In this case, the transaction
310
can not reuse its intents; it returns control to the client before
311
cleaning them up (other readers and writers would clean up dangling
312
intents as they encounter them) but will make an effort to clean up
313
after itself. The next attempt (if applicable) then runs as a new
314
transaction with **a new txn id**.
315
316
***Transaction interactions:***
317
318
There are several scenarios in which transactions interact:
319
320
- **Reader encounters write intent or value with newer timestamp far
321
enough in the future**: This is not a conflict. The reader is free
322
to proceed; after all, it will be reading an older version of the
323
value and so does not conflict. Recall that the write intent may
324
be committed with a later timestamp than its candidate; it will
325
never commit with an earlier one. **Side note**: if a SI transaction
326
reader finds an intent with a newer timestamp which the reader’s own
327
transaction has written, the reader always returns that intent's value.
328
329
- **Reader encounters write intent or value with newer timestamp in the
330
near future:** In this case, we have to be careful. The newer
331
intent may, in absolute terms, have happened in our read's past if
332
the clock of the writer is ahead of the node serving the values.
333
In that case, we would need to take this value into account, but
334
we just don't know. Hence the transaction restarts, using instead
335
a future timestamp (but remembering a maximum timestamp used to
336
limit the uncertainty window to the maximum clock skew). In fact,
337
this is optimized further; see the details under "choosing a time
338
stamp" below.
339
340
- **Reader encounters write intent with older timestamp**: the reader
341
must follow the intent’s transaction id to the transaction record.
342
If the transaction has already been committed, then the reader can
343
just read the value. If the write transaction has not yet been
344
committed, then the reader has two options. If the write conflict
345
is from an SI transaction, the reader can *push that transaction's
346
commit timestamp into the future* (and consequently not have to
347
read it). This is simple to do: the reader just updates the
348
transaction’s commit timestamp to indicate that when/if the
349
transaction does commit, it should use a timestamp *at least* as
350
high. However, if the write conflict is from an SSI transaction,
351
the reader must compare priorities. If the reader has the higher priority,
352
it pushes the transaction’s commit timestamp (that
353
transaction will then notice its timestamp has been pushed, and
354
restart). If it has the lower or same priority, it retries itself using as
355
a new priority `max(new random priority, conflicting txn’s
356
priority - 1)`.
358
- **Writer encounters uncommitted write intent**:
359
If the other write intent has been written by a transaction with a lower
360
priority, the writer aborts the conflicting transaction. If the write
361
intent has a higher or equal priority the transaction retries, using as a new
362
priority *max(new random priority, conflicting txn’s priority - 1)*;
363
the retry occurs after a short, randomized backoff interval.
365
- **Writer encounters newer committed value**:
366
The committed value could also be an unresolved write intent made by a
367
transaction that has already committed. The transaction restarts. On restart,
368
the same priority is reused, but the candidate timestamp is moved forward
369
to the encountered value's timestamp.
371
- **Writer encounters more recently read key**:
372
The *read timestamp cache* is consulted on each write at a node. If the write’s
373
candidate timestamp is earlier than the low water mark on the cache itself
374
(i.e. its last evicted timestamp) or if the key being written has a read
375
timestamp later than the write’s candidate timestamp, this later timestamp
376
value is returned with the write. A new timestamp forces a transaction
377
restart only if it is serializable.
379
**Transaction management**
380
381
Transactions are managed by the client proxy (or gateway in SQL Azure
382
parlance). Unlike in Spanner, writes are not buffered but are sent
383
directly to all implicated ranges. This allows the transaction to abort
384
quickly if it encounters a write conflict. The client proxy keeps track
385
of all written keys in order to resolve write intents asynchronously upon
386
transaction completion. If a transaction commits successfully, all intents
387
are upgraded to committed. In the event a transaction is aborted, all written
388
intents are deleted. The client proxy doesn’t guarantee it will resolve intents.
389
390
In the event the client proxy restarts before the pending transaction is
391
committed, the dangling transaction would continue to "live" until
392
aborted by another transaction. Transactions periodically heartbeat
393
their transaction record to maintain liveness.
394
Transactions encountered by readers or writers with dangling intents
395
which haven’t been heartbeat within the required interval are aborted.
396
In the event the proxy restarts after a transaction commits but before
397
the asynchronous resolution is complete, the dangling intents are upgraded
398
when encountered by future readers and writers and the system does
399
not depend on their timely resolution for correctness.
400
401
An exploration of retries with contention and abort times with abandoned
402
transaction is
403
[here](https://docs.google.com/document/d/1kBCu4sdGAnvLqpT-_2vaTbomNmX3_saayWEGYu1j7mQ/edit?usp=sharing).
404
405
**Transaction Records**
407
Please see [roachpb/data.proto](https://github.com/cockroachdb/cockroach/blob/master/roachpb/data.proto) for the up-to-date structures, the best entry point being `message Transaction`.
408
409
**Pros**
410
411
- No requirement for reliable code execution to prevent stalled 2PC
412
protocol.
413
- Readers never block with SI semantics; with SSI semantics, they may
414
abort.
415
- Lower latency than traditional 2PC commit protocol (w/o contention)
416
because second phase requires only a single write to the
417
transaction record instead of a synchronous round to all
418
transaction participants.
419
- Priorities avoid starvation for arbitrarily long transactions and
420
always pick a winner from between contending transactions (no
421
mutual aborts).
422
- Writes not buffered at client; writes fail fast.
423
- No read-locking overhead required for *serializable* SI (in contrast
424
to other SSI implementations).
425
- Well-chosen (i.e. less random) priorities can flexibly give
426
probabilistic guarantees on latency for arbitrary transactions
427
(for example: make OLTP transactions 10x less likely to abort than
428
low priority transactions, such as asynchronously scheduled jobs).
429
430
**Cons**
431
432
- Reads from non-lease holder replicas still require a ping to the lease holder
433
update *read timestamp cache*.
434
- Abandoned transactions may block contending writers for up to the
435
heartbeat interval, though average wait is likely to be
436
considerably shorter (see [graph in link](https://docs.google.com/document/d/1kBCu4sdGAnvLqpT-_2vaTbomNmX3_saayWEGYu1j7mQ/edit?usp=sharing)).
437
This is likely considerably more performant than detecting and
438
restarting 2PC in order to release read and write locks.
439
- Behavior different than other SI implementations: no first writer
440
wins, and shorter transactions do not always finish quickly.
441
Element of surprise for OLTP systems may be a problematic factor.
442
- Aborts can decrease throughput in a contended system compared with
443
two phase locking. Aborts and retries increase read and write
444
traffic, increase latency and decrease throughput.
445
446
**Choosing a Timestamp**
447
448
A key challenge of reading data in a distributed system with clock skew
449
is choosing a timestamp guaranteed to be greater than the latest
450
timestamp of any committed transaction (in absolute time). No system can
451
claim consistency and fail to read already-committed data.
452
453
Accomplishing consistency for transactions (or just single operations)
454
accessing a single node is easy. The timestamp is assigned by the node
455
itself, so it is guaranteed to be at a greater timestamp than all the
456
existing timestamped data on the node.
457
458
For multiple nodes, the timestamp of the node coordinating the
459
transaction `t` is used. In addition, a maximum timestamp `t+ε` is
460
supplied to provide an upper bound on timestamps for already-committed
461
data (`ε` is the maximum clock skew). As the transaction progresses, any
462
data read which have timestamps greater than `t` but less than `t+ε`
463
cause the transaction to abort and retry with the conflicting timestamp
464
t<sub>c</sub>, where t<sub>c</sub> \> t. The maximum timestamp `t+ε` remains
465
the same. This implies that transaction restarts due to clock uncertainty
466
can only happen on a time interval of length `ε`.
468
We apply another optimization to reduce the restarts caused
469
by uncertainty. Upon restarting, the transaction not only takes
470
into account t<sub>c</sub>, but the timestamp of the node at the time
471
of the uncertain read t<sub>node</sub>. The larger of those two timestamps
472
t<sub>c</sub> and t<sub>node</sub> (likely equal to the latter) is used
473
to increase the read timestamp. Additionally, the conflicting node is
474
marked as “certain”. Then, for future reads to that node within the
475
transaction, we set `MaxTimestamp = Read Timestamp`, preventing further
476
uncertainty restarts.
477
478
Correctness follows from the fact that we know that at the time of the read,
479
there exists no version of any key on that node with a higher timestamp than
480
t<sub>node</sub>. Upon a restart caused by the node, if the transaction
481
encounters a key with a higher timestamp, it knows that in absolute time,
482
the value was written after t<sub>node</sub> was obtained, i.e. after the
483
uncertain read. Hence the transaction can move forward reading an older version
484
of the data (at the transaction's timestamp). This limits the time uncertainty
485
restarts attributed to a node to at most one. The tradeoff is that we might
486
pick a timestamp larger than the optimal one (> highest conflicting timestamp),
487
resulting in the possibility of a few more conflicts.
488
489
We expect retries will be rare, but this assumption may need to be
490
revisited if retries become problematic. Note that this problem does not
491
apply to historical reads. An alternate approach which does not require
492
retries makes a round to all node participants in advance and
493
chooses the highest reported node wall time as the timestamp. However,
494
knowing which nodes will be accessed in advance is difficult and
495
potentially limiting. Cockroach could also potentially use a global
496
clock (Google did this with [Percolator](https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Peng.pdf)),
497
which would be feasible for smaller, geographically-proximate clusters.
498
499
# Linearizability
500
501
First a word about [***Spanner***](http://research.google.com/archive/spanner.html).
502
By combining judicious use of wait intervals with accurate time signals,
503
Spanner provides a global ordering between any two non-overlapping transactions
504
(in absolute time) with \~14ms latencies. Put another way:
505
Spanner guarantees that if a transaction T<sub>1</sub> commits (in absolute time)
506
before another transaction T<sub>2</sub> starts, then T<sub>1</sub>'s assigned commit
507
timestamp is smaller than T<sub>2</sub>'s. Using atomic clocks and GPS receivers,
508
Spanner reduces their clock skew uncertainty to \< 10ms (`ε`). To make
509
good on the promised guarantee, transactions must take at least double
510
the clock skew uncertainty interval to commit (`2ε`). See [*this
511
article*](http://www.cs.cornell.edu/~ie53/publications/DC-col51-Sep13.pdf)
512
for a helpful overview of Spanner’s concurrency control.
513
514
Cockroach could make the same guarantees without specialized hardware,
515
at the expense of longer wait times. If servers in the cluster were
516
configured to work only with NTP, transaction wait times would likely to
517
be in excess of 150ms. For wide-area zones, this would be somewhat
518
mitigated by overlap from cross datacenter link latencies. If clocks
519
were made more accurate, the minimal limit for commit latencies would
520
improve.
521
522
However, let’s take a step back and evaluate whether Spanner’s external
523
consistency guarantee is worth the automatic commit wait. First, if the
524
commit wait is omitted completely, the system still yields a consistent
525
view of the map at an arbitrary timestamp. However with clock skew, it
526
would become possible for commit timestamps on non-overlapping but
527
causally related transactions to suffer temporal reverse. In other
528
words, the following scenario is possible for a client without global
529
ordering:
530
531
- Start transaction T<sub>1</sub> to modify value `x` with commit time s<sub>1</sub>
532
533
- On commit of T<sub>1</sub>, start T<sub>2</sub> to modify value `y` with commit time
535
536
- Read `x` and `y` and discover that s<sub>1</sub> \> s<sub>2</sub> (**!**)
537
538
The external consistency which Spanner guarantees is referred to as
539
**linearizability**. It goes beyond serializability by preserving
540
information about the causality inherent in how external processes
541
interacted with the database. The strength of Spanner’s guarantee can be
542
formulated as follows: any two processes, with clock skew within
543
expected bounds, may independently record their wall times for the
544
completion of transaction T<sub>1</sub> (T<sub>1</sub><sup>end</sup>) and start of transaction
545
T<sub>2</sub> (T<sub>2</sub><sup>start</sup>) respectively, and if later
546
compared such that T<sub>1</sub><sup>end</sup> \< T<sub>2</sub><sup>start</sup>,
547
then commit timestamps s<sub>1</sub> \< s<sub>2</sub>.
548
This guarantee is broad enough to completely cover all cases of explicit
549
causality, in addition to covering any and all imaginable scenarios of implicit
550
causality.
551
552
Our contention is that causality is chiefly important from the
553
perspective of a single client or a chain of successive clients (*if a
554
tree falls in the forest and nobody hears…*). As such, Cockroach
555
provides two mechanisms to provide linearizability for the vast majority
556
of use cases without a mandatory transaction commit wait or an elaborate
557
system to minimize clock skew.
558
559
1. Clients provide the highest transaction commit timestamp with
560
successive transactions. This allows node clocks from previous
561
transactions to effectively participate in the formulation of the
562
commit timestamp for the current transaction. This guarantees
563
linearizability for transactions committed by this client.
564
565
Newly launched clients wait at least 2 \* ε from process start
566
time before beginning their first transaction. This preserves the
567
same property even on client restart, and the wait will be
568
mitigated by process initialization.
569
570
All causally-related events within Cockroach maintain
571
linearizability.
572
573
2. Committed transactions respond with a commit wait parameter which
574
represents the remaining time in the nominal commit wait. This
575
will typically be less than the full commit wait as the consensus
576
write at the coordinator accounts for a portion of it.
577
578
Clients taking any action outside of another Cockroach transaction
579
(e.g. writing to another distributed system component) can either
580
choose to wait the remaining interval before proceeding, or
581
alternatively, pass the wait and/or commit timestamp to the
582
execution of the outside action for its consideration. This pushes
583
the burden of linearizability to clients, but is a useful tool in
584
mitigating commit latencies if the clock skew is potentially
585
large. This functionality can be used for ordering in the face of
586
backchannel dependencies as mentioned in the
587
[AugmentedTime](http://www.cse.buffalo.edu/~demirbas/publications/augmentedTime.pdf)
588
paper.
589
590
Using these mechanisms in place of commit wait, Cockroach’s guarantee can be
591
formulated as follows: any process which signals the start of transaction
592
T<sub>2</sub> (T<sub>2</sub><sup>start</sup>) after the completion of
593
transaction T<sub>1</sub> (T<sub>1</sub><sup>end</sup>), will have commit
594
timestamps such thats<sub>1</sub> \< s<sub>2</sub>.
595
596
# Logical Map Content
597
598
Logically, the map contains a series of reserved system key/value
599
pairs preceding the actual user data (which is managed by the SQL
600
subsystem).
602
- `\x02<key1>`: Range metadata for range ending `\x03<key1>`. This a "meta1" key.
603
- ...
604
- `\x02<keyN>`: Range metadata for range ending `\x03<keyN>`. This a "meta1" key.
605
- `\x03<key1>`: Range metadata for range ending `<key1>`. This a "meta2" key.
606
- ...
607
- `\x03<keyN>`: Range metadata for range ending `<keyN>`. This a "meta2" key.
608
- `\x04{desc,node,range,store}-idegen`: ID generation oracles for various component types.
609
- `\x04status-node-<varint encoded Store ID>`: Store runtime metadata.
610
- `\x04tsd<key>`: Time-series data key.
611
- `<key>`: A user key. In practice, these keys are managed by the SQL
612
subsystem, which employs its own key anatomy.
613
614
# Node Storage
615
616
Nodes maintain a separate instance of RocksDB for each disk. Each
617
RocksDB instance hosts any number of ranges. RPCs arriving at a
618
RoachNode are multiplexed based on the disk name to the appropriate
619
RocksDB instance. A single instance per disk is used to avoid
620
contention. If every range maintained its own RocksDB, global management
621
of available cache memory would be impossible and writers for each range
622
would compete for non-contiguous writes to multiple RocksDB logs.
623
624
In addition to the key/value pairs of the range itself, various range
625
metadata is maintained.
626
627
- participating replicas
628
629
- consensus metadata
630
631
- split/merge activity
632
633
A really good reference on tuning Linux installations with RocksDB is
634
[here](http://docs.basho.com/riak/latest/ops/advanced/backends/leveldb/).
635
636
# Range Metadata
637
638
The default approximate size of a range is 64M (2\^26 B). In order to
639
support 1P (2\^50 B) of logical data, metadata is needed for roughly
640
2\^(50 - 26) = 2\^24 ranges. A reasonable upper bound on range metadata
641
size is roughly 256 bytes (3\*12 bytes for the triplicated node
642
locations and 220 bytes for the range key itself). 2\^24 ranges \* 2\^8
643
B would require roughly 4G (2\^32 B) to store--too much to duplicate
644
between machines. Our conclusion is that range metadata must be
645
distributed for large installations.
646
647
To keep key lookups relatively fast in the presence of distributed metadata,
648
we store all the top-level metadata in a single range (the first range). These
649
top-level metadata keys are known as *meta1* keys, and are prefixed such that
650
they sort to the beginning of the key space. Given the metadata size of 256
651
bytes given above, a single 64M range would support 64M/256B = 2\^18 ranges,
652
which gives a total storage of 64M \* 2\^18 = 16T. To support the 1P quoted
653
above, we need two levels of indirection, where the first level addresses the
654
second, and the second addresses user data. With two levels of indirection, we
655
can address 2\^(18 + 18) = 2\^36 ranges; each range addresses 2\^26 B, and
656
altogether we address 2\^(36+26) B = 2\^62 B = 4E of user data.
657
658
For a given user-addressable `key1`, the associated *meta1* record is found
659
at the successor key to `key1` in the *meta1* space. Since the *meta1* space
660
is sparse, the successor key is defined as the next key which is present. The
661
*meta1* record identifies the range containing the *meta2* record, which is
662
found using the same process. The *meta2* record identifies the range
663
containing `key1`, which is again found the same way (see examples below).
665
Concretely, metadata keys are prefixed by `\0\0meta{1,2}`; the two null
666
characters provide for the desired sorting behaviour. Thus, `key1`'s
667
*meta1* record will reside at the successor key to `\0\0\meta1<key1>`.
668
Jul 29, 2015
669
Note: we append the end key of each range to meta{1,2} records because
670
the RocksDB iterator only supports a Seek() interface which acts as a
671
Ceil(). Using the start key of the range would cause Seek() to find the
672
key *after* the meta indexing record we’re looking for, which would
673
result in having to back the iterator up, an option which is both less
674
efficient and not available in all cases.
675
676
The following example shows the directory structure for a map with
677
three ranges worth of data. Ellipses indicate additional key/value pairs to
678
fill an entire range of data. Except for the fact that splitting ranges
679
requires updates to the range metadata with knowledge of the metadata layout,
680
the range metadata itself requires no special treatment or bootstrapping.
681
682
**Range 0** (located on servers `dcrama1:8000`, `dcrama2:8000`,
683
`dcrama3:8000`)
684
685
- `\0\0meta1\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
686
- `\0\0meta2<lastkey0>`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
687
- `\0\0meta2<lastkey1>`: `dcrama4:8000`, `dcrama5:8000`, `dcrama6:8000`
688
- `\0\0meta2\xff`: `dcrama7:8000`, `dcrama8:8000`, `dcrama9:8000`
689
- ...
690
- `<lastkey0>`: `<lastvalue0>`
691
692
**Range 1** (located on servers `dcrama4:8000`, `dcrama5:8000`,
693
`dcrama6:8000`)
694
695
- ...
696
- `<lastkey1>`: `<lastvalue1>`
697
698
**Range 2** (located on servers `dcrama7:8000`, `dcrama8:8000`,
699
`dcrama9:8000`)
700
701
- ...
702
- `<lastkey2>`: `<lastvalue2>`
703
704
Consider a simpler example of a map containing less than a single
705
range of data. In this case, all range metadata and all data are
706
located in the same range:
707
708
**Range 0** (located on servers `dcrama1:8000`, `dcrama2:8000`,
709
`dcrama3:8000`)*
710
711
- `\0\0meta1\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
712
- `\0\0meta2\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
713
- `<key0>`: `<value0>`
714
- `...`
715
716
Finally, a map large enough to need both levels of indirection would
717
look like (note that instead of showing range replicas, this
718
example is simplified to just show range indexes):
719
720
**Range 0**
721
722
- `\0\0meta1<lastkeyN-1>`: Range 0
723
- `\0\0meta1\xff`: Range 1
724
- `\0\0meta2<lastkey1>`: Range 1
725
- `\0\0meta2<lastkey2>`: Range 2
726
- `\0\0meta2<lastkey3>`: Range 3
727
- ...
728
- `\0\0meta2<lastkeyN-1>`: Range 262143
729
730
**Range 1**
731
732
- `\0\0meta2<lastkeyN>`: Range 262144
733
- `\0\0meta2<lastkeyN+1>`: Range 262145
734
- ...
735
- `\0\0meta2\xff`: Range 500,000
736
- ...
737
- `<lastkey1>`: `<lastvalue1>`
738
739
**Range 2**
740
741
- ...
742
- `<lastkey2>`: `<lastvalue2>`
743
744
**Range 3**
745
746
- ...
747
- `<lastkey3>`: `<lastvalue3>`
748
749
**Range 262144**
750
751
- ...
752
- `<lastkeyN>`: `<lastvalueN>`
753
754
**Range 262145**
755
756
- ...
757
- `<lastkeyN+1>`: `<lastvalueN+1>`
758
759
Note that the choice of range `262144` is just an approximation. The
760
actual number of ranges addressable via a single metadata range is
761
dependent on the size of the keys. If efforts are made to keep key sizes
762
small, the total number of addressable ranges would increase and vice
763
versa.
764
765
From the examples above it’s clear that key location lookups require at
766
most three reads to get the value for `<key>`:
767
768
1. lower bound of `\0\0meta1<key>`
769
2. lower bound of `\0\0meta2<key>`,
770
3. `<key>`.
771
772
For small maps, the entire lookup is satisfied in a single RPC to Range 0. Maps
773
containing less than 16T of data would require two lookups. Clients cache both
774
levels of range metadata, and we expect that data locality for individual
775
clients will be high. Clients may end up with stale cache entries. If on a
776
lookup, the range consulted does not match the client’s expectations, the
777
client evicts the stale entries and possibly does a new lookup.
778
779
# Raft - Consistency of Range Replicas
780
781
Each range is configured to consist of three or more replicas, as specified by
782
their ZoneConfig. The replicas in a range maintain their own instance of a
783
distributed consensus algorithm. We use the [*Raft consensus algorithm*](https://raftconsensus.github.io)
784
as it is simpler to reason about and includes a reference implementation
785
covering important details.
786
[ePaxos](https://www.cs.cmu.edu/~dga/papers/epaxos-sosp2013.pdf) has
787
promising performance characteristics for WAN-distributed replicas, but
788
it does not guarantee a consistent ordering between replicas.
789
790
Raft elects a relatively long-lived leader which must be involved to
791
propose commands. It heartbeats followers periodically and keeps their logs
792
replicated. In the absence of heartbeats, followers become candidates
793
after randomized election timeouts and proceed to hold new leader
794
elections. Cockroach weights random timeouts such that the replicas with
795
shorter round trip times to peers are more likely to hold elections
796
first (not implemented yet). Only the Raft leader may propose commands;
797
followers will simply relay commands to the last known leader.
799
Our Raft implementation was developed together with CoreOS, but adds an extra
800
layer of optimization to account for the fact that a single Node may have
801
millions of consensus groups (one for each Range). Areas of optimization
802
are chiefly coalesced heartbeats (so that the number of nodes dictates the
803
number of heartbeats as opposed to the much larger number of ranges) and
804
batch processing of requests.
805
Future optimizations may include two-phase elections and quiescent ranges
806
(i.e. stopping traffic completely for inactive ranges).
807
808
# Range Leases
809
810
As outlined in the Raft section, the replicas of a Range are organized as a
811
Raft group and execute commands from their shared commit log. Going through
812
Raft is an expensive operation though, and there are tasks which should only be
813
carried out by a single replica at a time (as opposed to all of them).
814
In particular, it is desirable to serve authoritative reads from a single
815
Replica (ideally from more than one, but that is far more difficult).
817
For these reasons, Cockroach introduces the concept of **Range Leases**:
818
This is a lease held for a slice of (database, i.e. hybrid logical) time and is
819
established by committing a special log entry through Raft containing the
820
interval the lease is going to be active on, along with the Node:RaftID
821
combination that uniquely describes the requesting replica. Reads and writes
822
must generally be addressed to the replica holding the lease; if none does, any
823
replica may be addressed, causing it to try to obtain the lease synchronously.
824
Requests received by a non-lease holder (for the HLC timestamp specified in the
825
request's header) fail with an error pointing at the replica's last known
826
lease holder. These requests are retried transparently with the updated lease by the
827
gateway node and never reach the client.
828
829
The replica holding the lease is in charge or involved in handling
830
Range-specific maintenance tasks such as
831
832
* gossiping the sentinel and/or first range information
833
* splitting, merging and rebalancing
834
835
and, very importantly, may satisfy reads locally, without incurring the
836
overhead of going through Raft.
837
838
Since reads bypass Raft, a new lease holder will, among other things, ascertain
839
that its timestamp cache does not report timestamps smaller than the previous
840
lease holder's (so that it's compatible with reads which may have occurred on
841
the former lease holder). This is accomplished by letting leases enter
842
a <i>stasis period</i> (which is just the expiration minus the maximum clock
843
offset) before the actual expiration of the lease, so that all the next lease
844
holder has to do is set the low water mark of the timestamp cache to its
845
new lease's start time.
846
847
As a lease enters its stasis period, no more reads or writes are served, which
848
is undesirable. However, this would only happen in practice if a node became
849
unavailable. In almost all practical situations, no unavailability results
850
since leases are usually long-lived (and/or eagerly extended, which can avoid
851
the stasis period) or proactively transferred away from the lease holder, which
852
can also avoid the stasis period by promising not to serve any further reads
853
until the next lease goes into effect.
854
855
## Colocation with Raft leadership
857
The range lease is completely separate from Raft leadership, and so without
858
further efforts, Raft leadership and the Range lease might not be held by the
859
same Replica. Since it's expensive to not have these two roles colocated (the
860
lease holder has to forward each proposal to the leader, adding costly RPC
861
round-trips), each lease renewal or transfer also attempts to colocate them.
862
In practice, that means that the mismatch is rare and self-corrects quickly.
864
## Command Execution Flow
865
866
This subsection describes how a lease holder replica processes a read/write
867
command in more details. Each command specifies (1) a key (or a range
868
of keys) that the command accesses and (2) the ID of a range which the
869
key(s) belongs to. When receiving a command, a RoachNode looks up a
870
range by the specified Range ID and checks if the range is still
871
responsible for the supplied keys. If any of the keys do not belong to the
872
range, the RoachNode returns an error so that the client will retry
873
and send a request to a correct range.
874
875
When all the keys belong to the range, the RoachNode attempts to
876
process the command. If the command is an inconsistent read-only
877
command, it is processed immediately. If the command is a consistent
878
read or a write, the command is executed when both of the following
879
conditions hold:
880
881
- The range replica has a range lease.
882
- There are no other running commands whose keys overlap with
883
the submitted command and cause read/write conflict.
884
885
When the first condition is not met, the replica attempts to acquire
886
a lease or returns an error so that the client will redirect the
887
command to the current lease holder. The second condition guarantees that
888
consistent read/write commands for a given key are sequentially
889
executed.
890
891
When the above two conditions are met, the lease holder replica processes the
892
command. Consistent reads are processed on the lease holder immediately.
893
Write commands are committed into the Raft log so that every replica
894
will execute the same commands. All commands produce deterministic
895
results so that the range replicas keep consistent states among them.
896
897
When a write command completes, all the replica updates their response
898
cache to ensure idempotency. When a read command completes, the lease holder
899
replica updates its timestamp cache to keep track of the latest read
900
for a given key.
901
902
There is a chance that a range lease gets expired while a command is
903
executed. Before executing a command, each replica checks if a replica
904
proposing the command has a still lease. When the lease has been
905
expired, the command will be rejected by the replica.
906
907
908
# Splitting / Merging Ranges
909
910
RoachNodes split or merge ranges based on whether they exceed maximum or
911
minimum thresholds for capacity or load. Ranges exceeding maximums for
912
either capacity or load are split; ranges below minimums for *both*
913
capacity and load are merged.
914
915
Ranges maintain the same accounting statistics as accounting key
916
prefixes. These boil down to a time series of data points with minute
917
granularity. Everything from number of bytes to read/write queue sizes.
918
Arbitrary distillations of the accounting stats can be determined as the
919
basis for splitting / merging. Two sensible metrics for use with
920
split/merge are range size in bytes and IOps. A good metric for
921
rebalancing a replica from one node to another would be total read/write
922
queue wait times. These metrics are gossipped, with each range / node
923
passing along relevant metrics if they’re in the bottom or top of the
924
range it’s aware of.
925
926
A range finding itself exceeding either capacity or load threshold
927
splits. To this end, the range lease holder computes an appropriate split key
928
candidate and issues the split through Raft. In contrast to splitting,
929
merging requires a range to be below the minimum threshold for both
930
capacity *and* load. A range being merged chooses the smaller of the
931
ranges immediately preceding and succeeding it.
932
933
Splitting, merging, rebalancing and recovering all follow the same basic
934
algorithm for moving data between roach nodes. New target replicas are
935
created and added to the replica set of source range. Then each new
936
replica is brought up to date by either replaying the log in full or
937
copying a snapshot of the source replica data and then replaying the log
938
from the timestamp of the snapshot to catch up fully. Once the new
939
replicas are fully up to date, the range metadata is updated and old,
940
source replica(s) deleted if applicable.
941
942
**Coordinator** (lease holder replica)
943
944
```
945
if splitting
946
SplitRange(split_key): splits happen locally on range replicas and
947
only after being completed locally, are moved to new target replicas.
948
else if merging
949
Choose new replicas on same servers as target range replicas;
950
add to replica set.
951
else if rebalancing || recovering
952
Choose new replica(s) on least loaded servers; add to replica set.
953
```
954
955
**New Replica**
956
957
*Bring replica up to date:*
958
959
```
960
if all info can be read from replicated log
961
copy replicated log
962
else
963
snapshot source replica
964
send successive ReadRange requests to source replica
965
referencing snapshot
966
967
if merging
968
combine ranges on all replicas
969
else if rebalancing || recovering
970
remove old range replica(s)
971
```
972
973
RoachNodes split ranges when the total data in a range exceeds a
974
configurable maximum threshold. Similarly, ranges are merged when the
975
total data falls below a configurable minimum threshold.
976
977
**TBD: flesh this out**: Especially for merges (but also rebalancing) we have a
978
range disappearing from the local node; that range needs to disappear
979
gracefully, with a smooth handoff of operation to the new owner of its data.
980
981
Ranges are rebalanced if a node determines its load or capacity is one
982
of the worst in the cluster based on gossipped load stats. A node with
983
spare capacity is chosen in the same datacenter and a special-case split
984
is done which simply duplicates the data 1:1 and resets the range
985
configuration metadata.
986
987
# Node Allocation (via Gossip)
988
989
New nodes must be allocated when a range is split. Instead of requiring
990
every RoachNode to know about the status of all or even a large number
991
of peer nodes --or-- alternatively requiring a specialized curator or
992
master with sufficiently global knowledge, we use a gossip protocol to
993
efficiently communicate only interesting information between all of the
994
nodes in the cluster. What’s interesting information? One example would
995
be whether a particular node has a lot of spare capacity. Each node,
996
when gossiping, compares each topic of gossip to its own state. If its
997
own state is somehow “more interesting” than the least interesting item
998
in the topic it’s seen recently, it includes its own state as part of
999
the next gossip session with a peer node. In this way, a node with
1000
capacity sufficiently in excess of the mean quickly becomes discovered
1001
by the entire cluster. To avoid piling onto outliers, nodes from the
1002
high capacity set are selected at random for allocation.
1003
1004
The gossip protocol itself contains two primary components:
1005
1006
- **Peer Selection**: each node maintains up to N peers with which it
1007
regularly communicates. It selects peers with an eye towards
1008
maximizing fanout. A peer node which itself communicates with an
1009
array of otherwise unknown nodes will be selected over one which
1010
communicates with a set containing significant overlap. Each time
1011
gossip is initiated, each nodes’ set of peers is exchanged. Each
1012
node is then free to incorporate the other’s peers as it sees fit.
1013
To avoid any node suffering from excess incoming requests, a node
1014
may refuse to answer a gossip exchange. Each node is biased
1015
towards answering requests from nodes without significant overlap
1016
and refusing requests otherwise.
1017
1018
Peers are efficiently selected using a heuristic as described in
1019
[Agarwal & Trachtenberg (2006)](https://drive.google.com/file/d/0B9GCVTp_FHJISmFRTThkOEZSM1U/edit?usp=sharing).
1020
1021
**TBD**: how to avoid partitions? Need to work out a simulation of
1022
the protocol to tune the behavior and see empirically how well it
1023
works.
1024
1025
- **Gossip Selection**: what to communicate. Gossip is divided into
1026
topics. Load characteristics (capacity per disk, cpu load, and
1027
state [e.g. draining, ok, failure]) are used to drive node
1028
allocation. Range statistics (range read/write load, missing
1029
replicas, unavailable ranges) and network topology (inter-rack
1030
bandwidth/latency, inter-datacenter bandwidth/latency, subnet
1031
outages) are used for determining when to split ranges, when to
1032
recover replicas vs. wait for network connectivity, and for
1033
debugging / sysops. In all cases, a set of minimums and a set of
1034
maximums is propagated; each node applies its own view of the
1035
world to augment the values. Each minimum and maximum value is
1036
tagged with the reporting node and other accompanying contextual
1037
information. Each topic of gossip has its own protobuf to hold the
1038
structured data. The number of items of gossip in each topic is
1039
limited by a configurable bound.
1040
1041
For efficiency, nodes assign each new item of gossip a sequence
1042
number and keep track of the highest sequence number each peer
1043
node has seen. Each round of gossip communicates only the delta
1044
containing new items.
1045
1046
# Node Accounting
1047
1048
The gossip protocol discussed in the previous section is useful to
1049
quickly communicate fragments of important information in a
1050
decentralized manner. However, complete accounting for each node is also
1051
stored to a central location, available to any dashboard process. This
1052
is done using the map itself. Each node periodically writes its state to
1053
the map with keys prefixed by `\0node`, similar to the first level of
1054
range metadata, but with an ‘`node`’ suffix. Each value is a protobuf
1055
containing the full complement of node statistics--everything
1056
communicated normally via the gossip protocol plus other useful, but
1057
non-critical data.
1058
1059
The range containing the first key in the node accounting table is
1060
responsible for gossiping the total count of nodes. This total count is
1061
used by the gossip network to most efficiently organize itself. In
1062
particular, the maximum number of hops for gossipped information to take
1063
before reaching a node is given by `ceil(log(node count) / log(max
1064
fanout)) + 1`.
1065
1066
# Key-prefix Accounting and Zones
1068
Arbitrarily fine-grained accounting is specified via
1069
key prefixes. Key prefixes can overlap, as is necessary for capturing
1070
hierarchical relationships. For illustrative purposes, let’s say keys
1071
specifying rows in a set of databases have the following format:
1072
1073
`<db>:<table>:<primary-key>[:<secondary-key>]`
1074
1075
In this case, we might collect accounting with
1076
key prefixes:
1077
1078
`db1`, `db1:user`, `db1:order`,
1079
1080
Accounting is kept for the entire map by default.
1081
1082
## Accounting
1083
to keep accounting for a range defined by a key prefix, an entry is created in
1084
the accounting system table. The format of accounting table keys is:
1085
1086
`\0acct<key-prefix>`
1087
1088
In practice, we assume each RoachNode capable of caching the
1089
entire accounting table as it is likely to be relatively small.
1090
1091
Accounting is kept for key prefix ranges with eventual consistency for
1092
efficiency. There are two types of values which comprise accounting:
1093
counts and occurrences, for lack of better terms. Counts describe
1094
system state, such as the total number of bytes, rows,
1095
etc. Occurrences include transient performance and load metrics. Both
1096
types of accounting are captured as time series with minute
1097
granularity. The length of time accounting metrics are kept is
1098
configurable. Below are examples of each type of accounting value.
1099
1100
**System State Counters/Performance**
1101
1102
- Count of items (e.g. rows)
1103
- Total bytes
1104
- Total key bytes
1105
- Total value length
1106
- Queued message count
1107
- Queued message total bytes
1108
- Count of values \< 16B
1109
- Count of values \< 64B
1110
- Count of values \< 256B
1111
- Count of values \< 1K
1112
- Count of values \< 4K
1113
- Count of values \< 16K
1114
- Count of values \< 64K
1115
- Count of values \< 256K
1116
- Count of values \< 1M
1117
- Count of values \> 1M
1118
- Total bytes of accounting
1119
1120
1121
**Load Occurrences**
1122
1123
- Get op count
1124
- Get total MB
1125
- Put op count
1126
- Put total MB
1127
- Delete op count
1128
- Delete total MB
1129
- Delete range op count
1130
- Delete range total MB
1131
- Scan op count
1132
- Scan op MB
1133
- Split count
1134
- Merge count
1135
1136
Because accounting information is kept as time series and over many
1137
possible metrics of interest, the data can become numerous. Accounting
1138
data are stored in the map near the key prefix described, in order to
1139
distribute load (for both aggregation and storage).
1140
1141
Accounting keys for system state have the form:
1142
`<key-prefix>|acctd<metric-name>*`. Notice the leading ‘pipe’
1143
character. It’s meant to sort the root level account AFTER any other
1144
system tables. They must increment the same underlying values as they
1145
are permanent counts, and not transient activity. Logic at the
1146
RoachNode takes care of snapshotting the value into an appropriately
1147
suffixed (e.g. with timestamp hour) multi-value time series entry.
1148
1149
Keys for perf/load metrics:
1150
`<key-prefix>acctd<metric-name><hourly-timestamp>`.
1151
1152
`<hourly-timestamp>`-suffixed accounting entries are multi-valued,
1153
containing a varint64 entry for each minute with activity during the
1154
specified hour.
1155
1156
To efficiently keep accounting over large key ranges, the task of
1157
aggregation must be distributed. If activity occurs within the same
1158
range as the key prefix for accounting, the updates are made as part
1159
of the consensus write. If the ranges differ, then a message is sent
1160
to the parent range to increment the accounting. If upon receiving the
1161
message, the parent range also does not include the key prefix, it in
1162
turn forwards it to its parent or left child in the balanced binary
1163
tree which is maintained to describe the range hierarchy. This limits
1164
the number of messages before an update is visible at the root to `2*log N`,
1165
where `N` is the number of ranges in the key prefix.
1166
1167
## Zones
1168
zones are stored in the map with keys prefixed by
1169
`\0zone` followed by the key prefix to which the zone
1170
configuration applies. Zone values specify a protobuf containing
1171
the datacenters from which replicas for ranges which fall under
1172
the zone must be chosen.
1173
1174
Please see [config/config.proto](https://github.com/cockroachdb/cockroach/blob/master/config/config.proto) for up-to-date data structures used, the best entry point being `message ZoneConfig`.
1175
1176
If zones are modified in situ, each RoachNode verifies the
1177
existing zones for its ranges against the zone configuration. If
1178
it discovers differences, it reconfigures ranges in the same way
1179
that it rebalances away from busy nodes, via special-case 1:1
1180
split to a duplicate range comprising the new configuration.
1181
1182
# SQL
1183
1184
Each node in a cluster can accept SQL client connections. CockroachDB
1185
supports the PostgreSQL wire protocol, to enable reuse of native
1186
PostgreSQL client drivers. Connections using SSL and authenticated
1187
using client certificates are supported and even encouraged over
1188
unencrypted (insecure) and password-based connections.
1189
1190
Each connection is associated with a SQL session which holds the
1191
server-side state of the connection. Over the lifespan of a session
1192
the client can send SQL to open/close transactions, issue statements
1193
or queries or configure session parameters, much like with any other
1194
SQL database.
1195
1196
## Language support
1197
1198
CockroachDB also attempts to emulate the flavor of SQL supported by
1199
PostgreSQL, although it also diverges in significant ways:
1200
1201
- CockroachDB exclusively implements MVCC-based consistency for
1202
transactions, and thus only supports SQL's isolation levels SNAPSHOT
1203
and SERIALIZABLE. The other traditional SQL isolation levels are
1204
internally mapped to either SNAPSHOT or SERIALIZABLE.
1205
1206
- CockroachDB implements its own [SQL type system](RFCS/typing.md)
1207
which only supports a limited form of implicit coercions between
1208
types compared to PostgreSQL. The rationale is to keep the
1209
implementation simple and efficient, capitalizing on the observation
1210
that 1) most SQL code in clients is automatically generated with
1211
coherent typing already and 2) existing SQL code for other databases
1212
will need to be massaged for CockroachDB anyways.
1213
1214
## SQL architecture
1215
1216
Client connections over the network are handled in each node by a
1217
pgwire server process (goroutine). This handles the stream of incoming
1218
commands and sends back responses including query/statement results.
1219
The pgwire server also handles pgwire-level prepared statements,
1220
binding prepared statements to arguments and looking up prepared
1221
statements for execution.
1222
1223
Meanwhile the state of a SQL connection is maintained by a Session
1224
object and a monolithic `planner` object (one per connection) which
1225
coordinates execution between the session, the current SQL transaction
1226
state and the underlying KV store.
1227
1228
Upon receiving a query/statement (either directly or via an execute
1229
command for a previously prepared statement) the pgwire server forwards
1230
the SQL text to the `planner` associated with the connection. The SQL
1231
code is then transformed into a SQL query plan.
1232
The query plan is implemented as a tree of objects which describe the
1233
high-level data operations needed to resolve the query, for example
1234
"join", "index join", "scan", "group", etc.
1235
1236
The query plan objects currently also embed the run-time state needed
1237
for the execution of the query plan. Once the SQL query plan is ready,
1238
methods on these objects then carry the execution out in the fashion
1239
of "generators" in other programming languages: each node *starts* its
1240
children nodes and from that point forward each child node serves as a
1241
*generator* for a stream of result rows, which the parent node can
1242
consume and transform incrementally and present to its own parent node
1243
also as a generator.
1244
1245
The top-level planner consumes the data produced by the top node of
1246
the query plan and returns it to the client via pgwire.
1247
1248
## Data mapping between the SQL model and KV
1249
1250
Every SQL table has a primary key in CockroachDB. (If a table is created
1251
without one, an implicit primary key is provided automatically.)
1252
The table identifier, followed by the value of the primary key for
1253
each row, are encoded as the *prefix* of a key in the underlying KV
1254
store.
1255
1256
Each remaining column or *column family* in the table is then encoded
1257
as a value in the underlying KV store, and the column/family identifier
1258
is appended as *suffix* to the KV key.
1259
1260
For example:
1261
1262
- after table `customers` is created in a database `mydb` with a
1263
primary key column `name` and normal columns `address` and `URL`, the KV pairs
1264
to store the schema would be:
1265
1266
| Key | Values |
1267
| ---------------------------- | ------ |
1268
| `/system/databases/mydb/id` | 51 |
1269
| `/system/tables/customer/id` | 42 |
1270
| `/system/desc/51/42/address` | 69 |
1271
| `/system/desc/51/42/url` | 66 |
1272
1273
(The numeric values on the right are chosen arbitrarily for the
1274
example; the structure of the schema keys on the left is simplified
1275
for the example and subject to change.) Each database/table/column
1276
name is mapped to a spontaneously generated identifier, so as to
1277
simplify renames.
1278
1279
Then for a single row in this table:
1280
1281
| Key | Values |
1282
| ----------------- | -------------------------------- |
1283
| `/51/42/Apple/69` | `1 Infinite Loop, Cupertino, CA` |
1284
| `/51/42/Apple/66` | `http://apple.com/` |
1285
1286
Each key has the table prefix `/51/42` followed by the primary key
1287
prefix `/Apple` followed by the column/family suffix (`/66`,
1288
`/69`). The KV value is directly encoded from the SQL value.
1289
1290
Efficient storage for the keys is guaranteed by the underlying RocksDB engine
1291
by means of prefix compression.
1292
1293
Finally, for SQL indexes, the KV key is formed using the SQL value of the
1294
indexed columns, and the KV value is the KV key prefix of the rest of
1295
the indexed row.
1296
1297
# References
1298
1299
[0]: http://rocksdb.org/
1300
[1]: https://github.com/google/leveldb
1301
[2]: https://ramcloud.stanford.edu/wiki/download/attachments/11370504/raft.pdf
1302
[3]: http://research.google.com/archive/spanner.html
1303
[4]: http://research.google.com/pubs/pub36971.html
1304
[5]: https://github.com/cockroachdb/cockroach/tree/master/sql
1305
[7]: https://godoc.org/github.com/cockroachdb/cockroach/kv
1306
[8]: https://github.com/cockroachdb/cockroach/tree/master/kv
1307
[9]: https://godoc.org/github.com/cockroachdb/cockroach/server
1308
[10]: https://github.com/cockroachdb/cockroach/tree/master/server
1309
[11]: https://godoc.org/github.com/cockroachdb/cockroach/storage
1310
[12]: https://github.com/cockroachdb/cockroach/tree/master/storage