36
Chapter 19 Distributed Databases

Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

Embed Size (px)

Citation preview

Page 1: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

Chapter 19

Distributed Databases

Page 2: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

2

Distributed Database System A distributed DBS consists of loosely coupled sites

that share no physical component

DBSs that run on each site are independent of each other

Transactions may access data at one or more sites

Page 3: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

3

Homogeneous/Heterogeneous DDB In a homogeneous distributed database

All sites have identical software

Are aware of each other and agree to cooperate in processing user requests

Each site surrenders part of its autonomy in terms of right to change schemas or software

Appears to user as a single system

In a heterogeneous distributed database

Different sites may use different schemas and software

• Difference in schema is a problem for query processing

• Difference in software is a problem for transaction processing

Page 4: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

4

Distributed Data Storage Assume relational data model

Replication

System maintains multiple copies of data, stored in different sites, for faster retrieval and fault

tolerance

Fragmentation

Relation is partitioned into several fragments stored in distinct sites

Replication & fragmentation can be combined

Relation is partitioned into several fragments: system maintains several identical replicas of each

such fragment

Page 5: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

5

Data Replication A relation or fragment of a relation is replicated if it is

stored redundantly in two or more sites

Full replication of a relation is the case where the relation is stored at all sites

Fully redundant databases are those in which every site contains a copy of the entire database

Page 6: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

6

Data Replication Advantages of Replication

Availability: failure of site containing relation r does not result in unavailability of r is replicas exist.

Parallelism: queries on r may be processed by several nodes in parallel

Reduced data transfer: relation r is available locally at each site containing a replica of r

Disadvantages of Replication Increased cost of updates: each replica must be updated

Increased complexity of concurrency control on distinct replicas may lead to inconsistent data unless special concurrency control mechanisms are implemented

Page 7: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

7

Data Fragmentation Division of relation r into fragments r1, r2, …, rn which

contain sufficient information to reconstruct relation r

Horizontal fragmentation: each tuple of r is assigned to one or more fragments

Vertical fragmentation: the schema for relation r is split into several smaller schemas

All schemas must contain a common candidate key (or superkey) to ensure lossless join

property

A special attribute, the tuple-id attribute may be added to each schema to serve as a candidate

key

Page 8: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

8

Horizontal Fragmentation of account

branch_name account_number balance

HillsideHillsideHillside

A-305A-226A-155

50033662

account1 = branch_name=“Hillside” (account )

branch_name account_number balance

ValleyviewValleyviewValleyviewValleyview

A-177A-402A-408A-639

205100001123750

Page 9: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

9

Vertical Fragmentation of Employeebranch_name customer_name tuple_id

HillsideHillsideValleyviewValleyviewHillsideValleyviewValleyview

LowmanCampCampKahnKahnKahnGreen

deposit1 = branch_name, customer_name, tuple_id (employee)

1234567

account_number balance tuple_id

50033620510000621123750

1234567

A-305A-226A-177A-402A-155A-408A-639

deposit2 = account_number, balance, tuple_id (employee)

Page 10: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

10

Advantages of Fragmentation Horizontal

allows parallel processing on fragments of a relation

allows a relation to be split so that tuples are located where they are most frequently accessed

Vertical

allows tuples to be split so that each part of the tuple is stored where it is most frequently accessed

tuple-id attribute allows efficient joining of vertical fragments

allows parallel processing on a relation

Vertical & horizontal fragmentation can be mixed

Fragments may be fragmented to an arbitrary depth

Page 11: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

11

Distributed Transactions Transaction may access data at several sites

A site has a local transaction manager responsible for

Maintaining a log for recovery purposes

Participating in coordinating the concurrent execution of the transactions executing at that site.

A site has a transaction coordinator responsible for

Starting the execution of transactions originated at the site

Distributing subtransactions at appropriate sites for execution

Coordinating the termination of each transaction that originates at the site, which may result in the transaction being committed at all sites or aborted at all sites

Page 12: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

12

Transaction System Architecture

Page 13: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

13

Commit Protocols Commit protocols are used to ensure atomicity across

sites

a transaction which executes at multiple sites must either be committed at all the sites, or aborted at all the sites

not acceptable to have a transaction committed at one site and aborted at another

The two-phase commit (2PC) protocol is widely used

The three-phase commit (3PC) protocol is more complicated and more expensive, but avoids some drawbacks of two-phase commit protocol. This protocol is not used in practice

Page 14: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

14

Two Phase Commit Protocol (2PC) Assumption: fail-stop model – failed sites simply stop

working, and do not cause any other harm, such as sending incorrect messages to other sites.

Execution of the protocol is initiated by the coordinator after the last step of the transaction has been

reached

The protocol involves all the local sites at which the transaction executed

