Commit Graph
27 Commits
Author SHA1 Message Date
Claude 88a3a6e4f0 Add timeout parameter to metadata_server and chunk_server init/step functions
Unified the function signatures across all process types (metadata server,
chunk server, and simulation client) to accept an int *timeout parameter.
This provides consistency in the simulation framework and allows all
process types to control their wakeup times.

**Changes:**

**Headers (metadata_server.h, chunk_server.h):**
- Updated metadata_server_init() to accept int *timeout parameter
- Updated metadata_server_step() to accept int *timeout parameter
- Updated chunk_server_init() to accept int *timeout parameter
- Updated chunk_server_step() to accept int *timeout parameter

**Implementations (metadata_server.c, chunk_server.c):**
- Modified init functions to set *timeout = -1 (no timeout needed)
- Modified step functions to set *timeout = -1 (no timeout needed)
- Both server types currently don't require periodic wakeups, but the
  parameter allows for future timeout-based behavior

**System Integration (system.c):**
- Updated spawn_simulated_process() to declare timeout variable once
  and pass it to all process init functions uniformly
- Updated update_simulation() to declare timeout variable once and
  pass it to all process step functions uniformly
- Simplified control flow by removing special case for client timeout
  handling - now all process types use the same timeout logic
- Consolidated timeout-to-wakeup_time conversion after switch statements

**Benefits:**
- Consistent API across all process types
- Cleaner code with reduced duplication
- Future-proof for server timeout requirements
- All processes now managed uniformly by simulation framework

The simulation continues to run correctly with all process types
handling timeout parameter appropriately.
2025-11-02 12:33:40 +00:00
Claude 38fdab24d3 Implement simulation client for deterministic testing
Added a simulation client that can run inside the TinyDFS simulation
framework, enabling fully deterministic end-to-end testing of the
distributed file system.

**New Files:**
- src/simulation_client.h - Header defining SimulationClient structure
  and API (init, step, free functions)
- src/simulation_client.c - Implementation of simulation client with
  test operations (create dir, create file, write, read, list, delete)

**Key Changes:**

**System Framework (src/system.c):**
- Added ProcessType enum (METADATA_SERVER, CHUNK_SERVER, CLIENT)
  to distinguish different simulated process types
- Extended Process union to include SimulationClient
- Modified spawn_simulated_process() to detect --client flag and
  initialize client processes using simulation_client_init()
- Updated update_simulation() to call simulation_client_step() for
  client processes, handling timeout management
- Fixed cleanup_simulation() to properly set current_process before
  freeing, preventing NULL pointer dereferences in mock_close()
- Added simulated_time static variable (struct timespec) for
  deterministic clock mocking
- Added helper function is_client() to detect client processes

**API Updates (src/system.h):**
- Exported startup_simulation() function for proper initialization
- Added function declarations for simulation management

**Test Updates (src/main_test.c):**
- Added startup_simulation() call for proper initialization
- Spawns simulation client process with "--client" flag
- Increased iteration limit to 100,000 to allow operations to complete
- Added progress logging every 10,000 iterations
- Added explicit error checking and debug output

**Simulation Client Features:**
- Non-blocking operation model using tinydfs_isdone() and
  tinydfs_process_events()
- State machine for sequential test operations
- Comprehensive test scenario:
  1. Create directory (/test_dir)
  2. Create file (/test_dir/test_file.txt)
  3. Write data to file
  4. Read data back
  5. List directory contents
  6. Delete file
- Proper integration with TinyDFS client library
- Returns poll descriptors from tinydfs_process_events()
- Timeout management for simulation scheduling

**Bug Fixes:**
- Fixed missing return statement in spawn_simulated_process()
- Fixed NULL pointer dereference in cleanup by setting current_process
- Added missing stdlib.h include for atoi()

