I’m stuck on Stage #WA6.
I’ve tried running this locally and using print debug statements; all looks fine. The code from the previous task is essentially the same, and I’m outputting the expected correlation ID.
Here are my logs:
[compile] Compiling codecrafters-kafka v0.1.0 (/app)
[compile] Finished `release` profile [optimized] target(s) in 16.76s
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
Debug = true
[tester::#WA6] Running tests for Stage #WA6 (Parse Correlation ID)
[tester::#WA6] $ ./your_program.sh /tmp/server.properties
[tester::#WA6] Connecting to broker at: localhost:9092
[your_program] Logs from your program will appear here!
[your_program] accepted new connection
[your_program] Buffer length: 12
[tester::#WA6] Connection to broker at localhost:9092 successful
[tester::#WA6] Sending "ApiVersions" (version: 4) request (Correlation id: 1699631701)
[tester::#WA6] Hexdump of sent "ApiVersions" request:
[tester::#WA6] Idx | Hex | ASCII
[tester::#WA6] -----+-------------------------------------------------+-----------------
[tester::#WA6] 0000 | 00 00 00 23 00 12 00 04 65 4e 52 55 00 09 6b 61 | ...#....eNRU..ka
[tester::#WA6] 0010 | 66 6b 61 2d 63 6c 69 00 0a 6b 61 66 6b 61 2d 63 | fka-cli..kafka-c
[tester::#WA6] 0020 | 6c 69 04 30 2e 31 00 | li.0.1.
[tester::#WA6]
[your_program] Read 12 byte(s)
[tester::#WA6] error reading from connection: read tcp 127.0.0.1:39756->127.0.0.1:9092: read: connection reset by peer
[tester::#WA6] Test failed
[tester::#WA6] Terminating program
[your_program] Total bytes read: 12
[your_program] Correlation ID: 1699631701
[your_program] Sent 4 byte(s) for message size
[your_program] Sent 4 byte(s) for correlation ID
[tester::#WA6] Program terminated successfully
The main error seems to be this:
error reading from connection: read tcp 127.0.0.1:39756->127.0.0.1:9092: read: connection reset by peer
And here’s a snippet of my code:
fn read_bytes_from_stream(_stream: &mut TcpStream, buf: &mut [u8]) -> usize {
let mut total_bytes_read = 0;
println!("Buffer length: {}", buf.len());
while total_bytes_read < buf.len() {
match _stream.read(&mut buf[total_bytes_read..]) {
Ok(0) => {
println!("Connection closed by peer");
break
},
Ok(n) => {
println!("Read {} byte(s)", n);
total_bytes_read += n;
},
Err(e) => {
eprintln!("Failed to read: {}", e);
break
},
}
}
println!("Total bytes read: {}", total_bytes_read);
total_bytes_read
}
fn parse_correlation_id(bytes: &[u8], offset: usize, size: usize) -> i32 {
let correlation_id = i32::from_be_bytes(bytes[offset..size].try_into().unwrap());
correlation_id
}
The rest can be found here.
Thanks for your help.