Lamport Logical Clock Learning

Source: Internet
Author: User

1, Introduction

① How do you define the order in which all events in the system are occurring in a distributed environment? ② How are multiple processes competing for resources in a distributed environment mutually exclusive? ③ What is partial order, what is the role of partial order, what is insufficient? ④ what is the whole order, what is the role of the whole order, what is the shortage? ⑤ Why do I need a physical clock and how is the physical clock synchronized? The following is an introduction.

2, the definition of partial order, the occurrence of prior (happened before) relationship

Consider a single process A, at some point the event E1, after a period of time, the event E2, it can be said: E1 occurs in front of E2. Consider multiple processes, process A sends a message to process B, process A sends the message as event E1, and process B receives a message sent by process A as E2, which can be said: E1 occurs in front of E2. The above two examples show that E1 and E2 are a partial order relationship. The reason that these two examples represent the relationship is partial-order, because: when the need to judge A and e these two events who first who, in the partial order relationship is not able to judge.

The definition of partial order: (1) If a, B is two events in the same process, and a occurs before B, then a---> B (---> symbol indicates a preceding relationship,!---> symbol indicates a non-occurrence relationship)

(2) If A is a "one process sending message" event and B is a process receiving a message event, a---> B

(3) If a!--->b and b!---> A, it is called A, B is concurrent. As in event A and event e are concurrent.

A synonym for a preceding relationship, or partial order.

3, Full order

In 2, because the partial order cannot represent the sequence of events A and event E in the graph, and the ordering of all events in the distributed system is necessary, another method is needed to define the order of all events, that is, the Lamport logical clock is used to define the whole sequence of all events in the distributed system.

The Lamport logic clock is as follows:

First, define a clock Condition: if a---> B, then C (a) < C (b) (C (a) can be understood as the "moment of occurrence" of event a. Give two conditions C1 and C2,C1 definition: If a, B is two events in the same process and a---> B, Ci (a) <ci (b)

C2 Definition: If event A sends a message on behalf of the process I sent the message, and event B represents the receive process J receives the message, CI (a) < Cj (b). Obviously, when the conditions C1 and C2 were established, Clock condition was established.

On the basis of C1 and C2, two more processing operations are defined: IR1 and IR2 (refer to the description in the paper). The essence of IR1 and IR2 is that in the same process, a precedes B, you need to set the timestamp of B with a timestamp greater than a, and when a message is sent between different processes, set the timestamp of the event that received the message to be greater than the timestamp of the event that sent the message.

Definition of Full order:

The full-order definition of events that occur across processes can take different approaches . That is, the arbitrary total ordering mentioned in the paper.

This is understood as follows: For example, you can label each process with a sequence number. such as 1,2,3,4 ... When there are concurrent events between processes, the corresponding events of the process that specify the small sequence number occur earlier. For example: The sequence number of the set process P is 1, the sequence number of the process Q is 2, for event E and event A, it is considered that event E "earlier than" event a occurs.

Thus, the two operations, IR1 and IR2, together with the sequence of each process , can define a full order for all events, which solves the problem of how to sort all events in a distributed system. A complete sequence of the following:

(1:1,e)---(1:2,f)---(2:1,a)---(2,2,b) format explained: (Process Sequence Number: Event order, event name)

There is a small problem here: (1:2,f)---(2:1,a)??? ----may be a vector clock to solve it???

4, in a distributed environment, how can competing resources between multiple processes be mutually exclusive?

An existing critical resource, the access requirements for it are as follows:

The process that occupies a resource must release the resource when it authorizes its other resources

The authorization of resources must be carried out in the order in which the request for resources is made. That is, if process a requests resources prior to process B (with the guarantee of a full order relationship and, of course, the order of the requests), the resource should be allocated first to a when allocating resources.

The resource is eventually freed for any process that has access to the resource, i.e. requests for resources from any process will eventually be responded to. In other words, the process of owning a resource will end up using the resource and releasing it (no deadlock), thus guaranteeing that any resource request will eventually

be responded to.

How to solve the problem?

The Lamport master proposed an algorithm as follows:

? To request a resource, the process PI needs to send a request message in the form of TM:PI to all other processes in the system and put the message in its own request queue. (tm represents the timestamp of the message)

? When another process like PJ receives the TM:PI request, it puts it in its own request queue and sends a timestamp acknowledgement to pi.

When releasing a resource, Pi removes all tm:pi requests from its own message queue and sends a timestamp-free PI resource release message to all other processes.

? When other processes, such as PJ, receive a resource release message from PI, PJ removes all tm:pi request messages from its own message queue.

? If the following two conditions are met, the resource is assigned to the process pi:

A) After ordering the whole order, the TM:PI request is queued at the front of its request queue---that is, in the process pi, the tm:pi resource request message occurs first.