The simulation client successfully initializes and integrates into the
simulation loop, demonstrating the framework's ability to run client
code deterministically alongside servers.
2025-11-02 12:05:53 +00:00
cozis d267c0760e Bug fixes 2025-11-02 12:50:21 +01:00
Francesco CozzutoandGitHub ba34102453 Merge pull request #3 from cozis/claude/continue-deterministic-simulation-011CUiNFqeQQknvcwfkzVKME
Progress on DST using Claude
2025-11-02 11:46:12 +01:00
Claude d959306719 Add deterministic QueryPerformanceCounter/Frequency mocks
Implemented Windows high-resolution timing mocks for deterministic simulation:

**mock_QueryPerformanceCounter:**
- Returns deterministic counter based on simulated_time
- Uses fixed 10 MHz frequency (10,000,000 counts/second)
- Calculation: count = (tv_sec * freq) + (tv_nsec * freq / 1e9)
- Validates NULL pointer with ERROR_INVALID_PARAMETER
- Provides consistent, reproducible timing across test runs

**mock_QueryPerformanceFrequency:**
- Returns fixed frequency of 10 MHz (10,000,000 Hz)
- Common frequency on modern Windows systems
- Ensures deterministic behavior (no variation between runs)
- Validates NULL pointer with ERROR_INVALID_PARAMETER

**Integration:**
- Added function declarations to system.h
- Macros already existed (sys_QueryPerformanceCounter/Frequency)
- Complements existing mock_clock_gettime on Linux
- Both platforms now have deterministic high-resolution timing

This ensures Windows code using QueryPerformanceCounter for timing,
benchmarking, or rate limiting behaves deterministically in tests.
The fixed 10 MHz frequency provides nanosecond-level precision
(100ns per tick) suitable for most timing needs.
2025-11-02 10:44:20 +00:00
Claude aa4be1e203 Fix socket error handling: WSASetLastError on Windows and recv() return value
Corrected two critical issues with socket mock precision:

**1. Windows Socket Error Handling:**
- Socket functions on Windows use WSAGetLastError/WSASetLastError, NOT errno
- Added SET_SOCKET_ERROR macro that calls:
  - WSASetLastError() on Windows
  - errno assignment on Linux
- Mapped all POSIX error codes to WSA equivalents:
  - EWOULDBLOCK → WSAEWOULDBLOCK
  - EAFNOSUPPORT → WSAEAFNOSUPPORT
  - EMFILE → WSAEMFILE
  - EBADF → WSAEBADF
  - ENOTSOCK → WSAENOTSOCK
  - EINVAL → WSAEINVAL
  - EADDRINUSE → WSAEADDRINUSE
  - EDESTADDRREQ → WSAEDESTADDRREQ
  - ECONNABORTED → WSAECONNABORTED
  - EISCONN → WSAEISCONN
  - EINPROGRESS → WSAEINPROGRESS
  - EOPNOTSUPP → WSAEOPNOTSUPP
  - ENOTCONN → WSAENOTCONN
  - ECONNRESET → WSAECONNRESET
  - ENOPROTOOPT → WSAENOPROTOOPT
  - EPIPE → WSAESHUTDOWN (Windows equivalent)

**2. Fixed mock_recv() Return Value:**
- Now returns 0 when peer closes connection (orderly shutdown)
- Previously returned -1 with ECONNRESET
- This matches standard POSIX/Winsock behavior:
  - 0 = peer closed connection gracefully
  - -1 = error occurred (check errno/WSAGetLastError)
  - >0 = bytes received

**Updated Functions:**
- mock_socket, mock_bind, mock_listen, mock_accept
- mock_connect, mock_recv, mock_send
- mock_getsockopt, mock_setsockopt

All socket functions now use correct error reporting mechanism
for their platform, improving simulation accuracy.
2025-11-02 10:33:44 +00:00
Claude 14cc295c30 Improve mocking system precision with proper errno handling
Enhanced the deterministic simulation framework with comprehensive error handling:

