Skip to content

Commit

Permalink
Merge pull request #168 from codecrafters-io/reused-conn-logs
Browse files Browse the repository at this point in the history
refactor: update RespConnection struct to track total sent bytes
  • Loading branch information
ryan-gang authored Oct 21, 2024
2 parents 0012bc1 + a5ce597 commit d016ede
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 280 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ record_fixtures:
update_tester_utils:
go get -u github.com/codecrafters-io/tester-utils

test_dev: build
cd /Users/ryang/Developer/byox/build-your-own-redis && \
CODECRAFTERS_REPOSITORY_DIR=/Users/ryang/Developer/byox/build-your-own-redis \
CODECRAFTERS_TEST_CASES_JSON="[]" \
$(shell pwd)/dist/main.out

test_base_with_redis: build
CODECRAFTERS_REPOSITORY_DIR=./internal/test_helpers/pass_all \
CODECRAFTERS_TEST_CASES_JSON="[{\"slug\":\"jm1\",\"tester_log_prefix\":\"stage-1\",\"title\":\"Stage #1: Bind to a port\"},{\"slug\":\"rg2\",\"tester_log_prefix\":\"stage-2\",\"title\":\"Stage #2: Respond to PING\"},{\"slug\":\"wy1\",\"tester_log_prefix\":\"stage-3\",\"title\":\"Stage #3: Respond to multiple PINGs\"},{\"slug\":\"zu2\",\"tester_log_prefix\":\"stage-4\",\"title\":\"Stage #4: Handle concurrent clients\"},{\"slug\":\"qq0\",\"tester_log_prefix\":\"stage-5\",\"title\":\"Stage #5: Implement the ECHO command\"},{\"slug\":\"la7\",\"tester_log_prefix\":\"stage-6\",\"title\":\"Stage #6: Implement the SET \u0026 GET commands\"},{\"slug\":\"yz1\",\"tester_log_prefix\":\"stage-7\",\"title\":\"Stage #7: Expiry\"}]" \
Expand Down
9 changes: 7 additions & 2 deletions internal/resp/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ type RespConnection struct {
Callbacks RespConnectionCallbacks

// Offset tracking:
// SentBytes is the number of bytes sent using this connection.
// SentBytes is the number of bytes sent using this connection, but this can be mutated when it makes sense (ex: handshake bytes are not counted by redis for acks)
SentBytes int

// ReceivedBytes is the number of bytes received using this connection.
ReceivedBytes int

// TotalSentBytes is the total number of bytes sent using this connection, and is not reset or changed when the connection is reset
// This should be used for deciding if connection is new / reused and commands be logged as such
TotalSentBytes int
}

func NewRespConnectionFromAddr(addr string, callbacks RespConnectionCallbacks) (*RespConnection, error) {
Expand Down Expand Up @@ -83,7 +87,7 @@ func (c *RespConnection) Close() error {

func (c *RespConnection) SendCommand(command string, args ...string) error {
if c.Callbacks.BeforeSendCommand != nil {
if c.SentBytes > 0 {
if c.TotalSentBytes > 0 {
c.Callbacks.BeforeSendCommand(true, command, args...)
} else {
c.Callbacks.BeforeSendCommand(false, command, args...)
Expand Down Expand Up @@ -114,6 +118,7 @@ func (c *RespConnection) SendBytes(bytes []byte) error {
}

c.SentBytes += len(bytes)
c.TotalSentBytes += len(bytes)

// TODO: Check when this happens - is it a valid error?
if n != len(bytes) {
Expand Down
30 changes: 15 additions & 15 deletions internal/test_helpers/fixtures/rdb-read-value-with-expiry/pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Debug = true

[stage-13] Running tests for Stage #13: sm4
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2566692571 --dbfilename pear.rdb
[stage-13] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2910760570 --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 @@ -23,7 +23,7 @@ Debug = true

[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/rdbfiles3581289170 --dbfilename raspberry.rdb
[stage-12] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2724103995 --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 @@ -55,19 +55,19 @@ Debug = true

[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/rdbfiles2596974998 --dbfilename blueberry.rdb
[stage-11] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles2441645913 --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$9\r\nblueberry\r\n$5\r\napple\r\n$6\r\nbanana\r\n"
[stage-11] client: Received RESP array: ["blueberry", "apple", "banana"]
[stage-11] Received ["blueberry", "apple", "banana"]
[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] 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/rdbfiles383736839 --dbfilename orange.rdb
[stage-10] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3729886679 --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 @@ -79,7 +79,7 @@ Debug = true

[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/rdbfiles142773913 --dbfilename pear.rdb
[stage-9] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles3049301350 --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 +90,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/rdbfiles1708726226 --dbfilename blueberry.rdb
[stage-8] $ ./spawn_redis_server.sh --dir /private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles778895138 --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$75\r\n/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1708726226\r\n"
[stage-8] client: Received RESP array: ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1708726226"]
[stage-8] Received ["dir", "/private/var/folders/5l/z5y3dkwn68sgb6htzc5w7vnm0000gn/T/rdbfiles1708726226"]
[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] Test passed.
[stage-8] Terminating program
[stage-8] Program terminated successfully
Expand All @@ -107,15 +107,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 13:28:51.062
[stage-7] Fetching key "strawberry" at 13:28:51.062 (should not be expired)
[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] > 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 13:28:51.166 (should be expired)
[stage-7] Fetching key "strawberry" at 16:13:08.286 (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 d016ede

Please sign in to comment.