protected void checkCassandraException(Exception e) { _mexceptions.incr(); if (e instanceof AlreadyExistsException || e instanceof AuthenticationException || e instanceof DriverException || e instanceof DriverInternalError || e instanceof InvalidConfigurationInQueryException || e instanceof InvalidQueryException || e instanceof InvalidTypeException || e instanceof QueryExecutionException || e instanceof QueryTimeoutException || e instanceof QueryValidationException || e instanceof ReadTimeoutException || e instanceof SyntaxError || e instanceof TraceRetrievalException || e instanceof TruncateException || e instanceof UnauthorizedException || e instanceof UnavailableException || e instanceof ReadTimeoutException || e instanceof WriteTimeoutException) { throw new ReportedFailedException(e); } else { throw new RuntimeException(e); } }
/** * {@inheritDoc} * <p> * Returns true if successfully inserted lock. * Returns false if current lock is owned by this client. * Returns false if WriteTimeoutException thrown. * * @throws CannotAcquireLockException if any DriverException thrown while executing queries. */ @Override public boolean acquire(String clientId) throws CannotAcquireLockException { try { ResultSet resultSet = session.execute(insertLockQuery.bind(lockName, clientId)); Row currentLock = resultSet.one(); // we could already hold the lock and not be aware if a previous acquire had a writetimeout as a timeout is not a failure in cassandra if (currentLock.getBool("[applied]") || clientId.equals(currentLock.getString("client"))) { return true; } else { log.info("Lock currently held by {}", currentLock); return false; } } catch (WriteTimeoutException wte) { log.warn("Query to acquire lock for {} failed to execute: {}", clientId, wte.getMessage()); return false; } catch (DriverException de) { throw new CannotAcquireLockException(String.format("Query to acquire lock %s for client %s failed to execute", lockName, clientId), de); } }
@Override public void output( Collection<Metric> metrics ) { if( metrics.size() == 0 ) { return; } Map<RetentionTable, BatchStatement> stms = LazyMap.<RetentionTable, BatchStatement>lazyMap( new HashMap<>(), () -> new BatchStatement() ); for ( Metric metric : metrics ) { insertMetricIntoBatch( metric, stms ); } KeyspaceMetadata metadata = cluster.getMetadata().getKeyspace( keyspace ); for (RetentionTable table : stms.keySet()) { createTableIfNecessary( table, metadata ); } for ( BatchStatement batch : stms.values() ) { try { session.execute( batch ); } catch ( WriteTimeoutException e ) { log.info( "WriteTimeoutException while sending Metrics to cassandra." ); log.info( e.getMessage() ); log.info( "According to http://www.datastax.com/dev/blog/how-cassandra-deals-with-replica-failure, this is harmless" ); } } EventBusManager.fire( new DrainMetricOutputEvent( ( new PersistentCassandraDrainFactory<>().handledType() ), metrics.size() ) ); }
@Test public void testShouldReturnWriteTimeout() throws Exception { server.prime(when(query).then(writeTimeout(ConsistencyLevel.QUORUM, 3, 1, WriteType.BATCH))); thrown.expect(WriteTimeoutException.class); thrown.expect( match( (WriteTimeoutException wte) -> wte.getRequiredAcknowledgements() == 1 && wte.getReceivedAcknowledgements() == 3 && wte.getConsistencyLevel() == com.datastax.driver.core.ConsistencyLevel.QUORUM && wte.getWriteType() == com.datastax.driver.core.WriteType.BATCH)); query(); }
private void writeElement(int element) { String current = null; int tries = 0; while (true) // if beaten, try again { if (tries > maxRetries) { System.out.println(element + " fail"); break; } tries++; if (current == null) { current = session.execute(QueryBuilder.select("elements") .from(KS, CF) .where(eq("id", 0)) .setConsistencyLevel(ConsistencyLevel.SERIAL)) .one() .getString("elements"); } if (Sets.newHashSet(current.split(";")).contains(String.valueOf(element))) break; // we've timed out, but have actually written the value successfully. String next = current.equals("") ? String.valueOf(element) : current + ";" + element; Row result; try { result = session.execute(update.bind(next, current)).one(); } catch (WriteTimeoutException e) { current = null; continue; } if (result.getBool("[applied]")) { acked.add(element); System.out.println(element + " ok"); break; } current = result.getString("elements"); } }