目前面临一个问题,即在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
我有同样的问题,在我的情况下,问题是uwsgi默认情况下不启用线程,并且python-driver有一个内部线程池。
尝试启用线程:https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#a-note-on-python-threads