kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2e039ec87112b3a6ca513b02a71d13cc5bd1b36e7d6b50e3b11794083a267ce5 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-04-20 11:46 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-04 12:53 +02:00 |
| Parents | db652e0543ae |
1 file changed
added features/metadata/README.md +105 −0
| @@ -0,0 +1,105 @@ | ||
| 1 | +## Metadata | |
| 2 | + | |
| 3 | +The driver maintains global information about the Cassandra cluster it | |
| 4 | +is connected to. It is available via `Cluster#getMetadata()`. | |
| 5 | + | |
| 6 | +### Schema metadata | |
| 7 | + | |
| 8 | +Use `getKeyspace(String)` or `getKeyspaces()` to get keyspace-level | |
| 9 | +metadata. From there you can access the keyspace's objects (tables, and | |
| 10 | +UDTs and UDFs if relevant). | |
| 11 | + | |
| 12 | +#### Refreshes | |
| 13 | + | |
| 14 | +Schema metadata gets refreshed in the following circumstances: | |
| 15 | + | |
| 16 | +* **schema changes via the driver**: after successfully executing a | |
| 17 | + schema-altering query (ex: `CREATE TABLE`), the driver waits for | |
| 18 | + schema agreement (see below), then refreshes the schema. | |
| 19 | +* **third-party schema changes**: if another client (cqlsh, other driver | |
| 20 | + instance...) changes the schema, the driver gets notified by Cassandra | |
| 21 | + via a push notification. It refreshes the schema directly (there is no | |
| 22 | + need to wait for schema agreement since Cassandra has already done it). | |
| 23 | + | |
| 24 | +#### Schema agreement | |
| 25 | + | |
| 26 | +Schema changes need to be propagated to all nodes in the cluster. Once | |
| 27 | +they have settled on a common version, we say that they are in | |
| 28 | +agreement. | |
| 29 | + | |
| 30 | +As explained above, the driver waits for schema agreement after | |
| 31 | +executing a schema-altering query. This is to ensure that subsequent | |
| 32 | +requests (which might get routed to different nodes) see an up-to-date | |
| 33 | +version of the schema. | |
| 34 | + | |
| 35 | +```ditaa | |
| 36 | + Application Driver Cassandra | |
| 37 | +------+--------------------+------------------+----- | |
| 38 | + | | | | |
| 39 | + | CREATE TABLE... | | | |
| 40 | + |------------------->| | | |
| 41 | + | | send request | | |
| 42 | + | |----------------->| | |
| 43 | + | | | | |
| 44 | + | | success | | |
| 45 | + | |<-----------------| | |
| 46 | + | | | | |
| 47 | + | /--------------------\ | | |
| 48 | + | :Wait until all nodes+------>| | |
| 49 | + | :agree (or timeout) : | | |
| 50 | + | \--------------------/ | | |
| 51 | + | | ^ | | |
| 52 | + | | | | | |
| 53 | + | | +---------| | |
| 54 | + | | | | |
| 55 | + | | refresh schema | | |
| 56 | + | |----------------->| | |
| 57 | + | |<-----------------| | |
| 58 | + | complete query | | | |
| 59 | + |<-------------------| | | |
| 60 | + | | | | |
| 61 | +``` | |
| 62 | + | |
| 63 | +The schema agreement wait is performed synchronously, so the `execute` | |
| 64 | +call -- or the completion of the `ResultSetFuture` if you use the async | |
| 65 | +API -- will only return after it has completed. | |
| 66 | + | |
| 67 | +The check is implemented by repeatedly querying system tables for the | |
| 68 | +schema version reported by each node, until they all converge to the | |
| 69 | +same value. If that doesn't happen within a given timeout, the driver | |
| 70 | +will give up waiting. The default timeout is 10 seconds, it can be | |
| 71 | +customized when building your cluster: | |
| 72 | + | |
| 73 | +```java | |
| 74 | +Cluster cluster = Cluster.builder() | |
| 75 | + .addContactPoint("127.0.0.1") | |
| 76 | + .withMaxSchemaAgreementWaitSeconds(20) | |
| 77 | + .build(); | |
| 78 | +``` | |
| 79 | + | |
| 80 | +After executing a statement, you can check whether schema agreement was | |
| 81 | +successful or timed out: | |
| 82 | + | |
| 83 | +```java | |
| 84 | +ResultSet rs = session.execute("CREATE TABLE..."); | |
| 85 | +if (rs.getExecutionInfo().isSchemaInAgreement()) { | |
| 86 | + // schema is in agreement | |
| 87 | +} else { | |
| 88 | + // schema agreement timed out | |
| 89 | +} | |
| 90 | +``` | |
| 91 | + | |
| 92 | +You can also perform an on-demand check at any time: | |
| 93 | + | |
| 94 | +```java | |
| 95 | +if (cluster.getMetadata().checkSchemaAgreement()) { | |
| 96 | + // schema is in agreement | |
| 97 | +} else { | |
| 98 | + // schema is not in agreement | |
| 99 | +} | |
| 100 | +``` | |
| 101 | + | |
| 102 | +The on-demand check does not retry, it only queries system tables once | |
| 103 | +(so `maxSchemaAgreementWaitSeconds` does not apply). If you need | |
| 104 | +retries, you'll have to schedule them yourself (for example with a | |
| 105 | +custom executor). |