Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing Paxos with python in distributed system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of the method of using python to achieve Paxos in the distributed system, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the method of realizing Paxos with python in the distributed system. Let's take a look at it.

Consistent algorithm background

Problems solved by 1.Paxos consistency algorithm: data in a distributed system can not exist on a single node (host), otherwise a single point of failure may occur; multiple nodes (hosts) need to ensure that they have the same data.

two。 What is consistency: consistency is the consistency of data, which can be understood to mean that the values of data in multiple nodes are consistent in a distributed system.

3. Consistency model classification: generally divided into strong consistency and weak consistency, strong consistency ensures that the system changes the state of the cluster immediately after the change is submitted. Common models include: Paxos,Raft (muti-paxos), ZAB (muti-paxos); weak consistency is also called final consistency. The system does not guarantee to change the state of the cluster immediately after the change is submitted, but the final state is consistent over time. Common models include: DNS system, Gossip protocol

4. Consistency algorithm use cases: Google's Chubby distributed lock service, using Paxos algorithm; etcd distributed key-value database, using Raft algorithm; ZooKeeper distributed application coordination service and open source implementation of Chubby, using ZAB algorithm

It is not practical for simple-paxos to achieve consistency for a single static value, and the cluster system we need to implement (bank account service) wants to agree on a specific state (account balance) that changes over time. So you need to use Paxos to agree on each operation and treat each change as a state machine transition.

A Multi-Paxos is actually a sequence of simple Paxos instances (slots), each numbered sequentially. Each state transition is assigned a "slot number", and each member of the cluster performs the transition in a strict numerical order. In order to change the state of the cluster (for example, to handle a transfer operation), we try to achieve consistency on the operation in the next slot. Specifically, this means adding a slot number to each message and tracking the status of all protocols on a per-slot basis.

Running Paxos for each slot, at least two round trips would be too slow. Multi-Paxos is optimized by using the same set of ballot numbers for all slots and Prepare/Promise for all slots at the same time.

Client Proposer Acceptor Learner

| |-First Request

X- > | | Request

| | X- > | Prepare (N) |

| | |-> |-> | Accept! (NMagi _ I _ V)

|-> | Accepted (NMagi Ipene V) |

| | self.ballot_num: self.ballot_num = ballot_num # we've heard from a scout, so it might be the next leader self.node.send ([self.node.address], Accepting (leader=sender)) self.node.send ([sender], Promise (ballot_num=self.ballot_num, accepted_proposals=self.accepted_proposals)) def do_Accept (self, sender, ballot_num | Slot, proposal): if ballot_num > = self.ballot_num: self.ballot_num = ballot_num acc = self.accepted_proposals if slot not in acc or acc [slot] [0]

< ballot_num: acc[slot] = (ballot_num, proposal) self.node.send([sender], Accepted( slot=slot, ballot_num=self.ballot_num))Replica Replica类是Role类最复杂的子类,对应协议中的Learner和Proposal角色,它的主要职责是:提出新的proposal;在决定proposal时调用本地状态机;跟踪当前Leader;以及将新启动的节点添加到集群中。 Replica创建新的proposal以响应来自客户端的"invoke"消息,选择它认为是未使用的插槽,并向当前leader发送"Propose"消息。如果选定插槽的共识是针对不同proposal,则replica必须使用新插槽re-propose。 下图显示Replica的角色控制流程: Requester Local Rep Current Leader X---------->

| | Invoke |

| | X- > | Propose |

| Accepting |

Active leader will send Active messages in the form of a heartbeat. If no such message arrives before the LEADER_TIMEOUT expires, the Replica assumes that the Leader is dead and moves on to the next Leader. In this case, it is important that all replicas choose the same new leader, and we can sort the members and select the next leader in the list.

When a node joins the network, Bootstrap sends a Join message (below). Replica responds with a Welcome message containing its latest status, allowing the new node to be enabled quickly.

BootStrap Replica X-> | Join | Join | | Join |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report