| 1 | ## Metadata |
| 2 | |
| 3 | The driver maintains global information about the Cassandra cluster it |
| 4 | is connected to. It is available via |
| 5 | [Cluster#getMetadata()][getMetadata]. |
| 6 | |
| 7 | [getMetadata]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Cluster.html#getMetadata() |
| 8 | |
| 9 | ### Schema metadata |
| 10 | |
| 11 | Use [getKeyspace(String)][getKeyspace] or [getKeyspaces()][getKeyspaces] |
| 12 | to get keyspace-level metadata. From there you can access the keyspace's |
| 13 | objects (tables, and UDTs and UDFs if relevant). |
| 14 | |
| 15 | [getKeyspace]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getKeyspace(java.lang.String) |
| 16 | [getKeyspaces]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getKeyspaces() |
| 17 | |
| 18 | #### Refreshes |
| 19 | |
| 20 | Schema 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 | |
| 32 | Schema changes need to be propagated to all nodes in the cluster. Once |
| 33 | they have settled on a common version, we say that they are in |
| 34 | agreement. |
| 35 | |
| 36 | As explained above, the driver waits for schema agreement after |
| 37 | executing a schema-altering query. This is to ensure that subsequent |
| 38 | requests (which might get routed to different nodes) see an up-to-date |
| 39 | version 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 | |
| 69 | The schema agreement wait is performed synchronously, so the `execute` |
| 70 | call -- or the completion of the `ResultSetFuture` if you use the async |
| 71 | API -- will only return after it has completed. |
| 72 | |
| 73 | The check is implemented by repeatedly querying system tables for the |
| 74 | schema version reported by each node, until they all converge to the |
| 75 | same value. If that doesn't happen within a given timeout, the driver |
| 76 | will give up waiting. The default timeout is 10 seconds, it can be |
| 77 | customized when building your cluster: |
| 78 | |
| 79 | ```java |
| 80 | Cluster cluster = Cluster.builder() |
| 81 | .addContactPoint("127.0.0.1") |
| 82 | .withMaxSchemaAgreementWaitSeconds(20) |
| 83 | .build(); |
| 84 | ``` |
| 85 | |
| 86 | After executing a statement, you can check whether schema agreement was |
| 87 | successful or timed out: |
| 88 | |
| 89 | ```java |
| 90 | ResultSet rs = session.execute("CREATE TABLE..."); |
| 91 | if (rs.getExecutionInfo().isSchemaInAgreement()) { |
| 92 | // schema is in agreement |
| 93 | } else { |
| 94 | // schema agreement timed out |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | You can also perform an on-demand check at any time: |
| 99 | |
| 100 | ```java |
| 101 | if (cluster.getMetadata().checkSchemaAgreement()) { |
| 102 | // schema is in agreement |
| 103 | } else { |
| 104 | // schema is not in agreement |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | The on-demand check does not retry, it only queries system tables once |
| 109 | (so `maxSchemaAgreementWaitSeconds` does not apply). If you need |
| 110 | retries, you'll have to schedule them yourself (for example with a |
| 111 | custom executor). |
| 112 | |
| 113 | Check out the API docs for the features in this section: |
| 114 | |
| 115 | * [withMaxSchemaAgreementWaitSeconds(int)](http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Cluster.Builder.html#withMaxSchemaAgreementWaitSeconds(int)) |
| 116 | * [isSchemaInAgreement()](http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/ExecutionInfo.html#isSchemaInAgreement()) |
| 117 | * [checkSchemaAgreement()](http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#checkSchemaAgreement()) |
| 118 | |
| 119 | |
| 120 | ### Token metadata |
| 121 | |
| 122 | This feature is probably of less interest to regular driver users, but |
| 123 | it will be useful if you're writing an analytics client on top of the |
| 124 | driver. |
| 125 | |
| 126 | [Metadata][metadata] exposes a number of methods to manipulate tokens |
| 127 | and ranges: [getTokenRanges()][getTokenRanges], [getTokenRanges(String |
| 128 | keyspace, Host host)][getTokenRanges2], [getReplicas(String keyspace, |
| 129 | TokenRange 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 | |
| 135 | Each host exposes its primary tokens as [getTokens()][getTokens]. |
| 136 | |
| 137 | Finally, 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 | |
| 141 | As an example, here is how you could compute the splits to partition a |
| 142 | job (pseudocode): |
| 143 | |
| 144 | ``` |
| 145 | metadata = cluster.getMetadata() |
| 146 | for 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 | |
| 154 | For `estimateNumberOfSplits`, you need a way to estimate the total |
| 155 | number of partition keys (this is what analytics clients would |
| 156 | traditionally do with the Thrift operation `describe_splits_ex`). |
| 157 | Starting with Cassandra 2.1.5, this information is available in a system |
| 158 | table (see |
| 159 | [CASSANDRA-7688](https://issues.apache.org/jira/browse/CASSANDRA-7688)). |
| 160 | |
| 161 | [metadata]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html |
| 162 | [getTokenRanges]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getTokenRanges() |
| 163 | [getTokenRanges2]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getTokenRanges(java.lang.String,%20com.datastax.driver.core.Host) |
| 164 | [getReplicas]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#getReplicas(java.lang.String,%20com.datastax.driver.core.TokenRange) |
| 165 | [newToken]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#newToken(java.lang.String) |
| 166 | [newTokenRange]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Metadata.html#newTokenRange(com.datastax.driver.core.Token,%20com.datastax.driver.core.Token) |
| 167 | [TokenRange]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/TokenRange.html |
| 168 | [getTokens]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Host.html#getTokens() |
| 169 | [setToken]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/BoundStatement.html#setToken(int,%20com.datastax.driver.core.Token) |
| 170 | [getToken]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Row.html#getToken(int) |
| 171 | [getPKToken]: http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Row.html#getPartitionKeyToken() |