redes de suporte de sistemas distribuídos · distributed systems 20/21 –di/fct/nova / 2 goals in...

142
Distributed Systems 20/21 – DI/FCT/NOVA / 1 DISTRIBUTED SYSTEMS Lab 10 Nuno Preguiça, João Leitão, Sérgio Duarte

Upload: others

Post on 11-Aug-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 1

DISTRIBUTED SYSTEMS

Lab 10

Nuno Preguiça, João Leitão, Sérgio Duarte

Page 2: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 2

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 3: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 3

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 4: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 4

fcm192.168.1.132

Users:

REST

Spreadsheets:

PROJECT ARCHITECTURE OVERVIEW

fcsh192.168.1.45

Users:José

SOAP

Spreadsheets:...

fct192.168.1.2

Users:Sara

REST

Spreadsheets:...

Page 5: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 5

fcm192.168.1.132

Users:Magda

REST

Spreadsheets:

PROJECT ARCHITECTURE OVERVIEW

sara@fct

Rest Server

Soap Server

Proxy Server

Clientes finais emuladospelo Tester jose@fcsh magda@fcm

fcsh192.168.1.45

Users:José

SOAP

Spreadsheets:...

fct192.168.1.2

Users:Sara

REST

Spreadsheets:...

Page 6: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 6

fcm192.168.1.132

Users:

REST

Spreadsheets:

PROJECT ARCHITECTURE OVERVIEW

Rest Server

Soap Server

Proxy Server

REST Interactions1 2

sara@fct jose@fcsh magda@fcm

fcsh192.168.1.45

Users:José

SOAP

Spreadsheets:...

fct192.168.1.2

Users:Sara

REST

Spreadsheets:...

Page 7: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 7

fcm192.168.1.132

Users:

REST

Spreadsheets:

PROJECT ARCHITECTURE OVERVIEW

Rest Server

Soap Server

Proxy Server

SOAP Interactions 1 2

sara@fct jose@fcsh magda@fcm

fcsh192.168.1.45

Users:José

SOAP

Spreadsheets:...

fct192.168.1.2

Users:Sara

REST

Spreadsheets:...

Page 8: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 8

fcm192.168.1.132

Users:

REST

Spreadsheets:

PROJECT ARCHITECTURE OVERVIEW

Rest Server

Soap Server

Proxy Server

Rest Interactions1

2

sara@fct jose@fcsh magda@fcm

3

4

REST Interactions(with OAuth)

fcsh192.168.1.45

Users:José

SOAP

Spreadsheets:...

fct192.168.1.2

Users:Sara

REST

Spreadsheets:...

Page 9: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 9

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 10: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 10

GOALS OF REPLICATION

As the name implies, replication entails having replicas

(multiple copies) of a service. The idea is that these replicas

cooperate and coordinate among themselves to provide a

(single and unified) service to users.

• Usually, the user should not be aware or notice the

existence of replicas (Transparency)

• If one replica fails, the service can continue without the user

noticing the failure of that replica (Fault-Tolerance)

• Sometimes it is possible to take advantage of the additional

computational resources (across multiple replicas) to handle

more client requests or handle these requests faster

(Performance)

Page 11: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 11

GOALS OF REPLICATION

As the name imply replication entails having replicas (multiple

copies) of a service. The idea is that these replicas cooperate

and coordinate among themselves to provide a service to

users.

• Usually, the user should not be aware or notice the

existence of replicas (Transparency)

• If one replica fails, the service can continue without

the user noticing the failure of that replica (Fault-Tolerance)

• Sometimes it is possible to take advantage of the additional

computational resources (across multiple replicas) to handle

more client requests or handle these requests faster

(Performance)

Page 12: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 12

GOALS OF REPLICATION

As the name imply replication entails having replicas (multiple

copies) of a service. The idea is that these replicas cooperate

and coordinate among themselves to provide a service to

users.

• Usually, the user should not be aware or notice the

existence of replicas (Transparency)

• If one replica fails, the service can continue without

the user noticing the failure of that replica (Fault-Tolerance)