Let T be a transaction initiated at site Si, and let the transaction coordinator at Si be Ci

Page 15: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

15

Phase 1: Obtaining a Decision Coordinator asks all participants to prepare to commit

transaction Ti.

Ci adds the records <prepare T> to the log and forces log to stable storage

sends prepare T messages to all sites at which T executed

Upon receiving message, transaction manager at site determines if it can commit the transaction

if not, add a record <no T> to the log and send abort T message to Ci

if the transaction can be committed, then

• add the record <ready T> to the log

• force all records for T to stable storage

• send ready T message to Ci

Page 16: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

16

Phase 2: Recording the Decision

T can be committed of Ci received a ready T message from all the participating sites: otherwise, T must be aborted

Coordinator adds a decision record, <commit T> or <abort T>, to the log and forces record onto stable storage. Once the record stable storage it is irrevocable (even if failures occur)

Coordinator sends a message to each participant informing it of the decision (commit or abort)

Participants take appropriate action locally

Page 17: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

17

Handling of Failures - Site FailureWhen site Si recovers, it examines its log to determine the

fate of transactions active at the time of the failure

Log contains <commit T> record: transaction had completed, nothing to be done

Log contains <abort T> record: transaction had completed, nothing to be done

Log contains <ready T> record: site must consult Ci to determine the fate of T

• If T committed, redo (T); write <commit T> record

• If T aborted, undo (T)

The log contains no log records concerning T:

• Implies that Sk failed before responding to the prepare T message from Ci

• since the failure of Sk precludes the sending of such a response, coordinator Ci must abort T

• Sk must execute undo (T)

Page 18: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

18

Handling of Failures- Coordinator Failure If coordinator fails while the commit protocol for T is executing,

then participating sites must decide on T’s fate

1. If an active site contains a <commit T> record in its log, then T must be committed

2. If an active site contains an <abort T> record in its log, then T must be aborted

3. If some active participating site does not contain a <ready T> record in its log, then the failed coordinator Ci cannot have decided to commit T

• Can abort T; however, such a site must reject any subsequent <prepare T> message from Ci

4. If none of the above cases holds, then all active sites must have a <ready T> record in their logs, but no additional control records (such as <abort T> of <commit T>)

• In this case active sites must wait for Ci to recover, to find decision

Blocking: active sites may have to wait for failed coordinator to recover

Page 19: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

19

Handling of Failures - Network Partition If the coordinator and all its participants remain in one

partition, the failure has no effect on the commit protocol

If the coordinator and its participants belong to several partitions:

Sites that are not in the partition containing the coordinator think the coordinator has failed, and execute the

protocol to deal with failure of the coordinator.

• No harm results, but sites may still have to wait for decision from coordinator

The coordinator and the sites are in the same partition as the coordinator think that the sites in the other partition have failed, and follow the usual commit protocol.

Again, no harm results

Page 20: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

20

Recovery and Concurrency Control In-doubt transactions have a <ready T>, but neither a

<commit T>, nor an <abort T> log record

The recovering site must determine the commit-abort status of such transactions by contacting other sites; this can slow and potentially block recovery

Recovery algorithms can note lock information in the log

Instead of <ready T>, write out <ready T, L> L = list of locks held by T when the log is written (read locks can be omitted)

For every in-doubt transaction T, all the locks noted in the <ready T, L> log record are reacquired.

After lock reacquisition, transaction processing can resume; the commit/rollback of in-doubt transactions is

performed concurrently with the execution of new transactions

Page 21: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

21

Concurrency Control Modify concurrency control schemes for use in

distributed environment.

We assume that each site participates in the execution of a commit protocol to ensure global

transaction automicity.

We assume all replicas of any item are updated

Page 22: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

22

Single-Lock-Manager Approach System maintains a single lock manager that resides

in a single chosen site, say Si

When a transaction needs to lock a data item, it sends a lock request to Si and lock manager determines whether the lock can be granted immediately

If yes, lock manager sends a message to the site which initiated the request

If no, request is delayed until it can be granted, at which time a message is sent to the initiating

site

Page 23: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

23

Single-Lock-Manager Approach The transaction can read the data item from any one of

the sites at which a replica of the data item resides

Writes must be performed on all replicas of a data item

Advantages of scheme

Simple implementation

Simple deadlock handling

Disadvantages of scheme are

Bottleneck: lock manager site becomes a bottleneck

Vulnerability: system is vulnerable to lock manager site failure

Page 24: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

24

Deadlock Handling

Consider the following two transactions and history, with item X and transaction T1 at site 1, and item Y and transaction T2 at site 2:

T1: write (X)write (Y)

T2: write (Y)write (X)

X-lock on Xwrite (X) X-lock on Y

write (Y)wait for X-lock on X

Wait for X-lock on Y

Result: deadlock which cannot be detected locally at either site

