| 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://docs.datastax.com/en/drivers/java/3.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://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 | |
| 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 | #### Subscribing to schema changes |
| 31 | |
| 32 | Users interested in being notified of schema changes can implement the |
| 33 | [SchemaChangeListener][SchemaChangeListener] interface. |
| 34 | |
| 35 | Every listener must [be registered][registerListener] against a `Cluster` instance: |
| 36 | |
| 37 | ```java |
| 38 | Cluster cluster = ... |
| 39 | SchemaChangeListener myListener = ... |
| 40 | cluster.register(myListener); |
| 41 | ``` |
| 42 | |
| 43 | Once registered, the listener will be notified of all schema changes detected by the driver, |
| 44 | regardless of where they originate from. |
| 45 | |
| 46 | Note that it is preferable to register a listener only *after* the cluster is fully initialized, |
| 47 | otherwise the listener could be notified with a great deal of "Added" events as |
| 48 | the 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 | |
| 55 | Schema changes need to be propagated to all nodes in the cluster. Once |
| 56 | they have settled on a common version, we say that they are in |
| 57 | agreement. |
| 58 | |
| 59 | As explained above, the driver waits for schema agreement after |
| 60 | executing a schema-altering query. This is to ensure that subsequent |
| 61 | requests (which might get routed to different nodes) see an up-to-date |
| 62 | version 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 | |
| 92 | The schema agreement wait is performed synchronously, so the `execute` |
| 93 | call -- or the completion of the `ResultSetFuture` if you use the async |
| 94 | API -- will only return after it has completed. |
| 95 | |
| 96 | The check is implemented by repeatedly querying system tables for the |
| 97 | schema version reported by each node, until they all converge to the |
| 98 | same value. If that doesn't happen within a given timeout, the driver |
| 99 | will give up waiting. The default timeout is 10 seconds, it can be |
| 100 | customized when building your cluster: |
| 101 | |
| 102 | ```java |
| 103 | Cluster cluster = Cluster.builder() |
| 104 | .addContactPoint("127.0.0.1") |
| 105 | .withMaxSchemaAgreementWaitSeconds(20) |
| 106 | .build(); |
| 107 | ``` |
| 108 | |
| 109 | After executing a statement, you can check whether schema agreement was |
| 110 | successful or timed out: |
| 111 | |
| 112 | ```java |
| 113 | ResultSet rs = session.execute("CREATE TABLE..."); |
| 114 | if (rs.getExecutionInfo().isSchemaInAgreement()) { |
| 115 | // schema is in agreement |
| 116 | } else { |
| 117 | // schema agreement timed out |
| 118 | } |
| 119 | ``` |
| 120 | |
| 121 | You can also perform an on-demand check at any time: |
| 122 | |
| 123 | ```java |
| 124 | if (cluster.getMetadata().checkSchemaAgreement()) { |
| 125 | // schema is in agreement |
| 126 | } else { |
| 127 | // schema is not in agreement |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | The on-demand check does not retry, it only queries system tables once |
| 132 | (so `maxSchemaAgreementWaitSeconds` does not apply). If you need |
| 133 | retries, you'll have to schedule them yourself (for example with a |
| 134 | custom executor). |
| 135 | |
| 136 | Check 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 | |
| 145 | This feature is probably of less interest to regular driver users, but |
| 146 | it will be useful if you're writing an analytics client on top of the |
| 147 | driver. |
| 148 | |
| 149 | [Metadata][metadata] exposes a number of methods to manipulate tokens |
| 150 | and ranges: [getTokenRanges()][getTokenRanges], [getTokenRanges(String |
| 151 | keyspace, Host host)][getTokenRanges2], [getReplicas(String keyspace, |
| 152 | TokenRange 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 | |
| 158 | Each host exposes its primary tokens as [getTokens()][getTokens]. |
| 159 | |
| 160 | Finally, 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 | |
| 164 | As an example, here is how you could compute the splits to partition a |
| 165 | job (pseudocode): |
| 166 | |
| 167 | ``` |
| 168 | metadata = cluster.getMetadata() |
| 169 | for 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 | |
| 177 | For `estimateNumberOfSplits`, you need a way to estimate the total |
| 178 | number of partition keys (this is what analytics clients would |
| 179 | traditionally do with the Thrift operation `describe_splits_ex`). |
| 180 | Starting with Cassandra 2.1.5, this information is available in a system |
| 181 | table (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-- |