Skip to content

About

Shared replicated variable using Ricart-Agrawala mutual exclusion

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Distributed mutual exclusion

This distributed algorithm provides mutual exclusion to a shared replicated variable. It uses Ricart-Agrawala for mutual exclusion, Java, the Quarkus framework, gRPC for communication between nodes and a full mesh topology. The system is controlled using a REST API.

Node

A node knows its:

  • IP address
  • Set of neighboring IP addresses (a neighbor is a node in the full mesh)
  • Its latency
  • Whether it is alive
  • Lamport clock
  • Lamport timestamp of the last request
  • Whether it is requesting the critical section
  • How many replies it has received for a request to enter the CS
  • Set of IP addresses to which it has not yet replied, and will reply after leaving the CS
  • Whether it is in the critical section
  • Value of the shared variable

Everything is in the node class, which is injected as a singleton, i.e. application-scoped.

Algorithm

The Ricart–Agrawala algorithm is triggered by calling /requestCS, i.e. when requesting entry into the critical section. This REST endpoint sends requests to enter. When a node receives a request, it either approves it immediately if it is neither requesting nor in the CS itself, or if it is requesting but has a higher Lamport timestamp, or the same timestamp but a higher IP address (string comparison). If none of this holds, the request is stored in a queue and is granted only after the node leaves the critical section.

When a node receives a reply, it counts how many it has, and if the number equals the number of neighbors (n-1), it enters the CS.

Shared variable

Entering/leaving the critical section and writing/reading the shared variable are controlled separately. Only a node in the critical section may read and write the shared variable. A correct flow is for example:

  1. enter CS
  2. write to the shared variable
  3. read the shared variable
  4. leave CS

The shared variable is a string.

How to run the application

The application uses Quarkus; the build can be done using the commands below. You will get the directory target/quarkus-app, which can be copied to a VM and run there via

java -Dip=192.168.56.102 -jar /media/sf_shared/quarkus-run.jar

The IP should be statically set on the VM; the path points to the .jar file in the quarkus-app directory. It is necessary to provide the VM with the entire directory, not just the jar!

Swagger documentation is available at http://localhost:8080/q/swagger-ui To access it, the application must be run in dev mode

./mvnw quarkus:dev

or via the Quarkus CLI (requires download)

quarkus dev

Build

The jar can be created via

./mvnw package

or via the Quarkus CLI (requires download)

quarkus build

Quarkus readme

This project uses Quarkus, the Supersonic Subatomic Java Framework.

If you want to learn more about Quarkus, please visit its website: https://quarkus.io/.

Running the application in dev mode

You can run your application in dev mode that enables live coding using:

./mvnw quarkus:dev

NOTE: Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/.

Packaging and running the application

The application can be packaged using:

./mvnw package

It produces the quarkus-run.jar file in the target/quarkus-app/ directory. Be aware that it’s not an über-jar as the dependencies are copied into the target/quarkus-app/lib/ directory.

The application is now runnable using java -jar target/quarkus-app/quarkus-run.jar.

If you want to build an über-jar, execute the following command:

./mvnw package -Dquarkus.package.jar.type=uber-jar

The application, packaged as an über-jar, is now runnable using java -jar target/*-runner.jar.

Creating a native executable

You can create a native executable using:

./mvnw package -Dnative

Or, if you don't have GraalVM installed, you can run the native executable build in a container using:

./mvnw package -Dnative -Dquarkus.native.container-build=true

You can then execute your native executable with: ./target/semestralka-1.0.0-SNAPSHOT-runner

If you want to learn more about building native executables, please consult https://quarkus.io/guides/maven-tooling.

Related Guides

  • REST (guide): A Jakarta REST implementation utilizing build time processing and Vert.x. This extension is not compatible with the quarkus-resteasy extension, or any of the extensions that depend on it.
  • SmallRye OpenAPI (guide): Document your REST APIs with OpenAPI - comes with Swagger UI

Provided Code

gRPC

Create your first gRPC service

Related guide section...

REST

Easily start your REST Web Services

Related guide section...

About

Shared replicated variable using Ricart-Agrawala mutual exclusion

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages