Skip to content

Commit 7269b7e

Browse files
committed
chore:集群日志; release version
1 parent 800ba20 commit 7269b7e

4 files changed

Lines changed: 123 additions & 32 deletions

File tree

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/ClusterManager.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,21 @@ public async Task<Dictionary<string, bool>> RouteMessagesAsync(
246246
{
247247
try
248248
{
249+
_logger.LogDebug($"开始路由消息到连接 - ConnectionId: {connectionId}, MessageSize: {data.Length} bytes, MessageType: {messageType}");
249250
var success = await RouteMessageAsync(connectionId, data, messageType);
250251
results[connectionId] = success;
252+
if (success)
253+
{
254+
_logger.LogDebug($"成功路由消息到连接 - ConnectionId: {connectionId}");
255+
}
256+
else
257+
{
258+
_logger.LogWarning($"路由消息到连接失败 - ConnectionId: {connectionId}");
259+
}
251260
}
252261
catch (Exception ex)
253262
{
254-
_logger.LogError(ex, $"Failed to route message to connection {connectionId}");
263+
_logger.LogError(ex, $"路由消息到连接时发生异常 - ConnectionId: {connectionId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
255264
results[connectionId] = false;
256265
}
257266
});

Cyaim.WebSocketServer/Cyaim.WebSocketServer/Infrastructure/Cluster/ClusterRouter.cs

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,23 @@ public async Task UnregisterConnectionAsync(string connectionId)
221221
/// <returns>True if routed successfully, false otherwise / 路由成功返回 true,否则返回 false</returns>
222222
public async Task<bool> RouteMessageAsync(string connectionId, byte[] data, int messageType, Func<string, WebSocket, Task> localHandler)
223223
{
224+
_logger.LogInformation($"[ClusterRouter] 开始路由消息 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes, MessageType: {messageType}, RoutingTableSize: {_connectionRoutes.Count}");
225+
224226
if (_connectionRoutes.TryGetValue(connectionId, out var targetNodeId))
225227
{
228+
_logger.LogInformation($"[ClusterRouter] 连接在路由表中找到 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}");
229+
226230
if (targetNodeId == _nodeId)
227231
{
228232
// Local connection - handle directly / 本地连接 - 直接处理
229-
_logger.LogDebug($"Routing message to local connection {connectionId} on node {_nodeId}");
233+
_logger.LogInformation($"[ClusterRouter] 本地连接处理 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}");
230234

231235
if (_connectionProvider != null)
232236
{
233237
var webSocket = _connectionProvider.GetConnection(connectionId);
234238
if (webSocket != null && webSocket.State == WebSocketState.Open)
235239
{
240+
_logger.LogDebug($"[ClusterRouter] 本地连接可用 - ConnectionId: {connectionId}, WebSocketState: {webSocket.State}");
236241
if (localHandler != null)
237242
{
238243
await localHandler(connectionId, webSocket);
@@ -243,43 +248,55 @@ public async Task<bool> RouteMessageAsync(string connectionId, byte[] data, int
243248
var wsMessageType = (WebSocketMessageType)messageType;
244249
await _connectionProvider.SendAsync(connectionId, data, wsMessageType);
245250
}
251+
_logger.LogInformation($"[ClusterRouter] 本地连接消息发送成功 - ConnectionId: {connectionId}");
246252
return true;
247253
}
248254
else
249255
{
250-
_logger.LogWarning($"Local connection {connectionId} is not available or closed");
256+
var state = webSocket?.State.ToString() ?? "null";
257+
_logger.LogWarning($"[ClusterRouter] 本地连接不可用 - ConnectionId: {connectionId}, WebSocketState: {state}");
251258
// Remove stale connection / 删除过时的连接
252259
_connectionRoutes.TryRemove(connectionId, out _);
253260
return false;
254261
}
255262
}
256263
else
257264
{
258-
_logger.LogWarning($"Connection provider not set, cannot route to local connection {connectionId}");
265+
_logger.LogError($"[ClusterRouter] 连接提供者未设置 - ConnectionId: {connectionId}");
259266
return false;
260267
}
261268
}
262269
else
263270
{
264271
// Remote connection - forward via transport / 远程连接 - 通过传输转发
265-
_logger.LogDebug($"Routing message to remote connection {connectionId} on node {targetNodeId} (current node: {_nodeId})");
266-
return await ForwardToNodeAsync(targetNodeId, connectionId, data, messageType);
272+
_logger.LogInformation($"Routing message to remote connection {connectionId} on node {targetNodeId} (current node: {_nodeId}), message size: {data.Length} bytes");
273+
var result = await ForwardToNodeAsync(targetNodeId, connectionId, data, messageType);
274+
if (!result)
275+
{
276+
_logger.LogWarning($"Failed to forward message to remote connection {connectionId} on node {targetNodeId}");
277+
}
278+
return result;
267279
}
268280
}
269281
else
270282
{
271283
// Connection not found - query cluster to find the node / 未找到连接 - 查询集群以找到节点
272284
// 无论是否为 leader,都尝试查询连接位置(因为连接可能在其他节点)
273285
// Whether leader or not, try to query connection location (connection might be on another node)
274-
_logger.LogDebug($"Connection {connectionId} not found in local routing table, querying cluster...");
286+
_logger.LogWarning($"Connection {connectionId} not found in local routing table (current node: {_nodeId}), querying cluster... Available connections in routing table: {_connectionRoutes.Count}");
275287
var found = await QueryConnectionAsync(connectionId);
276288
if (found != null)
277289
{
278290
_logger.LogInformation($"Found connection {connectionId} on node {found} after query, forwarding message");
279-
return await ForwardToNodeAsync(found, connectionId, data, messageType);
291+
var result = await ForwardToNodeAsync(found, connectionId, data, messageType);
292+
if (!result)
293+
{
294+
_logger.LogWarning($"Failed to forward message to connection {connectionId} on node {found} after query");
295+
}
296+
return result;
280297
}
281298

282-
_logger.LogWarning($"Connection {connectionId} not found in routing table and query returned no result. Available connections in routing table: {_connectionRoutes.Count}");
299+
_logger.LogError($"Connection {connectionId} not found in routing table and query returned no result. Available connections in routing table: {_connectionRoutes.Count}. This connection may not be registered in the cluster.");
283300
return false;
284301
}
285302
}
@@ -296,16 +313,24 @@ private async Task<bool> ForwardToNodeAsync(string targetNodeId, string connecti
296313
{
297314
try
298315
{
316+
_logger.LogInformation($"开始转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes");
317+
299318
// 检查目标节点是否可用(如果传输层支持)
300319
// Check if target node is available (if transport supports it)
301320
if (_transport is Transports.WebSocketClusterTransport wsTransport)
302321
{
303-
if (!wsTransport.IsNodeConnected(targetNodeId))
322+
var isConnected = wsTransport.IsNodeConnected(targetNodeId);
323+
_logger.LogInformation($"节点连接状态检查 - TargetNodeId: {targetNodeId}, IsConnected: {isConnected}");
324+
if (!isConnected)
304325
{
305-
_logger.LogWarning($"Cannot forward message to node {targetNodeId} for connection {connectionId}: node is not connected");
326+
_logger.LogError($"无法转发消息到节点 {targetNodeId},连接 {connectionId}: 节点未连接");
306327
return false;
307328
}
308329
}
330+
else
331+
{
332+
_logger.LogDebug($"传输层类型: {_transport.GetType().Name},跳过节点连接状态检查");
333+
}
309334

310335
var forwardMessage = new ForwardWebSocketMessage
311336
{
@@ -327,19 +352,25 @@ private async Task<bool> ForwardToNodeAsync(string targetNodeId, string connecti
327352
Payload = JsonSerializer.Serialize(forwardMessage)
328353
};
329354

330-
_logger.LogDebug($"Attempting to forward message for connection {connectionId} to node {targetNodeId}, message size: {data.Length} bytes");
355+
_logger.LogInformation($"准备通过传输层发送消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, MessageId: {uniqueMessageId}");
331356

332357
await _transport.SendAsync(targetNodeId, message);
333358

334359
// 记录集群消息转发指标
335360
_metricsCollector?.RecordClusterMessageForwarded(_nodeId, targetNodeId);
336361

337-
_logger.LogDebug($"Successfully forwarded message for connection {connectionId} to node {targetNodeId}");
362+
_logger.LogInformation($"成功转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}");
338363
return true;
339364
}
365+
catch (InvalidOperationException ex)
366+
{
367+
_logger.LogError(ex, $"转发消息失败(操作无效)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, Error: {ex.Message}");
368+
_metricsCollector?.RecordError("cluster_forward_failed", _nodeId);
369+
return false;
370+
}
340371
catch (Exception ex)
341372
{
342-
_logger.LogError(ex, $"Failed to forward message for connection {connectionId} to node {targetNodeId}. Error: {ex.Message}, StackTrace: {ex.StackTrace}");
373+
_logger.LogError(ex, $"转发消息失败(异常)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
343374
_metricsCollector?.RecordError("cluster_forward_failed", _nodeId);
344375
return false;
345376
}
@@ -529,13 +560,16 @@ private async Task<bool> ForwardStreamToNodeAsync(
529560
/// <returns>Node ID where connection is located, or null if not found / 连接所在的节点 ID,如果未找到则返回 null</returns>
530561
private async Task<string> QueryConnectionAsync(string connectionId)
531562
{
563+
_logger.LogInformation($"查询连接位置 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, 当前路由表连接数: {_connectionRoutes.Count}");
564+
532565
var message = new ClusterMessage
533566
{
534567
Type = ClusterMessageType.QueryWebSocketConnection,
535568
Payload = JsonSerializer.Serialize(new { ConnectionId = connectionId })
536569
};
537570

538571
// Broadcast query / 广播查询
572+
_logger.LogDebug($"广播查询消息 - ConnectionId: {connectionId}");
539573
await _transport.BroadcastAsync(message);
540574

541575
// Wait for response with retries / 等待响应,带重试
@@ -547,12 +581,12 @@ private async Task<string> QueryConnectionAsync(string connectionId)
547581

548582
if (_connectionRoutes.TryGetValue(connectionId, out var nodeId))
549583
{
550-
_logger.LogDebug($"Found connection {connectionId} on node {nodeId} after {i + 1} query attempts");
584+
_logger.LogInformation($"查询成功 - ConnectionId: {connectionId}, NodeId: {nodeId}, 查询次数: {i + 1}");
551585
return nodeId;
552586
}
553587
}
554588

555-
_logger.LogWarning($"Connection {connectionId} not found after querying cluster");
589+
_logger.LogWarning($"查询失败 - ConnectionId: {connectionId} 在集群中未找到,当前路由表连接数: {_connectionRoutes.Count}");
556590
return null;
557591
}
558592

@@ -769,22 +803,26 @@ private async Task HandleForwardMessage(ClusterMessage message)
769803
{
770804
try
771805
{
806+
_logger.LogInformation($"[ClusterRouter] 收到转发消息 - MessageId: {message.MessageId}, FromNodeId: {message.FromNodeId}, ToNodeId: {message.ToNodeId}, CurrentNodeId: {_nodeId}");
807+
772808
var forward = JsonSerializer.Deserialize<ForwardWebSocketMessage>(message.Payload);
809+
_logger.LogInformation($"[ClusterRouter] 解析转发消息 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, DataSize: {forward.Data?.Length ?? 0} bytes, MessageType: {forward.MessageType}");
773810

774811
// If this is the target node, find local WebSocket and send
775812
// 如果这是目标节点,查找本地 WebSocket 并发送
776813
if (forward.TargetNodeId == _nodeId)
777814
{
815+
_logger.LogInformation($"[ClusterRouter] 这是目标节点,处理本地连接 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
816+
778817
// 记录集群消息接收指标
779818
_metricsCollector?.RecordClusterMessageReceived(forward.TargetNodeId);
780-
781-
_logger.LogDebug($"Received forward message for local connection {forward.ConnectionId}");
782819

783820
if (_connectionProvider != null)
784821
{
785822
var webSocket = _connectionProvider.GetConnection(forward.ConnectionId);
786823
if (webSocket != null && webSocket.State == WebSocketState.Open)
787824
{
825+
_logger.LogInformation($"[ClusterRouter] 本地连接可用,发送消息 - ConnectionId: {forward.ConnectionId}, WebSocketState: {webSocket.State}");
788826
var wsMessageType = (WebSocketMessageType)forward.MessageType;
789827
var success = await _connectionProvider.SendAsync(
790828
forward.ConnectionId,
@@ -793,34 +831,36 @@ private async Task HandleForwardMessage(ClusterMessage message)
793831

794832
if (success)
795833
{
796-
_logger.LogDebug($"Successfully forwarded message to local connection {forward.ConnectionId}");
834+
_logger.LogInformation($"[ClusterRouter] 成功转发消息到本地连接 - ConnectionId: {forward.ConnectionId}");
797835
}
798836
else
799837
{
800-
_logger.LogWarning($"Failed to send message to local connection {forward.ConnectionId}");
838+
_logger.LogError($"[ClusterRouter] 发送消息到本地连接失败 - ConnectionId: {forward.ConnectionId}");
801839
}
802840
}
803841
else
804842
{
805-
_logger.LogWarning($"Local connection {forward.ConnectionId} not found or closed");
843+
var state = webSocket?.State.ToString() ?? "null";
844+
_logger.LogError($"[ClusterRouter] 本地连接不可用 - ConnectionId: {forward.ConnectionId}, WebSocketState: {state}");
806845
// Remove stale connection / 删除过时的连接
807846
_connectionRoutes.TryRemove(forward.ConnectionId, out _);
808847
}
809848
}
810849
else
811850
{
812-
_logger.LogWarning($"Connection provider not set, cannot handle forward message for {forward.ConnectionId}");
851+
_logger.LogError($"[ClusterRouter] 连接提供者未设置 - ConnectionId: {forward.ConnectionId}");
813852
}
814853
}
815854
else if (forward.TargetNodeId != _nodeId)
816855
{
817856
// Not for us - forward again / 不是给我们的 - 再次转发
857+
_logger.LogWarning($"[ClusterRouter] 这不是目标节点,再次转发 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
818858
await ForwardToNodeAsync(forward.TargetNodeId, forward.ConnectionId, forward.Data, forward.MessageType);
819859
}
820860
}
821861
catch (Exception ex)
822862
{
823-
_logger.LogError(ex, "Error handling forward message");
863+
_logger.LogError(ex, $"[ClusterRouter] 处理转发消息时发生异常 - MessageId: {message.MessageId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
824864
}
825865
}
826866

0 commit comments

Comments
 (0)