To run the app locally, you need:
- Java 17 or later
- Maven 3.9+
- Docker
- minikube and kubectl (for Kubernetes deployment)
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
rm minikube-linux-amd64curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
rm kubectlThe files in ./data/ correspond to a very small mock graph which looks like this:
(all the edges are undirected).
In order to use a larger graph, you have to download it first.
You can download it from here.
You need to put the file with coordinates under ./data/graph.co.gz and the one
with arcs under ./data/graph.gr.gz for the Docker containers to work properly.
To build Java apps just run
mvn clean install
This should create two .jar files: leader/target/leader-1.0-SNAPSHOT.jar and
worker/target/worker-1.0-SNAPSHOT.jar, which you can run with
java -jar worker/target/worker-1.0-SNAPSHOT.jar
and similarly for the leader. However running the apps this way is discouraged as it would be quite difficult to make the workers communicate with the leader. Instead,
This is the recommended approach if you plan to deploy to Google Cloud later.
./minikube_start.shOr manually:
minikube start --driver=docker --cpus=4 --memory=4096 --disk-size=20g./k8s_build.sh./k8s_deploy.shThis script will:
- Mount your
./datadirectory to minikube - Create the namespace and all Kubernetes resources
- Start the workers and then the leader job
# View all pods
kubectl get pods -n graph-dist
# Watch pod status
kubectl get pods -n graph-dist -w
# View leader logs
kubectl logs -n graph-dist -l app=graph-leader -f
# View worker logs
kubectl logs -n graph-dist worker-0 -f./k8s_cleanup.sh # Remove Kubernetes resources
./minikube_stop.sh # Stop minikube- Install and configure the Google Cloud SDK
- Authenticate:
gcloud auth login - Set your project:
gcloud config set project YOUR_PROJECT_ID
gcloud container clusters create graph-cluster \
--zone us-central1-a \
--num-nodes 3 \
--machine-type e2-mediumgcloud container clusters get-credentials graph-cluster --zone us-central1-a# Build images locally first
./k8s_build.sh
# Tag images for GCR
docker tag graph-worker:v1 gcr.io/YOUR_PROJECT_ID/graph-worker:v1
docker tag graph-leader:v1 gcr.io/YOUR_PROJECT_ID/graph-leader:v1
# Push to GCR
docker push gcr.io/YOUR_PROJECT_ID/graph-worker:v1
docker push gcr.io/YOUR_PROJECT_ID/graph-leader:v1-
Update image references in
k8s/*.yamlfiles:- Change
image: graph-worker:v1toimage: gcr.io/YOUR_PROJECT_ID/graph-worker:v1 - Change
image: graph-leader:v1toimage: gcr.io/YOUR_PROJECT_ID/graph-leader:v1 - Change
imagePullPolicy: NevertoimagePullPolicy: Always
- Change
-
Upload graph data to a GCS bucket or use a different storage solution
-
Apply the manifests:
kubectl apply -f k8s/namespace.yaml kubectl apply -f k8s/configmap.yaml kubectl apply -f k8s/leader-service.yaml kubectl apply -f k8s/worker-statefulset.yaml kubectl apply -f k8s/leader-job.yaml
# Delete the cluster (to avoid charges)
gcloud container clusters delete graph-cluster --zone us-central1-aThe leader exposes a REST API for finding the shortest path between two nodes in the graph.
GET /shortest-path
from: The ID of the starting node.to: The ID of the ending node.
You can use curl to send a request to the API.
If you are running the application with minikube, the API will be available on http://localhost:8080.
curl "http://localhost:8080/shortest-path?from=1&to=7"You can also open the URL in your web browser:
http://localhost:8080/shortest-path?from=1&to=7
The API will return a JSON object with the distance and the path.
{
"distance" : 35,
"path": [1,2,5,9,8,7]
}