• Sometimes it is possible to take advantage of the additional

computational resources (across multiple replicas) to handle

more client requests or handle these requests faster

(Performance)

Page 13: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 13

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 14: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 14

KAFKA: PUBLISH/SUBSCRIBE

Apache Kafka is a popular system that provides indirect and

reliable communication through a publish/subscribe API

There are two main entities in this context:

• Publisher: Emits events/messages for a given topic

• Subscriber: That registers its interest in receiving

events/messages that are/were published in a given topic

(or set of topics)

Interactions in this model are independent of space and time,

meaning that two entities might exchange information without

knowing each other or without being online simultaneously.

Page 15: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 15

KAFKA: PUBLISH/SUBSCRIBE

In the context of the project one of the variants of replication

that you can build will use Kafka to propagate operations

received from a (end-user) client the server replicas within a

single domain.

This will be achieved by publishing the received operations as

events through Kafka in a topic (that can be the domain

name).

Notice that interactions between your servers and Kafka are

asynchronous (i.e., they are not instantaneous, there can be

delays which can impact the system).

Page 16: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 16

KAFKA: PROGRAMMING (PRELIMINARIES)

There is an Eclipse Java Project in the course web page that

has the examples that are going to be presented in this

section.

The project name is: sd2021-aula10-kafka

Page 17: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 17

KAFKA: PROGRAMMING (PRELIMINARIES)

You need to add maven dependencies to use Kafka in your

project (file pom.xml):

In the <dependencies> section:<dependency>

<groupId>org.apache.kafka</groupId>

<artifactId>kafka-clients</artifactId>

</dependency>

In the <dependencyManagement> section and within the

<dependencies> subsection:<dependency>

<groupId>org.apache.kafka</groupId>

<artifactId>kafka-clients</artifactId>

<version>2.8.0</version>

</dependency>

Page 18: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 18

KAFKA: PROGRAMMING (PRELIMINARIES)

To use and interact with Kafka you will need the Kafka server

(that internally also uses Zookeeper) executing.

In the project you have script files for linux/mac and windows

that executes a docker container with Kafka (and Zookeeper)

ready for use in either your local machine (address localhost)

and in the context of the SD project tester (in a container with

hostname kafka)

Linux/Mac: start-kafka.sh

Windows: start-kafka.bat

Page 19: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 19

KAFKA: PROGRAMMING (PRELIMINARIES)

To use it in your local machine (for instance to test your code in

Eclipse):

Linux/Mac: bash start-kafka.sh localhost

Windows: Start-kafka.bat localhost

Page 20: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 20

KAFKA: PROGRAMMING (PRELIMINARIES)

To use it with your containers in docker:

Linux/Mac: bash start-kafka.sh kafka

Windows: Start-kafka.bat kafka

Page 21: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 21

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 22: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 22

KAFKA: PROGRAMMING (PUBLISH EVENTS)

To show how you can publish a Message on a Given Topic we

have a simple application (main) that serves only as an example

of how you can publish messages.

In your project your own server are going to be responsible for

publishing messages and there is no need for new classes with a

main for this purpose.

The Javadoc of Kafka (for version 2.8.0) can be found in:

https://kafka.apache.org/28/javadoc/

Page 23: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 23

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaSendMsg

Page 24: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 24

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaSendMsg

This is simple code for asking the user to provide

the name of the topicwhere we are going to

publish the message andthe message itself

Page 25: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 25

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaSendMsg

We create a KafkaPublisher (we will see the code next) using the static method createPublisher. The argument is a string with

possible locations for the kafkaservers separated by commas

(hostname:port)

Page 26: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 26

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaSendMsg

We can now publish any number of messages we want. Each

message has a topic and a value (the message itself).

It returns the sequence number of this message in Kafka (notice that sequence numbers are per

Topic)

Page 27: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 27

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaSendMsg

If the sequence number of positive it will indicate success, otherwise -1 denotes an error (potentially because the Kafka server could not be contacted)

Page 28: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 28

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

Page 29: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 29

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

The static method used to instantiate this class that defines

a set of important properties

