Docsity
Trusted Seller

Distributed Computing - Computer Science Unige, Appunti di Programmazione In Ambienti Distribuiti

Toggle sidebar
bg1
Appunti di Luca Ferrari - Lorenzo La Corte - Davide Scarrà
Distributed Computing
A distributed system is a collection of autonomous computing nodes or elements that
appears to its users as a single coherent system.
Transaction, ACID and CAP
Transaction
A transaction for us is an independent modification in a system that stores data.
While it may modify several parts of the system, we think of it as a single modification.
ACID Properties
Atomicity, Consistency, Isolation, Durability.
Acid property is the classic set of properties to implement transactions because it makes it
easier to think about them.
1. Atomicity → Each transaction is treated as a single unit, that is it either succeeds
or fails completely.
If money is taken from my account, it gets to the destination.
2. Consistency (Correctness) →The system remains in a valid state.
All accounts have non-negative balance
3. Isolation → Even if transactions may be run concurrently, the system behaves as if
they’ve been running sequentially. Transactions are seen as “ordered”
4. Durability → Even in case of failure, the result of a committed transaction is not lost.
Transactions and database modifications are not kept in volatile memory but are
saved to permanent storage, such as disks.
A system that refuses some transactions is ACID compliant → no availability.
The CAP Theorem
In a system, you cannot have all of consistency, availability and partition tolerance
1. Consistency → every read receives the most recent write or an error.
Clients must not read stale data.
2. Availability → every request receives a non-error response.
The system is always available.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

Anteprima parziale del testo

Scarica Distributed Computing - Computer Science Unige e più Appunti in PDF di Programmazione In Ambienti Distribuiti solo su Docsity!

Appunti di Luca Ferrari - Lorenzo La Corte - Davide Scarrà

Distributed Computing

A distributed system is a collection of autonomous computing nodes or elements that appears to its users as a single coherent system.

Transaction, ACID and CAP

Transaction

A transaction for us is an independent modification in a system that stores data. While it may modify several parts of the system, we think of it as a single modification.

ACID Properties

A tomicity, C onsistency, I solation, D urability. Acid property is the classic set of properties to implement transactions because it makes it easier to think about them.

  1. Atomicity → Each transaction is treated as a single unit , that is it either succeeds or fails completely. If money is taken from my account, it gets to the destination.
  2. Consistency (Correctness) → The system remains in a valid state. All accounts have non-negative balance
  3. Isolation → Even if transactions may be run concurrently, the system behaves as if they’ve been running sequentially. Transactions are seen as “ordered”
  4. Durability → Even in case of failure, the result of a committed transaction is not lost. Transactions and database modifications are not kept in volatile memory but are saved to permanent storage, such as disks. A system that refuses some transactions is ACID compliant → no availability.

The CAP Theorem

In a system, you cannot have all of consistency, availability and partition tolerance

  1. Consistency → every read receives the most recent write or an error. Clients must not read stale data.
  2. Availability → every request receives a non-error response. The system is always available.
  1. Partition tolerance → the system keeps working even if an arbitrary number of messages between the nodes of our distributed system is dropped. Easy proof: Suppose the system is partitioned in two parts, G1 and G2 : no communication happens between them
  • A write happens in G
  • A read happens in G The result of the write is not accessible from G2 , so one of these happens:
  • The system returns an error ( we lose availability )
  • The system returns old data ( we lose consistency )
  • The system doesn’t reply ( we lose partition tolerance ) So, in any distributed system we have to trade-off , deciding what property to exclude. It’s possible to choose to lose partially every property instead of losing one completely.

Consensus

Scaling Vertically

We only use a single server, incrementing its memory when it’s needed. At some point the cost is not manageable, or a single server solution still doesn’t work. You can’t afford to lose money because there’s a black-out or a flood.

Scaling Horizontally

A desirable alternative would be to divide work between multiple servers:

  • We can the buy the servers with the best performance/price ratio
  • We can distribute them geographically for better redundancy (e.g., catastrophes) problems:
  1. Coordination : making them work together as if they were a single computer.
  2. Scalability : make it so that costs of coordination aren’t huge. Solving coordination problems is the main aspect linked to consensus.

→ read - fast : clients connect with a server and get a value for a specific key. This process permits fast reads but slow writes and we have to find a way of implementing shared logs.

How to Implement a Shared Log

We need to find a way to implement a shared memory so we will introduce some algorithms that have this aim.

Scenarios - each of these has assumptions about the system

  1. Fail-stop scenario:
  • One-to-one messages only, which can take arbitrary time and can be lost.
  • Computers can stop for an arbitrary time , but they are able to not lose data , so this means that when they recover they also recover their data. Here we get consistency and availability , but we drop partition tolerance. Kind of like having to communicate only via SMS when you have bad coverage.
  1. Byzantine Consensus , a worse failure model than fail-stop:
  • Broken nodes may send any message, maybe even acting maliciously.
  • This is the kind of scenario you need in cryptocurrencies

