Skip to content

Commit

Permalink
Merge remote-tracking branch 'hector-origin/master' into ImprovedConn…
Browse files Browse the repository at this point in the history
…ectionManager

Conflicts:
	core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java
  • Loading branch information
Shaun Kalley committed Sep 6, 2013
2 parents 024a32d + ebcbb9e commit 5d0c279
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 31 deletions.
Expand Up @@ -245,8 +245,9 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
break;

} catch (Exception ex) {
HectorException he = exceptionsTranslator.translate(ex);
if (he instanceof HUnavailableException) {
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;
} else if (he instanceof HInvalidRequestException || he instanceof HCassandraInternalException) {
Expand Down Expand Up @@ -314,6 +315,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();
Expand Down
Expand Up @@ -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);
}
Expand Up @@ -4,15 +4,13 @@
import java.util.NoSuchElementException;

import me.prettyprint.hector.api.exceptions.HCassandraInternalException;
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.HPoolRecoverableException;
import me.prettyprint.hector.api.exceptions.HPoolExhaustedException;
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;
Expand All @@ -25,54 +23,57 @@ public final class ExceptionsTranslatorImpl implements ExceptionsTranslator {

@Override
public HectorException translate(Throwable original) {
return translate(original, null);
}

@Override
public HectorException translate(Throwable original, CassandraHost host) {
HectorException he;
if (original instanceof HectorException) {
return (HectorException) original;
he = (HectorException) original;
} else if (original instanceof TApplicationException) {
return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage());
he = new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage());
} else if (original instanceof TTransportException) {
// if the underlying cause is a scoket timeout, reflect that directly
// 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);
if (original.getCause() instanceof SocketTimeoutException) {
he = new HTimedOutException(original);
} else {
return new HectorTransportException(original);
he = new HectorTransportException(original);
}
} else if (original instanceof org.apache.cassandra.thrift.TimedOutException) {
return new HTimedOutException(original);
he = new HTimedOutException(original);
} 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
// UnavailableException.
// 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);
he = new HUnavailableException(original);
} else {
HInvalidRequestException e = new HInvalidRequestException(original);
e.setWhy(why);
he = e;
}
HInvalidRequestException e = new HInvalidRequestException(original);
e.setWhy(why);
return e;
} else if (original instanceof HPoolExhaustedException ) {
return (HPoolExhaustedException) original;
} else if (original instanceof HPoolRecoverableException ) {
return (HPoolRecoverableException) original;
} else if (original instanceof HInactivePoolException ) {
return (HInactivePoolException) original;
} else if (original instanceof TProtocolException) {
return new HInvalidRequestException(original);
he = new HInvalidRequestException(original);
} else if (original instanceof org.apache.cassandra.thrift.NotFoundException) {
return new HNotFoundException(original);
he = new HNotFoundException(original);
} else if (original instanceof org.apache.cassandra.thrift.UnavailableException) {
return new HUnavailableException(original);
he = new HUnavailableException(original);
} else if (original instanceof TException) {
return new HectorTransportException(original);
he = new HectorTransportException(original);
} else if (original instanceof NoSuchElementException) {
return new HPoolExhaustedException(original);
he = new HPoolExhaustedException(original);
} else if (original instanceof IllegalStateException) {
return new PoolIllegalStateException(original);
he = new PoolIllegalStateException(original);
} else {
return new HectorException(original);
he = new HectorException(original);
}
he.setHost(host);
return he;
}

}
@@ -1,5 +1,7 @@
package me.prettyprint.hector.api.exceptions;

import me.prettyprint.cassandra.service.CassandraHost;

/**
* Base exception class for all Hector related exceptions.
*
Expand All @@ -10,6 +12,8 @@ public class HectorException extends RuntimeException {

private static final long serialVersionUID = -8498691501268563571L;

private CassandraHost host;

public HectorException(String msg) {
super(msg);
}
Expand All @@ -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 5d0c279

Please sign in to comment.