Page 30: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 30

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

This properties indicates the location of Kafka servers, it’s a

String with pairs hostname:portseparated by commas (as seen

previously)

Page 31: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 31

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

This indicates the format (particularly, how to serialize) the Keys of records. Keys are optional

when you publish a record in Kafka.

Here we assume these are String

Page 32: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 32

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

This indicates the format (particularly, how to serialize) the

values of records.

Here we assume these are also String

Page 33: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 33

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

This is the KafkaProducer which is the class in the Apache Kafka

library responsible for managing interactions of a Publisher with

the Kafka server(s).

Page 34: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 34

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (preamble)

You can terminate a KafkaProducer (if you are not

going to publish more messages) by using the close() method.

Page 35: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 35

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (publish methods)

Here we have two alternative methods to publish a record to

Kafka. They only differ in using or not a Key associated with that

record.

Page 36: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 36

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (publish methods)

To publish a Record to Kafka we use the method send of the KafkaProducer. The argument is a

ProducerRecord that is typified with the types of the Key and Value (both String in this case).

The arguments can either be the topic, key, value or only the topic and value.

Page 37: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 37

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (publish methods)

The send method returns what is called a Future (i.e., it returns before effectively communicating with the Kafka server)

The .get() invocation over this Future blocks until we get a reply from the Kafka server (making the method synchronous)

It return an object with Metadata for the record just created.

Page 38: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 38

KAFKA: PROGRAMMING (PUBLISH EVENTS)

Class KafkaPubisher (publish methods)

The .offset method of the RecordMetada object returned by the get operation provides the sequence number of this record in Kafka for this topic. Notice that sequence numbers are per Topic. Consumers consume records in an order that

respects this offset (if the topic has a single partition, you can check more about this in the documentation)

Page 39: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 39

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 40: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 40

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaReceiver

Page 41: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 41

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaReceiver

This is just asking the user for the topic in which this client

will be interested

Page 42: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 42

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaReceiver

Since Consumers can be interested in multiple topics,

we encapsulate that topic within a List

Page 43: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 43

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaReceiver

Here we create the KafkaSubscriber instance that will receive events from Kafka for the topic provided (we will see this class afterwards).

The arguments are a String with the possible location of Kafka servers (as before) and the List

with topics)

Page 44: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 44

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaReceiver

The start method initializes a thread that will consume events (i.e., Records) from Kafka for ever. It receives an instance of the ConsumerRecord interface (defined in-line here) with the code to handle each record (in this example it

simply prints the contents of the record)

Page 45: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 45

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

Page 46: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 46

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

The static method that creates an instance of this class has some

parameters that define the behaviour of the KafkaConsumer

Page 47: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 47

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

This it the list of Kafka servers as we seen previously for the Producer

Page 48: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 48

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

This parameter defines the behaviour of the

consumer in relation to where it starts to consume the stream of records for a

topic. Only one value should be used (the two lines here is to show the alternatives) check the

documentation to pick the correct one.

Page 49: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 49

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

The GROUP_ID_CONFIG parameter defines the group of the consumer. Consumers in the same groups receive different

records for each topic (and each record is received by

a single element of the group). In our case we

want every consumer to receive all the records, so all of them have to be in distinct groups (that is

why we use the System.nanoTime() that

should have different values in different

machines)

Page 50: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 50

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

We need to indicate how should Keys and Values within records be deserialized (i.e., their formats). As in the producer we are assuming both the Key and Value to be

Strings, hence we use the StringDeserializerfor both.

Page 51: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 51

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (preamble)

The KafkaConsumer is the class form the Kafka API that is responsible to interact with the Kafka servers as a consumer. We create it

here and will use it to obtain records from Kafka.

Notice that you can indicate the list of relevant topics on the Constructor, or use the

method subscribe to indicate this list at a later time (there are also methods to

unsubscribe from topics)

Page 52: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 52

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (methods to consume records)

Page 53: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 53

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (methods to consume records)

This is the method used in our example. It starts a new thread that will consume records (or events) from the topics subscribed by this consumer.

