Permalink
Newer
100644
1279 lines (1050 sloc)
61.1 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
Cockroach is a distributed key:value datastore (SQL and structured
8
data layers of cockroach have yet to be defined) which supports **ACID
9
transactional semantics** and **versioned values** as first-class
10
features. The primary design goal is **global consistency and
11
survivability**, hence the name. Cockroach aims to tolerate disk,
12
machine, rack, and even **datacenter failures** with minimal latency
13
disruption and **no manual intervention**. Cockroach nodes are
14
symmetric; a design goal is **homogenous deployment** (one binary) with
15
minimal configuration.
16
17
Cockroach implements a **single, monolithic sorted map** from key to
18
value where both keys and values are byte strings (not unicode).
19
Cockroach **scales linearly** (theoretically up to 4 exabytes (4E) of
20
logical data). The map is composed of one or more ranges and each range
21
is backed by data stored in [RocksDB](http://rocksdb.org/) (a
22
variant of LevelDB), and is replicated to a total of three or more
23
cockroach servers. Ranges are defined by start and end keys. Ranges are
24
merged and split to maintain total byte size within a globally
25
configurable min/max size interval. Range sizes default to target `64M` in
26
order to facilitate quick splits and merges and to distribute load at
27
hotspots within a key range. Range replicas are intended to be located
28
in disparate datacenters for survivability (e.g. `{ US-East, US-West,
29
Japan }`, `{ Ireland, US-East, US-West}`, `{ Ireland, US-East, US-West,
30
Japan, Australia }`).
31
32
Single mutations to ranges are mediated via an instance of a distributed
33
consensus algorithm to ensure consistency. We’ve chosen to use the
34
[Raft consensus algorithm](https://raftconsensus.github.io); all consensus
35
state is stored in RocksDB.
36
37
A single logical mutation may affect multiple key/value pairs. Logical
38
mutations have ACID transactional semantics. If all keys affected by a
39
logical mutation fall within the same range, atomicity and consistency
40
are guaranteed by Raft; this is the **fast commit path**. Otherwise, a
41
**non-locking distributed commit** protocol is employed between affected
42
ranges.
43
44
Cockroach provides [snapshot isolation](http://en.wikipedia.org/wiki/Snapshot_isolation) (SI) and
45
serializable snapshot isolation (SSI) semantics, allowing **externally
46
consistent, lock-free reads and writes**--both from a historical
47
snapshot timestamp and from the current wall clock time. SI provides
48
lock-free reads and writes but still allows write skew. SSI eliminates
49
write skew, but introduces a performance hit in the case of a
50
contentious system. SSI is the default isolation; clients must
51
consciously decide to trade correctness for performance. Cockroach
52
implements [a limited form of linearizability](#linearizability),
53
providing ordering for any observer or chain of observers.
54
55
Similar to
56
[Spanner](http://static.googleusercontent.com/media/research.google.com/en/us/archive/spanner-osdi2012.pdf)
57
directories, Cockroach allows configuration of arbitrary zones of data.
58
This allows replication factor, storage device type, and/or datacenter
59
location to be chosen to optimize performance and/or availability.
60
Unlike Spanner, zones are monolithic and don’t allow movement of fine
61
grained data on the level of entity groups.
62
63
# Architecture
64
65
Cockroach implements a layered architecture. The highest level of
66
abstraction is the SQL layer (currently unspecified in this document).
67
It depends directly on the [*structured data
68
API*](#structured-data-api), which provides familiar relational concepts
69
such as schemas, tables, columns, and indexes. The structured data API
70
in turn depends on the [distributed key value store](#key-value-api),
71
which handles the details of range addressing to provide the abstraction
72
of a single, monolithic key value store. The distributed KV store
73
communicates with any number of physical cockroach nodes. Each node
74
contains one or more stores, one per physical device.
75
76

77
78
Each store contains potentially many ranges, the lowest-level unit of
79
key-value data. Ranges are replicated using the Raft consensus protocol.
80
The diagram below is a blown up version of stores from four of the five
81
nodes in the previous diagram. Each range is replicated three ways using
82
raft. The color coding shows associated range replicas.
83
84

85
86
Each physical node exports a RoachNode service. Each RoachNode exports
87
one or more key ranges. RoachNodes are symmetric. Each has the same
88
binary and assumes identical roles.
89
90
Nodes and the ranges they provide access to can be arranged with various
91
physical network topologies to make trade offs between reliability and
92
performance. For example, a triplicated (3-way replica) range could have
93
each replica located on different:
94
95
- disks within a server to tolerate disk failures.
96
- servers within a rack to tolerate server failures.
97
- servers on different racks within a datacenter to tolerate rack power/network failures.
98
- servers in different datacenters to tolerate large scale network or power outages.
99
100
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).
101
102
# Cockroach Client
103
104
In order to support diverse client usage, Cockroach clients connect to
105
any node via HTTPS using protocol buffers or JSON. The connected node
106
proxies involved client work including key lookups and write buffering.
107
108
# Keys
109
110
Cockroach keys are arbitrary byte arrays. If textual data is used in
111
keys, utf8 encoding is recommended (this helps for cleaner display of
112
values in debugging tools). User-supplied keys are encoded using an
113
ordered code. System keys are either prefixed with null characters (`\0`
114
or `\0\0`) for system tables, or take the form of
115
`<user-key><system-suffix>` to sort user-key-range specific system
116
keys immediately after the user keys they refer to. Null characters are
117
used in system key prefixes to guarantee that they sort first.
118
119
# Versioned Values
120
121
Cockroach maintains historical versions of values by storing them with
122
associated commit timestamps. Reads and scans can specify a snapshot
123
time to return the most recent writes prior to the snapshot timestamp.
124
Older versions of values are garbage collected by the system during
125
compaction according to a user-specified expiration interval. In order
126
to support long-running scans (e.g. for MapReduce), all versions have a
127
minimum expiration.
128
129
Versioned values are supported via modifications to RocksDB to record
130
commit timestamps and GC expirations per key.
131
136
the low water mark of the cache appropriately. If a new range replica leader
137
is elected, it sets the low water mark for the cache to the current
140
# Lock-Free Distributed Transactions
141
142
Cockroach provides distributed transactions without locks. Cockroach
143
transactions support two isolation levels:
144
145
- snapshot isolation (SI) and
146
- *serializable* snapshot isolation (SSI).
147
148
*SI* is simple to implement, highly performant, and correct for all but a
149
handful of anomalous conditions (e.g. write skew). *SSI* requires just a touch
150
more complexity, is still highly performant (less so with contention), and has
151
no anomalous conditions. Cockroach’s SSI implementation is based on ideas from
152
the literature and some possibly novel insights.
153
154
SSI is the default level, with SI provided for application developers
155
who are certain enough of their need for performance and the absence of
156
write skew conditions to consciously elect to use it. In a lightly
157
contended system, our implementation of SSI is just as performant as SI,
158
requiring no locking or additional writes. With contention, our
159
implementation of SSI still requires no locking, but will end up
160
aborting more transactions. Cockroach’s SI and SSI implementations
161
prevent starvation scenarios even for arbitrarily long transactions.
162
163
See the [Cahill paper](https://drive.google.com/file/d/0B9GCVTp_FHJIcEVyZVdDWEpYYXVVbFVDWElrYUV0NHFhU2Fv/edit?usp=sharing)
164
for one possible implementation of SSI. This is another [great paper](http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf).
165
For a discussion of SSI implemented by preventing read-write conflicts
166
(in contrast to detecting them, called write-snapshot isolation), see
167
the [Yabandeh paper](https://drive.google.com/file/d/0B9GCVTp_FHJIMjJ2U2t6aGpHLTFUVHFnMTRUbnBwc2pLa1RN/edit?usp=sharing),
168
which is the source of much inspiration for Cockroach’s SSI.
169
170
Each Cockroach transaction is assigned a random priority and a
171
"candidate timestamp" at start. The candidate timestamp is the
172
provisional timestamp at which the transaction will commit, and is
173
chosen as the current clock time of the node coordinating the
174
transaction. This means that a transaction without conflicts will
175
usually commit with a timestamp that, in absolute time, precedes the
176
actual work done by that transaction.
177
178
In the course of coordinating a transaction between one or more
179
distributed nodes, the candidate timestamp may be increased, but will
181
SI and SSI is that the former allows the transaction's candidate
182
timestamp to increase and the latter does not.
187
in the [Hybrid Logical Clock paper](http://www.cse.buffalo.edu/tech-reports/2014-04.pdf).
188
HLC time uses timestamps which are composed of a physical component (thought of
189
as and always close to local wall time) and a logical component (used to
190
distinguish between events with the same physical component). It allows us to
191
track causality for related events similar to vector clocks, but with less
192
overhead. In practice, it works much like other logical clocks: When events
193
are received by a node, it informs the local HLC about the timestamp supplied
194
with the event by the sender, and when events are sent a timestamp generated by
195
the local HLC is attached.
196
197
For a more in depth description of HLC please read the paper. Our
198
implementation is [here](https://github.com/cockroachdb/cockroach/blob/master/util/hlc/hlc.go).
199
200
Cockroach picks a Timestamp for a transaction using HLC time. Throughout this
201
document, *timestamp* always refers to the HLC time which is a singleton
202
on each node. The HLC is updated by every read/write event on the node, and
203
the HLC time >= walltime. A read/write timestamp received in a cockroach request
204
from another node is not only used to version the operation, but also updates
205
the HLC on the node. This is useful in guaranteeing that all data read/written
206
on a node is at a timestamp < next HLC time.
213
transaction table (keys prefixed by *\0tx*) with state “PENDING”. In
214
parallel write an "intent" value for each datum being written as part
215
of the transaction. These are normal MVCC values, with the addition of
216
a special flag (i.e. “intent”) indicating that the value may be
218
the transaction id (unique and chosen at tx start time by client)
219
is stored with intent values. The tx id is used to refer to the
220
transaction table when there are conflicts and to make
221
tie-breaking decisions on ordering between identical timestamps.
223
original candidate timestamp in the absence of read/write conflicts);
224
the client selects the maximum from amongst all write timestamps as the
225
final commit timestamp.
228
transaction table (keys prefixed by *\0tx*). The value of the
229
commit entry contains the candidate timestamp (increased as
230
necessary to accommodate any latest read timestamps). Note that
231
the transaction is considered fully committed at this point and
232
control may be returned to the client.
233
234
In the case of an SI transaction, a commit timestamp which was
235
increased to accommodate concurrent readers is perfectly
236
acceptable and the commit may continue. For SSI transactions,
237
however, a gap between candidate and commit timestamps
238
necessitates transaction restart (note: restart is different than
239
abort--see below).
240
241
After the transaction is committed, all written intents are upgraded
242
in parallel by removing the “intent” flag. The transaction is
243
considered fully committed before this step and does not wait for
244
it to return control to the transaction coordinator.
245
246
In the absence of conflicts, this is the end. Nothing else is necessary
247
to ensure the correctness of the system.
248
249
**Conflict Resolution**
250
251
Things get more interesting when a reader or writer encounters an intent
252
record or newly-committed value in a location that it needs to read or
253
write. This is a conflict, usually causing either of the transactions to
254
abort or restart depending on the type of conflict.
255
256
***Transaction restart:***
257
258
This is the usual (and more efficient) type of behaviour and is used
259
except when the transaction was aborted (for instance by another
260
transaction).
261
In effect, that reduces to two cases; the first being the one outlined
262
above: An SSI transaction that finds upon attempting to commit that
263
its commit timestamp has been pushed. The second case involves a transaction
264
actively encountering a conflict, that is, one of its readers or writers
265
encounter data that necessitate conflict resolution
266
(see transaction interactions below).
270
begins anew reusing the same tx id. The prior run of the transaction might
271
have written some write intents, which need to be deleted before the
272
transaction commits, so as to not be included as part of the transaction.
273
These stale write intent deletions are done during the reexecution of the
275
the same keys as part of the reexecution of the transaction, or explicitly,
276
by cleaning up stale intents that are not part of the reexecution of the
277
transaction. Since most transactions will end up writing to the same keys,
278
the explicit cleanup run just before committing the transaction is usually
279
a NOOP.
280
281
***Transaction abort:***
282
283
This is the case in which a transaction, upon reading its transaction
284
table entry, finds that it has been aborted. In this case, the
285
transaction can not reuse its intents; it returns control to the client
286
before cleaning them up (other readers and writers would clean up
287
dangling intents as they encounter them) but will make an effort to
288
clean up after itself. The next attempt (if applicable) then runs as a
292
293
There are several scenarios in which transactions interact:
294
295
- **Reader encounters write intent or value with newer timestamp far
296
enough in the future**: This is not a conflict. The reader is free
297
to proceed; after all, it will be reading an older version of the
298
value and so does not conflict. Recall that the write intent may
299
be committed with a later timestamp than its candidate; it will
300
never commit with an earlier one. **Side note**: if a SI transaction
301
reader finds an intent with a newer timestamp which the reader’s own
303
304
- **Reader encounters write intent or value with newer timestamp in the
305
near future:** In this case, we have to be careful. The newer
306
intent may, in absolute terms, have happened in our read's past if
307
the clock of the writer is ahead of the node serving the values.
308
In that case, we would need to take this value into account, but
309
we just don't know. Hence the transaction restarts, using instead
310
a future timestamp (but remembering a maximum timestamp used to
311
limit the uncertainty window to the maximum clock skew). In fact,
312
this is optimized further; see the details under "choosing a time
313
stamp" below.
314
315
- **Reader encounters write intent with older timestamp**: the reader
316
must follow the intent’s transaction id to the transaction table.
317
If the transaction has already been committed, then the reader can
318
just read the value. If the write transaction has not yet been
319
committed, then the reader has two options. If the write conflict
320
is from an SI transaction, the reader can *push that transaction's
321
commit timestamp into the future* (and consequently not have to
322
read it). This is simple to do: the reader just updates the
323
transaction’s commit timestamp to indicate that when/if the
324
transaction does commit, it should use a timestamp *at least* as
325
high. However, if the write conflict is from an SSI transaction,
326
the reader must compare priorities. If the reader has the higher priority,
327
it pushes the transaction’s commit timestamp (that
335
priority, the writer aborts the conflicting transaction. If the write
336
intent has a higher or equal priority the transaction retries, using as a new
337
priority *max(new random priority, conflicting txn’s priority - 1)*;
338
the retry occurs after a short, randomized backoff interval.
340
- **Writer encounters newer committed value**:
341
The committed value could also be an unresolved write intent made by a
342
transaction that has already committed. The transaction restarts. On restart,
343
the same priority is reused, but the candidate timestamp is moved forward
344
to the encountered value's timestamp.
348
candidate timestamp is earlier than the low water mark on the cache itself
349
(i.e. its last evicted timestamp) or if the key being written has a read
350
timestamp later than the write’s candidate timestamp, this later timestamp
351
value is returned with the write. A new timestamp forces a transaction
352
restart only if it is serializable.
354
**Transaction management**
355
356
Transactions are managed by the client proxy (or gateway in SQL Azure
357
parlance). Unlike in Spanner, writes are not buffered but are sent
358
directly to all implicated ranges. This allows the transaction to abort
359
quickly if it encounters a write conflict. The client proxy keeps track
360
of all written keys in order to resolve write intents asynchronously upon
361
transaction completion. If a transaction commits successfully, all intents
362
are upgraded to committed. In the event a transaction is aborted, all written
363
intents are deleted. The client proxy doesn’t guarantee it will resolve intents.
367
transaction table until aborted by another transaction. Transactions
368
heartbeat the transaction table every five seconds by default.
369
Transactions encountered by readers or writers with dangling intents
370
which haven’t been heartbeat within the required interval are aborted.
375
376
An exploration of retries with contention and abort times with abandoned
377
transaction is
378
[here](https://docs.google.com/document/d/1kBCu4sdGAnvLqpT-_2vaTbomNmX3_saayWEGYu1j7mQ/edit?usp=sharing).
379
380
**Transaction Table**
381
382
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`.
383
384
**Pros**
385
386
- No requirement for reliable code execution to prevent stalled 2PC
387
protocol.
388
- Readers never block with SI semantics; with SSI semantics, they may
389
abort.
390
- Lower latency than traditional 2PC commit protocol (w/o contention)
391
because second phase requires only a single write to the
392
transaction table instead of a synchronous round to all
393
transaction participants.
394
- Priorities avoid starvation for arbitrarily long transactions and
395
always pick a winner from between contending transactions (no
396
mutual aborts).
397
- Writes not buffered at client; writes fail fast.
398
- No read-locking overhead required for *serializable* SI (in contrast
399
to other SSI implementations).
400
- Well-chosen (i.e. less random) priorities can flexibly give
401
probabilistic guarantees on latency for arbitrary transactions
402
(for example: make OLTP transactions 10x less likely to abort than
403
low priority transactions, such as asynchronously scheduled jobs).
404
405
**Cons**
406
407
- Reads from non-leader replicas still require a ping to the leader to
409
- Abandoned transactions may block contending writers for up to the
410
heartbeat interval, though average wait is likely to be
411
considerably shorter (see [graph in link](https://docs.google.com/document/d/1kBCu4sdGAnvLqpT-_2vaTbomNmX3_saayWEGYu1j7mQ/edit?usp=sharing)).
412
This is likely considerably more performant than detecting and
413
restarting 2PC in order to release read and write locks.
414
- Behavior different than other SI implementations: no first writer
415
wins, and shorter transactions do not always finish quickly.
416
Element of surprise for OLTP systems may be a problematic factor.
417
- Aborts can decrease throughput in a contended system compared with
418
two phase locking. Aborts and retries increase read and write
419
traffic, increase latency and decrease throughput.
420
421
**Choosing a Timestamp**
422
423
A key challenge of reading data in a distributed system with clock skew
424
is choosing a timestamp guaranteed to be greater than the latest
425
timestamp of any committed transaction (in absolute time). No system can
426
claim consistency and fail to read already-committed data.
427
429
accessing a single node is easy. The timestamp is assigned by the node
430
itself, so it is guaranteed to be at a greater timestamp than all the
431
existing timestamped data on the node.
432
433
For multiple nodes, the timestamp of the node coordinating the
434
transaction `t` is used. In addition, a maximum timestamp `t+ε` is
435
supplied to provide an upper bound on timestamps for already-committed
436
data (`ε` is the maximum clock skew). As the transaction progresses, any
437
data read which have timestamps greater than `t` but less than `t+ε`
438
cause the transaction to abort and retry with the conflicting timestamp
440
the same. This implies that transaction restarts due to clock uncertainty
441
can only happen on a time interval of length `ε`.
445
into account t<sub>c</sub>, but the timestamp of the node at the time
446
of the uncertain read t<sub>node</sub>. The larger of those two timestamps
447
t<sub>c</sub> and t<sub>node</sub> (likely equal to the latter) is used
448
to increase the read timestamp. Additionally, the conflicting node is
449
marked as “certain”. Then, for future reads to that node within the
450
transaction, we set `MaxTimestamp = Read Timestamp`, preventing further
451
uncertainty restarts.
452
453
Correctness follows from the fact that we know that at the time of the read,
454
there exists no version of any key on that node with a higher timestamp than
456
encounters a key with a higher timestamp, it knows that in absolute time,
457
the value was written after t<sub>node</sub> was obtained, i.e. after the
458
uncertain read. Hence the transaction can move forward reading an older version
459
of the data (at the transaction's timestamp). This limits the time uncertainty
460
restarts attributed to a node to at most one. The tradeoff is that we might
461
pick a timestamp larger than the optimal one (> highest conflicting timestamp),
462
resulting in the possibility of a few more conflicts.
463
464
We expect retries will be rare, but this assumption may need to be
465
revisited if retries become problematic. Note that this problem does not
466
apply to historical reads. An alternate approach which does not require
467
retries makes a round to all node participants in advance and
468
chooses the highest reported node wall time as the timestamp. However,
469
knowing which nodes will be accessed in advance is difficult and
470
potentially limiting. Cockroach could also potentially use a global
471
clock (Google did this with [Percolator](https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Peng.pdf)),
472
which would be feasible for smaller, geographically-proximate clusters.
473
474
# Linearizability
475
476
First a word about [***Spanner***](http://research.google.com/archive/spanner.html).
477
By combining judicious use of wait intervals with accurate time signals,
478
Spanner provides a global ordering between any two non-overlapping transactions
479
(in absolute time) with \~14ms latencies. Put another way:
480
Spanner guarantees that if a transaction T<sub>1</sub> commits (in absolute time)
481
before another transaction T<sub>2</sub> starts, then T<sub>1</sub>'s assigned commit
482
timestamp is smaller than T<sub>2</sub>'s. Using atomic clocks and GPS receivers,
483
Spanner reduces their clock skew uncertainty to \< 10ms (`ε`). To make
484
good on the promised guarantee, transactions must take at least double
485
the clock skew uncertainty interval to commit (`2ε`). See [*this
486
article*](http://www.cs.cornell.edu/~ie53/publications/DC-col51-Sep13.pdf)
487
for a helpful overview of Spanner’s concurrency control.
488
489
Cockroach could make the same guarantees without specialized hardware,
490
at the expense of longer wait times. If servers in the cluster were
491
configured to work only with NTP, transaction wait times would likely to
492
be in excess of 150ms. For wide-area zones, this would be somewhat
493
mitigated by overlap from cross datacenter link latencies. If clocks
494
were made more accurate, the minimal limit for commit latencies would
495
improve.
496
497
However, let’s take a step back and evaluate whether Spanner’s external
498
consistency guarantee is worth the automatic commit wait. First, if the
499
commit wait is omitted completely, the system still yields a consistent
500
view of the map at an arbitrary timestamp. However with clock skew, it
501
would become possible for commit timestamps on non-overlapping but
502
causally related transactions to suffer temporal reverse. In other
503
words, the following scenario is possible for a client without global
504
ordering:
505
506
- Start transaction T<sub>1</sub> to modify value `x` with commit time s<sub>1</sub>
507
508
- On commit of T<sub>1</sub>, start T<sub>2</sub> to modify value `y` with commit time
510
511
- Read `x` and `y` and discover that s<sub>1</sub> \> s<sub>2</sub> (**!**)
512
513
The external consistency which Spanner guarantees is referred to as
514
**linearizability**. It goes beyond serializability by preserving
515
information about the causality inherent in how external processes
516
interacted with the database. The strength of Spanner’s guarantee can be
517
formulated as follows: any two processes, with clock skew within
518
expected bounds, may independently record their wall times for the
519
completion of transaction T<sub>1</sub> (T<sub>1</sub><sup>end</sup>) and start of transaction
520
T<sub>2</sub> (T<sub>2</sub><sup>start</sup>) respectively, and if later
521
compared such that T<sub>1</sub><sup>end</sup> \< T<sub>2</sub><sup>start</sup>,
522
then commit timestamps s<sub>1</sub> \< s<sub>2</sub>.
523
This guarantee is broad enough to completely cover all cases of explicit
524
causality, in addition to covering any and all imaginable scenarios of implicit
525
causality.
526
527
Our contention is that causality is chiefly important from the
528
perspective of a single client or a chain of successive clients (*if a
529
tree falls in the forest and nobody hears…*). As such, Cockroach
530
provides two mechanisms to provide linearizability for the vast majority
531
of use cases without a mandatory transaction commit wait or an elaborate
532
system to minimize clock skew.
533
534
1. Clients provide the highest transaction commit timestamp with
535
successive transactions. This allows node clocks from previous
536
transactions to effectively participate in the formulation of the
537
commit timestamp for the current transaction. This guarantees
538
linearizability for transactions committed by this client.
539
540
Newly launched clients wait at least 2 \* ε from process start
541
time before beginning their first transaction. This preserves the
542
same property even on client restart, and the wait will be
543
mitigated by process initialization.
544
545
All causally-related events within Cockroach maintain
546
linearizability.
547
548
2. Committed transactions respond with a commit wait parameter which
549
represents the remaining time in the nominal commit wait. This
550
will typically be less than the full commit wait as the consensus
551
write at the coordinator accounts for a portion of it.
552
553
Clients taking any action outside of another Cockroach transaction
554
(e.g. writing to another distributed system component) can either
555
choose to wait the remaining interval before proceeding, or
556
alternatively, pass the wait and/or commit timestamp to the
557
execution of the outside action for its consideration. This pushes
558
the burden of linearizability to clients, but is a useful tool in
559
mitigating commit latencies if the clock skew is potentially
560
large. This functionality can be used for ordering in the face of
561
backchannel dependencies as mentioned in the
562
[AugmentedTime](http://www.cse.buffalo.edu/~demirbas/publications/augmentedTime.pdf)
563
paper.
564
565
Using these mechanisms in place of commit wait, Cockroach’s guarantee can be
566
formulated as follows: any process which signals the start of transaction
567
T<sub>2</sub> (T<sub>2</sub><sup>start</sup>) after the completion of
568
transaction T<sub>1</sub> (T<sub>1</sub><sup>end</sup>), will have commit
569
timestamps such thats<sub>1</sub> \< s<sub>2</sub>.
570
571
# Logical Map Content
572
573
Logically, the map contains a series of reserved system key / value
574
pairs covering accounting, range metadata, node accounting and
575
permissions before the actual key / value pairs for non-system data
576
(e.g. the actual meat of the map).
577
578
- `\0\0meta1` Range metadata for location of `\0\0meta2`.
579
- `\0\0meta1<key1>` Range metadata for location of `\0\0meta2<key1>`.
580
- ...
581
- `\0\0meta1<keyN>`: Range metadata for location of `\0\0meta2<keyN>`.
582
- `\0\0meta2`: Range metadata for location of first non-range metadata key.
583
- `\0\0meta2<key1>`: Range metadata for location of `<key1>`.
584
- ...
585
- `\0\0meta2<keyN>`: Range metadata for location of `<keyN>`.
586
- `\0acct<key0>`: Accounting for key prefix key0.
587
- ...
588
- `\0acct<keyN>`: Accounting for key prefix keyN.
589
- `\0node<node-address0>`: Accounting data for node 0.
590
- ...
591
- `\0node<node-addressN>`: Accounting data for node N.
592
- `\0perm<key0><user0>`: Permissions for user0 for key prefix key0.
593
- ...
594
- `\0perm<keyN><userN>`: Permissions for userN for key prefix keyN.
595
- `\0tree_root`: Range key for root of range-spanning tree.
596
- `\0tx<tx-id0>`: Transaction record for transaction 0.
597
- ...
598
- `\0tx<tx-idN>`: Transaction record for transaction N.
599
- `\0zone<key0>`: Zone information for key prefix key0.
600
- ...
601
- `\0zone<keyN>`: Zone information for key prefix keyN.
602
- `<>acctd<metric0>`: Accounting data for Metric 0 for empty key prefix.
603
- ...
604
- `<>acctd<metricN>`: Accounting data for Metric N for empty key prefix.
605
- `<key0>`: `<value0>` The first user data key.**
606
- ...
607
- `<keyN>`: `<valueN>` The last user data key.**
608
609
There are some additional system entries sprinkled amongst the
610
non-system keys. See the Key-Prefix Accounting section in this document
611
for further details.
612
613
# Node Storage
614
615
Nodes maintain a separate instance of RocksDB for each disk. Each
616
RocksDB instance hosts any number of ranges. RPCs arriving at a
617
RoachNode are multiplexed based on the disk name to the appropriate
618
RocksDB instance. A single instance per disk is used to avoid
619
contention. If every range maintained its own RocksDB, global management
620
of available cache memory would be impossible and writers for each range
621
would compete for non-contiguous writes to multiple RocksDB logs.
622
623
In addition to the key/value pairs of the range itself, various range
624
metadata is maintained.
625
626
- range-spanning tree node links
627
628
- participating replicas
629
630
- consensus metadata
631
632
- split/merge activity
633
634
A really good reference on tuning Linux installations with RocksDB is
635
[here](http://docs.basho.com/riak/latest/ops/advanced/backends/leveldb/).
636
637
# Range Metadata
638
639
The default approximate size of a range is 64M (2\^26 B). In order to
640
support 1P (2\^50 B) of logical data, metadata is needed for roughly
641
2\^(50 - 26) = 2\^24 ranges. A reasonable upper bound on range metadata
643
locations and 220 bytes for the range key itself*). 2\^24 ranges \* 2\^8
644
B would require roughly 4G (2\^32 B) to store--too much to duplicate
645
between machines. Our conclusion is that range metadata must be
646
distributed for large installations.
647
648
To keep key lookups relatively fast in the presence of distributed metadata,
649
we store all the top-level metadata in a single range (the first range). These
650
top-level metadata keys are known as *meta1* keys, and are prefixed such that
651
they sort to the beginning of the key space. Given the metadata size of 256
652
bytes given above, a single 64M range would support 64M/256B = 2\^18 ranges,
654
above, we need two levels of indirection, where the first level addresses the
655
second, and the second addresses user data. With two levels of indirection, we
656
can address 2\^(18 + 18) = 2\^36 ranges; each range addresses 2\^26 B, and
657
altogether we address 2\^(36+26) B = 2\^62 B = 4E of user data.
658
659
For a given user-addressable `key1`, the associated *meta1* record is found
660
at the successor key to `key1` in the *meta1* space. Since the *meta1* space
661
is sparse, the successor key is defined as the next key which is present. The
662
*meta1* record identifies the range containing the *meta2* record, which is
663
found using the same process. The *meta2* record identifies the range
664
containing `key1`, which is again found the same way (see examples below).
666
Concretely, metadata keys are prefixed by `\0\0meta{1,2}`; the two null
667
characters provide for the desired sorting behaviour. Thus, `key1`'s
668
*meta1* record will reside at the successor key to `\0\0\meta1<key1>`.
669
671
the RocksDB iterator only supports a Seek() interface which acts as a
672
Ceil(). Using the start key of the range would cause Seek() to find the
673
key *after* the meta indexing record we’re looking for, which would
674
result in having to back the iterator up, an option which is both less
675
efficient and not available in all cases.
676
677
The following example shows the directory structure for a map with
678
three ranges worth of data. Ellipses indicate additional key/value pairs to
679
fill an entire range of data. Except for the fact that splitting ranges
680
requires updates to the range metadata with knowledge of the metadata layout,
681
the range metadata itself requires no special treatment or bootstrapping.
682
683
**Range 0** (located on servers `dcrama1:8000`, `dcrama2:8000`,
684
`dcrama3:8000`)
685
686
- `\0\0meta1\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
687
- `\0\0meta2<lastkey0>`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
688
- `\0\0meta2<lastkey1>`: `dcrama4:8000`, `dcrama5:8000`, `dcrama6:8000`
689
- `\0\0meta2\xff`: `dcrama7:8000`, `dcrama8:8000`, `dcrama9:8000`
690
- ...
691
- `<lastkey0>`: `<lastvalue0>`
692
693
**Range 1** (located on servers `dcrama4:8000`, `dcrama5:8000`,
694
`dcrama6:8000`)
695
696
- ...
697
- `<lastkey1>`: `<lastvalue1>`
698
699
**Range 2** (located on servers `dcrama7:8000`, `dcrama8:8000`,
700
`dcrama9:8000`)
701
702
- ...
703
- `<lastkey2>`: `<lastvalue2>`
704
705
Consider a simpler example of a map containing less than a single
706
range of data. In this case, all range metadata and all data are
707
located in the same range:
708
709
**Range 0** (located on servers `dcrama1:8000`, `dcrama2:8000`,
710
`dcrama3:8000`)*
711
712
- `\0\0meta1\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
713
- `\0\0meta2\xff`: `dcrama1:8000`, `dcrama2:8000`, `dcrama3:8000`
714
- `<key0>`: `<value0>`
715
- `...`
716
717
Finally, a map large enough to need both levels of indirection would
718
look like (note that instead of showing range replicas, this
719
example is simplified to just show range indexes):
720
721
**Range 0**
722
723
- `\0\0meta1<lastkeyN-1>`: Range 0
724
- `\0\0meta1\xff`: Range 1
725
- `\0\0meta2<lastkey1>`: Range 1
726
- `\0\0meta2<lastkey2>`: Range 2
727
- `\0\0meta2<lastkey3>`: Range 3
728
- ...
729
- `\0\0meta2<lastkeyN-1>`: Range 262143
730
731
**Range 1**
732
733
- `\0\0meta2<lastkeyN>`: Range 262144
734
- `\0\0meta2<lastkeyN+1>`: Range 262145
735
- ...
736
- `\0\0meta2\xff`: Range 500,000
737
- ...
738
- `<lastkey1>`: `<lastvalue1>`
739
740
**Range 2**
741
742
- ...
743
- `<lastkey2>`: `<lastvalue2>`
744
745
**Range 3**
746
747
- ...
748
- `<lastkey3>`: `<lastvalue3>`
749
750
**Range 262144**
751
752
- ...
753
- `<lastkeyN>`: `<lastvalueN>`
754
755
**Range 262145**
756
757
- ...
758
- `<lastkeyN+1>`: `<lastvalueN+1>`
759
760
Note that the choice of range `262144` is just an approximation. The
761
actual number of ranges addressable via a single metadata range is
762
dependent on the size of the keys. If efforts are made to keep key sizes
763
small, the total number of addressable ranges would increase and vice
764
versa.
765
766
From the examples above it’s clear that key location lookups require at
767
most three reads to get the value for `<key>`:
768
769
1. lower bound of `\0\0meta1<key>`
770
2. lower bound of `\0\0meta2<key>`,
771
3. `<key>`.
772
773
For small maps, the entire lookup is satisfied in a single RPC to Range 0. Maps
774
containing less than 16T of data would require two lookups. Clients cache both
775
levels of range metadata, and we expect that data locality for individual
776
clients will be high. Clients may end up with stale cache entries. If on a
777
lookup, the range consulted does not match the client’s expectations, the
778
client evicts the stale entries and possibly does a new lookup.
779
782
Each range is configured to consist of three or more replicas, as specified by
783
their ZoneConfig. The replicas in a range maintain their own instance of a
784
distributed consensus algorithm. We use the [*Raft consensus algorithm*](https://raftconsensus.github.io)
787
[ePaxos](https://www.cs.cmu.edu/~dga/papers/epaxos-sosp2013.pdf) has
788
promising performance characteristics for WAN-distributed replicas, but
789
it does not guarantee a consistent ordering between replicas.
790
791
Raft elects a relatively long-lived leader which must be involved to
793
replicated. In the absence of heartbeats, followers become candidates
794
after randomized election timeouts and proceed to hold new leader
795
elections. Cockroach weights random timeouts such that the replicas with
796
shorter round trip times to peers are more likely to hold elections
797
first (not implemented yet). Only the Raft leader may propose commands;
798
followers will simply relay commands to the last known leader.
800
Our Raft implementation was developed together with CoreOS, but adds an extra
801
layer of optimization to account for the fact that a single Node may have
802
millions of consensus groups (one for each Range). Areas of optimization
803
are chiefly coalesced heartbeats (so that the number of nodes dictates the
804
number of heartbeats as opposed to the much larger number of ranges) and
805
batch processing of requests.
806
Future optimizations may include two-phase elections and quiescent ranges
807
(i.e. stopping traffic completely for inactive ranges).
808
809
# Range Leadership (Leader Leases)
810
811
As outlined in the Raft section, the replicas of a Range are organized as a
812
Raft group and execute commands from their shared commit log. Going through
813
Raft is an expensive operation though, and there are tasks which should only be
814
carried out by a single replica at a time (as opposed to all of them).
815
816
For these reasons, Cockroach introduces the concept of **Range Leadership**:
817
This is a lease held for a slice of (database, i.e. hybrid logical) time and is
818
established by committing a special log entry through Raft containing the
819
interval the leadership is going to be active on, along with the Node:RaftID
820
combination that uniquely describes the requesting replica. Reads and writes
821
must generally be addressed to the replica holding the lease; if none does, any
822
replica may be addressed, causing it to try to obtain the lease synchronously.
823
Requests received by a non-leader (for the HLC timestamp specified in the
824
request's header) fail with an error pointing at the replica's last known
825
leader. These requests are retried transparently with the updated leader by the
826
gateway node and never reach the client.
827
828
The replica holding the lease is in charge or involved in handling
829
Range-specific maintenance tasks such as
830
831
* gossiping the sentinel and/or first range information
832
* splitting, merging and rebalancing
833
834
and, very importantly, may satisfy reads locally, without incurring the
835
overhead of going through Raft.
836
837
Since reads bypass Raft, a new lease holder will, among other things, ascertain
838
that its timestamp cache does not report timestamps smaller than the previous
839
lease holder's (so that it's compatible with reads which may have occurred on
840
the former leader). This is accomplished by setting the low water mark of the
841
timestamp cache to the expiration of the previous lease plus the maximum clock
842
offset.
843
844
## Relationship to Raft leadership
845
846
Range leadership is completely separate from Raft leadership, and so without
847
further efforts, Raft and Range leadership may not be represented by the same
848
replica most of the time. This is convenient semantically since it decouples
849
these two types of leadership and allows the use of Raft as a "black box", but
850
for reasons of performance, it is desirable to have both on the same replica.
851
Otherwise, sending a command through Raft always incurs the overhead of being
852
proposed to the Range leader's Raft instance first, which must relay it to the
853
Raft leader, which finally commits it into the log and updates its followers,
854
including the Range leader. This yields correct results but wastes several
855
round-trip delays, and so we will make sure that in the vast majority of cases
856
Range and Raft leadership coincide. A fairly easy method for achieving this is
857
to have each new lease period (extension or new) be accompanied by a
858
stipulation to the lease holder's replica to start Raft elections (unless it's
859
already leading), though some care should be taken that Range leadership is
860
relatively stable and long-lived to avoid a large number of Raft leadership
861
transitions.
862
863
## Command Execution Flow
864
865
This subsection describes how a leader replica processes a read/write
866
command in more details. Each command specifies (1) a key (or a range
867
of keys) that the command accesses and (2) the ID of a range which the
868
key(s) belongs to. When receiving a command, a RoachNode looks up a
869
range by the specified Range ID and checks if the range is still
870
responsible for the supplied keys. If any of the keys do not belong to the
871
range, the RoachNode returns an error so that the client will retry
872
and send a request to a correct range.
873
874
When all the keys belong to the range, the RoachNode attempts to
875
process the command. If the command is an inconsistent read-only
876
command, it is processed immediately. If the command is a consistent
877
read or a write, the command is executed when both of the following
878
conditions hold:
879
880
- The range replica has a leader lease.
881
- There are no other running commands whose keys overlap with
882
the submitted command and cause read/write conflict.
883
884
When the first condition is not met, the replica attempts to acquire
885
a lease or returns an error so that the client will redirect the
886
command to the current leader. The second condition guarantees that
887
consistent read/write commands for a given key are sequentially
888
executed.
889
890
When the above two conditions are met, the leader replica processes the
891
command. Consistent reads are processed on the leader immediately.
892
Write commands are commited into the Raft log so that every replica
893
will execute the same commands. All commands produce deterministic
894
results so that the range replicas keep consistent states among them.
895
896
When a write command completes, all the replica updates their response
897
cache to ensure idempotency. When a read command completes, the leader
898
replica updates its timestamp cache to keep track of the latest read
899
for a given key.
900
901
There is a chance that a leader lease gets expired while a command is
902
executed. Before executing a command, each replica checks if a replica
903
proposing the command has a still lease. When the lease has been
904
expired, the command will be rejected by the replica.
905
906
907
# Splitting / Merging Ranges
908
909
RoachNodes split or merge ranges based on whether they exceed maximum or
910
minimum thresholds for capacity or load. Ranges exceeding maximums for
911
either capacity or load are split; ranges below minimums for *both*
912
capacity and load are merged.
913
914
Ranges maintain the same accounting statistics as accounting key
915
prefixes. These boil down to a time series of data points with minute
916
granularity. Everything from number of bytes to read/write queue sizes.
917
Arbitrary distillations of the accounting stats can be determined as the
919
split/merge are range size in bytes and IOps. A good metric for
920
rebalancing a replica from one node to another would be total read/write
921
queue wait times. These metrics are gossipped, with each range / node
922
passing along relevant metrics if they’re in the bottom or top of the
923
range it’s aware of.
924
925
A range finding itself exceeding either capacity or load threshold
926
splits. To this end, the range leader computes an appropriate split key
927
candidate and issues the split through Raft. In contrast to splitting,
928
merging requires a range to be below the minimum threshold for both
929
capacity *and* load. A range being merged chooses the smaller of the
930
ranges immediately preceding and succeeding it.
931
932
Splitting, merging, rebalancing and recovering all follow the same basic
933
algorithm for moving data between roach nodes. New target replicas are
934
created and added to the replica set of source range. Then each new
935
replica is brought up to date by either replaying the log in full or
936
copying a snapshot of the source replica data and then replaying the log
937
from the timestamp of the snapshot to catch up fully. Once the new
938
replicas are fully up to date, the range metadata is updated and old,
939
source replica(s) deleted if applicable.
940
941
**Coordinator** (leader replica)
942
943
```
944
if splitting
945
SplitRange(split_key): splits happen locally on range replicas and
946
only after being completed locally, are moved to new target replicas.
947
else if merging
948
Choose new replicas on same servers as target range replicas;
949
add to replica set.
950
else if rebalancing || recovering
951
Choose new replica(s) on least loaded servers; add to replica set.
952
```
953
954
**New Replica**
955
956
*Bring replica up to date:*
957
958
```
959
if all info can be read from replicated log
960
copy replicated log
961
else
962
snapshot source replica
963
send successive ReadRange requests to source replica
964
referencing snapshot
965
966
if merging
967
combine ranges on all replicas
968
else if rebalancing || recovering
969
remove old range replica(s)
970
```
971
972
RoachNodes split ranges when the total data in a range exceeds a
973
configurable maximum threshold. Similarly, ranges are merged when the
974
total data falls below a configurable minimum threshold.
975
976
**TBD: flesh this out**: Especially for merges (but also rebalancing) we have a
977
range disappearing from the local node; that range needs to disappear
978
gracefully, with a smooth handoff of operation to the new owner of its data.
979
980
Ranges are rebalanced if a node determines its load or capacity is one
981
of the worst in the cluster based on gossipped load stats. A node with
982
spare capacity is chosen in the same datacenter and a special-case split
983
is done which simply duplicates the data 1:1 and resets the range
984
configuration metadata.
985
986
# Range-Spanning Binary Tree
987
988
A crucial enhancement to the organization of range metadata is to
989
augment the bi-level range metadata lookup with a minimum spanning tree,
990
implemented as a left-leaning red-black tree over all ranges in the map.
991
This tree structure allows the system to start at any key prefix and
992
efficiently traverse an arbitrary key range with minimal RPC traffic,
993
minimal fan-in and fan-out, and with bounded time complexity equal to
994
`2*log N` steps, where `N` is the total number of ranges in the system.
995
996
Unlike the range metadata rows prefixed with `\0\0meta[1|2]`, the
997
metadata for the range-spanning tree (e.g. parent range and left / right
998
child ranges) is stored directly at the ranges as non-map metadata. The
999
metadata for each node of the tree (e.g. links to parent range, left
1000
child range, and right child range) is stored with the range metadata.
1001
In effect, the tree metadata is stored implicitly. In order to traverse
1002
the tree, for example, you’d need to query each range in turn for its
1003
metadata.
1004
1005
Any time a range is split or merged, both the bi-level range lookup
1006
metadata and the per-range binary tree metadata are updated as part of
1007
the same distributed transaction. The total number of nodes involved in
1008
the update is bounded by 2 + log N (i.e. 2 updates for meta1 and
1009
meta2, and up to log N updates to balance the range-spanning tree).
1010
The range corresponding to the root node of the tree is stored in
1011
*\0tree_root*.
1012
1013
As an example, consider the following set of nine ranges and their
1014
associated range-spanning tree:
1015
1016
R0: `aa - cc`, R1: `*cc - lll`, R2: `*lll - llr`, R3: `*llr - nn`, R4: `*nn - rr`, R5: `*rr - ssss`, R6: `*ssss - sst`, R7: `*sst - vvv`, R8: `*vvv - zzzz`.
1017
1018

1019
1020
The range-spanning tree has many beneficial uses in Cockroach. It
1021
provides a ready made solution to scheduling mappers and sorting /
1022
reducing during map-reduce operations. It also provides a mechanism
1023
for visiting every Raft replica range which comprises a logical key
1024
range. This is used to periodically find the oldest extant write
1025
intent over the entire system.
1026
1027
The range-spanning tree provides a convenient mechanism for planning
1028
and executing parallel queries. These provide the basis for
1029
[Dremel](http://static.googleusercontent.com/media/research.google.com/en/us/pubs/archive/36632.pdf)-like
1030
query execution trees and it’s easy to imagine supporting a subset of
1031
SQL or even javascript-based user functions for complex data analysis
1032
tasks.
1033
1034
1035
1036
# Node Allocation (via Gossip)
1037
1038
New nodes must be allocated when a range is split. Instead of requiring
1039
every RoachNode to know about the status of all or even a large number
1040
of peer nodes --or-- alternatively requiring a specialized curator or
1041
master with sufficiently global knowledge, we use a gossip protocol to
1042
efficiently communicate only interesting information between all of the
1043
nodes in the cluster. What’s interesting information? One example would
1044
be whether a particular node has a lot of spare capacity. Each node,
1045
when gossiping, compares each topic of gossip to its own state. If its
1046
own state is somehow “more interesting” than the least interesting item
1047
in the topic it’s seen recently, it includes its own state as part of
1048
the next gossip session with a peer node. In this way, a node with
1049
capacity sufficiently in excess of the mean quickly becomes discovered
1050
by the entire cluster. To avoid piling onto outliers, nodes from the
1051
high capacity set are selected at random for allocation.
1052
1053
The gossip protocol itself contains two primary components:
1054
1055
- **Peer Selection**: each node maintains up to N peers with which it
1056
regularly communicates. It selects peers with an eye towards
1057
maximizing fanout. A peer node which itself communicates with an
1058
array of otherwise unknown nodes will be selected over one which
1059
communicates with a set containing significant overlap. Each time
1060
gossip is initiated, each nodes’ set of peers is exchanged. Each
1061
node is then free to incorporate the other’s peers as it sees fit.
1062
To avoid any node suffering from excess incoming requests, a node
1063
may refuse to answer a gossip exchange. Each node is biased
1064
towards answering requests from nodes without significant overlap
1065
and refusing requests otherwise.
1066
1067
Peers are efficiently selected using a heuristic as described in
1068
[Agarwal & Trachtenberg (2006)](https://drive.google.com/file/d/0B9GCVTp_FHJISmFRTThkOEZSM1U/edit?usp=sharing).
1069
1070
**TBD**: how to avoid partitions? Need to work out a simulation of
1071
the protocol to tune the behavior and see empirically how well it
1072
works.
1073
1074
- **Gossip Selection**: what to communicate. Gossip is divided into
1075
topics. Load characteristics (capacity per disk, cpu load, and
1076
state [e.g. draining, ok, failure]) are used to drive node
1077
allocation. Range statistics (range read/write load, missing
1078
replicas, unavailable ranges) and network topology (inter-rack
1079
bandwidth/latency, inter-datacenter bandwidth/latency, subnet
1080
outages) are used for determining when to split ranges, when to
1081
recover replicas vs. wait for network connectivity, and for
1082
debugging / sysops. In all cases, a set of minimums and a set of
1083
maximums is propagated; each node applies its own view of the
1084
world to augment the values. Each minimum and maximum value is
1085
tagged with the reporting node and other accompanying contextual
1086
information. Each topic of gossip has its own protobuf to hold the
1087
structured data. The number of items of gossip in each topic is
1088
limited by a configurable bound.
1089
1090
For efficiency, nodes assign each new item of gossip a sequence
1091
number and keep track of the highest sequence number each peer
1092
node has seen. Each round of gossip communicates only the delta
1093
containing new items.
1094
1095
# Node Accounting
1096
1097
The gossip protocol discussed in the previous section is useful to
1098
quickly communicate fragments of important information in a
1099
decentralized manner. However, complete accounting for each node is also
1100
stored to a central location, available to any dashboard process. This
1101
is done using the map itself. Each node periodically writes its state to
1102
the map with keys prefixed by `\0node`, similar to the first level of
1103
range metadata, but with an ‘`node`’ suffix. Each value is a protobuf
1104
containing the full complement of node statistics--everything
1105
communicated normally via the gossip protocol plus other useful, but
1106
non-critical data.
1107
1108
The range containing the first key in the node accounting table is
1109
responsible for gossiping the total count of nodes. This total count is
1110
used by the gossip network to most efficiently organize itself. In
1111
particular, the maximum number of hops for gossipped information to take
1112
before reaching a node is given by `ceil(log(node count) / log(max
1113
fanout)) + 1`.
1114
1115
# Key-prefix Accounting, Zones & Permissions
1116
1117
Arbitrarily fine-grained accounting and permissions are specified via
1118
key prefixes. Key prefixes can overlap, as is necessary for capturing
1119
hierarchical relationships. For illustrative purposes, let’s say keys
1120
specifying rows in a set of databases have the following format:
1121
1122
`<db>:<table>:<primary-key>[:<secondary-key>]`
1123
1124
In this case, we might collect accounting or specify permissions with
1125
key prefixes:
1126
1127
`db1`, `db1:user`, `db1:order`,
1128
1129
Accounting is kept for the entire map by default.
1130
1131
## Accounting
1132
to keep accounting for a range defined by a key prefix, an entry is created in
1133
the accounting system table. The format of accounting table keys is:
1134
1135
`\0acct<key-prefix>`
1136
1137
In practice, we assume each RoachNode capable of caching the
1138
entire accounting table as it is likely to be relatively small.
1139
1140
Accounting is kept for key prefix ranges with eventual consistency for
1141
efficiency. There are two types of values which comprise accounting:
1142
counts and occurrences, for lack of better terms. Counts describe
1143
system state, such as the total number of bytes, rows,
1144
etc. Occurrences include transient performance and load metrics. Both
1145
types of accounting are captured as time series with minute
1146
granularity. The length of time accounting metrics are kept is
1147
configurable. Below are examples of each type of accounting value.
1148
1149
**System State Counters/Performance**
1150
1151
- Count of items (e.g. rows)
1152
- Total bytes
1153
- Total key bytes
1154
- Total value length
1155
- Queued message count
1156
- Queued message total bytes
1157
- Count of values \< 16B
1158
- Count of values \< 64B
1159
- Count of values \< 256B
1160
- Count of values \< 1K
1161
- Count of values \< 4K
1162
- Count of values \< 16K
1163
- Count of values \< 64K
1164
- Count of values \< 256K
1165
- Count of values \< 1M
1166
- Count of values \> 1M
1167
- Total bytes of accounting
1168
1169
1170
**Load Occurrences**
1171
1172
- Get op count
1173
- Get total MB
1174
- Put op count
1175
- Put total MB
1176
- Delete op count
1177
- Delete total MB
1178
- Delete range op count
1179
- Delete range total MB
1180
- Scan op count
1181
- Scan op MB
1182
- Split count
1183
- Merge count
1184
1185
Because accounting information is kept as time series and over many
1186
possible metrics of interest, the data can become numerous. Accounting
1187
data are stored in the map near the key prefix described, in order to
1188
distribute load (for both aggregation and storage).
1189
1190
Accounting keys for system state have the form:
1191
`<key-prefix>|acctd<metric-name>*`. Notice the leading ‘pipe’
1192
character. It’s meant to sort the root level account AFTER any other
1193
system tables. They must increment the same underlying values as they
1194
are permanent counts, and not transient activity. Logic at the
1195
RoachNode takes care of snapshotting the value into an appropriately
1196
suffixed (e.g. with timestamp hour) multi-value time series entry.
1197
1198
Keys for perf/load metrics:
1199
`<key-prefix>acctd<metric-name><hourly-timestamp>`.
1200
1201
`<hourly-timestamp>`-suffixed accounting entries are multi-valued,
1202
containing a varint64 entry for each minute with activity during the
1203
specified hour.
1204
1205
To efficiently keep accounting over large key ranges, the task of
1206
aggregation must be distributed. If activity occurs within the same
1207
range as the key prefix for accounting, the updates are made as part
1208
of the consensus write. If the ranges differ, then a message is sent
1209
to the parent range to increment the accounting. If upon receiving the
1210
message, the parent range also does not include the key prefix, it in
1211
turn forwards it to its parent or left child in the balanced binary
1212
tree which is maintained to describe the range hierarchy. This limits
1213
the number of messages before an update is visible at the root to `2*log N`,
1214
where `N` is the number of ranges in the key prefix.
1215
1216
## Zones
1217
zones are stored in the map with keys prefixed by
1218
`\0zone` followed by the key prefix to which the zone
1219
configuration applies. Zone values specify a protobuf containing
1220
the datacenters from which replicas for ranges which fall under
1221
the zone must be chosen.
1222
1223
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`.
1224
1225
If zones are modified in situ, each RoachNode verifies the
1226
existing zones for its ranges against the zone configuration. If
1227
it discovers differences, it reconfigures ranges in the same way
1228
that it rebalances away from busy nodes, via special-case 1:1
1229
split to a duplicate range comprising the new configuration.
1230
1232
permissions are stored in the map with keys prefixed by *\0perm* followed by
1233
the key prefix and user to which the specified permissions apply. The format of
1234
permissions keys is:
1235
1236
`\0perm<key-prefix><user>`
1237
1239
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 PermConfig`.
1240
1241
A default system root permission is assumed for the entire map
1242
with an empty key prefix and read/write as true.
1243
1244
When determining whether or not to allow a read or a write a key
1245
value (e.g. `db1:user:1` for user `spencer`), a RoachNode would
1246
read the following permissions values:
1247
1248
```
1249
\0perm<db1:user:1>spencer
1250
\0perm<db1:user>spencer
1251
\0perm<db1>spencer
1252
\0perm<>spencer
1253
```
1254
1255
If any prefix in the hierarchy provides the required permission,
1256
the request is satisfied; otherwise, the request returns an
1257
error.
1258
1259
The priority for a user permission is used to order requests at
1260
Raft consensus ranges and for choosing an initial priority for
1261
distributed transactions. When scheduling operations at the Raft
1262
consensus range, all outstanding requests are ordered by key
1263
prefix and each assigned priorities according to key, user and
1264
arrival time. The next request is chosen probabilistically using
1265
priorities to weight the choice. Each key can have multiple
1266
priorities as they’re hierarchical (e.g. for /user/key, one
1267
priority for root ‘/’, and one for ‘/user/key’). The most general
1268
priority is used first. If two keys share the most general, then
1269
they’re compared with the next most general if applicable, and so on.
1270
1271
# Key-Value API
1272
1273
see the protobufs in [roachpb/](https://github.com/cockroachdb/cockroach/blob/master/roachpb),
1274
in particular [roachpb/api.proto](https://github.com/cockroachdb/cockroach/blob/master/roachpb/api.proto) and the comments within.