b) Pi received a message that all other processes sent it a timestamp greater than the TM

Based on the????? of the algorithm To prove??? is set up.

Some of the proofs are understood as follows:

Proof of the establishment of: the contrary law.

If not, it means that after a resource is allocated to a process, assuming that the process is PM, there are other processes that do not release the resource, assuming PN, then for this process, it means that it has not released the resource, according to??, which means that it has not yet removed the request from its own queue, and, The PM also stores messages for the PN request resource . And the process PM can get resources, stating that it satisfies the two conditions, but according to the condition a) that the process PM resource request is the earliest, but in fact, PN request is earlier, because it is earlier than PM authorization, but the request has not been removed from the PM queue , so? Condition a) can not be satisfied, so that the contradiction is found in the place.

Certificate of Incorporation:

According to the "a", the allocation of resources is ordered according to the order of the resource request, and the request which is in the front of the queue is first satisfied. Therefore, it was established.

Certificate of Incorporation:

Under??? Could you please prove it? Set up. Assuming that the PI already has a resource that sends a time-stamped resource to release the request to all other processes, the other process removes the message sent to them when the process PI requested the resource (see?), which makes the resource request message in the request queue of the other process (non-PI process) become the oldest (because, The oldest PI resource request message has been deleted, so that the resource request message sent by any process can eventually be responded to.

5, the problem with the logic clock (anomalous behaviour) and the reason for introducing the physical clock

The logical clocks discussed above are not yet complete to sort events. Consider the following scenario: Small A sends a request a on computer A in city a, and then calls the small b that lives in B, letting small b generate a request B on computer B. In the physical world, it is seen that request a leads to the occurrence of request B, that is, A is because, B is the fruit. However, under the logic clock system, A's timestamp may appear after the timestamp of B. Why? Because the "call" method can be understood to occur outside of the logical clock system. For example, request a occurs on a process labeled 2, and request B occurs on a process labeled 1, and the timestamp of request B is less than the timestamp of request a when the order is complete.

How to solve such a situation? One way to solve this problem is to take the "call" event and add it to the system. At this point, simply using the Lamport logic clock will not be able to fully order all the events. Therefore, a physical clock is introduced to solve the problem.

After the physical clock is introduced, the correct operation of the physical clock requires certain conditions, that is, the physical clock values used by each computer cannot be too biased. Therefore, the physical clock synchronization is also proposed. There are two main ways to synchronize physical clocks: 1) There is a standard time server, each client to connect the time server synchronization clock, so that each client's clock is synchronized. 2) Berkeley algorithm: the clock server actively asks each client's current clock value, each client will tell the clock server their respective clocks, the clock server to collect these values, plus their own clock value, to obtain an average. The difference between the clock value of each client and the mean is calculated, and the clock server sends the difference to each client to make adjustments.

For example, if client A is 5 seconds less than the average, then client a needs to speed up its own clock count (not to reverse its clock value by 5 seconds), which is actually equivalent to increasing the acceleration in physics.

Limitations of the 6,lamport logic clock

A) When using the logical clock method to sort all events, it is necessary to ensure that all processes are involved (refer to the algorithm description above), that is, the process failure is not tolerated. This is almost impossible in a real-world distributed system. b) The algorithm assumes that the message arrives sequentially and is bound to pass successfully. This can be ensured by the message sequence number and acknowledgement mechanism (TCP protocol).

7, bibliography

Time clocks and the ordering of events in a distributed system paper

Time Clocks and the ordering of Events in a distributed System
The essence of full order, distributed consistency

My understanding of the Lamport Logical clock

Lamport Logical Clock Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.