Skip to content
Browse files

Merge pull request #634 from shaunkalley/AddedHostToException

added support to hector exceptions to display the host
  • Loading branch information...
2 parents 05e473a + 7ef2306 commit defc4e5e3591cf78796c5070bd3d5e0416ebe090 @zznate zznate committed
View
40 core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java
@@ -1,34 +1,22 @@
package me.prettyprint.cassandra.connection;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
import me.prettyprint.cassandra.connection.client.HClient;
import me.prettyprint.cassandra.connection.factory.HClientFactory;
import me.prettyprint.cassandra.connection.factory.HClientFactoryProvider;
-import me.prettyprint.cassandra.service.CassandraClientMonitor;
+import me.prettyprint.cassandra.service.*;
import me.prettyprint.cassandra.service.CassandraClientMonitor.Counter;
-import me.prettyprint.cassandra.service.CassandraHost;
-import me.prettyprint.cassandra.service.CassandraHostConfigurator;
-import me.prettyprint.cassandra.service.ExceptionsTranslator;
-import me.prettyprint.cassandra.service.ExceptionsTranslatorImpl;
-import me.prettyprint.cassandra.service.FailoverPolicy;
-import me.prettyprint.cassandra.service.JmxMonitor;
-import me.prettyprint.cassandra.service.Operation;
import me.prettyprint.hector.api.ClockResolution;
-import me.prettyprint.hector.api.exceptions.HCassandraInternalException;
-import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
-import me.prettyprint.hector.api.exceptions.HPoolRecoverableException;
-import me.prettyprint.hector.api.exceptions.HTimedOutException;
-import me.prettyprint.hector.api.exceptions.HUnavailableException;
-import me.prettyprint.hector.api.exceptions.HectorException;
-import me.prettyprint.hector.api.exceptions.HectorTransportException;
+import me.prettyprint.hector.api.exceptions.*;
+
import org.apache.cassandra.thrift.AuthenticationRequest;
import org.apache.cassandra.thrift.Cassandra;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
public class HConnectionManager {
private static final Logger log = LoggerFactory.getLogger(HConnectionManager.class);
@@ -257,7 +245,8 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
break;
} catch (Exception ex) {
- HectorException he = exceptionsTranslator.translate(ex);
+ CassandraHost host = getHost(pool, client);
+ HectorException he = exceptionsTranslator.translate(ex, host);
if ( he instanceof HUnavailableException) {
// break out on HUnavailableException as well since we can no longer satisfy the CL
throw he;
@@ -315,6 +304,17 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
}
}
+ private CassandraHost getHost(HClientPool pool, HClient client) {
+ CassandraHost host = null;
+ if (pool != null) {
+ host = pool.getCassandraHost();
+ }
+ if (host == null && client != null) {
+ host = client.getCassandraHost();
+ }
+ return host;
+ }
+
private void closeClient(HClient client) {
if ( client != null ) {
client.close();
View
7 core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslator.java
@@ -4,12 +4,13 @@
/**
* Translates exceptions throw by thrift or pool to HectorException instances.
- *
+ *
* @author Ran Tavory (ran@outbrain.com)
*
*/
public interface ExceptionsTranslator {
-
+
HectorException translate(Throwable originalException);
-
+
+ HectorException translate(Throwable originalException, CassandraHost host);
}
View
44 core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java
@@ -7,12 +7,12 @@
import me.prettyprint.hector.api.exceptions.HInactivePoolException;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hector.api.exceptions.HNotFoundException;
+import me.prettyprint.hector.api.exceptions.HPoolExhaustedException;
import me.prettyprint.hector.api.exceptions.HPoolRecoverableException;
import me.prettyprint.hector.api.exceptions.HTimedOutException;
import me.prettyprint.hector.api.exceptions.HUnavailableException;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.exceptions.HectorTransportException;
-import me.prettyprint.hector.api.exceptions.HPoolExhaustedException;
import me.prettyprint.hector.api.exceptions.PoolIllegalStateException;
import org.apache.thrift.TApplicationException;
@@ -25,8 +25,13 @@
@Override
public HectorException translate(Throwable original) {
+ return translate(original, null);
+ }
+
+ @Override
+ public HectorException translate(Throwable original, CassandraHost host) {
if (original instanceof HectorException) {
- return (HectorException) original;
+ return setHost((HectorException) original, host);
} else if (original instanceof TApplicationException) {
return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage());
} else if (original instanceof TTransportException) {
@@ -34,12 +39,12 @@ public HectorException translate(Throwable original) {
// TODO this may be an issue on the Cassandra side which warrants investigation.
// I seem to remember these coming back as TimedOutException previously
if (((TTransportException) original).getCause() instanceof SocketTimeoutException) {
- return new HTimedOutException(original);
+ return setHost(new HTimedOutException(original), host);
} else {
- return new HectorTransportException(original);
+ return setHost(new HectorTransportException(original), host);
}
} else if (original instanceof org.apache.cassandra.thrift.TimedOutException) {
- return new HTimedOutException(original);
+ return setHost(new HTimedOutException(original), host);
} else if (original instanceof org.apache.cassandra.thrift.InvalidRequestException) {
// See bug https://issues.apache.org/jira/browse/CASSANDRA-1862
// If a bootstrapping node is accessed it responds with IRE. It makes more sense to throw
@@ -47,32 +52,37 @@ public HectorException translate(Throwable original) {
// Hector wraps this caveat and fixes this.
String why = ((org.apache.cassandra.thrift.InvalidRequestException) original).getWhy();
if (why != null && why.contains("bootstrap")) {
- return new HUnavailableException(original);
+ return setHost(new HUnavailableException(original), host);
}
HInvalidRequestException e = new HInvalidRequestException(original);
e.setWhy(why);
- return e;
+ return setHost(e, host);
} else if (original instanceof HPoolExhaustedException ) {
- return (HPoolExhaustedException) original;
+ return setHost((HPoolExhaustedException) original, host);
} else if (original instanceof HPoolRecoverableException ) {
- return (HPoolRecoverableException) original;
+ return setHost((HPoolRecoverableException) original, host);
} else if (original instanceof HInactivePoolException ) {
- return (HInactivePoolException) original;
+ return setHost((HInactivePoolException) original, host);
} else if (original instanceof TProtocolException) {
- return new HInvalidRequestException(original);
+ return setHost(new HInvalidRequestException(original), host);
} else if (original instanceof org.apache.cassandra.thrift.NotFoundException) {
- return new HNotFoundException(original);
+ return setHost(new HNotFoundException(original), host);
} else if (original instanceof org.apache.cassandra.thrift.UnavailableException) {
- return new HUnavailableException(original);
+ return setHost(new HUnavailableException(original), host);
} else if (original instanceof TException) {
- return new HectorTransportException(original);
+ return setHost(new HectorTransportException(original), host);
} else if (original instanceof NoSuchElementException) {
- return new HPoolExhaustedException(original);
+ return setHost(new HPoolExhaustedException(original), host);
} else if (original instanceof IllegalStateException) {
- return new PoolIllegalStateException(original);
+ return setHost(new PoolIllegalStateException(original), host);
} else {
- return new HectorException(original);
+ return setHost(new HectorException(original), host);
}
}
+ private HectorException setHost(HectorException he, CassandraHost host) {
+ he.setHost(host);
+ return he;
+ }
+
}
View
21 core/src/main/java/me/prettyprint/hector/api/exceptions/HectorException.java
@@ -1,5 +1,7 @@
package me.prettyprint.hector.api.exceptions;
+import me.prettyprint.cassandra.service.CassandraHost;
+
/**
* Base exception class for all Hector related exceptions.
*
@@ -10,6 +12,8 @@
private static final long serialVersionUID = -8498691501268563571L;
+ private CassandraHost host;
+
public HectorException(String msg) {
super(msg);
}
@@ -21,4 +25,21 @@ public HectorException(Throwable t) {
public HectorException(String s, Throwable t) {
super(s, t);
}
+
+ public CassandraHost getHost() {
+ return host;
+ }
+
+ public void setHost(CassandraHost host) {
+ this.host = host;
+ }
+
+ @Override
+ public String getMessage() {
+ if (host != null) {
+ return "[" + host.toString() + "] " + super.getMessage();
+ } else {
+ return super.getMessage();
+ }
+ }
}

0 comments on commit defc4e5

Please sign in to comment.
Something went wrong with that request. Please try again.