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.
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.
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.
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:
- enter CS
- write to the shared variable
- read the shared variable
- leave CS
The shared variable is a string.
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.jarThe 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:devor via the Quarkus CLI (requires download)
quarkus devThe jar can be created via
./mvnw packageor via the Quarkus CLI (requires download)
quarkus buildThis project uses Quarkus, the Supersonic Subatomic Java Framework.
If you want to learn more about Quarkus, please visit its website: https://quarkus.io/.
You can run your application in dev mode that enables live coding using:
./mvnw quarkus:devNOTE: Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/.
The application can be packaged using:
./mvnw packageIt 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-jarThe application, packaged as an über-jar, is now runnable using java -jar target/*-runner.jar.
You can create a native executable using:
./mvnw package -DnativeOr, 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=trueYou 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.
- 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
Create your first gRPC service
Easily start your REST Web Services