**Socket Operations:**
- mock_socket: Set EAFNOSUPPORT, EMFILE
- mock_bind: Set EBADF, ENOTSOCK, EINVAL, EADDRINUSE (with address collision detection)
- mock_listen: Set EBADF, ENOTSOCK, EDESTADDRREQ (check socket is bound)
- mock_accept: Set EBADF, EINVAL, EWOULDBLOCK, ECONNABORTED, EMFILE
- mock_connect: Set EBADF, EISCONN, EINVAL, EINPROGRESS
- mock_recv: Set EBADF, EOPNOTSUPP, ENOTCONN, ECONNRESET, EWOULDBLOCK
- mock_send: Set EBADF, EOPNOTSUPP, ENOTCONN, EPIPE, EWOULDBLOCK
- mock_getsockopt: Set EBADF, ENOPROTOOPT, EINVAL (implemented SO_ERROR)
- mock_setsockopt: Set EBADF, ENOPROTOOPT (no-op but validates input)

**File Operations (Linux):**
- mock_close: Set EBADF with bounds checking
- mock_flock: Set EBADF with validation
- mock_fsync: Set EBADF, EINVAL
- mock_read: Set EBADF with descriptor type checking
- mock_write: Set EBADF with descriptor type checking
- mock_fstat: Set EBADF with validation
- All file ops forward errno from real syscalls when appropriate

**File Operations (Windows):**
- mock_CloseHandle: SetLastError ERROR_INVALID_HANDLE
- mock_LockFile/UnlockFile: SetLastError ERROR_INVALID_HANDLE
- mock_FlushFileBuffers: SetLastError ERROR_INVALID_HANDLE
- mock_ReadFile/WriteFile: SetLastError ERROR_INVALID_HANDLE
- mock_GetFileSizeEx: SetLastError ERROR_INVALID_HANDLE
- All Windows file ops forward errors from real API calls

**Simulated Clock:**
- Implemented mock_clock_gettime with proper errno handling
- Returns simulated time for deterministic behavior
- Validates clock ID (CLOCK_REALTIME, CLOCK_MONOTONIC)
- Sets EFAULT for NULL pointer, EINVAL for invalid clock ID

**Improvements:**
- Added comprehensive bounds checking on all file descriptors
- Proper descriptor type validation before operations
- Peer connection validation in socket operations
- Address-in-use detection for bind operations
- Consistent error code semantics matching POSIX/Windows standards

All file I/O operations continue to forward to real OS as intended,
with mocking layer providing precise error simulation and validation.
2025-11-02 03:51:59 +00:00
Claude ef235f7543 Fix Windows API mock compilation bugs
Fixed 4 compilation errors in system.c:
- mock_CloseHandle: Changed undefined 'fd' to 'handle'
- mock_LockFile: Changed 'handle' to 'hFile' parameter
- mock_UnlockFile: Changed 'handle' to 'hFile' parameter
- mock_WriteFile: Added missing comma in WriteFile call

All mocks now use correct parameter names matching their function signatures.
2025-11-02 03:43:42 +00:00
cozis 8b284f5752 Progress 2025-11-02 04:37:20 +01:00
cozis eb93541c84 Test framework cleanup 2025-11-01 22:44:04 +01:00
cozis 7525a93035 Progress 2025-10-29 23:48:45 +01:00
cozis 174f37c6c0 Continue work on DST framework 2025-10-29 21:46:17 +01:00
Francesco CozzutoandGitHub 24c0134c03 Merge pull request #2 from cozis/claude/continue-deterministic-simulation-test-011CUbGqPQoT7vvhmry5QsUD
Continue deterministic simulation test
2025-10-29 11:10:44 +01:00
cozis 815e4867cf Minor cleanups 2025-10-29 11:10:01 +01:00
Claude 51cb3ba061 Complete deterministic simulation testing implementation
- Fixed compilation errors in descriptor type checks and field references
- Implemented sys_accept with pending connection queue support
- Completed sys_connect with full connection establishment between processes
- Implemented sys_getsockopt for SO_ERROR option
- Added network transit logic to transfer data between connected sockets
- Added peer tracking to descriptors for connection management
- Initialized byte queues for socket I/O buffers

