使用Python Cassandra驱动程序编写时的服务器错误

时间:2015-07-13 20:57:54

标签: python-2.7 cassandra datastax

code=1000 [Unavailable exception] message="Cannot achieve consistency level ONE" info={'required_replicas': 1, 'alive_replicas': 0, 'consistency': 'ONE'}
code=1100 [Coordinator node timed out waiting for replica nodes' responses] message="Operation timed out - received only 0 responses." info={'received_responses': 0, 'required_responses': 1, 'consistency': 'ONE'}
code=1200 [Coordinator node timed out waiting for replica nodes' responses] message="Operation timed out - received only 0 responses." info={'received_responses': 0, 'required_responses': 1, 'consistency': 'ONE'}

我通过python cassandra-driver 2.6版插入Cassandra Cassandra 2.0.13(单节点进行测试)

以下是我的键空间和表定义:

CREATE KEYSPACE test_keyspace WITH replication = {   'class': 'SimpleStrategy',   'replication_factor': '1' };


 CREATE TABLE test_table (
  key text PRIMARY KEY,
  column1 text,
  ...,
  column17 text
 ) WITH COMPACT STORAGE AND
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.000000 AND
  gc_grace_seconds=864000 AND
  read_repair_chance=0.100000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'SnappyCompressor'};

我尝试了什么:

1)多处理(协议版本设置为1) 每个流程都有自己的clustersession(default_timeout设置为30.0)

def get_cassandra_session():
    """creates cluster and gets the session base on key space"""
    # be aware that session cannot be shared between threads/processes
    # or it will raise OperationTimedOut Exception
    if CLUSTER_HOST2:
        cluster = cassandra.cluster.Cluster([CLUSTER_HOST1, CLUSTER_HOST2])
    else:
        # if only one address is available, we have to use older protocol version
        cluster = cassandra.cluster.Cluster([CLUSTER_HOST1], protocol_version=1)

    session = cluster.connect(KEY_SPACE)
    session.default_timeout = 30.0
    return session

2)批量插入(协议版本设置为2,因为在Cassandra 2.X上启用了BatchStatement)

def batch_insert(session, batch_queue, batch):
    try:
        insert_user = session.prepare("INSERT INTO " + db.TABLE + " (" + db.COLUMN1 + "," + db.COLUMN2 + "," + db.COLUMN3 +
                                      "," + db.COLUMN4 + ") VALUES (?,?,?,?)")
        while batch_queue.qsize() > 0:
            '''batch queue size is 1000'''
            row_tuple = batch_queue.get()
            batch.add(insert_user, row_tuple)

        session.execute(batch)
    except Exception as e:
        logger.error("batch insert fail.... %s", e)

上述函数由:

调用
 batch = BatchStatement(consistency_level=ConsistencyLevel.ONE)
 batch_insert(session, batch_queue, batch)

元组存储在batch_queue中。

3)同步执行 几天前,我发布了另一个问题Cassandra update fails,cassandra抱怨TimeOut问题。我正在使用同步执行进行更新。

任何人都可以提供帮助,这是我的代码问题还是python cassandra-driver问题还是Cassandra本身?

万分感谢!

2 个答案:

答案 0 :(得分:2)

如果您的问题是关于顶部的错误,那么这些是服务器端错误响应。

第一个说你联系的协调员不能满足CL.ONE的请求,它认为节点是活着的。如果所有副本都已关闭(更可能是复制因子较低),就会发生这种情况。

其他两个错误是超时,协调员没有在configured in the cassandra.yaml的时间内从“实时”节点获得响应。

所有这些都表明您所连接的群集不健康。这可能是因为它不堪重负(高GC暂停)或遇到网络问题。检查服务器日志以获取线索。

答案 1 :(得分:0)

我遇到了以下错误,看起来非常相似:

cassandra.Unavailable:来自服务器的错误:code = 1000 [Unavailable exception] message =“无法达到一致性级别LOCAL_ONE” info = {'consistency':'LOCAL_ONE','alive_replicas':0,'required_replicas':1}

当我在代码中添加sleep(0.5)时,它工作正常。我试图写得太快了...