【问题标题】:Issue with Hector API and Cassandra database: Undocumented exceptionHector API 和 Cassandra 数据库的问题:未记录的异常
【发布时间】:2012-04-17 04:52:47
【问题描述】:

每当我使用任何 Hector API 函数访问我的 Cassandra 数据库时,都会出现异常:

me.prettyprint.hector.api.exceptions.HectorException:所有主机池标记为已关闭。重试负担推给客户端。

我的服务器确实在后台运行 Cassandra 数据库。

我阅读了异常,它实际上没有记录。异常似乎是由于连接问题。

我该如何解决?

【问题讨论】:

    标签: cassandra hector


    【解决方案1】:

    如果 Hector 客户端无法连接到 Cassandra,您将收到该错误。这可能有很多原因和尝试的方法:

    • 确保代码中的连接属性(ip/host/port)配置正确。
    • 确保您可以使用 cassandra-cli 远程连接到它 - 这可能是网络问题。
    • 尝试在此处发布您的连接代码 -- 可能那里有问题。

    【讨论】:

      【解决方案2】:

      由于网络连接问题,我随机收到此错误,但重试几次通常会修复它。这是我用来重试 Hector API 函数的代码:

      /** An interface where inside the execute() method I call Hector */
      public interface Retriable<T> {
          T execute();
      }
      
      /**
       * Executes operation and retries N times in case of an exception
       * @param retriable
       * @param maxRetries
       * @param <T>
       * @return
       */
      public static <T> T executeWithRetry(Retriable<T> retriable, int maxRetries) {
          T result;
          int retries = 0;
          long sleepSec = 1;
          // retry in case of an exception:
          while (true) {
              try {
                  result = retriable.execute();
                  break;
              } catch (Exception e) {
                  if (retries == maxRetries) {
                      LOG.error("Exception occurred. Reached max retries.", e);
                      throw e;
                  }
                  retries++;
                  LOG.error(String.format("Exception occurred. Retrying in %d seconds - #%d", sleepSec, retries), e);
                  try {
                      Thread.sleep(sleepSec * 1000);
                      // increase sleepSec exponentially:
                      sleepSec *= 2;
                  } catch (InterruptedException e1) {
                      e1.printStackTrace();
                  }
              }
          }
          return result;
      }
      

      以及如何使用它的示例:

          ColumnFamilyResult<String, String> columns = executeWithRetry(new Retriable<ColumnFamilyResult<String, String>>() {
              @Override
              public ColumnFamilyResult<String, String> execute() {
                  return template.queryColumns(row.getKey());
              }
          });
      

      【讨论】:

        【解决方案3】:

        我在使用 cassandra-unit 2.0.2.1 时遇到了同样的错误,但 将版本降级2.0.2.0 解决了这个问题。这很奇怪,但我现在使用 2.0.2.0 和一些额外的 sbt 依赖项

                 "com.datastax.cassandra" % "cassandra-driver-core" % "2.0.1",
                 "org.cassandraunit" % "cassandra-unit" % "2.0.2.0" withSources() withJavadoc()
        

        【讨论】:

          猜你喜欢
          • 2011-11-12
          • 1970-01-01
          • 2014-08-03
          • 2013-07-19
          • 1970-01-01
          • 1970-01-01
          • 2012-03-12
          • 2011-01-13
          • 2011-09-25
          相关资源
          最近更新 更多