Page 25: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

25

Centralized Approach A global wait-for graph is constructed and maintained

in a single site; the deadlock-detection coordinator

Real graph: Real, but unknown, state of the system.

Constructed graph: Approximation generated by the controller during the execution of its algorithm .

the global wait-for graph can be constructed when: a new edge is inserted in or removed from one of the local

wait-for graphs

a number of changes have occurred in a local wait-for graph

the coordinator needs to invoke cycle-detection

If the coordinator finds a cycle, it selects a victim & notifies all sites. The sites roll back the victim transaction

Page 26: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

26

Local and Global Wait-For Graphs

Local

Global

Page 27: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

27

Wait-For Graph for False Cycles

Initial state:

Page 28: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

28

False Cycles (Cont.) Suppose that starting from the state shown in figure

1. T2 releases resources at S1

• resulting in a message remove T1 T2 message from the Transaction Manager at site S1 to the coordinator)

2. And then T2 requests a resource held by T3 at site S2

• resulting in a message insert T2 T3 from S2 to the coordinator

Suppose further that the insert message reaches before the delete message

this can happen due to network delays

The coordinator would then find a false cycle

T1 T2 T3 T1

The false cycle above never existed in reality.

False cycles cannot occur if two-phase locking is used

Page 29: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

29

Distributed Query Processing For centralized systems, the primary criterion for

measuring the cost of a particular strategy is the number of disk accesses

In a distributed system, other issues must be taken into account

The cost of a data transmission over the network.

The potential gain in performance from having several sites process parts of the query in parallel.

Page 30: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

30

Query Transformation Translating algebraic queries on fragments

It must be possible to construct relation r from its fragments Replace relation r by the expression to construct relation r

from its fragments

Consider the horizontal fragmentation of the account relation into

account1 = branch_name = “Hillside” (account )

account2 = branch_name = “Valleyview” (account )

The query branch_name = “Hillside” (account ) becomes

branch_name = “Hillside” (account1 account2)

which is optimized into

branch_name = “Hillside” (account1) branch_name = “Hillside” (account2)

Page 31: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

31

Example Query (Cont.) Since account1 has only tuples pertaining to the Hillside

branch, we can eliminate the selection operation.

Apply the definition of account2 to obtain

branch_name = “Hillside” ( branch_name = “Valleyview” (account )

This expression is the empty set regardless of the contents of the account relation

Final strategy is for the Hillside site to return account1 as the result of the query

Page 32: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

32

Simple Join Processing Consider the following relational algebra expression

in which the three relations are neither replicated nor fragmented

account depositor branch

account is stored at site S1

depositor at S2

branch at S3

For a query issued at site SI, the system needs to produce the result at site SI

Page 33: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

33

Possible Query Processing Strategies

Ship copies of all three relations to site SI & choose a strategy for processing the entire locally at site SI

Ship a copy of the account relation to site S2 & compute temp1 = account depositor at S2

Ship temp1 from S2 to S3, and compute temp2 = temp1 branch at S3. Ship the result temp2 to SI

Devise similar strategies, exchanging the roles S1, S2, S3

Must consider following factors: amount of data being shipped

cost of transmitting a data block between sites

relative processing speed at each site

Page 34: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

34

Semijoin Strategy Let r1 be a relation with schema R1 stores at site S1

Let r2 be a relation with schema R2 stores at site S2

Evaluate the expression r1 r2 & obtain the result at S1

1. Compute temp1 R1 R2 (r1) at S1

2. Ship temp1 from S1 to S2

3. Compute temp2 r2 temp1 at S2

4. Ship temp2 from S2 to S1

5. Compute r1 temp2 at S1. This is the same as r1 r2

Page 35: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

35

Formal Definition The semijoin of r1 with r2, is denoted by:

r1 r2

it is defined by:

R1 (r1 r2)

Thus, r1 r2 selects those tuples of r1 that contributed to r1 r2

In step 3 above, temp2 = r2 r1

For joins of several relations, the above strategy can be extended to a series of semijoin steps

Page 36: Chapter 19 Distributed Databases. 2 Distributed Database System n A distributed DBS consists of loosely coupled sites that share no physical component

36

Join Strategies that Exploit Parallelism Consider r1 r2 r3 r4 where relation ri is stored at site

Si. The result must be presented at site S1.

r1 is shipped to S2 and r1 r2 is computed at S2:

simultaneously r3 is shipped to S4 and r3 r4 is computed at

S4

S2 ships tuples of (r1 r2) to S1 as they produced;

S4 ships tuples of (r3 r4) to S1

Once tuples of (r1 r2) and (r3 r4) arrive at S1 (r1 r2) (r3

r4) is computed in parallel with the computation of (r1 r2) at

S2 and the computation of (r3 r4) at S4