Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: skip publishing the last empty struct when generator stops #2327

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/shared/util/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package util

import "reflect"

// IsZeroStruct checks if a struct is zero value.
func IsZeroStruct(v interface{}) bool {
val := reflect.ValueOf(v)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if !field.IsZero() {
return false
}
}
return true
}
41 changes: 41 additions & 0 deletions pkg/shared/util/struct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import "testing"

func TestIsZeroStruct(t *testing.T) {
type args struct {
v interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "zero_struct",
args: args{v: struct {
Name string
Age int
}{}},
want: true,
},
{
name: "non_zero_struct",
args: args{v: struct {
Name string
Age int
}{
Name: "John",
Age: 25,
}},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsZeroStruct(tt.args.v); got != tt.want {
t.Errorf("IsZeroStruct() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 8 additions & 0 deletions pkg/sources/generator/tickgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1"
"github.com/numaproj/numaflow/pkg/isb"
"github.com/numaproj/numaflow/pkg/shared/logging"
"github.com/numaproj/numaflow/pkg/shared/util"
"github.com/numaproj/numaflow/pkg/sources/sourcer"
)

Expand Down Expand Up @@ -198,6 +199,13 @@ loop:
// we implement Read With Wait semantics
select {
case r := <-mg.srcChan:
// when the channel is closed and empty, go will still read from the channel once and return the zero value.
// exclude the zero value from being passed to the next vertex,
// ensuring all messages produced by generator follow the same data schema.
if util.IsZeroStruct(r) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it zerostruct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I updated the PR description and addressed the comment. Don't need the isZero check, just use the second parameter should work.

mg.logger.Info("Zero value read from the generator channel, skipping...")
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, shall we return instead of continue?

}
msgs = append(msgs, mg.newReadMessage(r.key, r.data, r.offset, r.ts))
case <-timeout:
break loop
Expand Down
Loading