Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace GameFrameX.Capability.Online.ServerLifecycle.ServerMerge;
/// <summary>
/// 内置参与者:开服锚点合服标记(源服锚点 <c>MergedIntoServerId</c> 置目标服;幂等)。
/// <para>
/// 前置全量校验(dry-run 与真实执行共用):任一源服锚点未登记或已合入其他目标即整体拒绝
/// 前置全量校验(dry-run 与真实执行共用):目标服锚点已合入其他服(已退役)、任一源服锚点未登记或已合入其他目标即整体拒绝
/// (<see cref="OnlineErrorCode.StateOperationForbidden"/>)——dry-run 即暴露执行期失败,
/// 真实执行不会出现部分源服已标记的半途状态。
/// </para>
Expand Down Expand Up @@ -70,6 +70,12 @@ public async Task<OnlineServerMergeStepReport> MergeAsync(OnlineServerMergeConte
{
var details = new Dictionary<string, long>();
long affected = 0;

var targetAnchor = await _calendarStore.FindAsync(context.Plan.TargetServerId, cancellationToken).ConfigureAwait(false);
if (targetAnchor != null && targetAnchor.MergedIntoServerId != 0)
{
throw new OnlineServiceException(OnlineErrorCode.StateOperationForbidden, "Target server " + context.Plan.TargetServerId + " already merged into " + targetAnchor.MergedIntoServerId + "; cannot merge into a retired server.");
}
foreach (var sourceServerId in context.Plan.SourceServerIds)
{
var anchor = await _calendarStore.FindAsync(sourceServerId, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ private static async Task<OnlineServerMergeCliOptions> ParseArgumentsAsync(strin
return null;
}

if (sourceServerId <= 0)
{
await output.WriteLineAsync("ERROR: invalid --sources segment '" + segment + "' (server ids must be positive).").ConfigureAwait(false);
return null;
}

options.SourceServerIds.Add(sourceServerId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace GameFrameX.Capability.Online.ServerLifecycle.ServerMerge;

using System;
using System.Collections.Generic;
using GameFrameX.Capability.Online.Abstractions.Contracts;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -76,17 +76,25 @@ public async Task<OnlineServerMergeStepReport> MergeAsync(OnlineServerMergeConte
{
var counts = await _rewriter.RewriteServerDomainRowsAsync(context.Plan.SourceServerIds, context.Plan.TargetServerId, context.DryRun, _batchSize, cancellationToken).ConfigureAwait(false);
long affected = 0;
long skipped = 0;
foreach (var pair in counts)
{
// skipped 明细是运营对账信息(CAS 失配超限跳过),不计入受影响行数。
if (pair.Key.EndsWith("-skipped", StringComparison.Ordinal))
{
skipped += pair.Value;
continue;
}

affected += pair.Value;
}

if (!context.DryRun && skipped > 0)
{
// 拒绝落定:滞留行留在源服,重试按 HomeServerId == 源服 过滤只补写滞留行。
throw new OnlineServiceException(OnlineErrorCode.StateOperationForbidden, "Row key rewrite skipped " + skipped + " row(s) after CAS retry exhaustion; the merge is not settled. Retry the execute to rewrite the remaining rows.");
}

return new OnlineServerMergeStepReport("row-key-rewrite:server-domain", affected, counts);
}
}
46 changes: 46 additions & 0 deletions Tests/GameFrameX.Tests/Online/OnlineServerLifecycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,52 @@ public async Task Execute_PublishFailureAfterMarkExecuted_ReplayRepublishesWithS
Assert.Single(publisher.PublishedEvents);
}

[Fact]
public async Task MergeAsync_TargetServerAlreadyMerged_RejectedInDryRun()
{
var calendarStore = new InMemoryOnlineServerCalendarStore();
await calendarStore.TryRegisterAsync(new OnlineServerAnchor { ServerId = 1, OpenedAtUnixMilliseconds = 1000 });
await calendarStore.TryRegisterAsync(new OnlineServerAnchor { ServerId = 2, OpenedAtUnixMilliseconds = 2000 });
await calendarStore.MarkMergedAsync(1, 3);

// 服 1 已合入服 3(退役),不可再作为目标服;dry-run 即拒绝。
var participant = new OnlineServerAnchorMergeParticipant(calendarStore);
var exception = await Assert.ThrowsAsync<OnlineServiceException>(() => participant.MergeAsync(new OnlineServerMergeContext(OnlineServerMergePlan.Create(1, new long[] { 2 }, 1), true, 1), CancellationToken.None));
Assert.Equal(OnlineErrorCode.StateOperationForbidden, exception.Code);
}

[Fact]
public async Task MergeAsync_RowRewriteSkippedRows_ThrowsAndRefusesToSettle()
{
var rewriter = new SkippingRowRewriterStub();
var participant = new OnlineServerRowKeyRewriteParticipant(rewriter, 500);

// dry-run 只计数不拒绝;真实执行 skipped > 0 时抛异常,计划不落定。
var dryRunReport = await participant.MergeAsync(new OnlineServerMergeContext(OnlineServerMergePlan.Create(3, new long[] { 1 }, 1), true, 1), CancellationToken.None);
Assert.Equal(0, dryRunReport.AffectedCount);

var exception = await Assert.ThrowsAsync<OnlineServiceException>(() => participant.MergeAsync(new OnlineServerMergeContext(OnlineServerMergePlan.Create(3, new long[] { 1 }, 1), false, 1), CancellationToken.None));
Assert.Equal(OnlineErrorCode.StateOperationForbidden, exception.Code);
}

/// <summary>
/// 恒定返回 skipped 明细的重写端口桩(CAS 重试耗尽场景回归载体)。
/// </summary>
private sealed class SkippingRowRewriterStub : IOnlineServerMergeRowRewriter
{
public Task<IReadOnlyDictionary<string, long>> RewriteServerDomainRowsAsync(IReadOnlyList<long> sourceServerIds, long targetServerId, bool dryRun, int batchSize, CancellationToken cancellationToken = default)
{
IReadOnlyDictionary<string, long> counts = new Dictionary<string, long> { { "player-scope-skipped", 2 } };
return Task.FromResult(counts);
}

public Task<IReadOnlyDictionary<string, long>> CountServerDomainRowsAsync(IReadOnlyList<long> sourceServerIds, CancellationToken cancellationToken)
{
IReadOnlyDictionary<string, long> counts = new Dictionary<string, long> { { "player-scope", 2 } };
return Task.FromResult(counts);
}
}

internal static OnlineServerMergeService BuildMergeService(IOnlineServerMergeStore mergeStore, IOnlineServerCalendarStore calendarStore = null, IOnlinePlayerRouteRewriter routeRewriter = null)
{
calendarStore ??= new InMemoryOnlineServerCalendarStore();
Expand Down
Loading