From cc758a5d3a29260f9b94d4d73313327a8468ff8f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Nov 2025 00:00:54 +0000 Subject: [PATCH] Add web server to cluster demo and fix port bug - Add web server integration to cluster_demo.sh script - Configure web server port (8090) - Update build_if_needed() to check for toastyfs_web binary - Add get_web_binary() function - Start web server after chunk servers - Update status display to show web server - Update help text to show HTTP interface - Fix bug in web/main.c where --local-port incorrectly set upstream_port instead of local_port The cluster demo now starts a full cluster with: - Metadata server on port 8080 (ToastyFS protocol) - N chunk servers starting from port 8081 - Web server on port 8090 (HTTP interface) --- scripts/cluster_demo.sh | 55 +++++++++++++++++++++++++++++++++++++---- web/main.c | 11 ++++++--- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/scripts/cluster_demo.sh b/scripts/cluster_demo.sh index 8087f4c..644abe1 100755 --- a/scripts/cluster_demo.sh +++ b/scripts/cluster_demo.sh @@ -21,6 +21,7 @@ DATA_DIR="$PROJECT_DIR/cluster_data" METADATA_PORT=8080 CHUNK_SERVER_BASE_PORT=8081 +WEB_SERVER_PORT=8090 # Colors for output RED='\033[0;31m' @@ -46,8 +47,18 @@ print_error() { } build_if_needed() { + local need_build=false + if [ ! -f "$PROJECT_DIR/toastyfs.out" ] && [ ! -f "$PROJECT_DIR/toastyfs.exe" ]; then - print_info "Binary not found. Building ToastyFS..." + need_build=true + fi + + if [ ! -f "$PROJECT_DIR/toastyfs_web.out" ] && [ ! -f "$PROJECT_DIR/toastyfs_web.exe" ]; then + need_build=true + fi + + if [ "$need_build" = true ]; then + print_info "Binaries not found. Building ToastyFS..." cd "$PROJECT_DIR" make print_success "Build complete" @@ -65,6 +76,17 @@ get_binary() { fi } +get_web_binary() { + if [ -f "$PROJECT_DIR/toastyfs_web.out" ]; then + echo "$PROJECT_DIR/toastyfs_web.out" + elif [ -f "$PROJECT_DIR/toastyfs_web.exe" ]; then + echo "$PROJECT_DIR/toastyfs_web.exe" + else + print_error "ToastyFS web binary not found" + exit 1 + fi +} + start_cluster() { local num_chunk_servers=${1:-3} @@ -84,6 +106,7 @@ start_cluster() { print_info "Starting ToastyFS cluster..." print_info " Metadata server: 127.0.0.1:$METADATA_PORT" print_info " Chunk servers: $num_chunk_servers" + print_info " Web server: 127.0.0.1:$WEB_SERVER_PORT" echo # Start metadata server @@ -126,12 +149,26 @@ start_cluster() { sleep 0.5 done + # Start web server + local web_binary=$(get_web_binary) + print_info "Starting web server on port $WEB_SERVER_PORT..." + "$web_binary" \ + --upstream-addr 127.0.0.1 \ + --upstream-port $METADATA_PORT \ + --local-addr 127.0.0.1 \ + --local-port $WEB_SERVER_PORT \ + > "$LOG_DIR/web_server.log" 2>&1 & + + local web_pid=$! + echo "$web_pid" >> "$PID_FILE" + print_success "Web server started (PID: $web_pid)" + echo print_success "Cluster started successfully!" echo print_info "Connect to the cluster using:" - print_info " Address: 127.0.0.1" - print_info " Port: $METADATA_PORT" + print_info " ToastyFS Protocol: 127.0.0.1:$METADATA_PORT" + print_info " HTTP Interface: http://127.0.0.1:$WEB_SERVER_PORT" echo print_info "View logs at: $LOG_DIR/" print_info "Data stored at: $DATA_DIR/" @@ -184,12 +221,20 @@ show_status() { local running=0 local not_running=0 local line_num=0 + local total_lines=$(wc -l < "$PID_FILE") while IFS= read -r pid; do if [ -n "$pid" ]; then line_num=$((line_num + 1)) - local server_type="Chunk server $((line_num - 1))" - [ $line_num -eq 1 ] && server_type="Metadata server" + local server_type="" + + if [ $line_num -eq 1 ]; then + server_type="Metadata server" + elif [ $line_num -eq $total_lines ]; then + server_type="Web server" + else + server_type="Chunk server $((line_num - 1))" + fi if kill -0 "$pid" 2>/dev/null; then echo -e " ${GREEN}●${NC} $server_type (PID: $pid) - ${GREEN}running${NC}" diff --git a/web/main.c b/web/main.c index 14b6e81..e4bc998 100644 --- a/web/main.c +++ b/web/main.c @@ -10,6 +10,8 @@ #define UNREACHABLE __builtin_trap() +#define MAX_PROXIED_OPERATIONS (1<<10) + typedef enum { PROXIED_OPERATION_FREE, PROXIED_OPERATION_CREATE_DIR, @@ -43,8 +45,6 @@ typedef struct { } ProxiedOperation; -#define MAX_PROXIED_OPERATIONS (1<<10) - static int find_unused_struct(ProxiedOperation *arr, int num) { if (num == MAX_PROXIED_OPERATIONS) @@ -116,7 +116,7 @@ int main(int argc, char **argv) fprintf(stderr, "Error: Invalid port %s\n", argv[i]); return -1; } - upstream_port = (uint16_t) tmp; + local_port = (uint16_t) tmp; } else { fprintf(stderr, "Error: Invalid option %s\n", argv[i]); return -1; @@ -146,6 +146,9 @@ int main(int argc, char **argv) int num_proxied = 0; ProxiedOperation proxied[MAX_PROXIED_OPERATIONS]; + for (int i = 0; i < MAX_PROXIED_OPERATIONS; i++) + proxied[i].type = PROXIED_OPERATION_FREE; + for (;;) { #define POLL_CAPACITY (HTTP_SERVER_POLL_CAPACITY + TOASTY_POLL_CAPACITY) @@ -160,7 +163,7 @@ int main(int argc, char **argv) reg = (EventRegister) { .ptrs=ptrs, .polled=polled, - .max_polled=POLL_CAPACITY, + .max_polled=HTTP_SERVER_POLL_CAPACITY, .num_polled=0, }; if (http_server_register_events(&server, ®) < 0) {