robocanic commented on code in PR #1371: URL: https://github.com/apache/dubbo-admin/pull/1371#discussion_r2635154713
########## pkg/discovery/zk/zkwatcher/watcher_test.go: ########## @@ -0,0 +1,49 @@ +/* + * 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 zkwatcher + +import ( + "fmt" + "testing" + "time" + + "github.com/dubbogo/go-zookeeper/zk" + + "github.com/apache/dubbo-admin/pkg/core/logger" +) + +func TestLocalhost(t *testing.T) { + zkServers := []string{"localhost:2181"} + basePath := "/services" + conn, _, err := zk.Connect(zkServers, time.Second*10) + if err != nil { + logger.Fatalf("Failed to connect to zookeeper: %v", err) + } + watcher := NewRecursiveWatcher(conn, basePath) + + // Start listening + if err := watcher.Start(); err != nil { + logger.Fatalf("Failed to start watching: %v", err) Review Comment: fixed ########## pkg/core/discovery/subscriber/zk_metadata.go: ########## @@ -0,0 +1,150 @@ +/* + * 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 ( + "reflect" + "strings" + + "k8s.io/client-go/tools/cache" + + "github.com/apache/dubbo-admin/pkg/common/bizerror" + "github.com/apache/dubbo-admin/pkg/common/constants" + "github.com/apache/dubbo-admin/pkg/core/events" + "github.com/apache/dubbo-admin/pkg/core/logger" + meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1" + coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model" + "github.com/apache/dubbo-admin/pkg/core/store" +) + +type ZKMetadataEventSubscriber struct { + emitter events.Emitter + storeRouter store.Router +} + +func NewZKMetadataEventSubscriber(eventEmitter events.Emitter, storeRouter store.Router) *ZKMetadataEventSubscriber { + return &ZKMetadataEventSubscriber{ + emitter: eventEmitter, + storeRouter: storeRouter, + } +} + +func (z *ZKMetadataEventSubscriber) ResourceKind() coremodel.ResourceKind { + return meshresource.ZKMetadataKind +} + +func (z *ZKMetadataEventSubscriber) Name() string { + return "Discovery-" + z.ResourceKind().ToString() +} + +func (z *ZKMetadataEventSubscriber) ProcessEvent(event events.Event) error { + newObj, ok := event.NewObj().(*meshresource.ZKMetadataResource) + if !ok && event.NewObj() != nil { + return bizerror.NewAssertionError(reflect.TypeOf(newObj), event.NewObj()) + } + oldObj, ok := event.OldObj().(*meshresource.ZKMetadataResource) + if !ok && event.OldObj() != nil { + return bizerror.NewAssertionError(reflect.TypeOf(oldObj), event.OldObj()) + } + var processErr error + switch event.Type() { + case cache.Added, cache.Updated, cache.Replaced, cache.Sync: + if newObj == nil || newObj.Spec == nil { + errStr := "process zk metadata upsert event, but new obj is nil, skipped processing" + logger.Errorf(errStr) + return bizerror.New(bizerror.EventError, errStr) + } + processErr = z.processUpsert(newObj) + case cache.Deleted: + if oldObj == nil { + errStr := "process zk metadata delete event, but old obj is nil, skipped processing" + logger.Errorf(errStr) + return bizerror.New(bizerror.EventError, errStr) + } + // metadata is an ephemeral znode, dubbo client only adds/updates the metadata znode and never deletes. + // Plus, we can't identify the service only by the node path + logger.Infof("ignored zk metadata delete event") + } + if processErr != nil { + logger.Errorf("process zk metadata event failed, cause: %s, event: %s", processErr.Error(), event.String()) + return processErr + } + logger.Infof("process zk metadata event successfully, event: %s", event.String()) + return nil +} + +func (z *ZKMetadataEventSubscriber) processUpsert(metadataRes *meshresource.ZKMetadataResource) error { + paths := strings.Split(metadataRes.Spec.NodePath, constants.PathSeparator) + if paths[len(paths)-2] == constants.ProviderSide { + return processMetadataUpsert[*meshresource.ServiceProviderMetadataResource]( + metadataRes, meshresource.ToServiceProviderMetadataRes, z.storeRouter, z.emitter) + } else if paths[len(paths)-2] == constants.ConsumerSide { + return processMetadataUpsert[*meshresource.ServiceConsumerMetadataResource]( + metadataRes, meshresource.ToServiceConsumerMetadataByRawData, z.storeRouter, z.emitter) + } Review Comment: fixed -- 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]
