Cassandra更新失败 [英] Cassandra update fails

查看:950
本文介绍了Cassandra更新失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决
我在3个节点上测试更新,其中一个节点上的时间是1秒钟,因此在更新行时,写入时间总是在时间戳之后,cassandra不会更新行。我同步所有节点时间,问题已修复。



编辑:
我双重检查结果,部分更新失败。没有错误/异常消息



我有一个cassandra集群(Cassandra 2.0.13),它包含5个节点。使用 python(2.6.6)cassandra驱动程序(2.6.0c2)将数据插入数据库。我的服务器系统是Centos6.X



以下代码是我如何连接到cassandra和获取会话。我提供了最多2个节点的ip地址,并选择键空间。

  def get_cassandra_session():
创建集群并获取基于键空间的会话
#注意会话不能在线程/进程之间共享
#或者它将引发OperationTimedOut异常
如果CLUSTER_HOST2:
cluster = cassandra.cluster.Cluster([CLUSTER_HOST1,CLUSTER_HOST2])
else:
#如果只有一个地址可用,我们必须使用旧的协议版本
cluster = cassandra.cluster。群集([CLUSTER_HOST1],protocol_version = 1)

session = cluster.connect(KEY_SPACE)
返回会话

对于每一行,我有17列,如果键在数据库中不存在,我将使用 session insert其他列默认值,然后更新特定列的值。

  def insert_initial_row(session,key):
session。执行(INITIAL_INSERTION_STATEMENT,tuple(INITIAL_COLUMNS_VALUES))


def update_columnX(session,key,column):
session.execute(INSERT INTO+ TABLE + KEY +,+ COLUMN_X +)VALUES(%s,%s),(键,列))

def has_found(session,key):
键位于数据库中或不是
query =SELECT+*+FROM+ KEY_SPACE +。 + TABLE \
+WHERE+ KEY +=+'+ key +'
#返回列表
row = session.execute(query)
return True if row else False

以下是我如何调用它们:

 用于keys_set中的a_key:
keys_set包含100个无重复键
如果has_found(session,a_key )
update_columnX(session,a_key,column x value)
else:
键不在db中,用所有默认值初始化,然后更新列x
insert_initial_row(session,a_key)
如果has_found(sessin,a_key):
update_columnX(session,a_key,column x value)
else:
logger.error(not not correctly correctly ...)

我试图插入100行更新每一行的columnX,但只有那些100行的一部分可以更新,剩下的rowX是默认值。 insert_initial_row 已经被调用,并且初始化所有100行的默认值,但 update_columnX 不会。事件我将一致性级别更改为Quorum,它根本没有帮助。 未正确初始化...从未打印出来,我在 update_columnX 打印 $ c>并且该行被打印100次,所以它被调用100次,但不是所有的更新。



有什么想法吗?



感谢

解决方案

如果 session.execute 写入不成功(他们不满足要求的一致性级别),则驱动程序将引发以下异常之一:


  1. 不可用 - 没有足够的即时副本以满足请求的一致性级别,因此协调器节点立即失败请求,而不将其转发到任何副本。

  2. 超时 - 副本在cassandra超时之前没有响应协调器。

  3. 写入超时 - 副本在写入超时之前没有响应协调器。在cassandra.yaml中配置。有一个类似的读取超时,读取和写入超时在yaml中单独配置。

  4. 操作超时 - 操作所花费的时间超过了指定的客户端超时。

您可以尝试跟踪您的查询,并找出每个写入发生了什么。这将显示操作中涉及的协调器和副本节点,以及请求在每个副本中花费的时间。


Solved I was testing update on 3 nodes, and the time on one of those nodes was 1 second behind, so when update a row, the write time is always behind the timestamp, cassandra would not update the rows. I sync all nodes time, and the issue fixed.

Edit: I double checked the result, all insertions are succeed, partial updates failed. There's no error/exception messages

I have a cassandra cluster(Cassandra 2.0.13) which contains 5 nodes. Using python(2.6.6) cassandra driver(2.6.0c2) for inserting data into database. my server systems are Centos6.X

The following code is how i connect to cassandra and get session. I provided at most 2 nodes ip addresses, and select the keyspace.

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)
    return session 

For each row, I have 17 columns and if the key does not exist in database, I will use session insert key with the rest columns default values, and then update specific column's value.

def insert_initial_row(session, key):
    session.execute(INITIAL_INSERTION_STATEMENT, tuple(INITIAL_COLUMNS_VALUES))


def update_columnX(session, key, column):
    session.execute("INSERT INTO " + TABLE + "(" + KEY + "," + COLUMN_X + ") VALUES(%s, %s)", (key, column))

def has_found(session, key):
    """checks key is in database or not"""
    query = "SELECT " + "*" + " FROM " + KEY_SPACE + "." + TABLE \
            + " WHERE " + KEY + " = " + "'" + key + "'"
    # returns a list
    row = session.execute(query)
    return True if row else False

the following is how I invoke them:

for a_key in keys_set:
    """keys_set contains 100 no duplicate keys"""
    if has_found(session, a_key):
        update_columnX(session, a_key, "column x value")
    else:
        """the key is not in db, initialize it with all default values, then update column x"""
        insert_initial_row(session,  a_key)
        if has_found(sessin, a_key):
            update_columnX(session,  a_key, "column x value")
        else:
            logger.error("not initialized correctly...")

I was trying to insert 100 rows and update each row's columnX, but only partial of those 100 rows can be updated, the rest rows columnX are the default values.insert_initial_row has been invoked and initialized default values for all 100 lines, but the update_columnX does not. Event I change the consistency level to Quorum, it doesnt help at all. "not initialized correctly..." never printed out, and I added a print line in update_columnX and the line is printed 100 time, so it is invoked 100 times, but not all of them updated.

Any idea? Please help.

Thanks

解决方案

If your session.execute writes were not successful (they did not meet the required consistency level), then the driver will raise one of the following exceptions:

  1. Unavailable - There were not enough live replicas to satisfy the requested consistency level, so the coordinator node immediately failed the request without forwarding it to any replicas.
  2. Timeout - Replicas did not respond to the coordinator before cassandra timeout.
  3. Write timeout - Replicas did not respond to the coordinator before the write timeout. Configured in cassandra.yaml. There is a similar timeout for reads, read and write timeouts are configured separately in the yaml.
  4. Operation timeout - Operation took longer than the specified client side timeout. Configure in your application code.

You can try tracing your queries and find out what exactly happened for each write. This will show you the coordinators and the replica nodes involved in the operation and how much time the request spent in each.

这篇关于Cassandra更新失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