It receives a class as argument that implements the RecordProcessor interface (that has a single method:

void onReceive(ConsumerRecord<String, String> r);

This method is executed for each record received from Kafka with the poll method

Page 54: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 54

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (methods to consume records)

The pool method of the KafkaConsumer contacts the Kafka server to obtain a Set of (previously unprocessed) records for the relevant topics.

The argument is the time (in miliseconds) for which the operation waits for new records. Upon timeout if no records exist it returns an empty set of Records.

The method can return immediately if there exist records ready for processing by this consumer.

Page 55: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 55

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (methods to consume records)

For each record returned by Kakfa we execute the method onReceive ofthe class passed as argument to the start method.

Notice that records are processed in the same order in which they were received.

Page 56: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 56

KAFKA: PROGRAMMING (SUBSCRIBING AND RECEIVINGEVENTS)

Class KafkaSubscriber (methods to consume records)

This method is similar to the previous one, but it does not create a new thread to continually receive records from Kafka (hence, it will block the receiver forever).

It uses a different interface for registering the class that will process each record.

The interface SubscribeListener (defined below) must be implemented by the class being passed as argument to this method.

Page 57: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 57

KAFKA: PROGRAMMING (EXECUTING EXAMPLES)

You can test the examples provided in eclipse.

To do so do the following steps:

1. Start the kafka container as show before in localhost mode

(bash start-kafka.sh localhost or

./start-kafka.sh localhost)

2. Launch the Receiver (in eclipse) and provide a topic (e.g., SD)

3. Launch the Sender (in eclipse) and provide a topic and

message (the topic must be the same)

4. You can execute the Sender more times to see increasing

sequence numbers

Page 58: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 58

KAFKA: PROGRAMMING (EXECUTING EXAMPLES)

Execution of the Sender (in Eclipse)

Execution of the Receiver (in Eclipse)

Page 59: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 59

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received

operations in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 60: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 60

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Page 61: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 61

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ

Page 62: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 62

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ

Page 63: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 63

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

Page 64: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 64

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

CONSUMES REQ

Page 65: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 65

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

CONSUMES REQ

Other replicas do notneed to reply to client

Page 66: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 66

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

Rest Reply to REQ

Page 67: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 67

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

Rest Reply to REQ ?

Page 68: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 68

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

Rest Reply to REQ

Wait for Reply

Page 69: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 69

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

Rest Reply to REQ

Wait for Reply

Reply

Page 70: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 70

KAFKA: ASYNCHRONY AND REST SERVERS

(end-user)Client

REST SERVER 1

KAFKA

MainthreadRest

Requestthread

Kafka Consumer

thread

OtherREST

SERVER Replicas

Rest RequestREQ PUBLISH REQ

CONSUMES REQEXECUTE REQ

Rest Reply to REQ

Wait for Reply

ReplyReply

Page 71: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 71

KAFKA: ASYNCHRONY AND REST SERVERS

To simplify this interaction, we provide in the Kafka project that

supports this Lab a SyncPoint classe that can be used by the

thread that receives the client request to wait for the reply, and

by the Kafka consumer thread to deliver the result of the

operation to the waiting thread.

This class also offers mechanisms that allow to ensure that clients

do not interact with a replica that are outdated in relation to what

the client has previously observed from the system. This will be

discussed in more detail on the theoretical lectures.

Like the Discovery, this class will have to be accessible both to the

REST Resources class and the Kafka Consumer class (you can

instantiate it in the Main of your Server(s), and pass as an

argument in the constructor of classes that need access to it)

Page 72: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 72

KAFKA: ASYNCHRONY AND REST SERVERSClass SyncPoint (relevant methods)

Page 73: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 73

KAFKA: ASYNCHRONY AND REST SERVERSClass SyncPoint (relevant methods)

This method should be used by the thread that received the request from the client to wait for the operation to

be ordered and delivered by kafka(and executed by the Consumer) to

obtain the reply that should be sent to to the client.

The argument is the sequence number of the record sent to Kafka as

shown in the previous example.

Page 74: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 74

KAFKA: ASYNCHRONY AND REST SERVERSClass SyncPoint (relevant methods)

This method is used by the KafkaConsumer to store the result of an operation received from

Kafka (and executed by it).

The long n parameter is the sequence number of the record received from Kafka. The String res

should encode the HTTP response code to send back to the client (depending on the outcome of

the execution of the operation)

Page 75: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 75

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 76: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 76

ZOOKEEPER: DISTRIBUTED TUPLE SPACE

Apache Zookeeper is a popular system that provides an API to

enable storing (control) information in a reliable and strongly

consistent way on a hierarchical tuple space.

Some relevant aspects of zookeeper:

• Tuples have a unique name and optionally a value

associated. A tuple is named a znode in the zookeeper

nomenclature.

• Tuples are hierarchical like the directory structure of a file

system. You can have a tuple named ’zoo’; a child tuple

named ‘one’ whose complete name is ‘/zoo/one’

• You can watch for modifications in a znode (and all its

children) and be notified by zookeeper whenever a change

happens.

Page 77: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 77

ZOOKEEPER: DISTRIBUTED TUPLE SPACE

In the context of the project one of the variants of replication

that you can build is primary/secondary, where there is a

leader that is responsible for executing operations, having all

replicas act as a backup to the primary.

However, when the primary fails, one of the secondary replicas

must take its place an become the new primary. Evidently only

one of the replicas can become the new primary.

This is an instance of the “leader election” problem, and

zookeeper can be used to address this problem in practice (for

most fault scenarios).

Page 78: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 78

ZOOKEEPER: PROGRAMMING (PRELIMINARIES)

There is an Eclipse Java Project in the course web page that

has the examples that are going to be presented in this section

of the Lab.

The project name is: sd2021-aula10-zookeeper

Page 79: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 79

ZOOKEEPER: PROGRAMMING (PRELIMINARIES)

You need to add maven dependencies to use Zookeeper in

your project (file pom.xml):

In the <dependencies> section:<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

</dependency>

In the <dependencyManagement> section and within the

<dependencies> subsection:<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.4.11</version>

</dependency>

Page 80: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 80

ZOOKEEPER: PROGRAMMING (PRELIMINARIES)

To use and interact with Zookeeper its server to be executing.

In the project you have script files for Linux/Mac and Windows

that execute a docker container with Zookeeper ready for use

in either your local machine (address localhost) and in the

context of the SD project tester (under the name kafka).

Linux/Mac: start-kafka.sh

Windows: start-kafka.bat

Notice that the script is the same as for Kafka, since

Kafka also requires Zookeeper to run, we have both

system executing in the same container (the port to

contact Zookeeper however is different)

Page 81: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 81

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 82: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 82

KAFKA: PROGRAMMING (CREATE A TUPLE)

To show how you can create Tuples (i.e., a znode) on Zookeeper,

we will use a simple application (main) that serves only as an

example.

In your project your own servers are going to be responsible for

creating and reading znodes from Zookeeper and there is no need

for new classes with a main for this purpose.

The Javadoc of Zookeeper (for version 3.7.0) can be found in:

https://www.javadoc.io/doc/org.apache.zookeeper/zookeeper/3.4.

11/index.html

Page 83: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 83

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)Class Writer (Example with Main)