Paxos

Paxos is about reaching consensus and creating a shared log in an asynchronous distributed system. → Basic Paxos reaches consensus on one line of a shared log. → Multi-Paxos reaches consensus on multiple lines of a shared log.

Key Idea

Suppose we are in a fail-stop scenario : at any point in time one of our computers can fail which means that it will no longer send messages to other computers or participate in the protocol until it comes back alive, but all computers remember where they were when they left off. In addition, the network can fail , and when the network fails, it looks like the node at the far end of a network link is failed so the protocol doesn't really care if a node fails or the network that talks to that node has failed. We need to reach consensus in this scenario. Say you have an old mobile phone, and want to decide with your friends whether to go for pizza or sushi; you want to all agree and go to the same place. A possible solution is majority voting , and this works but the problem is that nodes can stop work (fail): any message may get lost or arrive even after days. Our network can be arbitrarily slow, we want to be able to tolerate the fact that sometimes packets get lost and come back after a long time. For example, our old mobile phone doesn’t work and so my messages arrive very late. But:

  • Nobody loses their state,
  • Everybody is honest and makes no mistakes. If we want to tolerate n failures , we’ll need 2n+1 servers. A decision is taken when a majority of n+1 nodes agree on it.
  • key reason: 2 majorities must intersect
  • Hence at least one participant will see a conflict and avoid it. Note:
  • unlike regular elections , there are no conflict interests here, participants just want to agree on something
  • to deal with malicious behavior, you’ll need byzantine failure models and we are not dealing with it.

Phases

  1. Prepare : a proposer prepare and make a promise, specifying: round number + its identifier example: 42.1 (round 42, server 1)
  2. Promise : Acceptors return a “Promise” if the message has a higher round than every previous proposal number received. From that point on the acceptor will ignore all future proposals having a number less than n ; if the Acceptor accepted a proposal at some point in the past, it must include the previous proposal number and the corresponding accepted value.
Cases with multiple proposer

Case 3 - Failure of proposer in prepare phase In this particular case somebody else will take the role of the proposer. In particular (43.2, “pizza”) is the new proposal. 43 is the new round - 2 is the number of the machine proposing - “pizza” is the proposed value. Case 4 - Failure of proposer in accept phase In this particular case, when server 1 (proposer) fails:

  • server 3 has already accepted the first proposal,
  • server 2 hasn’t received the proposal.

So server 2 proposes another value and so server 3 sees the conflict and makes it explicit, notifying the other machines (server 2 in this case). In particular server 3 answer contains both the round identifiers of the two proposals and the already accepted value (“sushi”). Once server 2 knows there’s a conflict it accepts the old proposal because server 3 already accepted it.

Recap

Paxos can solve the leader election problem. In general it works on all cases: the worst (theoretically) can happen is a livelock where everyone’s stuck doing a loop of actions in parallel. Multi-Paxos builds a full log of decisions and it’s way faster (one round-trip per transaction). On the other hand Multi-Paxos is way more complex developing, debugging and testing hard and so there are very few implementations of it.

RAFT

RAFT is an easier and more comprehendible algorithm than Paxos. It has three main roles which are the leader , the follower , and the candidate. In the first case everything goes fine:

  1. Three nodes are set as followers,

2. Log Replication

Leader requests to append entries to followers. Followers receive entries and answer back to the leader. When everyone’s table is up to date the leader commits the entry with a heartbeat, so everyone knows it’s persistent. Each node saves and shows to others the last committed entry.

Case of Network Partition

Let’s analyze the Worst case possible: Leader ends up in a partition with minority of node so he can’t finalize/commit new rows:

A new Leader is elected and can finalize new rows, while at the same time the other partition can’t commit entries because they are in minority: When the network is established again logs are being synchronized through this process: L1 becomes a follower because has the minor term and everyone’s table is updated with the only things committed (by the partition with majority of nodes):

Log Compaction

When an entire machine fails it should take back all actions from other nodes and recover its state. The problem is that often the actions done can be a lot so recovery time increases as log length increases. When we have very large logs it may be inefficient to restore them at once and the best option is to save intermediate states with checkpoints ; to do this we need to:

  1. make state savable,
  2. store the state. There are three possible strategies:
  3. save states internally in the log.
  4. save states externally with RAFT.
  5. save states externally with other applications.

It simplifies the implementation but needs an alternative solution to wait for conditions.

2. Ordering

All operations on the same client will be serialize in FIFO order:

  • Writes are linearizable (they are executed in the order they are performed),
  • Reads are serializable (you might read stale data): this condition is weaker than linearizable, it’s ACID’s isolation.
3. Change Events

Clients can request to be notified of changes: when a change happens the client gets notified; they get notification of a change before seeing the result.

Main Concepts

