Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Close connection on invalida queries and cass errors
Browse files Browse the repository at this point in the history
  • Loading branch information
patricioe committed Oct 10, 2011
1 parent 296fd70 commit 1732aea
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
Expand Up @@ -237,23 +237,23 @@ public void operateWithFailover(Operation<?> op) throws HectorException {

} catch (Exception ex) {
HectorException he = exceptionsTranslator.translate(ex);
if ( he instanceof HInvalidRequestException || he instanceof HCassandraInternalException || he instanceof HUnavailableException) {
if ( he instanceof HUnavailableException) {
// break out on HUnavailableException as well since we can no longer satisfy the CL
throw he;
} else if ( he instanceof HectorTransportException) {
// client can be null in this situation
if ( client != null ) {
client.close();
}
} else if (he instanceof HInvalidRequestException || he instanceof HCassandraInternalException) {
closeClient(client);
throw he;
} else if (he instanceof HectorTransportException) {
closeClient(client);
markHostAsDown(pool.getCassandraHost());
excludeHosts.add(pool.getCassandraHost());
retryable = true;

monitor.incCounter(Counter.RECOVERABLE_TRANSPORT_EXCEPTIONS);

} else if (he instanceof HTimedOutException ) {
// DO NOT drecrement retries, we will be keep retrying on timeouts until it comes back
// if HLT.checkTimeout(cassandraHost): suspendHost(cassandraHost);
// if HLT.checkTimeout(cassandraHost): suspendHost(cassandraHost);
doTimeoutCheck(pool.getCassandraHost());

retryable = true;
Expand All @@ -275,8 +275,9 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
// that we don't add in time.
retryable = false;
}
if ( retries <= 0 || retryable == false) throw he;

if ( retries <= 0 || retryable == false)
throw he;

log.warn("Could not fullfill request on this host {}", client);
log.warn("Exception: ", he);
monitor.incCounter(Counter.SKIP_HOST_SUCCESS);
Expand All @@ -291,15 +292,21 @@ public void operateWithFailover(Operation<?> op) throws HectorException {
}
}
}


private void closeClient(HThriftClient client) {
if ( client != null ) {
client.close();
}
}

public HOpTimer getTimer() {
return timer;
return timer;
}

public void setTimer(HOpTimer timer) {
this.timer = timer;
this.timer = timer;
}

/**
* Use the HostTimeoutCheck and initiate a suspend if and only if
* we are configured for such AND there is more than one operating host pool
Expand Down
@@ -0,0 +1,68 @@
package me.prettyprint.cassandra.model;

import static me.prettyprint.hector.api.factory.HFactory.createColumn;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspace;
import static me.prettyprint.hector.api.factory.HFactory.createMutator;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.serializers.LongSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.ColumnSlice;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SliceQuery;

import org.junit.Before;
import org.junit.Test;

public class GetSliceQueryTest extends BaseEmbededServerSetupTest {

private final static String KEYSPACE = "Keyspace1";
private static final StringSerializer se = new StringSerializer();
private static final LongSerializer le = new LongSerializer();
private Cluster cluster;
private Keyspace keyspace;
private String cf = "Standard1";

@Before
public void setupCase() {
CassandraHostConfigurator chc = new CassandraHostConfigurator("127.0.0.1:9170");
chc.setMaxActive(2);
cluster = getOrCreateCluster("MyCluster", chc);
keyspace = createKeyspace(KEYSPACE, cluster);

createMutator(keyspace, se)
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear1", 1974L, se, le))
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear2", 1975L, se, le))
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear3", 1976L, se, le))
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear4", 1977L, se, le))
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear5", 1978L, se, le))
.addInsertion("getSliceTest_key3", cf, createColumn("birthyear6", 1979L, se, le))
.execute();
}

@Test
public void testNullKeyInvalidQuery() {
SliceQuery<String, String, Long> sq = HFactory.createSliceQuery(keyspace, se, se, le);
sq.setColumnFamily(cf);
sq.setRange("birthyear1", "birthyear4", false, 100);
// we are missing sq.setKey(...);

try {
sq.execute();
fail();
} catch (HInvalidRequestException he) {
// ok!
}

sq.setKey("getSliceTest_key3");
QueryResult<ColumnSlice<String, Long>> result = sq.execute();
assertEquals(4,result.get().getColumns().size());
}
}

0 comments on commit 1732aea

Please sign in to comment.