Copilot commented on code in PR #1622: URL: https://github.com/apache/dubbo-admin/pull/1622#discussion_r4438214992
########## 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) + return list, err +} + +func (w *trackedListerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) { + stream, err := w.ResourceListerWatcher.Watch(options) + if err != nil { + w.health.Store(false) + } + return stream, err +} + +func (d *discoveryComponent) SyncState(mesh string) SyncState { + informers := d.watched[mesh] + if len(informers) == 0 { + return SyncState{} + } + state := SyncState{InitialSyncComplete: true, Healthy: true} + for _, item := range informers { + if !item.informer.HasSynced() || item.informer.IsStopped() { + state.InitialSyncComplete = false + } + if !item.health.Load() || item.informer.IsStopped() { + state.Healthy = false + } + state.Generation += item.generation.Load() + } + for _, normalization := range d.normalizations { + if !normalization.healthy(mesh) { + state.Healthy = false + } + } + return state +} + +type trackedEmitter struct { + events.Emitter + generation *atomic.Uint64 +} + +func (e trackedEmitter) Send(event events.Event) { + e.generation.Add(1) + defer e.generation.Add(1) + e.Emitter.Send(event) +} + +// Normalization must finish before an informer can report synchronized. Errors +// remain unhealthy until the failed Resource is successfully reconciled. +type trackedNormalization struct { + events.Subscriber + mu sync.Mutex + failures map[string]string +} + +func (s *trackedNormalization) AsyncEnabled() bool { return false } + +func (s *trackedNormalization) ProcessEvent(event events.Event) error { + resource := event.NewObj() + if resource == nil { + resource = event.OldObj() + } + err := s.Subscriber.ProcessEvent(event) + if resource != nil { + s.mu.Lock() + if err == nil { + delete(s.failures, resource.ResourceKey()) + } else { + s.failures[resource.ResourceKey()] = resource.ResourceMesh() Review Comment: A transient normalization failure can leave this mesh unhealthy indefinitely. EventBus.Send only logs ProcessEvent errors, so the informer considers the delta processed; discovery also uses ResyncPeriod=0. For an unchanged ZooKeeper registration, a temporary store failure therefore leaves this entry in failures with no scheduled reconciliation after the store recovers. Add a retry/requeue path for failed resources and retain the unhealthy state until that retry succeeds, rather than relying on another registry mutation to replay the event. ########## 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) + return list, err +} + +func (w *trackedListerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) { + stream, err := w.ResourceListerWatcher.Watch(options) + if err != nil { + w.health.Store(false) + } + return stream, err +} + +func (d *discoveryComponent) SyncState(mesh string) SyncState { + informers := d.watched[mesh] + if len(informers) == 0 { + return SyncState{} + } + state := SyncState{InitialSyncComplete: true, Healthy: true} + for _, item := range informers { + if !item.informer.HasSynced() || item.informer.IsStopped() { + state.InitialSyncComplete = false + } + if !item.health.Load() || item.informer.IsStopped() { + state.Healthy = false + } + state.Generation += item.generation.Load() + } + for _, normalization := range d.normalizations { + if !normalization.healthy(mesh) { + state.Healthy = false + } + } + return state +} + +type trackedEmitter struct { + events.Emitter + generation *atomic.Uint64 +} + +func (e trackedEmitter) Send(event events.Event) { + e.generation.Add(1) + defer e.generation.Add(1) + e.Emitter.Send(event) +} + +// Normalization must finish before an informer can report synchronized. Errors +// remain unhealthy until the failed Resource is successfully reconciled. +type trackedNormalization struct { + events.Subscriber + mu sync.Mutex + failures map[string]string +} + +func (s *trackedNormalization) AsyncEnabled() bool { return false } Review Comment: Making these subscribers synchronous introduces a reentrant EventBus lock deadlock. EventBus.Send holds its RWMutex read lock while calling ProcessEvent, and both Nacos and ZK normalization emit follow-up events through Send again. If Subscribe or Unsubscribe requests the write lock between those sends, the nested read lock blocks behind that writer while the writer waits for the outer Send to finish. The informer then cannot progress. Release the bus lock before invoking synchronous callbacks with appropriate subscriber-lifetime handling, or track normalization completion without synchronous re-entry. ########## pkg/discovery/nacos2/listerwatcher/nacos_config.go: ########## @@ -156,7 +169,9 @@ func (lw *ConfigListerWatcher[T]) fetchAndProcessConfigs() error { return err } for _, item := range configPage.PageItems { - lw.processConfig(item) + if err := lw.processConfig(item); err != nil { + return err + } Review Comment: Returning on the first per-config error lets one malformed configuration block reconciliation of unrelated configurations indefinitely. processConfig now returns an error when normalization fails, so a persistently invalid early entry prevents later entries/pages from being processed or subscribed and prevents the removal pass from running on every poll. Continue reconciling the remaining entries and removals while accumulating an error for health reporting; a bad resource should not freeze discovery for the rest of that resource kind. ########## pkg/discovery/nacos2/listerwatcher/nacos_service.go: ########## @@ -134,9 +151,11 @@ func (lw *NacosServiceListerWatcher) fetchAndProcessService() error { if err != nil { return err } - slice.ForEach(serviceNames, func(index int, item string) { - lw.processNacosService(item) - }) + for _, item := range serviceNames { + if err := lw.processNacosService(item); err != nil { + return err + } Review Comment: This early return can permanently starve unrelated services. An application whose revision metadata contains invalid JSON makes resolveRevisions fail on every poll; if it appears before other services, those later services/pages are never reconciled, and vanished services never reach the unsubscribe/delete pass. Preserve the unhealthy result for that failed application, but continue processing the other services and complete removal bookkeeping before returning an aggregated polling error. ########## 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: Refresh re-evaluates identities from every remembered mesh using the current snapshot's resources and single SyncState, but ReadSnapshot and FilterTools supply mesh-scoped snapshots. For example, after mesh A starts an INACTIVE grace period, refreshing an unhealthy mesh B changes A to UNKNOWN; the next A refresh restarts its grace period despite uninterrupted A downtime. Alternating requests can consequently keep A's unavailable tools visible indefinitely. Scope updates and downtime history to the snapshot's mesh and retain other meshes unchanged, or enforce mesh-specific index instances in this API. ########## pkg/mcp/business/arguments.go: ########## @@ -0,0 +1,161 @@ +/* + * 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 business + +import ( + "encoding/json" + "fmt" + "strconv" + + meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1" + "github.com/apache/dubbo-admin/pkg/common/util/genericinvoke" +) + +func RestoreArguments(contract *ResolvedContract, arguments map[string]any) ([]any, error) { + if contract == nil || contract.Operation == nil { + return nil, fmt.Errorf("resolved contract is required") + } + if err := ValidateParameterBindings(contract.Parameters, contract.Operation.ParameterTypes); err != nil { + return nil, err + } + validator, err := CompileInputSchema(contract.InputSchema) + if err != nil { + return nil, err + } + if err := validator.Validate(arguments); err != nil { + return nil, fmt.Errorf("arguments do not match input schema: %w", err) + } + + types := make(map[string]*meshproto.MCPTypeDefinition, len(contract.RelatedTypes)) + for _, typ := range contract.RelatedTypes { + if typ != nil { + types[typ.Type] = typ + } + } + result := make([]any, len(contract.Operation.ParameterTypes)) + for _, binding := range contract.Parameters { + value, exists := arguments[binding.Alias] + if !exists { + return nil, fmt.Errorf("required argument %q is missing", binding.Alias) + } + position := int(binding.Position) + decoded, err := decodeValue(contract.Operation.ParameterTypes[position], value, types) + if err != nil { + return nil, fmt.Errorf("argument %q: %w", binding.Alias, err) + } + result[position] = decoded + } + return result, nil +} + +func decodeValue(typeName string, value any, types map[string]*meshproto.MCPTypeDefinition) (any, error) { + if value == nil { + if isNullableType(typeName) { + return nil, nil + } + return nil, fmt.Errorf("null is not valid for %s", typeName) + } + if isScalarType(typeName) { + if typeName == "long" || typeName == "java.lang.Long" { + text, ok := value.(string) + if !ok { + return nil, fmt.Errorf("expected decimal string for %s", typeName) + } + if _, err := strconv.ParseInt(text, 10, 64); err != nil { + return nil, fmt.Errorf("invalid %s value: %w", typeName, err) + } + return text, nil Review Comment: Return the parsed int64 rather than the original string here. RestoreArguments passes these values unchanged to GenericService.Invoke, including nested DTO fields and collection elements. The pinned dubbo-go provider realizes generic arguments through MapGeneralizer and mapstructure.Decode without weak typing, so a numeric string cannot populate an int64 parameter or field and valid long-bearing calls fail during realization. Decimal strings can remain the JSON input representation, but the RPC arguments need int64 values. -- 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]
