Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Java restaurant deliveries #977

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,8 @@ jobs:
run: |-
cd samples/grpc/local-drone-control-java
mvn compile -nsu -Dakka-projection.version=`cat ~/.version`

- name: Compile Java Projection gRPC Restaurant Drone Deliveries sample
run: |-
cd samples/grpc/restaurant-drone-deliveries-service-java
mvn compile -nsu -Dakka-projection.version=`cat ~/.version`
10 changes: 10 additions & 0 deletions samples/grpc/restaurant-drone-deliveries-service-java/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Akka sample by Lightbend

Licensed under Public Domain (CC0)

To the extent possible under law, the person who associated CC0 with
this Template has waived all copyright and related or neighboring
rights to this Template.

You should have received a copy of the CC0 legalcode along with this
work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
81 changes: 81 additions & 0 deletions samples/grpc/restaurant-drone-deliveries-service-java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Restaurant Drone Deliveries Service

The sample show-cases a service for drones doing restaurant deliveries.

It is intended to be used together with the local-drone-control sample.

* Keeps track of a coarse grained location of each drone to the cloud
* FIXME Accepts restaurant delivery requests which are then fed to the right local drone control

## Running the sample code

1. Start a local PostgresSQL server on default port 5432. The included `docker-compose.yml` starts everything required for running locally.

```shell
docker compose up --wait

# creates the tables needed for Akka Persistence
# as well as the offset store table for Akka Projection
docker exec -i postgres_db psql -U postgres -t < ddl-scripts/create_tables.sql
```

2. Start a first node:

```shell
mvn compile exec:exec -DAPP_CONFIG=local1.conf
```

3. (Optional) Start another node with different ports:

```shell
mvn compile exec:exec -DAPP_CONFIG=local2.conf
```

4. (Optional) More can be started:

```shell
mvn compile exec:exec -DAPP_CONFIG=local3.conf
```

5. Check for service readiness

```shell
curl http://localhost:9101/ready
```


Query a location for drone coordinates

```shell
grpcurl -d '{"location":"sweden/stockholm/kungsholmen"}' -plaintext localhost:8101 central.drones.DroneOverviewService/GetCoarseDroneLocations
```

Query the state for a specific drone

```shell
grpcurl -d '{"drone_id":"drone1"}' -plaintext localhost:8101 central.drones.DroneOverviewService.GetDroneOverview
```

Set up a restaurant

```shell
grpcurl -d '{"restaurant_id":"restaurant1","coordinates":{"latitude": 59.330324, "longitude": 18.039568}, "local_control_location_id": "sweden/stockholm/kungsholmen" }' -plaintext localhost:8101 central.deliveries.RestaurantDeliveriesService.SetUpRestaurant
```

Set up another restaurant, closest to a different local drone control

```shell
grpcurl -d '{"restaurant_id":"restaurant2","coordinates":{"latitude": 59.342046, "longitude": 18.059095}, "local_control_location_id": "sweden/stockholm/norrmalm" }' -plaintext localhost:8101 central.deliveries.RestaurantDeliveriesService.SetUpRestaurant
```

Register a delivery for the first restaurant

```shell
grpcurl -d '{"restaurant_id":"restaurant1","delivery_id": "order1","coordinates":{"latitude": 59.330841, "longitude": 18.038885}}' -plaintext localhost:8101 central.deliveries.RestaurantDeliveriesService.RegisterDelivery
```

Register a delivery for the second restaurant

```shell
grpcurl -d '{"restaurant_id":"restaurant2","delivery_id": "order2","coordinates":{"latitude": 59.340128, "longitude": 18.056303}}' -plaintext localhost:8101 central.deliveries.RestaurantDeliveriesService.RegisterDelivery
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
CREATE TABLE IF NOT EXISTS event_journal(
slice INT NOT NULL,
entity_type VARCHAR(255) NOT NULL,
persistence_id VARCHAR(255) NOT NULL,
seq_nr BIGINT NOT NULL,
db_timestamp timestamp with time zone NOT NULL,

event_ser_id INTEGER NOT NULL,
event_ser_manifest VARCHAR(255) NOT NULL,
event_payload BYTEA NOT NULL,

deleted BOOLEAN DEFAULT FALSE NOT NULL,
writer VARCHAR(255) NOT NULL,
adapter_manifest VARCHAR(255),
tags TEXT ARRAY,

meta_ser_id INTEGER,
meta_ser_manifest VARCHAR(255),
meta_payload BYTEA,

PRIMARY KEY(persistence_id, seq_nr)
);

CREATE INDEX IF NOT EXISTS event_journal_slice_idx ON event_journal(slice, entity_type, db_timestamp, seq_nr);

CREATE TABLE IF NOT EXISTS snapshot(
slice INT NOT NULL,
entity_type VARCHAR(255) NOT NULL,
persistence_id VARCHAR(255) NOT NULL,
seq_nr BIGINT NOT NULL,
write_timestamp BIGINT NOT NULL,
ser_id INTEGER NOT NULL,
ser_manifest VARCHAR(255) NOT NULL,
snapshot BYTEA NOT NULL,
meta_ser_id INTEGER,
meta_ser_manifest VARCHAR(255),
meta_payload BYTEA,

PRIMARY KEY(persistence_id)
);

CREATE TABLE IF NOT EXISTS durable_state (
slice INT NOT NULL,
entity_type VARCHAR(255) NOT NULL,
persistence_id VARCHAR(255) NOT NULL,
revision BIGINT NOT NULL,
db_timestamp timestamp with time zone NOT NULL,

state_ser_id INTEGER NOT NULL,
state_ser_manifest VARCHAR(255),
state_payload BYTEA NOT NULL,
tags TEXT ARRAY,

-- additional column
location VARCHAR(255),

PRIMARY KEY(persistence_id, revision)
);

-- to query drones by location
CREATE INDEX IF NOT EXISTS durable_state_drone_location_idx ON durable_state(location);


-- Timestamp based offsets are stored in this table.
CREATE TABLE IF NOT EXISTS akka_projection_timestamp_offset_store (
projection_name VARCHAR(255) NOT NULL,
projection_key VARCHAR(255) NOT NULL,
slice INT NOT NULL,
persistence_id VARCHAR(255) NOT NULL,
seq_nr BIGINT NOT NULL,
-- timestamp_offset is the db_timestamp of the original event
timestamp_offset timestamp with time zone NOT NULL,
-- timestamp_consumed is when the offset was stored
-- the consumer lag is timestamp_consumed - timestamp_offset
timestamp_consumed timestamp with time zone NOT NULL,
PRIMARY KEY(slice, projection_name, timestamp_offset, persistence_id, seq_nr)
);

CREATE TABLE IF NOT EXISTS akka_projection_management (
projection_name VARCHAR(255) NOT NULL,
projection_key VARCHAR(255) NOT NULL,
paused BOOLEAN NOT NULL,
last_updated BIGINT NOT NULL,
PRIMARY KEY(projection_name, projection_key)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE event_journal;
DROP TABLE snapshot;
DROP TABLE durable_state;
DROP TABLE akka_projection_timestamp_offset_store;
DROP TABLE akka_projection_management;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '2.2'
services:
postgres-db:
image: postgres:latest
container_name: postgres_db
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
healthcheck:
test: ['CMD', 'pg_isready', "-q", "-d", "postgres", "-U", "postgres"]
interval: 5s
retries: 5
start_period: 5s
timeout: 5s
Loading