【问题标题】:cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {})cassandra.cluster.NoHostAvailable: ('无法完成对任何主机的操作', {})
【发布时间】:2018-01-16 05:16:42
【问题描述】:

目前面临一个问题,即在 uWSGI 下运行的烧瓶应用程序在一段时间后出现上述错误。

出现的异常信息是:

cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {})

这是 wsgi.py 的应用程序代码

import sys
import logging

sys.stdout = sys.stderr

from cqlengine.connection import (
cluster as cql_cluster, session as cql_session)
from url.settings import CLUSTER

log = logging.getLogger('uwsgi')

try:
    from uwsgidecorators import postfork
except ImportError:
    # We're not in a uWSGI context, no need to hook Cassandra session
    # initialization to the postfork event.
    pass
else:

@postfork
def cassandra_init():
    """ Initialize a new Cassandra session in the context.

        Ensures that a new session is returned for every new request.
    """
    if cql_cluster is not None:
        cql_cluster.shutdown()
    if cql_session is not None:
        cql_session.shutdown()
    from url.settings import connect_cassandra
    connect_cassandra()
    log.info('Connection with cassandra completed')
from url.app import app as application

这是 connect_cassandra 方法的代码。

CLUSTER = ['XX.XX.XX.XX', 'XX.XX.XX.XX']


def connect_cassandra():
    # next, setup the connection to your cassandra server(s)...
    # see http://datastax.github.io/python-
      driver/api/cassandra/cluster.html for options
    # the list of hosts will be passed to create a Cluster() instance
    from cassandra.cqlengine import connection
    from cassandra.cqlengine.management import sync_table
    from url.models import Links, LinksAnalytics

    connection.setup(CLUSTER, "contentstudio", protocol_version=3)
    sync_table(Links)
    sync_table(LinksAnalytics)

重新启动应用程序可以正常工作 20-30 分钟后,它会停止给出此错误,有时它可以正常工作,这真的很令人沮丧。

此外,在收到此异常消息之间:

cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: XX.XX.XX.XX dc1>: ConnectionException('Host has been marked down or removed',)})

使用 nodetool tpstats 更新 1

节点 1

Pool Name                         Active   Pending      Completed   Blocked  All time blocked
ReadStage                              0         0        1225692         0                 0
MiscStage                              0         0              0         0                 0
CompactionExecutor                     0         0         853120         0                 0
MutationStage                          0         0          62573         0                 0
MemtableReclaimMemory                  0         0           1133         0                 0
PendingRangeCalculator                 0         0              2         0                 0
GossipStage                            0         0        4175516         0                 0
SecondaryIndexManagement               0         0              0         0                 0
HintsDispatcher                        0         0              0         0                 0
RequestResponseStage                   0         0          64064         0                 0
Native-Transport-Requests              0         0       12887762         0             16587
ReadRepairStage                        0         0           6887         0                 0
CounterMutationStage                   0         0              0         0                 0
MigrationStage                         0         0             34         0                 0
MemtablePostFlush                      0         0           1268         0                 0
PerDiskMemtableFlushWriter_0           0         0           1123         0                 0
ValidationExecutor                     0         0              0         0                 0
Sampler                                0         0              0         0                 0
MemtableFlushWriter                    0         0           1125         0                 0
InternalResponseStage                  0         0             45         0                 0
ViewMutationStage                      0         0              0         0                 0
AntiEntropyStage                       0         0              0         0                 0
CacheCleanupExecutor                   0         0              0         0                 0

Message type           Dropped
READ                         0
RANGE_SLICE                  0
_TRACE                       0
HINT                         0
MUTATION                     0
COUNTER_MUTATION             0
BATCH_STORE                  0
BATCH_REMOVE                 0
REQUEST_RESPONSE             0
PAGED_RANGE                  0
READ_REPAIR                  0

节点 2

