AlexStocks commented on code in PR #36: URL: https://github.com/apache/dubbo-net/pull/36#discussion_r4510583477
########## Registry/ProviderDiscovery.cs: ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using DubboNet.Clients.DataModle; +using DubboNet.Clients.RegistryClient; +using MyCommonHelper; +using org.apache.zookeeper; +using org.apache.zookeeper.data; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net; +using System.Threading; +using System.Threading.Tasks; + +namespace DubboNet.Registry +{ + // Owns this client's Nacos provider subscriptions and in-flight discovery. + // The client owns registry leases and its ZooKeeper watcher; the service driver + // alone owns callable endpoint state. Metadata has a separate subscriber. + internal sealed class ProviderDiscovery : IDisposable + { + private readonly MyZookeeper _zookeeper; + private readonly MyNacos _nacos; + private readonly string _rootPath; + private readonly Watcher _watcher; + private readonly Func<string, Task> _onNacosChanged; + private readonly Action _onRegistryRecovered; + private readonly ConcurrentDictionary<(string Service, bool FullPath), Lazy<Task<DubboServiceEndPointInfos>>> _inFlight = new(); + private int _disposed; + + internal ProviderDiscovery(MyZookeeper zookeeper, MyNacos nacos, string rootPath, + Watcher watcher, Func<string, Task> onNacosChanged, Action onRegistryRecovered) + { + if ((zookeeper is null) == (nacos is null)) + throw new ArgumentException("Exactly one Provider registry is required."); + if (string.IsNullOrWhiteSpace(rootPath)) + throw new ArgumentException("Provider registry root cannot be empty.", nameof(rootPath)); + if (zookeeper != null && watcher is null) + throw new ArgumentNullException(nameof(watcher)); + _zookeeper = zookeeper; + _nacos = nacos; + _rootPath = rootPath; + _watcher = watcher; + _onNacosChanged = onNacosChanged ?? throw new ArgumentNullException(nameof(onNacosChanged)); + _onRegistryRecovered = onRegistryRecovered ?? throw new ArgumentNullException(nameof(onRegistryRecovered)); + } + + internal bool IsDisposed => Volatile.Read(ref _disposed) != 0; + + public void Dispose() + { + if (Interlocked.Exchange(ref _disposed, 1) != 0) return; + // Remove only our Nacos Naming subscriptions. Metadata and other + // clients have distinct subscriber identities on the shared connection. + _nacos?.RemoveSubscriber(this); + // ZooKeeper watches are one-shot and have no per-watcher unsubscribe API; + // the owning client watcher checks IsDisposed before handling callbacks. + } + + private Task OnNacosChangedAsync(string serviceName) => + IsDisposed ? Task.CompletedTask : _onNacosChanged(serviceName); + + internal bool TryGetServiceNameFromProviderPath( + string providerPath, + out string serviceName) + { + serviceName = null; + if (string.IsNullOrEmpty(providerPath) + || !providerPath.StartsWith(_rootPath, StringComparison.Ordinal) + || !providerPath.EndsWith(ZooKeeperRegistryPaths.ProvidersSuffix, StringComparison.Ordinal)) + { + return false; + } + + serviceName = providerPath.Substring( + _rootPath.Length, + providerPath.Length - _rootPath.Length - ZooKeeperRegistryPaths.ProvidersSuffix.Length); + return !string.IsNullOrWhiteSpace(serviceName); + } + + internal async Task<DubboServiceEndPointInfos> GetProviderEndPointsAsync(string serviceName, bool isFullPath = false) Review Comment: 🟡 **本 PR 会让 #34 的双语代码地图失效,而两个 PR 都没有承接方** 从本 PR head 交叉核对 #34(`f708e6b`):#34 的 108 个 `.cs` 链接里有 **18 个**指向本 PR 搬走的文件(en/zh 各 9),另有 `CreateMetadataInvocationAsync` / `ConcurrentGetProviderEndPoints` / `GetSeviceProviderEndPointsAsync` 三个方法名在本 PR 后 **0 命中**,而它们在 #34 的架构图、表格和 registry/metadata 页里出现 12 次;#34 的「状态归属(当前代码)」表也把 `Clients/` 下的 `DubboMetadataManager` 写成现状。 **Why:** 两个 PR 的说明都写「后合并者更新链接」,但本 PR 的 32 个文件里没有任何 `Docs/`,PR 下的 issue comment 也明确说 #36 不会把文档工作混入重构。Markdown 相对路径不参与 Git 冲突,build / e2e / license 三个 workflow 都不检查链接,`.licenserc.yaml` ignore 了 `**/*.md`。按现状无论谁先合并,`main` 都会留下死链和过时描述。 **Suggestion:** 合并前把「路径 + 方法名 + 所有权表」的同步作为一个明确 commit 落到其中一个 PR(推荐让 #34 rebase 到本 PR 之后重写),或把两个 PR 合成一个。 ########## Registry/ProviderDiscovery.cs: ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using DubboNet.Clients.DataModle; +using DubboNet.Clients.RegistryClient; +using MyCommonHelper; +using org.apache.zookeeper; +using org.apache.zookeeper.data; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Net; +using System.Threading; +using System.Threading.Tasks; + +namespace DubboNet.Registry +{ + // Owns this client's Nacos provider subscriptions and in-flight discovery. + // The client owns registry leases and its ZooKeeper watcher; the service driver + // alone owns callable endpoint state. Metadata has a separate subscriber. + internal sealed class ProviderDiscovery : IDisposable + { + private readonly MyZookeeper _zookeeper; + private readonly MyNacos _nacos; + private readonly string _rootPath; + private readonly Watcher _watcher; + private readonly Func<string, Task> _onNacosChanged; + private readonly Action _onRegistryRecovered; + private readonly ConcurrentDictionary<(string Service, bool FullPath), Lazy<Task<DubboServiceEndPointInfos>>> _inFlight = new(); + private int _disposed; + + internal ProviderDiscovery(MyZookeeper zookeeper, MyNacos nacos, string rootPath, + Watcher watcher, Func<string, Task> onNacosChanged, Action onRegistryRecovered) + { + if ((zookeeper is null) == (nacos is null)) + throw new ArgumentException("Exactly one Provider registry is required."); + if (string.IsNullOrWhiteSpace(rootPath)) + throw new ArgumentException("Provider registry root cannot be empty.", nameof(rootPath)); + if (zookeeper != null && watcher is null) + throw new ArgumentNullException(nameof(watcher)); + _zookeeper = zookeeper; + _nacos = nacos; + _rootPath = rootPath; + _watcher = watcher; + _onNacosChanged = onNacosChanged ?? throw new ArgumentNullException(nameof(onNacosChanged)); + _onRegistryRecovered = onRegistryRecovered ?? throw new ArgumentNullException(nameof(onRegistryRecovered)); + } + + internal bool IsDisposed => Volatile.Read(ref _disposed) != 0; + + public void Dispose() + { + if (Interlocked.Exchange(ref _disposed, 1) != 0) return; + // Remove only our Nacos Naming subscriptions. Metadata and other + // clients have distinct subscriber identities on the shared connection. + _nacos?.RemoveSubscriber(this); Review Comment: 💭 **订阅者身份变了,`MyNacos.RemoveSubscriber` 的注释没跟上** `Registry/MyNacos.cs` 的 XML 注释仍写「删除某个 **DubboClient** 或元数据管理器在共享 Nacos 客户端上的全部订阅」。本 PR 之后 provider 侧的 subscriber 是 `ProviderDiscovery`。 顺带确认一下这处改动本身是对的:我核过 `main`,以 `DubboClient` 身份订阅 Nacos 的只有 `GetProviderEndPointsAsync` 这一处,`DubboMetadataManager` 用的是自己的 `this`,所以删掉 `DubboClient.Dispose` 里那行 `_innerMyNacos?.RemoveSubscriber(this)`、改由 `ProviderDiscovery.Dispose()` 用自己的身份移除,是对称的,没有留下订阅泄漏。两个 owner 的隔离测试也验证到了点子上。 **Suggestion:** 把该注释改成「某个订阅所有者(DubboClient / ProviderDiscovery / 元数据管理器)」,避免下一个人照注释去找 client 身份。 ########## Clients/DubboClient.cs: ########## @@ -1218,55 +1120,17 @@ private static bool IsNoProviderError(string errorInfo) => && (errorInfo.Contains("no provider", StringComparison.OrdinalIgnoreCase) || errorInfo.Contains("no healthy provider", StringComparison.OrdinalIgnoreCase)); - internal bool TryGetServiceNameFromProviderPath( - string providerPath, - out string serviceName) - { - serviceName = null; - if (string.IsNullOrEmpty(providerPath) - || !providerPath.StartsWith(DubboRootPath, StringComparison.Ordinal) - || !providerPath.EndsWith(ZooKeeperRegistryPaths.ProvidersSuffix, StringComparison.Ordinal)) - { - return false; - } - - serviceName = providerPath.Substring( - DubboRootPath.Length, - providerPath.Length - DubboRootPath.Length - ZooKeeperRegistryPaths.ProvidersSuffix.Length); - return !string.IsNullOrWhiteSpace(serviceName); - } + internal bool TryGetServiceNameFromProviderPath(string providerPath, out string serviceName) => + _providerDiscovery.TryGetServiceNameFromProviderPath(providerPath, out serviceName); - /// <summary> - /// 对GetSeviceProviderEndPointsAsync重复并发的封装(用于应对短时间并行同时对同一个serviceName节点信息进行获取,同时重复获会耗费不必要的性能,这里会复用可复用的Task完成获取任务) - /// EN: Coalesces concurrent provider-discovery calls for the same service into one reusable task. - /// </summary> - /// <param name="serviceName"></param> - /// <param name="isFullPath"></param> - /// <returns></returns> - private async Task<DubboServiceEndPointInfos> ConcurrentGetProviderEndPoints(string serviceName, bool isFullPath = false) - { - Task<DubboServiceEndPointInfos> getProviderEndPointsTask = - _concurrentGetProviderEndPointsTasks.GetOrAdd( - serviceName, - _ => GetSeviceProviderEndPointsAsync(serviceName, isFullPath)); - try - { - return await getProviderEndPointsTask.ConfigureAwait(false); - } - finally - { - _concurrentGetProviderEndPointsTasks.TryRemove( - new KeyValuePair<string, Task<DubboServiceEndPointInfos>>( - serviceName, - getProviderEndPointsTask)); - } - } + private Task<DubboServiceEndPointInfos> DiscoverProvidersAsync(string serviceName, bool isFullPath = false) => Review Comment: 🟡 **`_providerDiscovery` 被 Dispose 置 null,这个转发方法没有兜底** `Dispose(bool)` 在本 PR 的第 1312 行把 `_providerDiscovery = null`,而这里直接解引用。并发交错下(调用方已进入 `QueryInternalAsync` → `DiscoverProvidersAsync`,另一线程完成 Dispose)会抛 `NullReferenceException` 而不是 `ObjectDisposedException`。我核过:`QueryAsync(DubboInvocation)` 入口只有 `ArgumentNullException.ThrowIfNull(invocation)`,没有 `ThrowIfDisposed()`(其它重载在 719/737/755/772 行有),入口检查挡不住这个 TOCTOU。 这不全是本 PR 引入的(`_metadataManager` 早就被置 null),但新字段加入了同一个竞态,而 `ProviderDiscovery` 恰好是唯一带 `IsDisposed` 检查的组件——本来可以给出语义正确的异常。 **Suggestion:** 把 `_providerDiscovery` 声明为 `readonly` 并去掉置空(`ProviderDiscovery.Dispose()` 幂等,持有引用不会阻止 client 被回收),让并发调用落到 `ObjectDisposedException.ThrowIf(IsDisposed, this)`;或在本方法里显式 `ThrowIfDisposed()`。 ########## Tests/UnitTestForDubboNet/MyNacosTest.cs: ########## @@ -104,6 +106,94 @@ await nacos.GetProvidersAsync(Service, Group), } } + [Fact] + public async Task ProviderDiscovery_DisposeRemovesOnlyItsOwnNamingSubscription() + { + var server = new FakeNacos(); + server.Add("1", 20880); + using MyNacos nacos = server.CreateClient(); + int callsA = 0, callsB = 0; + using var first = new ProviderDiscovery(null!, nacos, Group, null!, + _ => { callsA++; return Task.CompletedTask; }, () => { }); + using var second = new ProviderDiscovery(null!, nacos, Group, null!, + _ => { callsB++; return Task.CompletedTask; }, () => { }); + + Assert.Single((await first.GetProviderEndPointsAsync(Service)).EndPoints); + Assert.Single((await second.GetProviderEndPointsAsync(Service)).EndPoints); + Assert.Equal(1, server.NamingSubscribes); + await server.PushNaming(); + Assert.Equal((1, 1), (callsA, callsB)); + + first.Dispose(); + Assert.Single(server.NamingListeners); + await server.PushNaming(); + Assert.Equal((1, 2), (callsA, callsB)); + second.Dispose(); + Assert.Empty(server.NamingListeners); + } + + [Fact] + public async Task ProviderDiscovery_CoalescesConcurrentLookupsAndDoesNotCacheTheResult() + { + var server = new FakeNacos(); + server.Add("1", 20880); + server.InstanceGate = new TaskCompletionSource<List<Instance>>( + TaskCreationOptions.RunContinuationsAsynchronously); + using MyNacos nacos = server.CreateClient(); + using var discovery = new ProviderDiscovery(null!, nacos, Group, null!, + _ => Task.CompletedTask, () => { }); + + Task<DubboServiceEndPointInfos>[] callers = Enumerable.Range(0, 32) + .Select(_ => discovery.GetProviderEndPointsAsync(Service)).ToArray(); + await server.InstanceReadStarted.Task.WaitAsync(TimeSpan.FromSeconds(5)); + Assert.Equal(1, server.InstanceReads); + server.InstanceGate.SetResult(server.Instances[Assert.Single(server.Services)]); + Assert.All(await Task.WhenAll(callers), result => Assert.Single(result.EndPoints)); + Assert.Equal(1, server.InstanceReads); + + Assert.Single((await discovery.GetProviderEndPointsAsync(Service)).EndPoints); + Assert.Equal(2, server.InstanceReads); + } + + [Fact] + public async Task ProviderDiscovery_DisposeDoesNotCancelAnInFlightLookup() + { + var server = new FakeNacos(); + server.Add("1", 20880); + server.InstanceGate = new TaskCompletionSource<List<Instance>>( + TaskCreationOptions.RunContinuationsAsynchronously); + using MyNacos nacos = server.CreateClient(); + using var discovery = new ProviderDiscovery(null!, nacos, Group, null!, + _ => Task.CompletedTask, () => { }); + + Task<DubboServiceEndPointInfos> pending = discovery.GetProviderEndPointsAsync(Service); + await server.InstanceReadStarted.Task.WaitAsync(TimeSpan.FromSeconds(5)); + discovery.Dispose(); + await Assert.ThrowsAsync<ObjectDisposedException>(() => discovery.GetProviderEndPointsAsync(Service)); + server.InstanceGate.SetResult(server.Instances[Assert.Single(server.Services)]); + Assert.Single((await pending).EndPoints); Review Comment: 🟡 **Dispose 后返回的「僵尸端点」会被上层当成可调用状态** 这个测试把「Dispose 不取消在途查询 → 仍返回一条 endpoint(本行)→ 但 `NamingListeners` 已空(下一行)」固化成预期行为。问题在 `DubboClient` 侧没有区分这份结果:`ReflushProviderAsync` / `QueryInternalAsync` 拿到 `ErrorInfo == null` 的 `DubboServiceEndPointInfos` 就会 `AddDubboServiceDriver`——客户端已经 Dispose,却新建了一份「可调用 Provider」快照,而且**永远收不到后续 Provider 变更**:`Dispose()` 调 `RemoveSubscriber(this)` 把 `ProviderDiscovery` 写进 `_removedSubscribers`,`ReconcileNamingSubscriptionsAsync` 对已移除 subscriber 直接 `return false`,订阅被静默跳过,但 `DiscoverAsync` 仍返回 endpoints。 **Why:** 「不取消在途 SDK 调用」本身可以接受,但不能让结果继续进入客户端状态机;否则失败模式从明确的 `ObjectDisposedException` 退化成「静默产生一份不会更新的路由表」。 **Suggestion:** 在 `GetProviderEndPointsAsync` 的 `finally` 之前(`await pending.Value` 之后)再查一次 `IsDisposed`,命中则返回带 `ErrorInfo` 的结果或抛 `ObjectDisposedException`;并把这个测试的断言相应改成「结果被丢弃」,保留订阅清理的断言。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