Zookeeper implementation starts from the idea of a distributed file system , to coordinate apps and then this idea was edited in order to make it work. ZooKeeper designers have seen that their engineers were using the NFS to coordinate their applications but this worked only partly. So, Zookeeper works like a filesystem for small pieces of data , with some modifications:

  1. there are also notification of changes,
  2. there aren’t partial read/write. The system works with APIs: ● create(path, data, acl, flags) where acl is the access control list and flags are options ● delete(path, expectedVersion) ● setData(path, data, expectedVersion) ● getData(path, watch) watch is a flag: if it’s set to true it notifies changes of the data to the client. ● exists(path, watch) ● getChildren(path, watch) ● void sync() complete all pending operations. ● setACL(path, acl, expectedVersion) ● getACL(path)

Data Model

Is built through the use of hierarchical namespace. Znodes are like both file and directories:

  • they have data and children.
  • they have a namespace (ex: worker1,worker2,...). Data is read and written in its entirety (there aren’t partial write or read).

flags in creating a node

Ephemeral : Znode is deleted when the creator fails. if a worker is created by a session, if the session fails or ends also the workers are deleted. also the locks are ephemeral. Sequence : append a monotonically-increasing counter. locks are enumerated in sequential order.

Example of Configuration

Observation of data : A worker starts and gets the configuration:

Note: every node only watches the one in front of the queue.

Shared Locks

These are more relaxed locks which we need for reading. We don’t want anybody to write while we are reading, but it is possible to read while someone is also reading.

  • Creating the lock id = create(“myApp/locks/s-”, sequence=True, ephemeral=True)
  • see who is the last in the queue: getChildren(“myApp/locks/”) If no exclusive “x-” lock before id → go ahead Otherwise: - if exists(last “x-” before id, watch=True) → wait for the event
  • Else, return to getChildren. Note: every node only watches the x- one in front of the queue.

Implementation

All servers have a full copy of the state in memory. Uses a consensus protocol that’s similar to RAFT → there’s a leader. In this case, we mean leader in architecture, while before (in the configuration) we were talking about ZNodes’s leader. Update is committed when a majority of servers saved the change :

  • As we’re used to, we need 2m+1 servers to tolerate m failures.

Reads and Writes

Read → contact one of the servers and he just answers:

  • Two messages for read,
  • They are done locally with data present on the machine. Write → the machine sends the message to the leader, the leader propagates the message and commits.
  • 10 messages for writing in 5 servers,
  • These operations are way more expensive than reads. This is the reason why this system works well with read-intensive workloads.

Operations per Second

If the number of servers grows:

  • the write operations grow,
  • but the read requests are still the same. The ideal number of machines depends on many aspects such as their cost, failure resistance and percentage of reads:

One write at a time in the whole system : when you are writing somewhere, you can’t read ; these are unreasonable for a huge system.

Clocks or Timestamps

Pieces of information are annotated with time:

  • We know when they got in the system ,
  • If they were canceled, we know when. To do this, we need a super-precise clock : below typical Internet latencies, which can be up to hundreds of milliseconds. Spanner will wait to handle uncertainties in the clock: it's the key to drive uncertainties down.

TrueTime

A system created to evaluate time with high reliability. The key idea is bounded uncertainty : TrueTime.now() returns an interval: [earliest, latest] Meaning: time is between earliest and latest. Each datacenter has a set of time master machines:

  • Regular ones, equipped with GPS, - Armageddon masters , with atomic clocks. Talking to a Time Master : Not all details are given; assuming an approach similar to the Network Time Protocol (NTP): Time offset: Round-trip delay: Measurement as described before is repeated and statistics are extracted. The output is an interval: 𝑡 + ε = [𝑒𝑎𝑟𝑙𝑖𝑒𝑠𝑡, 𝑙𝑎𝑡𝑒𝑠𝑡]

One of the time masters could be wrong: to weed out liars, an algorithm is needed. Problem: given n [𝑎𝑖, 𝑏𝑖]intervals, find the sub-interval that is consistent with most cases:

Intersection Algorithm (Marzullo)

  1. Create a list containing (𝑎, + 1) e (𝑏, − 1) pairs for each [𝑎, 𝑏]interval and sort them by the first element. Let’s call these values (𝑣 ; 𝑖

𝑖

𝑖

  1. Compute the cumulative sums 𝑠𝑖 of all 𝑑𝑗 values where𝑗 <= 𝑖 it’s the number of intervals overlapping in [𝑣𝑖, 𝑣𝑖 + 1]
  2. Find the maximum value 𝑠 ; the result will be 𝑀
[𝑣

𝑀

𝑀

+ 1]

Catering for Local Clock Error

After the clock is synchronized, the uncertainty grows according to worst-case assumptions on computers’ clock drift : the clock tends to lose precision after long periods but locks that ‘go crazy’ are very rare (6 times less than CPUs that do), TrueTime Precision :