Pool Name                         Active   Pending      Completed   Blocked  All time blocked
ReadStage                              0         0          29325         0                 0
MiscStage                              0         0              0         0                 0
CompactionExecutor                     0         0         407325         0                 0
MutationStage                          0         0          62573         0                 0
MemtableReclaimMemory                  0         0           1133         0                 0
PendingRangeCalculator                 0         0              4         0                 0
GossipStage                            0         0        4174442         0                 0
SecondaryIndexManagement               0         0              0         0                 0
HintsDispatcher                        0         0              0         0                 0
RequestResponseStage                   0         0           6845         0                 0
Native-Transport-Requests              0         0         989812         0                 0
ReadRepairStage                        0         0            102         0                 0
CounterMutationStage                   0         0              0         0                 0
MigrationStage                         0         0             26         0                 0
MemtablePostFlush                      0         0           1268         0                 0
PerDiskMemtableFlushWriter_0           0         0           1123         0                 0
ValidationExecutor                     0         0              0         0                 0
Sampler                                0         0              0         0                 0
MemtableFlushWriter                    0         0           1125         0                 0
InternalResponseStage                  0         0              0         0                 0
ViewMutationStage                      0         0              0         0                 0
AntiEntropyStage                       0         0              0         0                 0
CacheCleanupExecutor                   0         0              0         0                 0

Message type           Dropped
READ                         0
RANGE_SLICE                  0
_TRACE                       0
HINT                         0
MUTATION                     0
COUNTER_MUTATION             0
BATCH_STORE                  0
BATCH_REMOVE                 0
REQUEST_RESPONSE             0
PAGED_RANGE                  0
READ_REPAIR                  0

【问题讨论】:

  • 如果您在应用程序发出这些消息时在节点上启动nodetool tpstats,您会得到什么?你有掉过消息吗?
  • @ThomasArnaud 我已经添加了 nodetool tpstats,现在没有出现错误。我正在尝试重现此错误并将再次发布 nodetool tpstats。
  • 如果你没有任何丢弃的消息,这意味着节点没有过载,问题出在客户端。也许负载对驱动程序来说太高了。我不是 python 专家,但似乎每个连接的默认最大运行请求为 100。Java 驱动程序建议对 LOCAL 主机使用 1024 最大请求。也许你应该尝试提升它:datastax.github.io/python-driver/api/cassandra/…
  • 感谢您的帮助,我将尝试将最大请求数提高到 1024,看看问题是否仍然存在。
  • @Azhar 你找到解决这个问题的方法了吗?

标签: python python-3.x cassandra cassandra-3.0 nosql


【解决方案1】:

我遇到了同样的问题,就我而言,问题是默认情况下 uwsgi 不启用线程,而 python-driver 有一个内部线程池。

尝试启用线程:https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#a-note-on-python-threads

【讨论】:

    【解决方案2】:

    Cassandra 是一个用 Java 编写的 NoSQL 分布式 DBMS。 Java 使用称为HEAP 的特殊数据结构。 Java 对象驻留在堆区域中。堆是在 JVM(Java 虚拟机)启动时创建的,并且在应用程序运行时可能会增加或减少大小。 Cassandra 以 4 种方式使用内存:

    • Java 堆
    • 堆外内存
    • 操作系统页面缓存
    • 操作系统 TCP/IP 堆栈 I/O 缓存

    您收到的错误是由于操作系统系统内存 (OS RAM) 不足所致。在您的情况下,可能堆大小与内存不符。

    有一些指导方针和建议:

    HEAP_SIZE 通常在系统内存的 1/4 到 1/2 之间,但不大于 32 GB。 NEW_HEAP_SIZE 确定分配给较新对象的堆内存量。数据库计算此属性的默认值(以 MB 为单位),取值较小者:

    • 100 倍核心数
    • MAX_HEAP_SIZE 的 1/4

    cassandra-env.sh 使用以下公式自动将最小和最大大小配置为相同的值: Max(Min(1/2 RAM, 1024 Megabytes), Min(1/4 RAM, 32765 Megabytes))

    要调整 JVM 堆大小,请取消注释并在 jvm-server.options 文件中设置以下参数:

    Minimum (-Xms)

    Maximum (-Xmx)

    New generation (-Xmn)

    如果您使用 docker for cassandra,您可以在 docker-compose.yml 文件中的 cassandra 服务的环境中设置这些参数。

    更多信息:Cassandra AWS System Memory GuidelinesChaning heap size parameters

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-24
      • 2021-07-27
      • 2016-07-11
      • 2021-01-28
      • 2019-05-27
      • 2014-04-15
      • 2013-10-31
      • 2013-07-25
      相关资源
      最近更新 更多