Elffuss BraidHub

kikocis/java-driver public

6cbc334fa86f · 5.5 KiBRenderedRawPermalink
1## Address resolution
2
3Each node in the Cassandra cluster is uniquely identified by an IP address that the driver will use to establish
4connections.
5
6* for contact points, these are provided as part of configuring the `Cluster` object;
7* for other nodes, addresses will be discovered dynamically, either by inspecting `system.peers` on already connected
8 nodes, or via push notifications received from the [control host](../control_connection) when new nodes are discovered
9 by gossip.
10
11
12### Cassandra-side configuration
13
14The address that each Cassandra node shares with clients is the **broadcast RPC address**; it is controlled by various
15properties in [cassandra.yaml]:
16
17* [rpc_address] or [rpc_interface] is the address that the Cassandra process *binds to*. You must set one or the other,
18 not both (for more details, see the inline comments in the default `cassandra.yaml` that came with your installation);
19* [broadcast_rpc_address] \(introduced in Cassandra 2.1) is the address to share with clients, if it is different than
20 the previous one (the reason for having a separate property is if the bind address is not public to clients, because
21 there is a router in between).
22
23If `broadcast_rpc_address` is not set, it defaults to `rpc_address`/`rpc_interface`. If `rpc_address`/`rpc_interface`
24is 0.0.0.0 (all interfaces), then `broadcast_rpc_address` *must* be set.
25
26If you're not sure which address a Cassandra node is broadcasting, launch cqlsh locally on the node, execute the
27following query and take node of the result:
28
29```
30cqlsh> select broadcast_address from system.local;
31
32 broadcast_address
33-------------------
34 172.1.2.3
35```
36
37Then connect to *another* node in the cluster and run the following query, injecting the previous result:
38
39```
40cqlsh> select rpc_address from system.peers where peer = '172.1.2.3';
41
42 rpc_address
43-------------
44 1.2.3.4
45```
46
47That last result is the broadcast RPC address. Ensure that it is accessible from the client machine where the driver
48will run.
49
50
51### Driver-side address translation
52
53Sometimes it's not possible for Cassandra nodes to broadcast addresses that will work for each and every client; for
54instance, they might broadcast private IPs because most clients are in the same network, but a particular client could
55be on another network and go through a router.
56
57For such cases, you can register a driver-side component that will perform additional address translation. Write a class
58that implements [AddressTranslater] and register an instance with your `Cluster`:
59
60```java
61public class MyAddressTranslater implements AddressTranslater {
62 public InetSocketAddress translate(InetSocketAddress address) {
63 ... // your custom translation logic
64 }
65}
66
67Cluster cluster = Cluster.builder()
68 .addContactPoint("1.2.3.4")
69 .withAddressTranslater(new MyAddressTranslater())
70 .build();
71```
72
73Notes:
74
75* the contact points provided while creating the `Cluster` are not translated, only addresses
76 retrieved from or sent by Cassandra nodes are;
77* you might want to implement [CloseableAddressTranslater] if your
78 implementation has state that should be cleaned up when the `Cluster`
79 shuts down. This is provided as a separate interface for historical
80 reasons, the `close()` method was merged in `AddressTranslater` in 3.0.
81
82
83### EC2 multi-region
84
85If you deploy both Cassandra and client applications on Amazon EC2, and your cluster spans multiple regions, you'll have
86to configure your Cassandra nodes to broadcast public RPC addresses.
87
88However, this is not always the most cost-effective: if a client and a node are in the same region, it would be cheaper
89to connect over the private IP. Ideally, you'd want to pick the best address in each case.
90
91The driver provides an address translator that does just that: [EC2MultiRegionAddressTranslater].
92
93```java
94Cluster cluster = Cluster.builder()
95 .addContactPoint("1.2.3.4")
96 .withAddressTranslater(new EC2MultiRegionAddressTranslater())
97 .build();
98```
99
100With this configuration, you keep broadcasting public RPC addresses. But each time the driver connects to a new
101Cassandra node:
102
103* if the node is *in the same EC2 region*, the public IP will be translated to the intra-region private IP;
104* otherwise, it will not be translated.
105
106(To achieve this, `EC2MultiRegionAddressTranslater` performs a reverse DNS lookup of the origin address, to find the
107domain name of the target instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the
108private/public switch automatically based on location).
109
110
111
112[AddressTranslater]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/AddressTranslater.html
113[CloseableAddressTranslater]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/CloseableAddressTranslater.html
114[EC2MultiRegionAddressTranslater]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslater.html
115
116[cassandra.yaml]: https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html
117[rpc_address]: https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html?scroll=reference_ds_qfg_n1r_1k__rpc_address
118[rpc_interface]: https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html?scroll=reference_ds_qfg_n1r_1k__rpc_interface
119[broadcast_rpc_address]: https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html#reference_ds_qfg_n1r_1k__broadcast_rpc_address