Elffuss BraidHub

kikocis/java-driver public

3.1.x · 8.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/3.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/3.0/com/datastax/driver/core/Metadata.html#getKeyspace-java.lang.String-
16[getKeyspaces]: http://docs.datastax.com/en/drivers/java/3.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#### Subscribing to schema changes
31
32Users interested in being notified of schema changes can implement the
33[SchemaChangeListener][SchemaChangeListener] interface.
34
35Every listener must [be registered][registerListener] against a `Cluster` instance:
36
37```java
38Cluster cluster = ...
39SchemaChangeListener myListener = ...
40cluster.register(myListener);
41```
42
43Once registered, the listener will be notified of all schema changes detected by the driver,
44regardless of where they originate from.
45
46Note that it is preferable to register a listener only *after* the cluster is fully initialized,
47otherwise the listener could be notified with a great deal of "Added" events as
48the driver builds the schema metadata from scratch for the first time.
49
50[SchemaChangeListener]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SchemaChangeListener.html
51[registerListener]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Cluster.html#register-com.datastax.driver.core.SchemaChangeListener-
52
53#### Schema agreement
54
55Schema changes need to be propagated to all nodes in the cluster. Once
56they have settled on a common version, we say that they are in
57agreement.
58
59As explained above, the driver waits for schema agreement after
60executing a schema-altering query. This is to ensure that subsequent
61requests (which might get routed to different nodes) see an up-to-date
62version of the schema.
63
64```ditaa
65 Application Driver Cassandra
66------+--------------------+------------------+-----
67 | | |
68 | CREATE TABLE... | |
69 |------------------->| |
70 | | send request |
71 | |----------------->|
72 | | |
73 | | success |
74 | |<-----------------|
75 | | |
76 | /--------------------\ |
77 | :Wait until all nodes+------>|
78 | :agree (or timeout) : |
79 | \--------------------/ |
80 | | ^ |
81 | | | |
82 | | +---------|
83 | | |
84 | | refresh schema |
85 | |----------------->|
86 | |<-----------------|
87 | complete query | |
88 |<-------------------| |
89 | | |
90```
91
92The schema agreement wait is performed synchronously, so the `execute`
93call -- or the completion of the `ResultSetFuture` if you use the async
94API -- will only return after it has completed.
95
96The check is implemented by repeatedly querying system tables for the
97schema version reported by each node, until they all converge to the
98same value. If that doesn't happen within a given timeout, the driver
99will give up waiting. The default timeout is 10 seconds, it can be
100customized when building your cluster:
101
102```java
103Cluster cluster = Cluster.builder()
104 .addContactPoint("127.0.0.1")
105 .withMaxSchemaAgreementWaitSeconds(20)
106 .build();
107```
108
109After executing a statement, you can check whether schema agreement was
110successful or timed out:
111
112```java
113ResultSet rs = session.execute("CREATE TABLE...");
114if (rs.getExecutionInfo().isSchemaInAgreement()) {
115 // schema is in agreement
116} else {
117 // schema agreement timed out
118}
119```
120
121You can also perform an on-demand check at any time:
122
123```java
124if (cluster.getMetadata().checkSchemaAgreement()) {
125 // schema is in agreement
126} else {
127 // schema is not in agreement
128}
129```
130
131The on-demand check does not retry, it only queries system tables once
132(so `maxSchemaAgreementWaitSeconds` does not apply). If you need
133retries, you'll have to schedule them yourself (for example with a
134custom executor).
135
136Check out the API docs for the features in this section:
137
138* [withMaxSchemaAgreementWaitSeconds(int)](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Cluster.Builder.html#withMaxSchemaAgreementWaitSeconds-int-)
139* [isSchemaInAgreement()](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ExecutionInfo.html#isSchemaInAgreement--)
140* [checkSchemaAgreement()](http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#checkSchemaAgreement--)
141
142
143### Token metadata
144
145This feature is probably of less interest to regular driver users, but
146it will be useful if you're writing an analytics client on top of the
147driver.
148
149[Metadata][metadata] exposes a number of methods to manipulate tokens
150and ranges: [getTokenRanges()][getTokenRanges], [getTokenRanges(String
151keyspace, Host host)][getTokenRanges2], [getReplicas(String keyspace,
152TokenRange range)][getReplicas], [newToken(String)][newToken] and
153[newTokenRange(Token start, Token end)][newTokenRange].
154
155[TokenRange][TokenRange] provides various operations on ranges
156(splitting, merging, etc.).
157
158Each host exposes its primary tokens as [getTokens()][getTokens].
159
160Finally, you can inject tokens in CQL queries with
161[BoundStatement#setToken][setToken], and retrieve them from results with
162[Row#getToken][getToken] and [Row#getPartitionKeyToken][getPKToken].
163
164As an example, here is how you could compute the splits to partition a
165job (pseudocode):
166
167```
168metadata = cluster.getMetadata()
169for range : metadata.getTokenRanges() {
170 hosts = metadata.getReplicas(keyspace, range)
171 int n = estimateNumberOfSplits() // more on that below
172 for split : range.splitEvenly(n)
173 // pick a host to process split
174}
175```
176
177For `estimateNumberOfSplits`, you need a way to estimate the total
178number of partition keys (this is what analytics clients would
179traditionally do with the Thrift operation `describe_splits_ex`).
180Starting with Cassandra 2.1.5, this information is available in a system
181table (see
182[CASSANDRA-7688](https://issues.apache.org/jira/browse/CASSANDRA-7688)).
183
184[metadata]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html
185[getTokenRanges]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#getTokenRanges--
186[getTokenRanges2]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#getTokenRanges-java.lang.String-com.datastax.driver.core.Host-
187[getReplicas]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#getReplicas-java.lang.String-com.datastax.driver.core.TokenRange-
188[newToken]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#newToken-java.lang.String-
189[newTokenRange]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Metadata.html#newTokenRange-com.datastax.driver.core.Token-com.datastax.driver.core.Token-
190[TokenRange]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/TokenRange.html
191[getTokens]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Host.html#getTokens--
192[setToken]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/BoundStatement.html#setToken-int-com.datastax.driver.core.Token-
193[getToken]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Row.html#getToken-int-
194[getPKToken]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Row.html#getPartitionKeyToken--