This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
/
HThriftClient.java
199 lines (170 loc) · 6.07 KB
/
HThriftClient.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
package me.prettyprint.cassandra.connection;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.atomic.AtomicLong;
import me.prettyprint.cassandra.service.CassandraHost;
import me.prettyprint.cassandra.service.SystemProperties;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hector.api.exceptions.HectorTransportException;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HThriftClient {
private static Logger log = LoggerFactory.getLogger(HThriftClient.class);
private static final AtomicLong serial = new AtomicLong(0);
final CassandraHost cassandraHost;
private final long mySerial;
private final int timeout;
private String keyspaceName;
private long useageStartTime;
private TTransport transport;
private Cassandra.Client cassandraClient;
HThriftClient(CassandraHost cassandraHost) {
this.cassandraHost = cassandraHost;
this.timeout = getTimeout(cassandraHost);
mySerial = serial.incrementAndGet();
}
/**
* Returns a new Cassandra.Client on each invocation using the underlying transport
*
*/
public Cassandra.Client getCassandra() {
if ( !isOpen() ) {
throw new IllegalStateException("getCassandra called on client that was not open. You should not have gotten here.");
}
if ( cassandraClient == null ) {
cassandraClient = new Cassandra.Client(new TBinaryProtocol(transport));
}
return cassandraClient;
}
public Cassandra.Client getCassandra(String keyspaceNameArg) {
getCassandra();
if ( keyspaceNameArg != null && !StringUtils.equals(keyspaceName, keyspaceNameArg)) {
if ( log.isDebugEnabled() )
log.debug("keyspace reseting from {} to {}", keyspaceName, keyspaceNameArg);
keyspaceName = keyspaceNameArg;
try {
cassandraClient.set_keyspace(keyspaceName);
} catch (InvalidRequestException ire) {
throw new HInvalidRequestException(ire);
} catch (TException e) {
throw new HectorTransportException(e);
}
}
return cassandraClient;
}
HThriftClient close() {
if ( log.isDebugEnabled() ) {
log.debug("Closing client {}", this);
}
if ( isOpen() ) {
try {
transport.flush();
} catch (Exception e) {
log.error("Could not flush transport (to be expected if the pool is shutting down) in close for client: " + toString(), e);
} finally {
try {
transport.close();
} catch (Exception e) {
log.error("Error on transport close for client: " +toString(), e);
}
}
}
return this;
}
HThriftClient open() {
if ( isOpen() ) {
throw new IllegalStateException("Open called on already open connection. You should not have gotten here.");
}
if ( log.isDebugEnabled() ) {
log.debug("Creating a new thrift connection to {}", cassandraHost);
}
TSocket socket = new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout);
if ( cassandraHost.getUseSocketKeepalive() ) {
try {
socket.getSocket().setKeepAlive(true);
} catch (SocketException se) {
throw new HectorTransportException("Could not set SO_KEEPALIVE on socket: ", se);
}
}
if (cassandraHost.getUseThriftFramedTransport()) {
transport = new TFramedTransport(socket);
} else {
transport = socket;
}
try {
transport.open();
} catch (TTransportException e) {
// Thrift exceptions aren't very good in reporting, so we have to catch the exception here and
// add details to it.
log.debug("Unable to open transport to " + cassandraHost.getName());
//clientMonitor.incCounter(Counter.CONNECT_ERROR);
throw new HectorTransportException("Unable to open transport to " + cassandraHost.getName() +" , " +
e.getLocalizedMessage(), e);
}
return this;
}
boolean isOpen() {
boolean open = false;
if (transport != null) {
open = transport.isOpen();
}
if ( log.isDebugEnabled() ) {
log.debug("Transport open status {} for client {}", open, this);
}
return open;
}
/**
* If CassandraHost was not null we use {@link CassandraHost#getCassandraThriftSocketTimeout()}
* if it was greater than zero. Otherwise look for an environment
* variable name CASSANDRA_THRIFT_SOCKET_TIMEOUT value.
* If doesn't exist, returns 0.
* @param cassandraHost
*/
private int getTimeout(CassandraHost cassandraHost) {
int timeoutVar = 0;
if ( cassandraHost != null && cassandraHost.getCassandraThriftSocketTimeout() > 0 ) {
timeoutVar = cassandraHost.getCassandraThriftSocketTimeout();
} else {
String timeoutStr = System.getProperty(
SystemProperties.CASSANDRA_THRIFT_SOCKET_TIMEOUT.toString());
if (timeoutStr != null && timeoutStr.length() > 0) {
try {
timeoutVar = Integer.valueOf(timeoutStr);
} catch (NumberFormatException e) {
log.error("Invalid value for CASSANDRA_THRIFT_SOCKET_TIMEOUT", e);
}
}
}
return timeoutVar;
}
public void startToUse() {
useageStartTime = System.currentTimeMillis();
}
/**
* @return Time in MS since it was used.
*/
public long getSinceLastUsed() {
return System.currentTimeMillis() - useageStartTime;
}
@Override
public String toString() {
return String.format(NAME_FORMAT, cassandraHost.getUrl(), mySerial);
}
/**
* Compares the toString of these clients
*/
@Override
public boolean equals(Object obj) {
return this.toString().equals(obj.toString());
}
private static final String NAME_FORMAT = "CassandraClient<%s-%d>";
}