Page 84: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 84

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

The code creates a persistent znode(without a valueassociated) and a child znode that is

ephemeral to which it associates

a value.

Page 85: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 85

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This is simple codefor asking the user

to provide thename of the

znode that it willcreate and the

value to store in the internal znode

that will becreated.

Page 86: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 86

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

The ZookeeperProcessor class is an auxiliary class that we will use to interact with

Zookeeper (we will see its code in a bit).

The argument, similar to Kafka, is a string containing the location of zookeeper servers (pairs hostname:port separated by commas)

Page 87: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 87

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This method is creating a znode as indicated by the user input. This znode has no value

associated and it PERSISTENT

PERSISTENT means that this znode will continue to exist even if the process that

created it stops (or crashes)

Page 88: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 88

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This method is creating a znode as indicated by the user input. This znode has no value

associated and it PERSISTENT

PERSISTENT means that this znode will continue to exist even if the process that

created it stops (or crashes)

Since znodes must have unique names

this method might fail if the znode already

exists (the write method returns null in that case otherwise it returns the name of the created znode)

Page 89: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 89

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This method is creating a child znode of the previous one (hence its path starts with the

name of the previous znode)

Page 90: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 90

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This method is creating a child znode of the previous one (hence its path starts with the

name of the previous znode)

It also associates a value to this znode, meaning that the Zookeeper server will store the

