26
1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second example: Mandelbrot set

1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

Embed Size (px)

Citation preview

Page 1: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

1

Contents

• Distributed data structures• Different implementations• First example: bank transactions• JavaSpaces concepts• Replicated-worker pattern• Second example: Mandelbrot set

Page 2: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

2

Distributed data structures

• “A distributed data structure is a data structure that can be accessed and manipulated by multiple processes at the same time”

• In most distributed systems are hard to achieve• Message passing (CORBA) or RMI (remote

method invocation) in Java are good examples

Page 3: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

3

Ordinary systems

• Ordinary systems tend to barricade data structures behind one central manager

• Attempting to parallelize across more than one machine faces to a bottleneck

• Multiple processes can’t access the data in a concurrently way

Page 4: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

4

JavaSpace solution

• JavaSpaces solve this problem with a different approach

• Data structures are represented as collection of objects than can be independently accessed in a concurrent manner

• Processes no longer wait in line

Page 5: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

5

First example

• Bank transactions• Two bank tellers are making a deposit to the

same bank account at the same time.• A deposit consist in 3 different operations

– Looking up– Modifying– Setting the account’s value

Page 6: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

6

First example

• Problems arise in this scenario:– The bank account value is $200– The two Bank tellers want to deposit respectively $100

and $50– Bank teller #1 looks up the balance of $200– Bank teller #2 looks up the balance and reads $200 too,

since bank teller #1 hasn’t completed the deposit yet– Bank teller #1 performs the deposit and records a

balance of $300– Bank teller #2 performs the deposit and records a

balance of $250

Page 7: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

7

First example

• The bank account has been corrupted• The problem occurs because updating the bank

balance is not an atomic operation• To solve the problem updating a shared value

needs to be an atomic operation• With JavaSpaces we get this atomicity “for free”

Page 8: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

8

First Example

• Pseudocode for the bank transaction

SharedVar template = new SharedVar(“bank account”);SharedVar result = (SharedVar) space.take(template, null,

Long.MAX_VALUE);result.value = new Integer(result.value.intValue() + 100);space.write(result, null, Lease.FOREVER);

• The race condition can’t occur here, since only one process can “own” the bank account at the same time

Page 9: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

9

First Example

• Isn’t production ready yet• What append if one bank teller removes the Entry

that describes the bank account and fails to return it for a program crash or a network down?

• Luckily there are several ways to improve our code. We’ll see that topics later…

Page 10: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

10

JavaSpace concepts

• Topics that especially shine in JavaSpaces• Processes involved in the computation no longer

wait in line, they can work on independent pieces of data at the same time, as long as they don’t “step on each others toes”

• Every program written having JavaSpaces in mind should implement this concept tightly

Page 11: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

11

JavaSpaces concepts

• The program data has to be broken in several (and smaller) pieces, allowing two or more processes to use the program data simultaneously

Page 12: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

12

JavaSpace concepts: example

• Imagine now that two process are accessing the same array at two different locations at the same time

• There are two different way to do this in JavaSpaces

• The first is the „classic“ implementation• The second is „JavaSpaces-approved“ and

improves the parallelization

Page 13: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

13

JavaSpace concepts: 1 code

• Pseudocodepublic class array1 implements Entry {

public int data[]; public array1() {};}

• The array is defined as a “continuous” memory area

• Only one process can “own” the array-Entry at the same time

Page 14: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

14

JavaSpace concepts: 1 code

• The first process grabs the array, works with it and puts it back

• The second process can begin to work only after the end of the first process, even if they were interested in different pieces of data

Page 15: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

15

JavaSpaces concepts: 2 code

• Pseudocodepublic class array2 implements Entry {

public int data; public int offset; public array2() {}}

• The array is composed of several integer-Entries

Page 16: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

16

JavaSpaces concepts: 2 code

• The two process work on different pieces of data at the same time

• The tasks don’t wait for a resource that can be shared

Page 17: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

17

Replicated-worker pattern

• This pattern is designed to solve intensive computation problems in terms of smaller tasks that can be computed concurrently

• This pattern involves– one Master, along with– any number of workers

Page 18: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

18

Replicated-worker pattern

• The Master takes a problem, divides it up into smaller tasks and deposit them into the space

• The workers spend their lives waiting for tasks, removing them from the space, computing them and writing the results back into the space

• The Master collects then the tasks’ results and combine them into a meaningful overall solution

Page 19: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

19

Replicated-worker pattern

• Pseudocode for the Master:public class Master { for (int i = 0; i < totalTasks; i++) { Task task = new Task(…); space.write(task, …); }

  for (int i = 0; i < totalTasks; i++) { Result template = new Result(…); Result result = (Result)space.take(template, …); //… combine results }}

Page 20: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

20

Replicated-worker pattern

• The master iterates through all the tasks that need to be computed creating an Entry for each and writing it into the space

• The master iterates again, this time removing as many result entries from the space as there were tasks and combining them into some meaningful form

Page 21: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

21

Replicated-worker pattern

• Pseudocode for the workers:public class Worker { for (;;) { Task template = new Task(...); Task task = (Task)space.take(template, ...); Result result = compute(task); space.write(result, ...); }}

Page 22: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

22

Replicated-worker pattern

• A worker repeatedly removes a task from the space, computes it, and writes the result of the computation back into the space, where it will be picked up by the master process

Page 23: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

23

Second example

• Mandelbrot Set• The problem consists mainly in the computation

of an image, following a specific mathematical formula

• We are interested in the nature of the problem, where each pixel can be computed independently of all the rest

• JavaSpaces solves this problem very cleanly

Page 24: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

24

Second example: Master

• The master generates a task for each scanline that need to be displayed writing it to the space

• It then “sleeps” waiting for the workers to do their job

• It finally collects the result (in whatever order it finds them) and combine them drawing the result to the screen

Page 25: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

25

Second example: Master

• First iteration (building tasks)

• Second iteration (combine them)

Page 26: 1 Contents Distributed data structures Different implementations First example: bank transactions JavaSpaces concepts Replicated-worker pattern Second

26

Second example: Workers

• Each worker repeatedly removes a task from the space, computes the color values for the scanline represented by the task and writes the result back to the space for the master to pick up