/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java

https://github.com/smzorro/hector · Java · 238 lines · 159 code · 49 blank · 30 comment · 5 complexity · 3c077a3c852540bb46396af7e0212cd7 MD5 · raw file

  1. package me.prettyprint.cassandra.service;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. /**
  7. * Encapsulates the information required for connecting to a Cassandra host.
  8. * Also exposes pool configuration parameters for that host.
  9. *
  10. * @author zznate(nate@riptano.com)
  11. *
  12. */
  13. public final class CassandraHost {
  14. private static Logger log = LoggerFactory.getLogger(CassandraHost.class);
  15. /**
  16. * The default port number to which we will connect
  17. */
  18. public static final int DEFAULT_PORT = 9160;
  19. public static final int DEFAULT_MAX_ACTIVE = 50;
  20. /**
  21. * By default, we will use TSocket transport on thrift (matches default Cassandra configs)
  22. */
  23. public static final boolean DEFAULT_USE_FRAMED_THRIFT_TRANSPORT = true;
  24. /**
  25. * The default max wait time when exhausted happens, default value is negative, which means
  26. * it'll block indefinitely.
  27. */
  28. public static final long DEFAULT_MAX_WAITTIME_WHEN_EXHAUSTED = -1;
  29. /**
  30. * The default max idle number determines how many idle connections may reside in the pool.
  31. * If -1 then it's infinity.
  32. */
  33. public static final int DEFAULT_MAX_IDLE = -1;
  34. public static final boolean DEFAULT_LIFO = true;
  35. public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 18000000;
  36. public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1;
  37. private final String host, ip, url;
  38. private final int port;
  39. private final String name;
  40. private int maxActive = DEFAULT_MAX_ACTIVE;
  41. private int maxIdle = DEFAULT_MAX_IDLE;
  42. private boolean lifo = DEFAULT_LIFO;
  43. private long minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
  44. private long timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
  45. private long maxWaitTimeWhenExhausted = DEFAULT_MAX_WAITTIME_WHEN_EXHAUSTED;
  46. private int cassandraThriftSocketTimeout;
  47. private ExhaustedPolicy exhaustedPolicy = ExhaustedPolicy.WHEN_EXHAUSTED_BLOCK;
  48. private boolean useThriftFramedTransport = DEFAULT_USE_FRAMED_THRIFT_TRANSPORT;
  49. private boolean useSocketKeepalive;
  50. //TODO(ran): private FailoverPolicy failoverPolicy = DEFAULT_FAILOVER_POLICY;
  51. public CassandraHost(String url) {
  52. this(url, parsePortFromUrl(url));
  53. }
  54. public CassandraHost(String url2, int port) {
  55. url2 = parseHostFromUrl(url2);
  56. this.port = port;
  57. StringBuilder b = new StringBuilder();
  58. InetAddress address;
  59. String turl, tip;
  60. try {
  61. address = InetAddress.getByName(url2);
  62. turl = isPerformNameResolution() ? address.getHostName() : url2;
  63. tip = address.getHostAddress();
  64. } catch (UnknownHostException e) {
  65. log.error("Unable to resolve host {}", url2);
  66. turl = url2;
  67. tip = url2;
  68. }
  69. this.host = turl;
  70. ip = tip;
  71. b.append(url2);
  72. b.append("(");
  73. b.append(ip);
  74. b.append("):");
  75. b.append(port);
  76. name = b.toString();
  77. url = String.format("%s:%d",host,port);
  78. }
  79. public String getUrl() {
  80. return url;
  81. }
  82. /**
  83. * Checks whether name resolution should occur.
  84. *
  85. * @return
  86. */
  87. public boolean isPerformNameResolution() {
  88. String sysprop = System.getProperty(
  89. SystemProperties.HECTOR_PERFORM_NAME_RESOLUTION.toString());
  90. return sysprop != null && Boolean.parseBoolean(sysprop);
  91. }
  92. public String getName() {
  93. return name;
  94. }
  95. public String getHost() {
  96. return host;
  97. }
  98. public String getIp() {
  99. return ip;
  100. }
  101. public int getPort() {
  102. return port;
  103. }
  104. @Override
  105. public String toString() {
  106. return name;
  107. }
  108. /**
  109. * Returns true if the ip and port are equal
  110. */
  111. @Override
  112. public boolean equals(Object obj) {
  113. if (! (obj instanceof CassandraHost)) {
  114. return false;
  115. }
  116. CassandraHost other = (CassandraHost) obj;
  117. return other.ip.equals(ip) && other.port == port;
  118. }
  119. @Override
  120. public int hashCode() {
  121. return ip.hashCode();
  122. }
  123. public int getMaxActive() {
  124. return maxActive;
  125. }
  126. public void setMaxActive(int maxActive) {
  127. this.maxActive = maxActive;
  128. }
  129. public int getMaxIdle() {
  130. return maxIdle;
  131. }
  132. public void setMaxIdle(int maxIdle) {
  133. this.maxIdle = maxIdle;
  134. }
  135. public long getMaxWaitTimeWhenExhausted() {
  136. return maxWaitTimeWhenExhausted;
  137. }
  138. public void setMaxWaitTimeWhenExhausted(long maxWaitTimeWhenExhausted) {
  139. this.maxWaitTimeWhenExhausted = maxWaitTimeWhenExhausted;
  140. }
  141. public ExhaustedPolicy getExhaustedPolicy() {
  142. return exhaustedPolicy;
  143. }
  144. public void setExhaustedPolicy(ExhaustedPolicy exhaustedPolicy) {
  145. this.exhaustedPolicy = exhaustedPolicy;
  146. }
  147. public int getCassandraThriftSocketTimeout() {
  148. return cassandraThriftSocketTimeout;
  149. }
  150. public void setCassandraThriftSocketTimeout(int cassandraThriftSocketTimeout) {
  151. this.cassandraThriftSocketTimeout = cassandraThriftSocketTimeout;
  152. }
  153. public boolean getUseThriftFramedTransport() {
  154. return useThriftFramedTransport;
  155. }
  156. public void setUseThriftFramedTransport(boolean useThriftFramedTransport) {
  157. this.useThriftFramedTransport = useThriftFramedTransport;
  158. }
  159. public static String parseHostFromUrl(String urlPort) {
  160. return urlPort.lastIndexOf(':') > 0 ? urlPort.substring(0, urlPort.lastIndexOf(':')) : urlPort;
  161. }
  162. public static int parsePortFromUrl(String urlPort) {
  163. return urlPort.lastIndexOf(':') > 0 ? Integer.parseInt(urlPort.substring(urlPort.lastIndexOf(':')+1, urlPort.length())) : DEFAULT_PORT;
  164. }
  165. public boolean getLifo() {
  166. return lifo;
  167. }
  168. public void setLifo(boolean lifo) {
  169. this.lifo = lifo;
  170. }
  171. public long getMinEvictableIdleTimeMillis() {
  172. return minEvictableIdleTimeMillis;
  173. }
  174. public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
  175. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  176. }
  177. public long getTimeBetweenEvictionRunsMillis() {
  178. return timeBetweenEvictionRunsMillis;
  179. }
  180. public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  181. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  182. }
  183. public boolean getUseSocketKeepalive() {
  184. return useSocketKeepalive;
  185. }
  186. public void setUseSocketKeepalive(boolean useSocketKeepalive) {
  187. this.useSocketKeepalive = useSocketKeepalive;
  188. }
  189. }