The simulation now supports:
- Socket creation and binding
- Connection establishment between processes
- Bidirectional data transfer via byte queues
- Accept queue management for listen sockets
- Deterministic network simulation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 09:48:30 +00:00
cozis 7bc4071344 Start work on DST 2025-10-29 10:41:23 +01:00
cozis fb8ac1d5c9 Fix compiler errors 2025-10-28 17:33:52 +01:00
cozis c5a21608d7 Split project over multiple files 2025-10-28 17:24:25 +01:00
Francesco CozzutoandGitHub 017c2c3428 Merge pull request #1 from cozis/claude/list-missing-implementations-011CUZKcCqHqBMS8gDqNAXyn
Resolved some TODOs using Claude
2025-10-28 11:18:33 +01:00
cozis 00f2570231 Progress 2025-10-28 11:16:06 +01:00
Claude 223bbbefb2 Fix chunk removal: Check entire tree instead of premature deletion
Implement simple and correct approach for determining which chunks can
be safely removed:

When a write overwrites chunks:
1. Update the file tree with new chunk hashes
2. For each old hash, walk the entire tree to check if still in use
3. Only mark chunks for removal if NOT found anywhere in the tree

Changes:
- Modified file_tree_write() to return removed_hashes array
- After updating chunks, check each old_hash with entity_uses_hash()
- Walks entire tree from root to verify hash is truly unreferenced
- Caller receives array of hashes safe to remove
- Only those hashes are added to chunk servers' rem_list

This approach is:
- Simple: No complex reference counting needed
- Correct: Handles deduplication naturally (same content, same hash)
- Safe: Only removes chunks with zero references
- Easy to understand: Linear tree walk on each write

Performance can be optimized later if needed. Correctness first!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 09:30:37 +00:00
Claude e89b6b5de1 Fix all implementable TODO items in TinyDFS.c
This commit addresses the following TODO items:

1. Fixed proper error codes (lines 2063, 2068, 2071, 2081)
   - Changed generic -1 returns to specific FILETREE_* error codes

2. Added overflow check for chunk_size (line 2659)
   - Added validation that chunk_size fits in uint32_t before casting

3. Handled error event flags in connection polling (line 1559)
   - Added checks for POLLERR, POLLHUP, and POLLNVAL events

4. Fixed absolute path edge case (line 1773)
   - Properly handle ".." in absolute paths (stays at root)
   - Reject ".." in relative paths when at the start

5. Documented authentication verification (line 2903)
   - Added comment about production authentication requirements

6. Implemented chunk removal on overwrite (line 2790)
   - Old chunks are now added to rem_list for garbage collection
   - Zero hashes (new chunks) are properly handled

7. Completed download chunk implementation (lines 3247, 3253, 3256)
   - Implemented full download request with hash, offset, and length
   - Added error handling for failed connections

8. Implemented state update chunk management (line 3308)
   - Move chunks between main and orphaned directories
   - Validate chunk presence and report missing chunks

9. Implemented process_chunk_server_download_error (line 3450)
   - Handle download failures and retry next pending download

10. Implemented process_chunk_server_download_success (line 3456)
    - Store downloaded chunks and continue with pending downloads

11. Handled chunk server connection disconnect (line 3721)
    - Reset downloading state on disconnect for retry

12. Implemented AUTH message on reconnect (line 3777)
    - Send authentication message when reconnecting to metadata server

13. Implemented read list processing (line 4324)
    - Parse and validate list response message format

14. Documented write operation stub (line 4631)
    - Added comments explaining what full implementation would require

Remaining TODOs are architectural notes for future enhancements:
- Line 124: Chunk orphaning strategy (now implemented)
- Line 662: Test parent_path function (testing task)
- Line 3673: IPv6 support (future enhancement)
- Line 3766: Periodic chunk hash verification (future feature)
- Line 3768: Periodic download management (mostly implemented)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 09:09:15 +00:00
cozis ea18992f6a Progress 2025-10-28 09:57:48 +01:00
cozis 3f0f4c2443 Update 2025-10-27 22:50:51 +01:00
cozis 19ea9a0718 Progress 2025-10-27 21:22:42 +01:00
cozis 3c92b8d343 Progress 2025-10-27 18:54:49 +01:00
cozis 1f4a9956da First commit 2025-10-27 18:01:33 +01:00