private <T> T executeWithSession(SessionCallable<T> sessionCallable) { ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy(); ReconnectionSchedule schedule = reconnectionPolicy.newSchedule(); long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis(); while (true) { try { return sessionCallable.executeWithSession(session.get()); } catch (NoHostAvailableException e) { long timeLeft = deadline - System.currentTimeMillis(); if (timeLeft <= 0) { throw e; } else { long delay = Math.min(schedule.nextDelayMs(), timeLeft); log.warn(e.getCustomMessage(10, true, true)); log.warn("Reconnecting in %dms", delay); try { Thread.sleep(delay); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new RuntimeException("interrupted", interrupted); } } } } }
@OnScheduled public void onScheduled(ProcessContext context) { final boolean connectionProviderIsSet = context.getProperty(CONNECTION_PROVIDER_SERVICE).isSet(); if (connectionProviderIsSet) { CassandraSessionProviderService sessionProvider = context.getProperty(CONNECTION_PROVIDER_SERVICE).asControllerService(CassandraSessionProviderService.class); cluster.set(sessionProvider.getCluster()); cassandraSession.set(sessionProvider.getCassandraSession()); return; } try { connectToCassandra(context); } catch (NoHostAvailableException nhae) { getLogger().error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae); getLogger().error(nhae.getCustomMessage(10, true, false)); throw new ProcessException(nhae); } catch (AuthenticationException ae) { getLogger().error("Invalid username/password combination", ae); throw new ProcessException(ae); } }
getLogger().error(nhae.getCustomMessage(10, true, false)); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_RETRY);
getLogger().error(nhae.getCustomMessage(10, true, false)); fileToProcess = session.penalize(fileToProcess); session.transfer(fileToProcess, REL_RETRY);
@Test(groups = "unit") public void should_build_formatted_message_with_stack_traces() { NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); String message = e.getCustomMessage(3, true, true); assertThat(message).startsWith("All host(s) tried for query failed (tried:\n"); assertThat(message).contains("/127.0.0.1:9042\nmock stack trace 1\n"); assertThat(message).contains("/127.0.0.3:9042\nmock stack trace 3\n"); assertThat(message).contains("/127.0.0.2:9042\nmock stack trace 2\n"); }
@Test(groups = "unit") public void should_build_formatted_message_without_stack_traces() { NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); String message = e.getCustomMessage(3, true, false); assertThat(message).startsWith("All host(s) tried for query failed (tried:\n"); assertThat(message) .contains( "/127.0.0.1:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 1)\n"); assertThat(message) .contains( "/127.0.0.2:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 2)\n"); assertThat(message) .contains( "/127.0.0.3:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 3)\n"); }
@Test(groups = "unit") public void should_build_formatted_message_with_stack_traces() { NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); String message = e.getCustomMessage(3, true, true); assertThat(message).startsWith("All host(s) tried for query failed (tried:\n"); assertThat(message).contains("/127.0.0.1:9042\nmock stack trace 1\n"); assertThat(message).contains("/127.0.0.3:9042\nmock stack trace 3\n"); assertThat(message).contains("/127.0.0.2:9042\nmock stack trace 2\n"); }
private <T> T executeWithSession(SessionCallable<T> sessionCallable) { ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy(); ReconnectionSchedule schedule = reconnectionPolicy.newSchedule(); long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis(); while (true) { try { return sessionCallable.executeWithSession(session.get()); } catch (NoHostAvailableException e) { long timeLeft = deadline - System.currentTimeMillis(); if (timeLeft <= 0) { throw e; } else { long delay = Math.min(schedule.nextDelayMs(), timeLeft); log.warn(e.getCustomMessage(10, true, true)); log.warn("Reconnecting in %dms", delay); try { Thread.sleep(delay); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new RuntimeException("interrupted", interrupted); } } } } }
private <T> T executeWithSession(SessionCallable<T> sessionCallable) { ReconnectionPolicy reconnectionPolicy = cluster.getConfiguration().getPolicies().getReconnectionPolicy(); ReconnectionSchedule schedule = reconnectionPolicy.newSchedule(); long deadline = System.currentTimeMillis() + noHostAvailableRetryTimeout.toMillis(); while (true) { try { return sessionCallable.executeWithSession(session.get()); } catch (NoHostAvailableException e) { long timeLeft = deadline - System.currentTimeMillis(); if (timeLeft <= 0) { throw e; } else { long delay = Math.min(schedule.nextDelayMs(), timeLeft); log.warn(e.getCustomMessage(10, true, true)); log.warn("Reconnecting in %dms", delay); try { Thread.sleep(delay); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new RuntimeException("interrupted", interrupted); } } } } }
@Test(groups = "unit") public void should_build_formatted_message_without_stack_traces() { NoHostAvailableException e = new NoHostAvailableException(buildMockErrors(3)); String message = e.getCustomMessage(3, true, false); assertThat(message).startsWith("All host(s) tried for query failed (tried:\n"); assertThat(message) .contains( "/127.0.0.1:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 1)\n"); assertThat(message) .contains( "/127.0.0.2:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 2)\n"); assertThat(message) .contains( "/127.0.0.3:9042 (com.datastax.driver.core.exceptions.NoHostAvailableExceptionTest$MockError: mock error 3)\n"); }
@OnScheduled public void onScheduled(final ProcessContext context) { ComponentLog log = getLogger(); // Initialize the prepared statement cache int statementCacheSize = context.getProperty(STATEMENT_CACHE_SIZE).evaluateAttributeExpressions().asInteger(); statementCache = CacheBuilder.newBuilder() .maximumSize(statementCacheSize) .<String, PreparedStatement>build() .asMap(); try { connectToCassandra(context); } catch (final NoHostAvailableException nhae) { log.error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae); // Log up to 10 error messages. Otherwise if a 1000-node cluster was specified but there was no connectivity, // a thousand error messages would be logged. However we would like information from Cassandra itself, so // cap the error limit at 10, format the messages, and don't include the stack trace (it is displayed by the // logger message above). log.error(nhae.getCustomMessage(10, true, false)); throw new ProcessException(nhae); } catch (final AuthenticationException ae) { log.error("Invalid username/password combination", ae); throw new ProcessException(ae); } }
@OnScheduled public void onScheduled(final ProcessContext context) { ComponentLog log = getLogger(); try { connectToCassandra(context); final int fetchSize = context.getProperty(FETCH_SIZE).evaluateAttributeExpressions().asInteger(); if (fetchSize > 0) { synchronized (cluster.get()) { cluster.get().getConfiguration().getQueryOptions().setFetchSize(fetchSize); } } } catch (final NoHostAvailableException nhae) { log.error("No host in the Cassandra cluster can be contacted successfully to execute this query", nhae); // Log up to 10 error messages. Otherwise if a 1000-node cluster was specified but there was no connectivity, // a thousand error messages would be logged. However we would like information from Cassandra itself, so // cap the error limit at 10, format the messages, and don't include the stack trace (it is displayed by the // logger message above). log.error(nhae.getCustomMessage(10, true, false)); throw new ProcessException(nhae); } catch (final AuthenticationException ae) { log.error("Invalid username/password combination", ae); throw new ProcessException(ae); } }
@OnScheduled public void onScheduled(ProcessContext context) { try { connectToCassandra(context); } catch (NoHostAvailableException nhae) { getLogger().error("No host in the Cassandra cluster can be contacted successfully to execute this statement", nhae); getLogger().error(nhae.getCustomMessage(10, true, false)); throw new ProcessException(nhae); } catch (AuthenticationException ae) { getLogger().error("Invalid username/password combination", ae); throw new ProcessException(ae); } }
getLogger().error(nhae.getCustomMessage(10, true, false)); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_RETRY);
getLogger().error(nhae.getCustomMessage(10, true, false)); fileToProcess = session.penalize(fileToProcess); session.transfer(fileToProcess, REL_RETRY);