Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pika commands that write to multiple keys in a single operation may cause inconsistencies between the master and slave nodes due to improper binlog handling #1638

Closed
cheniujh opened this issue Jun 21, 2023 · 2 comments · Fixed by #1658

Comments

@cheniujh
Copy link
Collaborator

cheniujh commented Jun 21, 2023

【主从同步修复】pika中一次操作了多个key的,有写性质的命令,由于binlog处理不当,可能引起主从不一致

现象:从节点消费binlog中的写命令时(WriteDB动作),没有以key为单位加锁,而是确保对于同一个key的操作,都是线性地WriteDB,具体做法是确保对同一个key的写操作都是在同一个线程中被线性执行的(通过对key进行hash来选定Worker),而一次性涉及到多个key的命令,都是对第一个key进行hash来选定worker的(实际上这条命令又对多个key有写操作),此时如果其他几个key也有并发的写操作,从节点把binlog apply 到DB时的顺序就可能乱掉,造成主从不一致

实例1:RPopLpush
image

实例2:Del命令
注:这是从节点的打印输出,binlog是从节点自己写binlog的顺序(实际上也是主节点生产binlog和WriteDB的顺序), DB:是从节点实际上WriteDB的命令顺序/执行顺序
image

@cheniujh cheniujh changed the title 【主从同步待修复】所有一次操作了多个key的,有写性质的命令,binlog都应该拆开写多条 【主从同步待修复】pika中一次操作了多个key的,有写性质的命令,由于binlog处理不当,可能引起主从不一致 Jun 21, 2023
@cheniujh
Copy link
Collaborator Author

一种可能的解决方案是对于这种命令,写多条binlog(原来是写一条),但是性能会打折扣,所以请大家探讨一下是否有更好的解决方案

@cheniujh
Copy link
Collaborator Author

调试建议:把主节点生产binlog,从节点消费binlog的实际顺序(写到自己的binlog+WriteDB)都打印出来,这样从节点WriteDB顺序如果错乱了就能看见

1.在pika_consensus.cc 约518行加一个打印(也就是stable_logger_->Logger()->Put(binlog) 后面一行:

  std::cout << "binlog: ";
  for(auto&it:cmd_ptr->argv()){
    std::cout << it << " ";
  }
  std::cout << std::endl;

2.在pika_repl_bgworker.cc,约265行添加一个打印( c_ptr->Do(slot)下一行:

  std::cout << "DB: ";
  for(auto& it :c_ptr->argv()){
    std::cout << it << " ";
  }
  std::cout << std::endl;

@cheniujh cheniujh changed the title 【主从同步待修复】pika中一次操作了多个key的,有写性质的命令,由于binlog处理不当,可能引起主从不一致 **[Master-Slave Synchronization Pending Repair]** In Pika, commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. Jun 7, 2024
@cheniujh cheniujh changed the title **[Master-Slave Synchronization Pending Repair]** In Pika, commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. In Pika, commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. Jun 7, 2024
@cheniujh cheniujh changed the title In Pika, commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. pika commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. Jun 7, 2024
@cheniujh cheniujh changed the title pika commands that operate on multiple keys in a single operation and have write characteristics may lead to master-slave inconsistency due to improper handling of binlogs. In Pika, commands that write to multiple keys in a single operation may cause inconsistencies between the master and slave nodes due to improper binlog handling Jun 7, 2024
@cheniujh cheniujh changed the title In Pika, commands that write to multiple keys in a single operation may cause inconsistencies between the master and slave nodes due to improper binlog handling pika commands that write to multiple keys in a single operation may cause inconsistencies between the master and slave nodes due to improper binlog handling Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant