From eb524f432c2e880c00edb82b0196b39fa778ab25 Mon Sep 17 00:00:00 2001 From: Shiva Sharma Date: Thu, 26 Nov 2026 19:30:31 +0530 Subject: [PATCH] fix(jindo): remove duplicate send on MetadataSyncDoneCh in jindocache and jindofsx Signed-off-by: Shiva Sharma --- pkg/ddc/jindo/metadata.go | 1 - pkg/ddc/jindocache/metadata.go | 1 - pkg/ddc/jindocache/metadata_test.go | 15 ++++++++ pkg/ddc/jindofsx/metadata.go | 1 - pkg/ddc/jindofsx/metadata_test.go | 15 ++++++++ pkg/utils/testutil/metadata_sync.go | 56 +++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 pkg/utils/testutil/metadata_sync.go diff --git a/pkg/ddc/jindo/metadata.go b/pkg/ddc/jindo/metadata.go index 5d29765822d..b080bc9f541 100644 --- a/pkg/ddc/jindo/metadata.go +++ b/pkg/ddc/jindo/metadata.go @@ -161,7 +161,6 @@ func (e *JindoEngine) syncMetadataInternal() (err error) { e.Log.Error(err, "LoadMetadata failed when syncing metadata", "name", e.name, "namespace", e.namespace) result.Err = err result.Done = false - resultChan <- result return }*/ result.Done = true diff --git a/pkg/ddc/jindocache/metadata.go b/pkg/ddc/jindocache/metadata.go index 4cee842298b..a9351b10b0d 100644 --- a/pkg/ddc/jindocache/metadata.go +++ b/pkg/ddc/jindocache/metadata.go @@ -175,7 +175,6 @@ func (e *JindoCacheEngine) syncMetadataInternal() (err error) { if closed := base.SafeSend(resultChan, result); closed { e.Log.Info("Recover from sending result to a closed channel", "result", result) } - resultChan <- result }(e.MetadataSyncDoneCh) } return diff --git a/pkg/ddc/jindocache/metadata_test.go b/pkg/ddc/jindocache/metadata_test.go index f14ef147e3f..c4c64f69b6d 100644 --- a/pkg/ddc/jindocache/metadata_test.go +++ b/pkg/ddc/jindocache/metadata_test.go @@ -24,6 +24,7 @@ import ( datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "github.com/fluid-cloudnative/fluid/pkg/utils/fake" + "github.com/fluid-cloudnative/fluid/pkg/utils/testutil" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -348,3 +349,17 @@ func TestSyncMetadataInternal(t *testing.T) { } } } + +func TestSyncMetadataInternalChannelLifecycle(t *testing.T) { + client := fake.NewFakeClientWithScheme(testScheme, &datav1alpha1.Dataset{ + ObjectMeta: metav1.ObjectMeta{Name: "spark", Namespace: "fluid"}, + }) + engine := &JindoCacheEngine{ + name: "spark", namespace: "fluid", Client: client, Log: fake.NullLogger(), runtime: &datav1alpha1.JindoRuntime{}, + } + if err := testutil.VerifyMetadataSyncChannelClosedOnce(engine.syncMetadataInternal, func() <-chan base.MetadataSyncResult { + return engine.MetadataSyncDoneCh + }); err != nil { + t.Fatal(err) + } +} diff --git a/pkg/ddc/jindofsx/metadata.go b/pkg/ddc/jindofsx/metadata.go index d258e5d3acd..a12316d87da 100644 --- a/pkg/ddc/jindofsx/metadata.go +++ b/pkg/ddc/jindofsx/metadata.go @@ -175,7 +175,6 @@ func (e *JindoFSxEngine) syncMetadataInternal() (err error) { if closed := base.SafeSend(resultChan, result); closed { e.Log.Info("Recover from sending result to a closed channel", "result", result) } - resultChan <- result }(e.MetadataSyncDoneCh) } return diff --git a/pkg/ddc/jindofsx/metadata_test.go b/pkg/ddc/jindofsx/metadata_test.go index c92dddea58f..83883ffca78 100644 --- a/pkg/ddc/jindofsx/metadata_test.go +++ b/pkg/ddc/jindofsx/metadata_test.go @@ -24,6 +24,7 @@ import ( datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" "github.com/fluid-cloudnative/fluid/pkg/ddc/base" "github.com/fluid-cloudnative/fluid/pkg/utils/fake" + "github.com/fluid-cloudnative/fluid/pkg/utils/testutil" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -348,3 +349,17 @@ func TestSyncMetadataInternal(t *testing.T) { } } } + +func TestSyncMetadataInternalChannelLifecycle(t *testing.T) { + client := fake.NewFakeClientWithScheme(testScheme, &datav1alpha1.Dataset{ + ObjectMeta: metav1.ObjectMeta{Name: "spark", Namespace: "fluid"}, + }) + engine := &JindoFSxEngine{ + name: "spark", namespace: "fluid", Client: client, Log: fake.NullLogger(), runtime: &datav1alpha1.JindoRuntime{}, + } + if err := testutil.VerifyMetadataSyncChannelClosedOnce(engine.syncMetadataInternal, func() <-chan base.MetadataSyncResult { + return engine.MetadataSyncDoneCh + }); err != nil { + t.Fatal(err) + } +} diff --git a/pkg/utils/testutil/metadata_sync.go b/pkg/utils/testutil/metadata_sync.go new file mode 100644 index 00000000000..2b9f3bc1a09 --- /dev/null +++ b/pkg/utils/testutil/metadata_sync.go @@ -0,0 +1,56 @@ +/* +Copyright 2026 The Fluid Authors. + +Licensed 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 testutil + +import ( + "errors" + "fmt" + "time" +) + +// VerifyMetadataSyncChannelClosedOnce verifies that an async metadata sync initializes +// its result channel on the first call, receives one result and clears it on a follow-up +// call, and closes the channel cleanly without blocking on a duplicate send. +func VerifyMetadataSyncChannelClosedOnce[T any](syncFn func() error, getCh func() <-chan T) error { + if err := syncFn(); err != nil { + return fmt.Errorf("first sync call failed: %w", err) + } + ch := getCh() + if ch == nil { + return errors.New("expected metadata sync channel to be initialized") + } + + deadline := time.Now().Add(5 * time.Second) + for getCh() != nil { + if err := syncFn(); err != nil { + return fmt.Errorf("follow-up sync call failed: %w", err) + } + if time.Now().After(deadline) { + return errors.New("timed out waiting for metadata sync channel to be cleared") + } + } + + select { + case _, ok := <-ch: + if ok { + return errors.New("unexpected second value sent to metadata sync channel") + } + return nil + case <-time.After(time.Second): + return errors.New("timed out waiting for metadata sync channel to close; goroutine likely blocked on duplicate send") + } +}