contents of value associated to this znode

Page 91: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 91

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

The creation mode is EPHEMERAL_SEQUENTIAL.There are two implications here:

EPHEMERAL: Means that this zone will only exist while the connection of this process to zookeeper remains open. If this process closes its connection,

terminates, or crashes, Zookeeper will automatically remove this znode.

Page 92: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 92

Class Writer (Example with Main)

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

The creation mode is EPHEMERAL_SEQUENTIAL.There are two implications here:

SEQUENTIAL: Means that the name of the znode will be generated by Zookeeper by appending a

sequential number to the end of the znode name provided in this call. This makes sure that a znode will

always be possible to create.

The numbers used to name znodes that are meanwhile removed are not reused.

Page 93: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 93

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

Class ZookeeperProcessor (Preamble)

Page 94: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 94

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

This is the constructor of the ZookeeperProcessor class.

It instantiates a Zookeeper class which is the class in the Zookeeper Java API that is used to interact with the Zookeeper server.

The first argument is the String containing server information, the second is the session timeout (in milliseconds) and the third is a class that implements the interface Watcher.

Class ZookeeperProcessor (Preamble)

Page 95: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 95

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

The Watcher interface has a singe method:void process(WatchedEvent event)

This will be called to handle asynchronous events issued by Zookeeper (for instance when the contents/childs of a watched znode change)

Class ZookeeperProcessor (Preamble)

Page 96: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 96

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

Class ZookeeperProcessor (write methods)

Page 97: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 97

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

Class ZookeeperProcessor (write methods)

These methods both create a znode.

The only difference is that the first does not associate any data to the created znode, while the second does.

Page 98: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 98

ZOOKEEPER: PROGRAMMING (CREATE A TUPLE)

Class ZookeeperProcessor (write methods)

To create a znode we use the create method of the Zookeeper class.

The arguments are in order: i) the name of the znode (complete name that must start with / )ii) the value to associate with the znode (or null if no value is to be associated)

iii) the access control list of the znode (here we use the UNSAFE default that allows all to read and modify the znode)

iv) the mode associated with the created znode (PERSISTENT or EPHEMERAL_SEQUENTIAL)

Page 99: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 99

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 100: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 100

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

Page 101: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 101

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

This is a simple example that reads the childs of a znode (whose name is indicated by the user) and keeps monitoring that znode (and their

children) for changes.

Page 102: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 102

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

This is simple code for asking the user to

provide the name of the znode to read and

to monitor

Class GetChildren

Page 103: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 103

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

The ZookeeperProcessor class is an auxiliary class that we will use to interact with Zookeeper (we will see its code in a bit).

It is the same we used in the previous example

Page 104: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 104

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

The method getChildren reads a List containing the name of all child znodes of the znode whose name is provided as value to the path

argument (the return value is being ignored). The second argument is a class that implements the Watcher

interface (defined here inline)

Page 105: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 105

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

Whenever a change occurs to the contents of the watched znode (including changes in its children) an event will be

issued by zookeeper and the process method of the watcher executed.

In this example the watcher only reads and prints the list of child znodes.

Page 106: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 106

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class GetChildren

The application blocks waiting for user input, such that you can execute the example and see the changes on the child

znodes as writers start and stop.

Page 107: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 107

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

Class ZookeeperProcessor (read methods)

Page 108: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 108

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

These are the methods to read the childs of a znode and a default Watcher (that only prints the event received from

Zookeeper)

Class ZookeeperProcessor (read methods)

Page 109: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 109

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

