Skip to content

Commit

Permalink
feat(docker): autoheal
Browse files Browse the repository at this point in the history
  • Loading branch information
Trantorian1 committed Nov 28, 2024
1 parent 147ee4f commit d73ca62
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN cargo build --release
FROM debian:bookworm
# Install runtime dependencies
RUN apt-get -y update && \
apt-get install -y openssl ca-certificates tini &&\
apt-get install -y openssl ca-certificates tini curl &&\
apt-get autoremove -y; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*
Expand Down
42 changes: 24 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ============================================================================ #
# STARKNET NODE BENCHMAR RUNNER #
# STARKNET NODE RUNNER #
# ============================================================================ #

define HELP
Expand Down Expand Up @@ -66,21 +66,27 @@ Targets:
endef
export HELP

SECRETS := .secrets/rpc_api.secret
SECRETS := .secrets/rpc_api.secret
DB_PATH := /var/lib/madara

DOCKER_COMPOSE := docker compose -f compose.yaml
DOCKER_TAG := madara:latest
DOCKER_IMAGE := ghcr.io/madara-alliance/$(DOCKER_TAG)
DOCKER_GZ := image.tar.gz

# dim white italic
DIM := \033[2;3;37m
DIM := \033[2;3;37m

# bold cyan
INFO := \033[1;36m
INFO := \033[1;36m

# bold green
PASS := \033[1;32m
PASS := \033[1;32m

# bold red
WARN := \033[1;31m
WARN := \033[1;31m

RESET := \033[0m
RESET := \033[0m

.PHONY: all
all: help
Expand All @@ -92,27 +98,27 @@ help:
.PHONY: start
start: images $(SECRETS)
@echo -e "$(DIM)running$(RESET) $(PASS)madara$(RESET)"
@docker compose -f compose.yaml up -d
@$(DOCKER_COMPOSE) up -d

.PHONY: stop
stop:
@echo -e "$(DIM)stopping$(RESET) $(WARN)madara$(RESET)"
@docker compose -f compose.yaml stop
@$(DOCKER_COMPOSE) stop

.PHONY: logs
logs:
@echo -e "$(DIM)logs for$(RESET) $(INFO)madara$(RESET)";
@docker compose -f compose.yaml logs -f -n 100;
@$(DOCKER_COMPOSE) logs -f -n 100 madara;

.PHONY: images
images: image.tar.gz
images: $(DOCKER_GZ)

image.tar.gz:
$(DOCKER_GZ):
@echo -e "$(DIM)downloading$(RESET) $(PASS)madara$(RESET)"
@docker pull ghcr.io/madara-alliance/madara:latest
@docker tag ghcr.io/madara-alliance/madara:latest madara:latest
@docker rmi ghcr.io/madara-alliance/madara:latest
@docker image save -o image.tar.gz madara:latest
@docker pull $(DOCKER_IMAGE)
@docker tag $(DOCKER_IMAGE) $(DOCKER_TAG)
@docker rmi $(DOCKER_IMAGE)
@docker image save -o $(DOCKER_GZ) $(DOCKER_TAG)

.PHONY: clean
clean: stop
Expand All @@ -133,12 +139,12 @@ clean-db:
esac
@make --silent clean
@echo -e "$(DIM)removing madara database on host$(RESET)"
@sudo rm -rf /var/lib/madara;
@sudo rm -rf $(DB_PATH);

.PHONY: fclean
fclean: clean-db
@echo -e "$(DIM)removing local images tar.gz$(RESET)"
@rm -rf image.tar.gz
@rm -rf $(DOCKER_GZ)
@echo -e "$(WARN)artefacts cleaned$(RESET)"

.PHONY: restart
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Ensure you have all the necessary dependencies available on your host system.
| ---------- | ---------- | ----------------------------------------------------------------- |
| Rust | rustc 1.81 | `curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \| sh` |
| Clang | Latest | `sudo apt-get install clang` |
| Openssl | 0.10 | `sudo apt install openssl` |

Once all dependencies are satisfied, you can clone the Madara repository:

Expand Down Expand Up @@ -128,7 +129,7 @@ cargo run --release -- \
```

> [!NOTE]
> Head to the [Configuration](#configuration) section to learn more about
> Head to the [Configuration](#%EF%B8%8F-configuration) section to learn more about
> customizing your node.
#### 4. Presets
Expand Down Expand Up @@ -225,6 +226,14 @@ make clean-db # Removes the madara db, including files on the host
make restart # Restarts the madara node
```

> [!IMPORTANT]
> By default, `make start` will try and restart Madara indefinitely if it is
> found to be unhealthy using [docker autoheal](https://github.com/willfarrell/docker-autoheal).
> This is done by checking the availability of `http://localhost:9944/health`,
> which means your container will be marked as `unhealthy` and restart if you
> have disabled the RPC service! You can run `watch docker ps` to monitor the
> health of your containers.
To change runtime arguments, you can update the script in `madara-runner.sh`:

```bash
Expand Down Expand Up @@ -349,9 +358,8 @@ Here is a list of all the supported methods with their current status:
|| `starknet_getEvents` |
|| `starknet_getNonce` |
|| `starknet_getCompiledCasm` (v0.8.0) |
| 🚧 | `getMessageStatus` (v0.8.0) |
| 🚧 | `getStorageProof` (v0.8.0) |
|| `getTransactionStatus` (v0.8.0) |
| 🚧 | `starknet_getMessageStatus` (v0.8.0) |
| 🚧 | `starknet_getStorageProof` (v0.8.0) |

</details>

Expand Down
28 changes: 20 additions & 8 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ services:
cpu_count: 4
mem_limit: "16gb"
ports:
- "9944"
- 9944:9944
labels:
- "autoheal=true"
environment:
RPC_API_KEY_FILE: /run/secrets/rpc_api_key
- RPC_API_KEY_FILE=/run/secrets/rpc_api_key
secrets:
- rpc_api_key
volumes:
- /var/lib/madara:/tmp/madara
- type: bind
source: ./madara-runner.sh
target: /usr/local/bin/runner.sh
read_only: true
bind:
create_host_path: false
- ./madara-runner.sh:/usr/local/bin/runner.sh:ro
entrypoint:
- /usr/local/bin/runner.sh
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9944/health"]
interval: 10s
timeout: 5s
retries: 3
start_period: 10s
restart: on-failure:3

autoheal:
image: willfarrell/autoheal:latest
tty: true
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock

secrets:
rpc_api_key:
Expand Down
16 changes: 10 additions & 6 deletions crates/tests/src/rpc/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ mod test_rpc_read_calls {
}
impl Drop for SharedMadaraInstance {
fn drop(&mut self) {
let mut guard = MADARA_HANDLE_COUNT.lock().expect("poisoned lock");
*guard -= 1;
if *guard == 0 {
// :/
tokio::task::spawn_blocking(|| *MADARA.blocking_lock() = None);
}
// let mut guard = MADARA_HANDLE_COUNT.lock().expect("poisoned lock");
// *guard -= 1;
// if *guard == 0 {
// // :/
// tokio::task::spawn_blocking(|| *MADARA.blocking_lock() = None);
// }
}
}
#[allow(clippy::await_holding_lock)]
Expand Down Expand Up @@ -650,6 +650,10 @@ mod test_rpc_read_calls {
};
let expected_txn_status = TransactionStatus::AcceptedOnL2(TransactionExecutionStatus::Succeeded);

// TODO: The shared madara state needs a rework as we only run these
// tests with `--test-threads=1`. These tests
tokio::task::spawn_blocking(|| *MADARA.blocking_lock() = None);

assert_eq!(txn_status, expected_txn_status);
}

Expand Down
10 changes: 7 additions & 3 deletions madara-runner.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/bin/sh
export RPC_API_KEY=$(cat $RPC_API_KEY_FILE)
if [ -f "$RPC_API_KEY_FILE" ]; then
export RPC_API_KEY=$(cat "$RPC_API_KEY_FILE")
else
echo "Error: RPC_API_KEY_FILE not found!" >&2
exit 1
fi

./madara \
tini -- ./madara \
--name madara \
--network mainnet \
--rpc-external \
--rpc-cors all \
--full \
--l1-endpoint $RPC_API_KEY

0 comments on commit d73ca62

Please sign in to comment.