Copilot commented on code in PR #1621:
URL: https://github.com/apache/dubbo-admin/pull/1621#discussion_r4438213584


##########
pkg/core/discovery/subscriber/nacos_availability.go:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.
+ */
+
+package subscriber
+
+import (
+       "encoding/json"
+       "fmt"
+       "reflect"
+       "strings"
+
+       "k8s.io/client-go/tools/cache"
+
+       meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/common/constants"
+       "github.com/apache/dubbo-admin/pkg/core/events"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/core/store/index"
+)
+
+func nacosRPCInstance(service *meshresource.NacosServiceResource, instance 
*meshproto.NacosInstance) *meshresource.RPCInstanceResource {
+       res := meshresource.ToRPCInstance(service.Mesh, service.Name, 
instance.Ip, instance.Port, instance.Metadata)
+       available := instance.Healthy && instance.Enabled && instance.Weight > 0
+       res.Spec.Available = &available
+       return res
+}
+
+func (n *NacosServiceEventSubscriber) reconcileNacosMetadata(service 
*meshresource.NacosServiceResource) error {
+       next := make(map[string]*meshresource.RPCInstanceMetadataResource)
+       prefix := service.Name + "@"
+       if strings.HasPrefix(service.Name, "providers:") {
+               prefix = "interface:" + service.Name + ":"
+               exported, err := nacosInterfaceExports(service)
+               if err != nil {
+                       return err
+               }
+               next = exported
+       } else if service.Spec != nil {
+               for _, spec := range service.Spec.Revisions {
+                       if spec == nil || spec.App != service.Name || 
spec.Revision == "" {
+                               continue

Review Comment:
   resolveRevisions resolves the application from instance metadata or URL 
parameters, but this condition discards its result whenever that application 
differs from the Nacos service name. The new revision test already uses this 
case: service com.example.OrderService with application orders. 
nacosRPCInstance also sets AppName to the service name, so the resulting 
instance can never join the fetched revision and availability remains UNKNOWN. 
Please use the same resolved application identity on both Resources, retaining 
service-name fallback and correct reconciliation/deletion ownership.



##########
pkg/core/discovery/sync.go:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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.
+ */
+
+package discovery
+
+import (
+       "sync"
+       "sync/atomic"
+
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/runtime"
+       "k8s.io/apimachinery/pkg/watch"
+       "k8s.io/client-go/tools/cache"
+
+       "github.com/apache/dubbo-admin/pkg/core/controller"
+       "github.com/apache/dubbo-admin/pkg/core/events"
+)
+
+type SyncState struct {
+       InitialSyncComplete bool
+       Healthy             bool
+       Generation          uint64
+}
+
+type watchedInformer struct {
+       informer   controller.Informer
+       health     atomic.Bool
+       generation atomic.Uint64
+}
+
+type trackedListerWatcher struct {
+       controller.ResourceListerWatcher
+       health *atomic.Bool
+}
+
+func (w *trackedListerWatcher) Close() {
+       if closer, ok := w.ResourceListerWatcher.(interface{ Close() }); ok {
+               closer.Close()
+       }
+}
+
+func (w *trackedListerWatcher) List(options metav1.ListOptions) 
(runtime.Object, error) {
+       list, err := w.ResourceListerWatcher.List(options)
+       w.health.Store(err == nil)

Review Comment:
   On recovery, a successful List marks the mesh healthy before the reflector 
has processed that list's replacement deltas and normalization. HasSynced 
remains true after the initial synchronization, and generation advances only 
when an event is processed, so ReadSnapshot can observe identical healthy 
states around stale or partially reconciled Resources and treat them as 
confirmed protocol evidence. Please keep this list generation incomplete until 
its queued reconciliation finishes, and include relist boundaries in the 
generation tracking.



##########
pkg/core/discovery/subscriber/zk_metadata.go:
##########
@@ -191,3 +265,50 @@ func recordMetadataPlatformEvent(router store.Router, res 
coremodel.Resource, ac
                })
        }
 }
+
+func zkInterfaceRegistration(mesh, path string) 
*meshresource.RPCInstanceMetadataResource {
+       parts := strings.Split(path, constants.PathSeparator)
+       if len(parts) != 5 || parts[1] != "dubbo" || parts[3] != "providers" {
+               return nil
+       }
+       decoded, err := url.PathUnescape(parts[4])
+       if err != nil {
+               return nil
+       }
+       endpoint, err := url.Parse(decoded)
+       if err != nil || endpoint.Hostname() == "" || endpoint.Scheme == "" {
+               return nil
+       }
+       port, err := strconv.ParseInt(endpoint.Port(), 10, 64)
+       if err != nil || port <= 0 {
+               return nil
+       }
+       app := endpoint.Query().Get(constants.Application)
+       service := parts[2]
+       if service == "" {
+               return nil
+       }
+       group, version := endpoint.Query().Get(constants.GroupKey), 
endpoint.Query().Get(constants.VersionKey)
+       available := endpoint.Query().Get("disabled") != "true" && 
endpoint.Query().Get("enabled") != "false"
+       if weight := endpoint.Query().Get("weight"); weight != "" {
+               value, err := strconv.ParseFloat(weight, 64)
+               if err != nil {
+                       return nil
+               }
+               available = available && value > 0
+       }
+       name := fmt.Sprintf("interface:%s:%s:%s:%d:%s:%s:%s",
+               service, app, endpoint.Hostname(), port, group, version, 
endpoint.Scheme)

Review Comment:
   Different provider znodes can produce the same key here: changing only 
enabled, weight, timeout, or another URL parameter leaves this name unchanged. 
If the replacement registration is upserted before the old znode's deletion is 
processed, processApplicationMetadataDelete deletes the replacement's Resource 
too. A live provider then disappears from availability evidence until another 
relist. Please give each source znode a distinct normalized key, such as a 
digest of its full path, or reconcile all source registrations before deleting 
the shared Resource.



##########
pkg/discovery/zk/zkwatcher/watcher.go:
##########
@@ -35,202 +42,223 @@ const (
        NodeChanged EventType = "NodeChanged"
 )
 
-// ZookeeperEvent represents a zookeeper event
 type ZookeeperEvent struct {
-       Type EventType
-       // node path
-       Path string
-       // node data
-       Data string
-       // whether it is a leaf node
+       Type     EventType
+       Path     string
+       Data     string
        LeafNode bool
 }
 
-// RecursiveWatcher recursive watcher
+var errStopped = errors.New("zookeeper watcher stopped")
+
+// RecursiveWatcher owns one snapshot and the watches installed while reading 
it.
+// A topology change invalidates the snapshot so the reflector can reconcile 
deletes.
 type RecursiveWatcher struct {
-       conn      *zk.Conn
+       conn      Connection
        basePath  string
        eventChan chan ZookeeperEvent
+       errorChan chan error
        stopChan  chan struct{}
+       doneChan  chan struct{}
        mu        sync.Mutex
+       started   bool
+       stopped   bool
+       err       error
+       workers   sync.WaitGroup
+       snapshot  []ZookeeperEvent
 }
 
-// NewRecursiveWatcher create a new recursive watcher
-func NewRecursiveWatcher(conn *zk.Conn, basePath string) *RecursiveWatcher {
-       return &RecursiveWatcher{
+func NewRecursiveWatcher(conn Connection, basePath string) *RecursiveWatcher {
+       rw := &RecursiveWatcher{
                conn:      conn,
                basePath:  basePath,
                eventChan: make(chan ZookeeperEvent, 1000),
+               errorChan: make(chan error, 1),
                stopChan:  make(chan struct{}),
+               doneChan:  make(chan struct{}),
        }
+       go func() {
+               <-rw.stopChan
+               rw.workers.Wait()
+               close(rw.eventChan)
+               close(rw.errorChan)
+               close(rw.doneChan)
+       }()
+       return rw
 }
 
-// StartAsync begin watching
+// StartAsync reads the entire tree before returning, with watches already 
armed.
+// Each RecursiveWatcher may only be started once.
 func (rw *RecursiveWatcher) StartAsync() error {
-       logger.Infof("Start watching path: %s", rw.basePath)
-       // Recursively watch the initial path
-       return rw.watchPathRecursively(rw.basePath)
+       rw.mu.Lock()
+       if rw.stopped {
+               rw.mu.Unlock()
+               return errStopped
+       }
+       if rw.started {
+               rw.mu.Unlock()
+               return errors.New("zookeeper watcher already started")
+       }
+       rw.started = true
+       // Keep the counter positive until every initial worker has been 
registered.
+       rw.workers.Add(1)
+       rw.mu.Unlock()
+       defer rw.workers.Done()
+
+       if err := rw.watchPathRecursively(rw.basePath); err != nil {
+               rw.cancel(err)
+               return err
+       }
+       if err := rw.checkConnection(); err != nil {
+               rw.cancel(err)
+               return err
+       }
+       rw.workers.Add(1)
+       go rw.monitorConnection()
+       return rw.Err()
 }
 
-// Stop watching
 func (rw *RecursiveWatcher) Stop() {
-       logger.Infof("Stop watching path: %s", rw.basePath)
+       rw.cancel(nil)
+       <-rw.doneChan
+}
+
+func (rw *RecursiveWatcher) cancel(err error) {
+       rw.mu.Lock()
+       defer rw.mu.Unlock()
+       if rw.stopped {
+               return
+       }
+       rw.stopped = true
+       rw.err = err
+       if err != nil {
+               rw.errorChan <- err
+       }
        close(rw.stopChan)
-       close(rw.eventChan)
 }
 
-// EventChan return event channel
+func (rw *RecursiveWatcher) Err() error {
+       rw.mu.Lock()
+       defer rw.mu.Unlock()
+       if rw.err != nil {
+               return rw.err
+       }
+       if rw.stopped {
+               return errStopped
+       }
+       return nil
+}
+
+// Snapshot must be called after StartAsync returns successfully.
+func (rw *RecursiveWatcher) Snapshot() []ZookeeperEvent {
+       return rw.snapshot
+}
+
 func (rw *RecursiveWatcher) EventChan() <-chan ZookeeperEvent {
        return rw.eventChan
 }
 
-// watchPathRecursively watch path recursively
-func (rw *RecursiveWatcher) watchPathRecursively(path string) error {
-       // Watch data changes for the current path
-       go rw.watchDataChanges(path)
+func (rw *RecursiveWatcher) ErrorChan() <-chan error {
+       return rw.errorChan
+}
 
-       // Get children of current path and watch
-       children, _, err := rw.conn.Children(path)
-       if err != nil {
-               logger.Errorf("Failed to get children of path: %s, cause: %v", 
path, err)
+func (rw *RecursiveWatcher) watchPathRecursively(path string) error {
+       if err := rw.Err(); err != nil {
                return err
        }
-       // Watch children list changes
-       go rw.watchChildrenChanges(path)
+       data, stat, dataWatch, err := rw.conn.GetW(path)
+       if err != nil {
+               return fmt.Errorf("GetW %s: %w", path, err)

Review Comment:
   A missing base znode is treated as a failed discovery snapshot indefinitely. 
Valid interface-only ZooKeeper registries need not contain /services, 
/dubbo/mapping, or /dubbo/config, but the factory starts watchers for all three 
and SyncState requires every informer to synchronize. Consequently, those 
meshes remain UNKNOWN even when their provider registrations were read 
successfully, and confirmed-down tools never age out. Please handle an absent 
optional root as an empty synchronized snapshot while watching for its 
creation, rather than treating zk.ErrNoNode as a registry outage.



##########
pkg/mcp/availability/index.go:
##########
@@ -0,0 +1,320 @@
+/*
+ * 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.
+ */
+
+package availability
+
+import (
+       "sort"
+       "strings"
+       "sync"
+       "time"
+
+       meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/core/discovery"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+)
+
+type State string
+
+const (
+       Active   State = "ACTIVE"
+       Inactive State = "INACTIVE"
+       Unknown  State = "UNKNOWN"
+)
+
+type Identity struct {
+       Mesh        string
+       ServiceName string
+       Group       string
+       Version     string
+}
+
+type SyncState = discovery.SyncState
+
+type Evidence struct {
+       Complete  bool
+       Available bool
+       Protocols []string
+}
+
+type Result struct {
+       Identity      Identity
+       Protocol      string
+       State         State
+       InactiveSince time.Time
+       Evidence      Evidence
+}
+
+type Snapshot struct {
+       // Identities includes published bindings even after their discovery 
Resources disappear.
+       Identities []Identity
+       Services   []*meshresource.ServiceResource
+       Providers  []*meshresource.ServiceProviderMetadataResource
+       Instances  []*meshresource.RPCInstanceResource
+       Metadata   []*meshresource.RPCInstanceMetadataResource
+       Sync       SyncState
+       Now        time.Time
+}
+
+type LiveServiceIndex struct {
+       grace time.Duration
+       mu    sync.Mutex
+       items map[string]Result
+}
+
+func NewLiveServiceIndex(grace time.Duration) *LiveServiceIndex {
+       if grace <= 0 {
+               grace = 60 * time.Second
+       }
+       return &LiveServiceIndex{grace: grace, items: make(map[string]Result)}
+}
+
+func (i *LiveServiceIndex) Refresh(snapshot Snapshot) []Result {
+       now := snapshot.Now
+       if now.IsZero() {
+               now = time.Now()
+       }
+       identities := make(map[Identity]struct{})
+       for _, identity := range snapshot.Identities {
+               identities[identity] = struct{}{}
+       }
+       for _, service := range snapshot.Services {
+               if service != nil && service.Spec != nil {
+                       identities[Identity{
+                               Mesh: service.Mesh, ServiceName: 
service.Spec.Name,
+                               Group: service.Spec.Group, Version: 
service.Spec.Version,
+                       }] = struct{}{}
+               }
+       }
+       for _, provider := range snapshot.Providers {
+               if provider != nil && provider.Spec != nil {
+                       identities[Identity{
+                               Mesh: provider.Mesh, ServiceName: 
provider.Spec.ServiceName,
+                               Group: provider.Spec.Group, Version: 
provider.Spec.Version,
+                       }] = struct{}{}
+               }
+       }
+
+       i.mu.Lock()
+       defer i.mu.Unlock()
+       for _, previous := range i.items {
+               identities[previous.Identity] = struct{}{}
+       }

Review Comment:
   ReadSnapshot is mesh-scoped, but this loop re-evaluates identities retained 
from every mesh using the current snapshot's Resources and single SyncState. 
For example, an unhealthy mesh-B tools/list resets a confirmed-down mesh-A 
entry to UNKNOWN; the next mesh-A request starts its grace period again. 
Alternating requests can therefore keep a down tool listed indefinitely. Please 
refresh only the addressed mesh and preserve other meshes' entries, or 
explicitly scope each index instance to one mesh.



-- 
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]

Reply via email to