The only difference between the two variants of the getChildren method is the existence of a Watcher, that

indicates to Zookeeper if we want to be notified of changes in the read znode or not.

Class ZookeeperProcessor (read methods)

Page 110: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 110

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

To read the childs of a znode we use the getChildren method of the Zookeeper class.

The arguments are: i) the name of the znode to read (must start with / )ii) the watcher to receive notifications or false if no monitoring of the znode is necessary

Class ZookeeperProcessor (read methods)

Page 111: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 111

ZOOKEEPER: PROGRAMMING (READING CHILDS OFTUPLE)

To read the childs of a znode we use the getChildren method of the Zookeeper class.

The arguments are: i) the name of the znode to read (must start with / )ii) the watcher to receive notifications or false if no monitoring of the znode is necessary

Class ZookeeperProcessor (read methods)

The Zookeeper class also has methods to access data associated with a

znode (if any).

You can check the documentation for that.

Page 112: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 112

ZOOKEEPER: PROGRAMMING (EXECUTING EXAMPLES)You can test the examples provided in eclipse.

To do so do the following steps:

1. Start the Kafka container as show before in localhost mode

2. Launch one Writer and provide a znode (e.g., /SD) just to

create that znode (the value is irrelevant)

3. Launch the GetChildren (in eclipse) and provide the same

znode, the process will be watching for changes

4. Launch a second Writer (in eclipse) and provide the same

znode as you did initially

5. You can more copies of the Writer to see more changes and

increasing sequence numbers.

Page 113: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 113

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect

a primary

• Overall Revision of the Project Goals

Page 114: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 114

ZOOKEEPER: LEADER ELECTION

The idea is that you will use Zookeeper (if you select that option) to

elect the primary of a set of replicas for a given domain (and to

pick a new one when the previous primary fails).

How can you do this:

1. Each domain should have a persistent znode with the name

being the domain itself.

2. Each replica, when it start, creates a child znode on its domain

znode for itself. The child znode should be ephemeral and have

names with a sequential identifier.

3. Every replica reads and watches the znode for their domain.

The primary replica is the one whose child znode is the first

within the domain znode.

4. You can store control information in znodes if you need.

Page 115: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 115

GOALS

In the end of this lab you should be able to:

• Project Architecture Overview

• Understand the goals of replication

• Kafka

• Understand the abstractions provided by Kafka

• Understand how to publish events in Java

• Understand how to subscribe event in Java

• Understand the asynchronous processing of Kafka received operations

in the project.

• Zookeeper

• Understand the abstractions provided by Zookeeper

• Understand how to create a tuple in Java

• Understand how to monitor a tuple in Java

• Understand how Zookeeper can be used to (dynamically) elect a

primary

• Overall Revision of the Project Goals

Page 116: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 116

PROJECT REVISION AND EXERCISE

1. Use what you have learned in this class to complete your project

by supporting replication in your original REST servers.

Remember that you have multiple implementation alternatives as

specified in the project web page:

• Alternative 1: Replication using Kafka

• Alternative 2a: Replication based on Primary/Secondary (with a

fixed Primary)

• Alternative 2b: Replication based on Primary/Secondary using

Zookeeper to select the Primary

Page 117: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 117

QUICK OVERVIEW OF REPLICATION

• The goal in the project is to replicate the Spreadsheet server

within any domain (We assume that there is a single server for

Users in each domain that never fails as in the first project).

• Depending on the replication solution that you adopt, you will

need to handle write operations (those that change the state of

the service) in a different way.

• In the following we discuss the workflow of these operations for

each of the alternatives.

Page 118: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 118

REPLICATION USING KAFKA

fct192.168.1.2

REST

Spreadsheets:...

sara@fct

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Kafka

Page 119: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 119

REPLICATION USING KAFKA

sara@fct

Kafka

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 120: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 120

REPLICATION USING KAFKA

sara@fct

Kafka

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 121: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 121

REPLICATION USING KAFKA

sara@fct

Kafka

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 122: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 122

REPLICATION USING KAFKA

In your REST resources you must divide each operation that you

