Skip to content

Commit 08c9964

Browse files
committed
更新日志
1 parent 154b156 commit 08c9964

3 files changed

Lines changed: 44 additions & 44 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,21 @@ public async Task<Dictionary<string, bool>> RouteMessagesAsync(
246246
{
247247
try
248248
{
249-
_logger.LogDebug($"开始路由消息到连接 - ConnectionId: {connectionId}, MessageSize: {data.Length} bytes, MessageType: {messageType}");
249+
_logger.LogWarning($"[ClusterManager] 开始路由消息到连接 - ConnectionId: {connectionId}, MessageSize: {data.Length} bytes, MessageType: {messageType}, CurrentNodeId: {_nodeId}");
250250
var success = await RouteMessageAsync(connectionId, data, messageType);
251251
results[connectionId] = success;
252252
if (success)
253253
{
254-
_logger.LogDebug($"成功路由消息到连接 - ConnectionId: {connectionId}");
254+
_logger.LogWarning($"[ClusterManager] 成功路由消息到连接 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}");
255255
}
256256
else
257257
{
258-
_logger.LogWarning($"路由消息到连接失败 - ConnectionId: {connectionId}");
258+
_logger.LogError($"[ClusterManager] 路由消息到连接失败 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}");
259259
}
260260
}
261261
catch (Exception ex)
262262
{
263-
_logger.LogError(ex, $"路由消息到连接时发生异常 - ConnectionId: {connectionId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
263+
_logger.LogError(ex, $"[ClusterManager] 路由消息到连接时发生异常 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
264264
results[connectionId] = false;
265265
}
266266
});

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ 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}");
224+
_logger.LogWarning($"[ClusterRouter] 开始路由消息 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes, MessageType: {messageType}, RoutingTableSize: {_connectionRoutes.Count}");
225225

226226
if (_connectionRoutes.TryGetValue(connectionId, out var targetNodeId))
227227
{
228-
_logger.LogInformation($"[ClusterRouter] 连接在路由表中找到 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}");
228+
_logger.LogWarning($"[ClusterRouter] 连接在路由表中找到 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}");
229229

230230
if (targetNodeId == _nodeId)
231231
{
@@ -269,11 +269,11 @@ public async Task<bool> RouteMessageAsync(string connectionId, byte[] data, int
269269
else
270270
{
271271
// Remote connection - forward via transport / 远程连接 - 通过传输转发
272-
_logger.LogInformation($"Routing message to remote connection {connectionId} on node {targetNodeId} (current node: {_nodeId}), message size: {data.Length} bytes");
272+
_logger.LogWarning($"[ClusterRouter] 远程连接转发 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes");
273273
var result = await ForwardToNodeAsync(targetNodeId, connectionId, data, messageType);
274274
if (!result)
275275
{
276-
_logger.LogWarning($"Failed to forward message to remote connection {connectionId} on node {targetNodeId}");
276+
_logger.LogError($"[ClusterRouter] 远程连接转发失败 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}");
277277
}
278278
return result;
279279
}
@@ -283,20 +283,20 @@ public async Task<bool> RouteMessageAsync(string connectionId, byte[] data, int
283283
// Connection not found - query cluster to find the node / 未找到连接 - 查询集群以找到节点
284284
// 无论是否为 leader,都尝试查询连接位置(因为连接可能在其他节点)
285285
// Whether leader or not, try to query connection location (connection might be on another node)
286-
_logger.LogWarning($"Connection {connectionId} not found in local routing table (current node: {_nodeId}), querying cluster... Available connections in routing table: {_connectionRoutes.Count}");
286+
_logger.LogError($"[ClusterRouter] 连接不在本地路由表中 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, RoutingTableSize: {_connectionRoutes.Count}, 开始查询集群...");
287287
var found = await QueryConnectionAsync(connectionId);
288288
if (found != null)
289289
{
290-
_logger.LogInformation($"Found connection {connectionId} on node {found} after query, forwarding message");
290+
_logger.LogWarning($"[ClusterRouter] 查询成功找到连接 - ConnectionId: {connectionId}, FoundNodeId: {found}, 开始转发消息");
291291
var result = await ForwardToNodeAsync(found, connectionId, data, messageType);
292292
if (!result)
293293
{
294-
_logger.LogWarning($"Failed to forward message to connection {connectionId} on node {found} after query");
294+
_logger.LogError($"[ClusterRouter] 查询后转发失败 - ConnectionId: {connectionId}, FoundNodeId: {found}");
295295
}
296296
return result;
297297
}
298298

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.");
299+
_logger.LogError($"[ClusterRouter] 连接未找到且查询无结果 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, RoutingTableSize: {_connectionRoutes.Count}. 此连接可能未在集群中注册。");
300300
return false;
301301
}
302302
}
@@ -313,23 +313,23 @@ private async Task<bool> ForwardToNodeAsync(string targetNodeId, string connecti
313313
{
314314
try
315315
{
316-
_logger.LogInformation($"开始转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes");
316+
_logger.LogWarning($"[ClusterRouter] 开始转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, MessageSize: {data.Length} bytes");
317317

318318
// 检查目标节点是否可用(如果传输层支持)
319319
// Check if target node is available (if transport supports it)
320320
if (_transport is Transports.WebSocketClusterTransport wsTransport)
321321
{
322322
var isConnected = wsTransport.IsNodeConnected(targetNodeId);
323-
_logger.LogInformation($"节点连接状态检查 - TargetNodeId: {targetNodeId}, IsConnected: {isConnected}");
323+
_logger.LogWarning($"[ClusterRouter] 节点连接状态检查 - TargetNodeId: {targetNodeId}, IsConnected: {isConnected}, ConnectionId: {connectionId}");
324324
if (!isConnected)
325325
{
326-
_logger.LogError($"无法转发消息到节点 {targetNodeId},连接 {connectionId}: 节点未连接");
326+
_logger.LogError($"[ClusterRouter] 无法转发消息 - TargetNodeId: {targetNodeId}, ConnectionId: {connectionId}, 原因: 节点未连接");
327327
return false;
328328
}
329329
}
330330
else
331331
{
332-
_logger.LogDebug($"传输层类型: {_transport.GetType().Name},跳过节点连接状态检查");
332+
_logger.LogWarning($"[ClusterRouter] 传输层类型: {_transport.GetType().Name},跳过节点连接状态检查 - TargetNodeId: {targetNodeId}, ConnectionId: {connectionId}");
333333
}
334334

335335
var forwardMessage = new ForwardWebSocketMessage
@@ -352,25 +352,25 @@ private async Task<bool> ForwardToNodeAsync(string targetNodeId, string connecti
352352
Payload = JsonSerializer.Serialize(forwardMessage)
353353
};
354354

355-
_logger.LogInformation($"准备通过传输层发送消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, MessageId: {uniqueMessageId}");
355+
_logger.LogWarning($"[ClusterRouter] 准备通过传输层发送消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, MessageId: {uniqueMessageId}, CurrentNodeId: {_nodeId}");
356356

357357
await _transport.SendAsync(targetNodeId, message);
358358

359359
// 记录集群消息转发指标
360360
_metricsCollector?.RecordClusterMessageForwarded(_nodeId, targetNodeId);
361361

362-
_logger.LogInformation($"成功转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}");
362+
_logger.LogWarning($"[ClusterRouter] 成功转发消息 - ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}");
363363
return true;
364364
}
365365
catch (InvalidOperationException ex)
366366
{
367-
_logger.LogError(ex, $"转发消息失败(操作无效)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, Error: {ex.Message}");
367+
_logger.LogError(ex, $"[ClusterRouter] 转发消息失败(操作无效)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
368368
_metricsCollector?.RecordError("cluster_forward_failed", _nodeId);
369369
return false;
370370
}
371371
catch (Exception ex)
372372
{
373-
_logger.LogError(ex, $"转发消息失败(异常)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
373+
_logger.LogError(ex, $"[ClusterRouter] 转发消息失败(异常)- ConnectionId: {connectionId}, TargetNodeId: {targetNodeId}, CurrentNodeId: {_nodeId}, Error: {ex.Message}, StackTrace: {ex.StackTrace}");
374374
_metricsCollector?.RecordError("cluster_forward_failed", _nodeId);
375375
return false;
376376
}
@@ -560,7 +560,7 @@ private async Task<bool> ForwardStreamToNodeAsync(
560560
/// <returns>Node ID where connection is located, or null if not found / 连接所在的节点 ID,如果未找到则返回 null</returns>
561561
private async Task<string> QueryConnectionAsync(string connectionId)
562562
{
563-
_logger.LogInformation($"查询连接位置 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, 当前路由表连接数: {_connectionRoutes.Count}");
563+
_logger.LogWarning($"[ClusterRouter] 查询连接位置 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}, 当前路由表连接数: {_connectionRoutes.Count}");
564564

565565
var message = new ClusterMessage
566566
{
@@ -569,7 +569,7 @@ private async Task<string> QueryConnectionAsync(string connectionId)
569569
};
570570

571571
// Broadcast query / 广播查询
572-
_logger.LogDebug($"广播查询消息 - ConnectionId: {connectionId}");
572+
_logger.LogWarning($"[ClusterRouter] 广播查询消息 - ConnectionId: {connectionId}, CurrentNodeId: {_nodeId}");
573573
await _transport.BroadcastAsync(message);
574574

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

582582
if (_connectionRoutes.TryGetValue(connectionId, out var nodeId))
583583
{
584-
_logger.LogInformation($"查询成功 - ConnectionId: {connectionId}, NodeId: {nodeId}, 查询次数: {i + 1}");
584+
_logger.LogWarning($"[ClusterRouter] 查询成功 - ConnectionId: {connectionId}, NodeId: {nodeId}, 查询次数: {i + 1}, CurrentNodeId: {_nodeId}");
585585
return nodeId;
586586
}
587587
}
588588

589-
_logger.LogWarning($"查询失败 - ConnectionId: {connectionId} 在集群中未找到,当前路由表连接数: {_connectionRoutes.Count}");
589+
_logger.LogError($"[ClusterRouter] 查询失败 - ConnectionId: {connectionId} 在集群中未找到,当前路由表连接数: {_connectionRoutes.Count}, CurrentNodeId: {_nodeId}");
590590
return null;
591591
}
592592

@@ -803,16 +803,16 @@ private async Task HandleForwardMessage(ClusterMessage message)
803803
{
804804
try
805805
{
806-
_logger.LogInformation($"[ClusterRouter] 收到转发消息 - MessageId: {message.MessageId}, FromNodeId: {message.FromNodeId}, ToNodeId: {message.ToNodeId}, CurrentNodeId: {_nodeId}");
806+
_logger.LogWarning($"[ClusterRouter] 收到转发消息 - MessageId: {message.MessageId}, FromNodeId: {message.FromNodeId}, ToNodeId: {message.ToNodeId}, CurrentNodeId: {_nodeId}");
807807

808808
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}");
809+
_logger.LogWarning($"[ClusterRouter] 解析转发消息 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, DataSize: {forward.Data?.Length ?? 0} bytes, MessageType: {forward.MessageType}, CurrentNodeId: {_nodeId}");
810810

811811
// If this is the target node, find local WebSocket and send
812812
// 如果这是目标节点,查找本地 WebSocket 并发送
813813
if (forward.TargetNodeId == _nodeId)
814814
{
815-
_logger.LogInformation($"[ClusterRouter] 这是目标节点,处理本地连接 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
815+
_logger.LogWarning($"[ClusterRouter] 这是目标节点,处理本地连接 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
816816

817817
// 记录集群消息接收指标
818818
_metricsCollector?.RecordClusterMessageReceived(forward.TargetNodeId);
@@ -822,7 +822,7 @@ private async Task HandleForwardMessage(ClusterMessage message)
822822
var webSocket = _connectionProvider.GetConnection(forward.ConnectionId);
823823
if (webSocket != null && webSocket.State == WebSocketState.Open)
824824
{
825-
_logger.LogInformation($"[ClusterRouter] 本地连接可用,发送消息 - ConnectionId: {forward.ConnectionId}, WebSocketState: {webSocket.State}");
825+
_logger.LogWarning($"[ClusterRouter] 本地连接可用,发送消息 - ConnectionId: {forward.ConnectionId}, WebSocketState: {webSocket.State}, CurrentNodeId: {_nodeId}");
826826
var wsMessageType = (WebSocketMessageType)forward.MessageType;
827827
var success = await _connectionProvider.SendAsync(
828828
forward.ConnectionId,
@@ -831,30 +831,30 @@ private async Task HandleForwardMessage(ClusterMessage message)
831831

832832
if (success)
833833
{
834-
_logger.LogInformation($"[ClusterRouter] 成功转发消息到本地连接 - ConnectionId: {forward.ConnectionId}");
834+
_logger.LogWarning($"[ClusterRouter] 成功转发消息到本地连接 - ConnectionId: {forward.ConnectionId}, CurrentNodeId: {_nodeId}");
835835
}
836836
else
837837
{
838-
_logger.LogError($"[ClusterRouter] 发送消息到本地连接失败 - ConnectionId: {forward.ConnectionId}");
838+
_logger.LogError($"[ClusterRouter] 发送消息到本地连接失败 - ConnectionId: {forward.ConnectionId}, CurrentNodeId: {_nodeId}");
839839
}
840840
}
841841
else
842842
{
843843
var state = webSocket?.State.ToString() ?? "null";
844-
_logger.LogError($"[ClusterRouter] 本地连接不可用 - ConnectionId: {forward.ConnectionId}, WebSocketState: {state}");
844+
_logger.LogError($"[ClusterRouter] 本地连接不可用 - ConnectionId: {forward.ConnectionId}, WebSocketState: {state}, CurrentNodeId: {_nodeId}");
845845
// Remove stale connection / 删除过时的连接
846846
_connectionRoutes.TryRemove(forward.ConnectionId, out _);
847847
}
848848
}
849849
else
850850
{
851-
_logger.LogError($"[ClusterRouter] 连接提供者未设置 - ConnectionId: {forward.ConnectionId}");
851+
_logger.LogError($"[ClusterRouter] 连接提供者未设置 - ConnectionId: {forward.ConnectionId}, CurrentNodeId: {_nodeId}");
852852
}
853853
}
854854
else if (forward.TargetNodeId != _nodeId)
855855
{
856856
// Not for us - forward again / 不是给我们的 - 再次转发
857-
_logger.LogWarning($"[ClusterRouter] 这不是目标节点,再次转发 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
857+
_logger.LogError($"[ClusterRouter] 这不是目标节点,再次转发 - ConnectionId: {forward.ConnectionId}, TargetNodeId: {forward.TargetNodeId}, CurrentNodeId: {_nodeId}");
858858
await ForwardToNodeAsync(forward.TargetNodeId, forward.ConnectionId, forward.Data, forward.MessageType);
859859
}
860860
}

0 commit comments

Comments
 (0)