Skip to content

Commit

Permalink
Merge pull request #171 from codecrafters-io/add-logging
Browse files Browse the repository at this point in the history
Add HexDump logging for RDB files
  • Loading branch information
ryan-gang authored Oct 28, 2024
2 parents d016ede + f3eef20 commit 024884c
Show file tree
Hide file tree
Showing 12 changed files with 615 additions and 287 deletions.
18 changes: 18 additions & 0 deletions internal/rdb_file_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

"github.com/codecrafters-io/tester-utils/logger"
testerutils_random "github.com/codecrafters-io/tester-utils/random"
"github.com/codecrafters-io/tester-utils/test_case_harness"

Expand Down Expand Up @@ -100,3 +101,20 @@ func (r *RDBFileCreator) Write(keyValuePairs []KeyValuePair) error {

return nil
}

func (r *RDBFileCreator) Contents() ([]byte, error) {
contents, err := os.ReadFile(filepath.Join(r.Dir, r.Filename))
if err != nil {
return nil, err
}
return contents, nil
}

func (r *RDBFileCreator) PrintContentHexdump(logger *logger.Logger) error {
contents, err := r.Contents()
if err != nil {
return err
}
logger.Debugf("Hexdump of RDB file contents: \n%v\n", GetFormattedHexdump(contents))
return nil
}
1 change: 1 addition & 0 deletions internal/stages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func normalizeTesterOutput(testerOutput []byte) []byte {
"rdb_bytes": {regexp.MustCompile(`"\$[0-9]+\\r\\nREDIS.*"`)},
"info_replication_bytes": {regexp.MustCompile(`"\$[0-9]+\\r\\n# Replication\\r\\n[^"]+"`)},
"rdb_keys": {regexp.MustCompile(`\[stage-11\] .*Received .*`)},
"hexdump": {regexp.MustCompile(`[0-9a-fA-F]{4} \| [0-9a-fA-F ]{47} \| .{0,16}`)},
}