might receive from a user client or a server from another domain

in two parts:

1. The first part is the REST endpoint itself that will receive the

operation, publish it to Kafka, wait for the reply, and send the

response back to the client (either a user or the server from

another domain)

2. The second part will be called by the KafkaConsumer when it

receives the operation from Kafka, this should store the

response in the SyncPoint for the response to be possible to

be sent to the client.

Typically, you will have a topic per domain, you can use Records

with Key (the key can be the operation) the value can be any Java

object encoded in Json (with Gson)

Page 123: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 123

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 124: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 124

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

A replica will know if it is the primary according to

parameters passed to the main of the server by our tester

(check the project webpage for details)

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 125: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 125

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

Send HTTP 307: TEMPORARY REDIRECT with the equivalent URL to the request issued by the client but to the primary (with the IP of the primary)

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 126: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 126

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

Send HTTP 307: TEMPORARY REDIRECT with the equivalent URL to the request issued by the client but to the primary (with the IP of the primary)

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

throw new WebApplicationException(Response.temporaryRedirect(URI.create("https://...")).build());

Page 127: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 127

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 128: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 128

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 129: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 129

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 130: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 130

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

sara@fct

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 131: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 131

REPLICATION USING PRIMARY/SECONDARY(FIXED PRIMARY)

In your REST resources you must divide each operation that you

might receive from a user client or a server from another domain

in two parts:

1. The first part is the REST endpoint itself that will receive the

operation and call the execution endpoints on the local server

and all other replicas, wait for the reply from everyone, and

send the response back to the client (either a user or the

server from another domain)

2. The second part will be in an internal REST endpoint and is

called by the primary (and only by the primary) to execute

operations that he received from user clients or servers from

other domains.

Page 132: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 132

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 133: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 133

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 134: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 134

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 135: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 135

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 136: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 136

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

Send HTTP 307: TEMPORARY REDIRECT with the equivalent URL to the request issued by the client but to the primary

(with the IP of the primary)

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

throw new WebApplicationException(Response.temporaryRedirect(URI.create("https://...")).build());

Page 137: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 137

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

Same as in the previous Primary/Secondary Example

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.3

REST

Spreadsheets:... fct

192.168.1.4

REST

Spreadsheets:...

Page 138: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 138

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.4

REST

Spreadsheets:...

Page 139: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 139

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

sara@fct

Zookeeper

fct192.168.1.2

REST

Spreadsheets:...

fct192.168.1.4

REST

Spreadsheets:...

Page 140: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 140

REPLICATION USING PRIMARY/SECONDARY + ZOOKEEPER

Like the Primary/Secondary, but the Primary might swap from

replica to replica, and you must adapt the execution of your

system accordingly.

Page 141: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 141

WHAT ABOUT READ OPERATIONS

• Read operations can follow the same path of write operations,

but this will make read operations very slow.

• Alternatively, read operations can be handled by a single (any)

replica, as far as the returned value is at least as up-to-date as

the last value observed by the client issuing the request.

• This can force the a spreadsheet server to wait before replying

to the client if he becomes aware the client as seen/wrote a

version after the one currently stored in that server.

• To do this check operations (both write and reads) might need

to pass additional control information in all replies sent to

clients in the form of an additional header (whose name should

start with sheetsserver).

• More information in the theoretical lectures.

Page 142: Redes de suporte de sistemas distribuídos · Distributed Systems 20/21 –DI/FCT/NOVA / 2 GOALS In the end of this lab you should be able to: • Project Architecture Overview •

Distributed Systems 20/21 – DI/FCT/NOVA / 142

PROJECT REVISION AND EXERCISE

1. Use what you have learned in this class to complete your project

by supporting replication in your original REST servers.

Remember that you have multiple implementation alternatives as

specified in the project web page:

• Alternative 1: Replication using Kafka

• Alternative 2a: Replication based on Primary/Secondary (with a

fixed Primary)

• Alternative 2b: Replication based on Primary/Secondary using

Zookeeper to select the Primary PICK ONLY ONE OF THE REPLICATION ALTERNATIVES