-
Notifications
You must be signed in to change notification settings - Fork 355
/
AstyanaxContext.java
244 lines (202 loc) · 8.49 KB
/
AstyanaxContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.netflix.astyanax;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.netflix.astyanax.connectionpool.ConnectionFactory;
import com.netflix.astyanax.connectionpool.ConnectionPool;
import com.netflix.astyanax.connectionpool.ConnectionPoolConfiguration;
import com.netflix.astyanax.connectionpool.ConnectionPoolMonitor;
import com.netflix.astyanax.connectionpool.Host;
import com.netflix.astyanax.connectionpool.NodeDiscovery;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.connectionpool.impl.NodeDiscoveryImpl;
import com.netflix.astyanax.connectionpool.impl.BagOfConnectionsConnectionPoolImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.connectionpool.impl.RoundRobinConnectionPoolImpl;
import com.netflix.astyanax.connectionpool.impl.TokenAwareConnectionPoolImpl;
import com.netflix.astyanax.impl.FilteringHostSupplier;
import com.netflix.astyanax.impl.RingDescribeHostSupplier;
import com.netflix.astyanax.shallows.EmptyKeyspaceTracerFactory;
/**
* This object tracks the context of an astyanax instance of either a Cluster or
* Keyspace
*
* @author elandau
*
* @param <T>
*/
public class AstyanaxContext<Entity> {
private final ConnectionPool<?> cp;
private final NodeDiscovery discovery;
private final ConnectionPoolConfiguration cpConfig;
private final AstyanaxConfiguration asConfig;
private final String clusterName;
private final String keyspaceName;
private final Entity entity;
private final ConnectionPoolMonitor monitor;
public static class Builder {
protected ConnectionPool<?> cp;
protected NodeDiscovery discovery;
protected ConnectionPoolConfiguration cpConfig;
protected AstyanaxConfiguration asConfig;
protected String clusterName;
protected String keyspaceName;
protected KeyspaceTracerFactory tracerFactory = EmptyKeyspaceTracerFactory.getInstance();
protected Supplier<List<Host>> hostSupplier;
protected ConnectionPoolMonitor monitor = new CountingConnectionPoolMonitor();
public Builder forCluster(String clusterName) {
this.clusterName = clusterName;
return this;
}
public Builder forKeyspace(String keyspaceName) {
this.keyspaceName = keyspaceName;
return this;
}
public Builder withConnectionPoolConfiguration(ConnectionPoolConfiguration cpConfig) {
this.cpConfig = cpConfig;
return this;
}
public Builder withAstyanaxConfiguration(AstyanaxConfiguration asConfig) {
this.asConfig = asConfig;
return this;
}
public Builder withHostSupplier(Supplier<List<Host>> supplier) {
this.hostSupplier = supplier;
return this;
}
public Builder withTracerFactory(KeyspaceTracerFactory tracerFactory) {
this.tracerFactory = tracerFactory;
return this;
}
public Builder withConnectionPoolMonitor(ConnectionPoolMonitor monitor) {
this.monitor = monitor;
return this;
}
public NodeDiscoveryType getNodeDiscoveryType() {
if (cpConfig.getSeeds() != null) {
if (asConfig.getConnectionPoolType() == ConnectionPoolType.TOKEN_AWARE)
return NodeDiscoveryType.RING_DESCRIBE;
}
else {
if (asConfig.getConnectionPoolType() == ConnectionPoolType.TOKEN_AWARE) {
return NodeDiscoveryType.TOKEN_AWARE;
}
else {
return NodeDiscoveryType.DISCOVERY_SERVICE;
}
}
return asConfig.getDiscoveryType();
}
protected <T> ConnectionPool<T> createConnectionPool(ConnectionFactory<T> connectionFactory) {
ConnectionPool<T> connectionPool = null;
switch (asConfig.getConnectionPoolType()) {
case TOKEN_AWARE:
connectionPool = new TokenAwareConnectionPoolImpl<T>(cpConfig, connectionFactory, monitor);
break;
case BAG:
connectionPool = new BagOfConnectionsConnectionPoolImpl<T>(cpConfig, connectionFactory, monitor);
break;
case ROUND_ROBIN:
default:
connectionPool = new RoundRobinConnectionPoolImpl<T>(cpConfig, connectionFactory, monitor);
break;
}
if (hostSupplier != null) {
connectionPool.setHosts(hostSupplier.get());
}
return connectionPool;
}
public <T> AstyanaxContext<Keyspace> buildKeyspace(AstyanaxTypeFactory<T> factory) {
this.cpConfig.initialize();
ConnectionPool<T> cp = createConnectionPool(factory.createConnectionFactory(asConfig, cpConfig, tracerFactory,
monitor));
this.cp = cp;
final Keyspace keyspace = factory.createKeyspace(keyspaceName, cp, asConfig, tracerFactory);
Supplier<List<Host>> supplier = null;
switch (getNodeDiscoveryType()) {
case DISCOVERY_SERVICE:
Preconditions.checkNotNull(hostSupplier, "Missing host name supplier");
supplier = hostSupplier;
break;
case RING_DESCRIBE:
supplier = new RingDescribeHostSupplier(keyspace, cpConfig.getPort(), cpConfig.getLocalDatacenter());
break;
case TOKEN_AWARE:
if (hostSupplier == null) {
supplier = new RingDescribeHostSupplier(keyspace, cpConfig.getPort(), cpConfig.getLocalDatacenter());
}
else {
supplier = new FilteringHostSupplier(new RingDescribeHostSupplier(keyspace, cpConfig.getPort(), cpConfig.getLocalDatacenter()),
hostSupplier);
}
break;
case NONE:
supplier = null;
break;
}
if (supplier != null) {
discovery = new NodeDiscoveryImpl(StringUtils.join(Arrays.asList(clusterName, keyspaceName), "_"),
asConfig.getDiscoveryDelayInSeconds() * 1000, supplier, cp);
}
return new AstyanaxContext<Keyspace>(this, keyspace);
}
public <T> AstyanaxContext<Cluster> buildCluster(AstyanaxTypeFactory<T> factory) {
this.cpConfig.initialize();
ConnectionPool<T> cp = createConnectionPool(factory.createConnectionFactory(asConfig, cpConfig, tracerFactory,
monitor));
this.cp = cp;
if (hostSupplier != null) {
discovery = new NodeDiscoveryImpl(clusterName, asConfig.getDiscoveryDelayInSeconds() * 1000,
hostSupplier, cp);
}
return new AstyanaxContext<Cluster>(this, factory.createCluster(cp, asConfig, tracerFactory));
}
}
private AstyanaxContext(Builder builder, Entity entity) {
this.cpConfig = builder.cpConfig;
this.asConfig = builder.asConfig;
this.cp = builder.cp;
this.clusterName = builder.clusterName;
this.keyspaceName = builder.keyspaceName;
this.entity = entity;
this.discovery = builder.discovery;
this.monitor = builder.monitor;
}
public Entity getEntity() {
return this.entity;
}
public ConnectionPool<?> getConnectionPool() {
return this.cp;
}
public ConnectionPoolConfiguration getConnectionPoolConfiguration() {
return cpConfig;
}
public AstyanaxConfiguration getAstyanaxConfiguration() {
return asConfig;
}
public NodeDiscovery getNodeDiscovery() {
return this.discovery;
}
public ConnectionPoolMonitor getConnectionPoolMonitor() {
return this.monitor;
}
public void start() {
cp.start();
if (discovery != null)
discovery.start();
}
public void shutdown() {
if (discovery != null)
discovery.shutdown();
cp.shutdown();
}
public String getClusterName() {
return this.clusterName;
}
public String getKeyspaceName() {
return this.keyspaceName;
}
}