Actually , when i am trying to figure out stuff, i thought the rdb_file is going to sent after handling of +FULLRESYNC by replica , but i am getting the rdb_file along with it , for example:
"+FULLRESYNC 75cd7bc10c49047e0d163660f3b90625b1af31dc 0\r\n$88\r\nREDIS001
till now i am executing the receive_rdb afte this psync_response, now i have seen this is occuring i am parsing here itself
the code i am using
#-----------------------------------------------------------------------
case :gen_tcp.recv(socket, 0, :infinity) do
{:ok, data} →
Logger.debug(“Received PSYNC response: #{inspect(data)}”)
case parse_psync_response(data) do
{:ok, repl_id, offset, remaining_data} →
Logger.info(“PSYNC successful. Replication ID: #{repl_id}, Offset: #{offset}, Remaining Data: #{remaining_data}”)
case parse_data(remaining_data, 0) do
{:rdb_complete, rdb_data} →
Logger.info(“Received complete RDB file of size #{byte_size(rdb_data)} bytes”)
{:commands, commands} →
Enum.each(commands, fn command →
Logger.info(“Executing command: #{inspect(command)}”)
execute_set_command(command)
end)
{:error, reason} →
Logger.error(“Failed to receive RDB file: #{inspect(reason)}”)
end
case receive_rdb_file(socket) do
{:ok, :rdb_complete, rdb_data} →
process_rdb_data(rdb_data)
{:ok, :commands, commands} →
Enum.each(commands, fn command →
Logger.info(“Executing command: #{inspect(command)}”)
execute_set_command(command)
end)
{:error, reason} →
Logger.error(“Failed to receive RDB file: #{inspect(reason)}”)
end
:ok
{:error, reason} →
Logger.warning(“Error parsing PSYNC response: #{reason}”)
{:error, reason}
end
#----------------------------------------------------------------------
but sometimes, when i am not getting that extra data along with response, means when i am getting response like this
+FULLRESYNC 75cd7bc10c49047e0d163660f3b90625b1af31dc 0\r\n
i may think to execute receive_data function
#-----------------------------------------------------
defp receive_data(socket, buffer, expected_length) do
Logger.debug(“Receiving data. Buffer size: #{byte_size(buffer)}, Expected length: #{expected_length}”)
case :gen_tcp.recv(socket, 0, 5000) do
{:ok, data} →
Logger.debug(“Received raw bytes: #{inspect(data, limit: :infinity, binaries: :as_binaries)}”)
Logger.debug(“Received chunk of size: #{byte_size(data)} bytes”)
Logger.debug(“Received data (as string): #{inspect(data, charlists: :as_lists)}”)
new_buffer = buffer <> data
Logger.debug(“New buffer size: #{byte_size(new_buffer)} bytes”)
case parse_data(new_buffer, expected_length) do
{:continue, remaining, new_expected_length} →
Logger.info(“Incomplete data, continuing to receive. New expected length: #{new_expected_length}”)
receive_data(socket, remaining, new_expected_length)
{:rdb_complete, rdb_data} →
Logger.info(“Received complete RDB file of size #{byte_size(rdb_data)} bytes”)
{:ok, :rdb_complete, rdb_data}
{:commands, commands} →
Logger.info(“Received complete command: #{inspect(commands)}”)
# case command do
# [_command | args] →
# execute_set_command(args)
# end
{:ok, :commands, commands}
end
{:error, reason} →
Logger.error(“Error receiving data: #{inspect(reason)}”)
{:error, reason}
end
end
Actually i am new to elixir, so learning things is taking more time to figure out , and thanks for the suggestion of printing raw data , it has given me to figure out that extra data is carrying along with psync response,
before the test has failing 7/10 times, now it is failing 3/10 times i still need to make accurate .