Elffuss BraidHub

kikocis/java-driver public

2d827ffabdfe · 7.6 KiBRenderedRawPermalink
1## Metadata
2
3The driver maintains global information about the Cassandra cluster it
4is connected to. It is available via
5[Cluster#getMetadata()][getMetadata].
6
7[getMetadata]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Cluster.html#getMetadata()
8
9### Schema metadata
10
11Use [getKeyspace(String)][getKeyspace] or [getKeyspaces()][getKeyspaces]
12to get keyspace-level metadata. From there you can access the keyspace's
13objects (tables, and UDTs and UDFs if relevant).
14
15[getKeyspace]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getKeyspace(java.lang.String)
16[getKeyspaces]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getKeyspaces()
17
18#### Refreshes
19
20Schema metadata gets refreshed in the following circumstances:
21
22* **schema changes via the driver**: after successfully executing a
23 schema-altering query (ex: `CREATE TABLE`), the driver waits for
24 schema agreement (see below), then refreshes the schema.
25* **third-party schema changes**: if another client (cqlsh, other driver
26 instance...) changes the schema, the driver gets notified by Cassandra
27 via a push notification. It refreshes the schema directly (there is no
28 need to wait for schema agreement since Cassandra has already done it).
29
30#### Schema agreement
31
32Schema changes need to be propagated to all nodes in the cluster. Once
33they have settled on a common version, we say that they are in
34agreement.
35
36As explained above, the driver waits for schema agreement after
37executing a schema-altering query. This is to ensure that subsequent
38requests (which might get routed to different nodes) see an up-to-date
39version of the schema.
40
41```ditaa
42 Application Driver Cassandra
43------+--------------------+------------------+-----
44 | | |
45 | CREATE TABLE... | |
46 |------------------->| |
47 | | send request |
48 | |----------------->|
49 | | |
50 | | success |
51 | |<-----------------|
52 | | |
53 | /--------------------\ |
54 | :Wait until all nodes+------>|
55 | :agree (or timeout) : |
56 | \--------------------/ |
57 | | ^ |
58 | | | |
59 | | +---------|
60 | | |
61 | | refresh schema |
62 | |----------------->|
63 | |<-----------------|
64 | complete query | |
65 |<-------------------| |
66 | | |
67```
68
69The schema agreement wait is performed synchronously, so the `execute`
70call -- or the completion of the `ResultSetFuture` if you use the async
71API -- will only return after it has completed.
72
73The check is implemented by repeatedly querying system tables for the
74schema version reported by each node, until they all converge to the
75same value. If that doesn't happen within a given timeout, the driver
76will give up waiting. The default timeout is 10 seconds, it can be
77customized when building your cluster:
78
79```java
80Cluster cluster = Cluster.builder()
81 .addContactPoint("127.0.0.1")
82 .withMaxSchemaAgreementWaitSeconds(20)
83 .build();
84```
85
86After executing a statement, you can check whether schema agreement was
87successful or timed out:
88
89```java
90ResultSet rs = session.execute("CREATE TABLE...");
91if (rs.getExecutionInfo().isSchemaInAgreement()) {
92 // schema is in agreement
93} else {
94 // schema agreement timed out
95}
96```
97
98You can also perform an on-demand check at any time:
99
100```java
101if (cluster.getMetadata().checkSchemaAgreement()) {
102 // schema is in agreement
103} else {
104 // schema is not in agreement
105}
106```
107
108The on-demand check does not retry, it only queries system tables once
109(so `maxSchemaAgreementWaitSeconds` does not apply). If you need
110retries, you'll have to schedule them yourself (for example with a
111custom executor).
112
113Check out the API docs for the features in this section:
114
115* [withMaxSchemaAgreementWaitSeconds(int)](http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Cluster.Builder.html#withMaxSchemaAgreementWaitSeconds(int))
116* [isSchemaInAgreement()](http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/ExecutionInfo.html#isSchemaInAgreement())
117* [checkSchemaAgreement()](http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#checkSchemaAgreement())
118
119
120### Token metadata
121
122This feature is probably of less interest to regular driver users, but
123it will be useful if you're writing an analytics client on top of the
124driver.
125
126[Metadata][metadata] exposes a number of methods to manipulate tokens
127and ranges: [getTokenRanges()][getTokenRanges], [getTokenRanges(String
128keyspace, Host host)][getTokenRanges2], [getReplicas(String keyspace,
129TokenRange range)][getReplicas], [newToken(String)][newToken] and
130[newTokenRange(Token start, Token end)][newTokenRange].
131
132[TokenRange][TokenRange] provides various operations on ranges
133(splitting, merging, etc.).
134
135Each host exposes its primary tokens as [getTokens()][getTokens].
136
137Finally, you can inject tokens in CQL queries with
138[BoundStatement#setToken][setToken], and retrieve them from results with
139[Row#getToken][getToken] and [Row#getPartitionKeyToken][getPKToken].
140
141As an example, here is how you could compute the splits to partition a
142job (pseudocode):
143
144```
145metadata = cluster.getMetadata()
146for range : metadata.getTokenRanges() {
147 hosts = metadata.getReplicas(keyspace, range)
148 int n = estimateNumberOfSplits() // more on that below
149 for split : range.splitEvenly(n)
150 // pick a host to process split
151}
152```
153
154For `estimateNumberOfSplits`, you need a way to estimate the total
155number of partition keys (this is what analytics clients would
156traditionally do with the Thrift operation `describe_splits_ex`).
157Starting with Cassandra 2.1.5, this information is available in a system
158table (see
159[CASSANDRA-7688](https://issues.apache.org/jira/browse/CASSANDRA-7688)).
160
161[metadata]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html
162[getTokenRanges]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getTokenRanges()
163[getTokenRanges2]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getTokenRanges(java.lang.String,%20com.datastax.driver.core.Host)
164[getReplicas]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getReplicas(java.lang.String,%20com.datastax.driver.core.TokenRange)
165[newToken]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#newToken(java.lang.String)
166[newTokenRange]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Metadata.html#newTokenRange(com.datastax.driver.core.Token,%20com.datastax.driver.core.Token)
167[TokenRange]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/TokenRange.html
168[getTokens]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Host.html#getTokens()
169[setToken]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/BoundStatement.html#setToken(int,%20com.datastax.driver.core.Token)
170[getToken]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Row.html#getToken(int)
171[getPKToken]: http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Row.html#getPartitionKeyToken()