for replacement, regexes := range replacements {
Expand Down
94 changes: 75 additions & 19 deletions internal/test_helpers/fixtures/rdb-read-value-with-expiry/pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
Debug = true

[stage-13] Running tests for Stage #13: sm4
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2910760570 --dbfilename pear.rdb
[stage-13] Created RDB file with 3 key-value pairs: {"orange": "strawberry", "mango": "apple", "pineapple": "blueberry"}
[stage-13] Hexdump of RDB file contents: 
[stage-13] Idx | Hex | ASCII
[stage-13] -----+-------------------------------------------------+-----------------
[stage-13] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[stage-13] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[stage-13] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 03 03 fc 00 0c | s-bits.@........
[stage-13] 0030 | 28 8a c7 01 00 00 00 06 6f 72 61 6e 67 65 0a 73 | (.......orange.s
[stage-13] 0040 | 74 72 61 77 62 65 72 72 79 fc 00 9c ef 12 7e 01 | trawberry.....~.
[stage-13] 0050 | 00 00 00 05 6d 61 6e 67 6f 05 61 70 70 6c 65 fc | ....mango.apple.
[stage-13] 0060 | 00 0c 28 8a c7 01 00 00 00 09 70 69 6e 65 61 70 | ..(.......pineap
[stage-13] 0070 | 70 6c 65 09 62 6c 75 65 62 65 72 72 79 ff 34 d0 | ple.blueberry.4.
[stage-13] 0080 | 8c 58 51 4a 5d 62 0a | .XQJ]b.
[stage-13] 
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles444689312 --dbfilename pear.rdb
[stage-13] client: $ redis-cli GET orange
[stage-13] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$6\r\norange\r\n"
[stage-13] client: Received bytes: "$10\r\nstrawberry\r\n"
Expand All @@ -22,8 +36,21 @@ Debug = true
[stage-13] Program terminated successfully

[stage-12] Running tests for Stage #12: dq3
[stage-12] Created RDB file with key-value pairs: "grape"="pineapple", "apple"="raspberry", "banana"="apple", "mango"="orange", "orange"="pear"
[stage-12] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2724103995 --dbfilename raspberry.rdb
[stage-12] Created RDB file with 5 key-value pairs: {"grape": "pineapple", "apple": "raspberry", "banana": "apple", "mango": "orange", "orange": "pear"}
[stage-12] Hexdump of RDB file contents: 
[stage-12] Idx | Hex | ASCII
[stage-12] -----+-------------------------------------------------+-----------------
[stage-12] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[stage-12] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[stage-12] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 05 00 00 05 67 | [email protected]
[stage-12] 0030 | 72 61 70 65 09 70 69 6e 65 61 70 70 6c 65 00 05 | rape.pineapple..
[stage-12] 0040 | 61 70 70 6c 65 09 72 61 73 70 62 65 72 72 79 00 | apple.raspberry.
[stage-12] 0050 | 06 62 61 6e 61 6e 61 05 61 70 70 6c 65 00 05 6d | .banana.apple..m
[stage-12] 0060 | 61 6e 67 6f 06 6f 72 61 6e 67 65 00 06 6f 72 61 | ango.orange..ora
[stage-12] 0070 | 6e 67 65 04 70 65 61 72 ff 33 20 d4 0d 23 b2 9b | nge.pear.3 ..#..
[stage-12] 0080 | af 0a | ..
[stage-12] 
[stage-12] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3255364286 --dbfilename raspberry.rdb
[stage-12] client: $ redis-cli GET grape
[stage-12] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
[stage-12] client: Received bytes: "$9\r\npineapple\r\n"
Expand Down Expand Up @@ -54,20 +81,40 @@ Debug = true
[stage-12] Program terminated successfully

[stage-11] Running tests for Stage #11: jw4
[stage-11] Created RDB file with 3 keys: ["banana" "apple" "blueberry"]
[stage-11] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2441645913 --dbfilename blueberry.rdb
[stage-11] Created RDB file with 3 keys: ["banana", "apple", "blueberry"]
[stage-11] Hexdump of RDB file contents: 
[stage-11] Idx | Hex | ASCII
[stage-11] -----+-------------------------------------------------+-----------------
[stage-11] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[stage-11] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[stage-11] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 03 00 00 06 62 | [email protected]
[stage-11] 0030 | 61 6e 61 6e 61 09 70 69 6e 65 61 70 70 6c 65 00 | anana.pineapple.
[stage-11] 0040 | 05 61 70 70 6c 65 09 72 61 73 70 62 65 72 72 79 | .apple.raspberry
[stage-11] 0050 | 00 09 62 6c 75 65 62 65 72 72 79 04 70 65 61 72 | ..blueberry.pear
[stage-11] 0060 | ff 5c f3 42 ee 46 08 a6 3f 0a | .\.B.F..?.
[stage-11] 
[stage-11] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2441884045 --dbfilename blueberry.rdb
[stage-11] client: $ redis-cli KEYS *
[stage-11] client: Sent bytes: "*2\r\n$4\r\nKEYS\r\n$1\r\n*\r\n"
[stage-11] client: Received bytes: "*3\r\n$6\r\nbanana\r\n$9\r\nblueberry\r\n$5\r\napple\r\n"
[stage-11] client: Received RESP array: ["banana", "blueberry", "apple"]
[stage-11] Received ["banana", "blueberry", "apple"]
[stage-11] client: Received bytes: "*3\r\n$6\r\nbanana\r\n$5\r\napple\r\n$9\r\nblueberry\r\n"
[stage-11] client: Received RESP array: ["banana", "apple", "blueberry"]
[stage-11] Received ["banana", "apple", "blueberry"]
[stage-11] Test passed.
[stage-11] Terminating program
[stage-11] Program terminated successfully

[stage-10] Running tests for Stage #10: gc6
[stage-10] Created RDB file with single key-value pair: grape="pineapple"
[stage-10] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3729886679 --dbfilename orange.rdb
[stage-10] Created RDB file with a single key-value pair: {"grape": "pineapple"}
[stage-10] Hexdump of RDB file contents: 
[stage-10] Idx | Hex | ASCII
[stage-10] -----+-------------------------------------------------+-----------------
[stage-10] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[stage-10] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[stage-10] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 01 00 00 05 67 | [email protected]
[stage-10] 0030 | 72 61 70 65 09 70 69 6e 65 61 70 70 6c 65 ff 12 | rape.pineapple..
[stage-10] 0040 | 7d 54 51 cd 7a 5c 8d 0a | }TQ.z\..
[stage-10] 
[stage-10] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3226033919 --dbfilename orange.rdb
[stage-10] client: $ redis-cli GET grape
[stage-10] client: Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\ngrape\r\n"
[stage-10] client: Received bytes: "$9\r\npineapple\r\n"
Expand All @@ -78,8 +125,17 @@ Debug = true
[stage-10] Program terminated successfully

[stage-9] Running tests for Stage #9: jz6
[stage-9] Created RDB file with single key: "mango"
[stage-9] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3049301350 --dbfilename pear.rdb
[stage-9] Created RDB file with a single key: ["mango"]
[stage-9] Hexdump of RDB file contents: 
[stage-9] Idx | Hex | ASCII
[stage-9] -----+-------------------------------------------------+-----------------
[stage-9] 0000 | 52 45 44 49 53 30 30 31 31 fa 09 72 65 64 69 73 | REDIS0011..redis
[stage-9] 0010 | 2d 76 65 72 05 37 2e 32 2e 30 fa 0a 72 65 64 69 | -ver.7.2.0..redi
[stage-9] 0020 | 73 2d 62 69 74 73 c0 40 fe 00 fb 01 00 00 05 6d | [email protected]
[stage-9] 0030 | 61 6e 67 6f 09 70 69 6e 65 61 70 70 6c 65 ff 0c | ango.pineapple..
[stage-9] 0040 | 1b cb 91 b2 ed f6 19 0a | ........
[stage-9] 
[stage-9] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1527538990 --dbfilename pear.rdb
[stage-9] client: $ redis-cli KEYS *
[stage-9] client: Sent bytes: "*2\r\n$4\r\nKEYS\r\n$1\r\n*\r\n"
[stage-9] client: Received bytes: "*1\r\n$5\r\nmango\r\n"
Expand All @@ -90,12 +146,12 @@ Debug = true
[stage-9] Program terminated successfully

[stage-8] Running tests for Stage #8: zg5
[stage-8] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles778895138 --dbfilename blueberry.rdb
[stage-8] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2714192117 --dbfilename blueberry.rdb
[stage-8] client: $ redis-cli CONFIG GET dir
[stage-8] client: Sent bytes: "*3\r\n$6\r\nCONFIG\r\n$3\r\nGET\r\n$3\r\ndir\r\n"
[stage-8] client: Received bytes: "*2\r\n$3\r\ndir\r\n$74\r\n/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles778895138\r\n"
[stage-8] client: Received RESP array: ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles778895138"]
[stage-8] Received ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles778895138"]
[stage-8] client: Received bytes: "*2\r\n$3\r\ndir\r\n$75\r\n/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2714192117\r\n"
[stage-8] client: Received RESP array: ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2714192117"]
[stage-8] Received ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2714192117"]
[stage-8] Test passed.
[stage-8] Terminating program
[stage-8] Program terminated successfully
Expand All @@ -107,15 +163,15 @@ Debug = true
[stage-7] Received bytes: "+OK\r\n"
[stage-7] Received RESP simple string: "OK"
[stage-7] Received "OK"
[stage-7] Received OK at 16:13:08.183
[stage-7] Fetching key "strawberry" at 16:13:08.183 (should not be expired)
[stage-7] Received OK at 16:31:40.207
[stage-7] Fetching key "strawberry" at 16:31:40.207 (should not be expired)
[stage-7] > GET strawberry
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$10\r\nstrawberry\r\n"
[stage-7] Received bytes: "$4\r\npear\r\n"
[stage-7] Received RESP bulk string: "pear"
[stage-7] Received "pear"
[stage-7] Sleeping for 101ms
[stage-7] Fetching key "strawberry" at 16:13:08.286 (should be expired)
[stage-7] Fetching key "strawberry" at 16:31:40.311 (should be expired)
[stage-7] > GET strawberry
[stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$10\r\nstrawberry\r\n"
[stage-7] Received bytes: "$-1\r\n"
Expand Down
Loading

0 comments on commit 024884c

Please sign in to comment.