Hi, I’m trying to figure out what’s going on but as of now all I can think of is that the tester is trying to start the replica node without having started the master node first. Or else, how should I handle this?
Thanks!
This is my Server.cs code:
using System.Net;
using codecrafters_redis;
using codecrafters_redis.Receivers;
using codecrafters_redis.Servers;
var port = args.Length > 0 && (args[0] == "--port" || args[0] == "-p")
? int.Parse(args[1])
: Constants.DefaultRedisPort;
var masterHost = args.Length > 2 && args[2] == "--replicaof"
? args[3]
: null;
int? masterPort = args.Length > 2 && args[2] == "--replicaof"
? int.Parse(args[4])
: null;
ServerInfo.IsMaster = masterHost == null;
try
{
if (ServerInfo.IsMaster)
{
new MasterNode(IPAddress.Any, port, new MasterReceiver())
.Start();
}
else
{
new ReplicaNode(IPAddress.Any, port, masterHost!, masterPort!.Value, new ReplicaReceiver())
.Handshake()
.Start();
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}, stack: {ex.StackTrace}");
throw;
}
Eventually I’m getting the connection refused error
...
[replication-6] Running tests for Replication > Stage #6: Send handshake (2/3)
[replication-6] Master is running on port 6379
[replication-6] $ ./spawn_redis_server.sh --port 6380 --replicaof localhost 6379
[your_program] [replica-node-6380] Master node: localhost:6379
[replication-6] master: Received bytes: "*1\r\n$4\r\nPING\r\n"
[replication-6] master: Received RESP value: ["PING"]
[replication-6] Received ["PING"]
[replication-6] master: Sent "PONG"
[replication-6] master: Sent bytes: "+PONG\r\n"
[your_program] [replica-node-6380] Sending REPLCONF listening-port 6380
[replication-6] master: Received bytes: "*3\r\n$8\r\nREPLCONF\r\n$14\r\nlistening-port\r\n$4\r\n6380\r\n"
[replication-6] master: Received RESP value: ["REPLCONF", "listening-port", "6380"]
[replication-6] Received ["REPLCONF", "listening-port", "6380"]
[replication-6] master: Sent "OK"
[replication-6] master: Sent bytes: "+OK\r\n"
[replication-6] master: Received bytes: "*3\r\n$8\r\nREPLCONF\r\n$4\r\ncapa\r\n$6\r\npsync2\r\n"
[replication-6] master: Received RESP value: ["REPLCONF", "capa", "psync2"]
[replication-6] Received ["REPLCONF", "capa", "psync2"]
[replication-6] master: Sent "OK"
[replication-6] master: Sent bytes: "+OK\r\n"
[replication-6] Test passed.
[replication-6] Terminating program
[replication-6] Program terminated successfully
[replication-5] Running tests for Replication > Stage #5: Send handshake (1/3)
[replication-5] Master is running on port 6379.
[replication-5] $ ./spawn_redis_server.sh --port 6380 --replicaof localhost 6379
[your_program] [replica-node-6380] Master node: localhost:6379
[replication-5] master: Received bytes: "*1\r\n$4\r\nPING\r\n"
[replication-5] master: Received RESP value: ["PING"]
[replication-5] Received ["PING"]
[replication-5] master: Sent "PONG"
[replication-5] master: Sent bytes: "+PONG\r\n"
[replication-5] Test passed.
[replication-5] Terminating program
[your_program] [replica-node-6380] Sending REPLCONF listening-port 6380
[replication-5] Program terminated successfully
[replication-4] Running tests for Replication > Stage #4: Initial Replication ID and Offset
[replication-4] $ ./spawn_redis_server.sh
[your_program] [replica-node-6380] Master node: localhost:6379
[your_program] Handshake failed: Connection refused [::ffff:127.0.0.1]:6379, stack: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program] at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program] at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program] at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program] at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program] Connection refused [::ffff:127.0.0.1]:6379, stack: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program] at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program] at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program] at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program] at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program] at Program.<Main>$(String[] args) in /app/src/Server.cs:line 29
[your_program] Unhandled exception. System.Net.Sockets.SocketException (111): Connection refused [::ffff:127.0.0.1]:6379
[your_program] at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
[your_program] at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] --- End of stack trace from previous location ---
[your_program] at System.Net.Sockets.Socket.Connect(IPAddress[] addresses, Int32 port)
[your_program] at System.Net.Sockets.Socket.Connect(String host, Int32 port)
[your_program] at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
[your_program] at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
[your_program] at codecrafters_redis.Servers.ReplicaNode.Handshake() in /app/src/Servers/ReplicaNode.cs:line 37
[your_program] at Program.<Main>$(String[] args) in /app/src/Server.cs:line 29
[replication-4] dial tcp [::1]:6379: connect: connection refused
[replication-4] Test failed
[replication-4] Terminating program
[replication-4] Program terminated successfully
```strong text