Expected null bulk string (“$-1\r\n”), got BULK_STRING (#YZ1)

I’m stuck on stage 7 expiry.

I have implemented the expiry logic, as per the instructions in java, even though i am sending the null bulk string, it is failing. Can you guys please help me where i am missing.

Here are my logs:

remote: [stage-7] Received bytes: "+OK\r\n"
remote: [stage-7] Received RESP value: "OK"
remote: [stage-7] Received "OK"
remote: [stage-7] Received OK at 10:29:33.576
remote: [stage-7] Fetching key "apple" at 10:29:33.576 (should not be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [stage-7] Received bytes: "$6\r\nbanana\r\n"
remote: [stage-7] Received RESP value: "banana"
remote: [stage-7] Received "banana"
remote: [stage-7] Sleeping for 101ms
remote: [stage-7] Fetching key "apple" at 10:29:33.690 (should be expired)
remote: [stage-7] $ redis-cli GET apple
remote: [stage-7] Sent bytes: "*2\r\n$3\r\nGET\r\n$5\r\napple\r\n"
remote: [stage-7] Received bytes: "$5\r\n$-1\r\n\r\n"
remote: [stage-7] Received RESP value: "$-1\r\n"
remote: [stage-7] Expected null bulk string ("$-1\r\n"), got BULK_STRING
remote: [stage-7] Test failed
remote: [stage-7] Terminating program
remote: [stage-7] Program terminated successfully

And here’s a snippet of my code:


public class Get implements Command {

    @Override
    public void execute(BufferedReader inputStream, OutputStream outputStream) throws IOException {
        int length = Integer.parseInt(inputStream.readLine().substring(1));
        String key = inputStream.readLine();
        String result = InMemoryCache.getValue(key);
        result = (result == null) ? RedisParser.nullBulkStringResponse() : result;
        outputStream.write(
               RedisParser.stringFormatting(result).getBytes(StandardCharsets.UTF_8)
        );
    }
}




The response it expects is the null bulk string:

$-1\r\n

Your logs suggest that the value you are sending is:

"$5\r\n$-1\r\n\r\n"

which is a bulk string wrapping the value $-1\r\n.


It seems this is due to these lines:

result = (result == null) ? RedisParser.nullBulkStringResponse() : result;
outputStream.write(
    RedisParser.stringFormatting(result).getBytes(StandardCharsets.UTF_8)
);

Here you are setting the result as the null bulk string when the key is not found and then in the next line you apply the RedisParser.stringFormatting() function on it, which I am assuming transforms the null string into a bulk string.

Try directly returning the value of RedisParser.nullBulkStringResponse() when the key is not found.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Note: I’ve updated the title of this post to include the stage ID (#YZ1). You can learn about the stages rename here: Upcoming change: Stages overhaul.