You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lo...@apache.org on 2025/07/31 16:40:22 UTC

(beam) branch master updated: Add dataflow option go (#35413)

This is an automated email from the ASF dual-hosted git repository.

lostluck pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new b3c1dd30fb4 Add dataflow option go (#35413)
b3c1dd30fb4 is described below

commit b3c1dd30fb4f1aca5757ab723d090edce489d303
Author: LEEKYE <50...@users.noreply.github.com>
AuthorDate: Thu Jul 31 16:40:15 2025 +0000

    Add dataflow option go (#35413)
    
    * add element_processing_timeout option
    
    * implement element_processing_timeout feature
    
    * use element_processing_timeout from pipeline options in sampler
    
    * Introduce timeout flag in the caller and remove lower bound
    
    * style fix
---
 sdks/go/pkg/beam/core/metrics/sampler.go           | 14 ++++--
 sdks/go/pkg/beam/core/metrics/sampler_test.go      | 53 ++++++++++++++++++++--
 sdks/go/pkg/beam/core/runtime/harness/harness.go   | 22 ++++++++-
 .../pkg/beam/core/runtime/harness/harness_test.go  | 27 +++++++++++
 sdks/go/pkg/beam/core/runtime/harness/sampler.go   | 19 ++++----
 sdks/go/pkg/beam/options/jobopts/options.go        | 15 +++++-
 sdks/go/pkg/beam/options/jobopts/options_test.go   | 16 +++++++
 7 files changed, 146 insertions(+), 20 deletions(-)

diff --git a/sdks/go/pkg/beam/core/metrics/sampler.go b/sdks/go/pkg/beam/core/metrics/sampler.go
index 63968697b45..3b768d579d3 100644
--- a/sdks/go/pkg/beam/core/metrics/sampler.go
+++ b/sdks/go/pkg/beam/core/metrics/sampler.go
@@ -21,6 +21,7 @@ import (
 	"time"
 	"unsafe"
 
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
 )
 
@@ -31,18 +32,19 @@ type StateSampler struct {
 	transitionsAtLastSample   int64
 	nextLogTime               time.Duration
 	logInterval               time.Duration
+	restartLullTimeout        time.Duration
 }
 
 // NewSampler creates a new state sampler.
-func NewSampler(store *Store) StateSampler {
-	return StateSampler{store: store, nextLogTime: 5 * time.Minute, logInterval: 5 * time.Minute}
+func NewSampler(store *Store, elementProcessingTimeout time.Duration) StateSampler {
+	return StateSampler{store: store, nextLogTime: 5 * time.Minute, logInterval: 5 * time.Minute, restartLullTimeout: elementProcessingTimeout}
 }
 
 // Sample checks for state transition in processing a DoFn
-func (s *StateSampler) Sample(ctx context.Context, t time.Duration) {
+func (s *StateSampler) Sample(ctx context.Context, t time.Duration) error {
 	ps := loadCurrentState(s)
 	if ps.pid == "" {
-		return
+		return nil
 	}
 	s.store.mu.Lock()
 	defer s.store.mu.Unlock()
@@ -64,7 +66,11 @@ func (s *StateSampler) Sample(ctx context.Context, t time.Duration) {
 			log.Infof(ctx, "Operation ongoing in transform %v for at least %v ms without outputting or completing in state %v", ps.pid, s.millisSinceLastTransition, getState(ps.state))
 			s.nextLogTime += s.logInterval
 		}
+		if s.restartLullTimeout > 0 && s.millisSinceLastTransition > s.restartLullTimeout {
+			return errors.Errorf("Operation ongoing in transform %v for at least %v ms without outputting or completing in state %v, the SDK harness will be terminated and restarted", ps.pid, s.millisSinceLastTransition, getState(ps.state))
+		}
 	}
+	return nil
 }
 
 // SetLogInterval sets the logging interval for lull reporting.
diff --git a/sdks/go/pkg/beam/core/metrics/sampler_test.go b/sdks/go/pkg/beam/core/metrics/sampler_test.go
index 49f4f658fad..492d2f6748f 100644
--- a/sdks/go/pkg/beam/core/metrics/sampler_test.go
+++ b/sdks/go/pkg/beam/core/metrics/sampler_test.go
@@ -17,6 +17,7 @@ package metrics
 
 import (
 	"context"
+	"strings"
 	"sync/atomic"
 	"testing"
 	"time"
@@ -44,7 +45,7 @@ func TestSampler(t *testing.T) {
 	bctx := SetBundleID(ctx, "test")
 	interval := 200 * time.Millisecond
 	st := GetStore(bctx)
-	s := NewSampler(st)
+	s := NewSampler(st, 0*time.Minute)
 
 	pctx := SetPTransformID(bctx, "transform")
 	label := "transform"
@@ -88,7 +89,7 @@ func TestSampler_TwoPTransforms(t *testing.T) {
 	bctx := SetBundleID(ctx, "bundle")
 	interval := 200 * time.Millisecond
 	st := GetStore(bctx)
-	s := NewSampler(st)
+	s := NewSampler(st, 0*time.Minute)
 
 	ctxA := SetPTransformID(bctx, "transformA")
 	ctxB := SetPTransformID(bctx, "transformB")
@@ -136,6 +137,50 @@ func TestSampler_TwoPTransforms(t *testing.T) {
 	checkBundleState(bctx, t, s, 6, 0)
 }
 
+func TestSamplerWithoutRestartLullTimeout(t *testing.T) {
+	ctx := context.Background()
+	bctx := SetBundleID(ctx, "test")
+	interval := 20 * time.Minute
+	st := GetStore(bctx)
+	s := NewSampler(st, 0*time.Minute)
+
+	pctx := SetPTransformID(bctx, "transform")
+
+	pt := NewPTransformState("transform")
+
+	pt.Set(pctx, StartBundle)
+	if got, want := s.Sample(bctx, interval), error(nil); got != want {
+		t.Errorf("s.sample(bctx, interval) = %v, want %v", got, want)
+	}
+}
+
+func TestSamplerWithRestartLullTimeout(t *testing.T) {
+	ctx := context.Background()
+	bctx := SetBundleID(ctx, "test")
+	interval := 4 * time.Minute
+	st := GetStore(bctx)
+	s := NewSampler(st, 10*time.Minute)
+
+	pctx := SetPTransformID(bctx, "transform")
+
+	pt := NewPTransformState("transform")
+
+	pt.Set(pctx, StartBundle)
+	if got, want := s.Sample(bctx, interval), error(nil); got != want {
+		t.Errorf("s.sample(bctx, interval) = %v, want %v", got, want)
+	}
+	if got, want := s.Sample(bctx, interval), error(nil); got != want {
+		t.Errorf("s.sample(bctx, interval) = %v, want %v", got, want)
+	}
+	if got, want := s.Sample(bctx, interval), error(nil); got != want {
+		t.Errorf("s.sample(bctx, interval) = %v, want %v", got, want)
+	}
+	err := s.Sample(bctx, interval)
+	if err == nil || !strings.Contains(err.Error(), "the SDK harness will be terminated and restarted") {
+		t.Errorf("s.sample(bctx, interval) = %v, want %v", err, "the SDK harness will be terminated and restarted")
+	}
+}
+
 // goos: darwin
 // goarch: amd64
 // pkg: github.com/apache/beam/sdks/v2/go/pkg/beam/core/metrics
@@ -165,7 +210,7 @@ func BenchmarkMsec_Sample(b *testing.B) {
 	pt := NewPTransformState("transform")
 	pt.Set(pctx, StartBundle)
 	st := GetStore(bctx)
-	s := NewSampler(st)
+	s := NewSampler(st, 0*time.Minute)
 	interval := 200 * time.Millisecond
 	for i := 0; i < b.N; i++ {
 		s.Sample(bctx, interval)
@@ -182,7 +227,7 @@ func BenchmarkMsec_Combined(b *testing.B) {
 	bctx := SetBundleID(ctx, "benchmark")
 
 	st := GetStore(bctx)
-	s := NewSampler(st)
+	s := NewSampler(st, 0*time.Minute)
 	done := make(chan int)
 	interval := 200 * time.Millisecond
 	go func(done chan int, s StateSampler) {
diff --git a/sdks/go/pkg/beam/core/runtime/harness/harness.go b/sdks/go/pkg/beam/core/runtime/harness/harness.go
index 20aad81c123..ea2d7dba76f 100644
--- a/sdks/go/pkg/beam/core/runtime/harness/harness.go
+++ b/sdks/go/pkg/beam/core/runtime/harness/harness.go
@@ -410,9 +410,15 @@ func (c *control) handleInstruction(ctx context.Context, req *fnpb.InstructionRe
 
 		data := NewScopedDataManager(c.data, instID)
 		state := NewScopedStateReaderWithCache(c.state, instID, c.cache)
+		timeoutDuration := parseTimeoutDurationFlag(ctx, beam.PipelineOptions.Get("element_processing_timeout"))
 
-		sampler := newSampler(store)
-		go sampler.start(ctx, samplePeriod)
+		sampler := newSampler(store, timeoutDuration)
+		go func() {
+			samplerErr := sampler.start(ctx, samplePeriod)
+			if samplerErr != nil {
+				log.Exitf(ctx, "Failed to sample: %v, the SDK harness will be terminated.", samplerErr)
+			}
+		}()
 
 		err = plan.Execute(ctx, string(instID), exec.DataContext{Data: data, State: state})
 
@@ -692,6 +698,18 @@ func (c *control) handleInstruction(ctx context.Context, req *fnpb.InstructionRe
 	}
 }
 
+// Parses the element_processing_timeout flag and returns the corresponding time.Duration.
+// The element_processing_timeout flag is expected to be a duration string (e.g., "5m", "1h", etc.)or -1.
+// Otherwise, it defaults to no timeout (0 minutes).
+func parseTimeoutDurationFlag(ctx context.Context, elementProcessingTimeout string) time.Duration {
+	userSpecifiedTimeout, err := time.ParseDuration(elementProcessingTimeout)
+	if err != nil {
+		log.Errorf(ctx, "Failed to parse element_processing_timeout: %v, there will be no timeout for processing an element in a PTransform operation", err)
+		return 0 * time.Minute
+	}
+	return userSpecifiedTimeout
+}
+
 // getPlanOrResponse returns the plan for the given instruction id.
 // Otherwise, provides an error response.
 // However, if that plan is known as inactive, it returns both the plan and response as nil,
diff --git a/sdks/go/pkg/beam/core/runtime/harness/harness_test.go b/sdks/go/pkg/beam/core/runtime/harness/harness_test.go
index 8c25db613eb..79ca26e3d2a 100644
--- a/sdks/go/pkg/beam/core/runtime/harness/harness_test.go
+++ b/sdks/go/pkg/beam/core/runtime/harness/harness_test.go
@@ -16,9 +16,11 @@
 package harness
 
 import (
+	"context"
 	"fmt"
 	"strings"
 	"testing"
+	"time"
 
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec"
 	fnpb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/fnexecution_v1"
@@ -229,3 +231,28 @@ func TestCircleBuffer(t *testing.T) {
 		}
 	})
 }
+
+func TestElementProcessingTimeoutParsing(t *testing.T) {
+	ctx := context.Background()
+	if got, want := parseTimeoutDurationFlag(ctx, "5m"), 5*time.Minute; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, "1h"), 1*time.Hour; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, "1m5s"), 1*time.Minute+5*time.Second; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, "5s1m"), 5*time.Second+1*time.Minute; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, "-1"), 0*time.Minute; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, ""), 0*time.Minute; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+	if got, want := parseTimeoutDurationFlag(ctx, "5mmm"), 0*time.Minute; got != want {
+		t.Errorf("parseTimeoutDurationFlag() = %v, want %v", got, want)
+	}
+}
diff --git a/sdks/go/pkg/beam/core/runtime/harness/sampler.go b/sdks/go/pkg/beam/core/runtime/harness/sampler.go
index 5246c1fe69c..0bfe82c9b00 100644
--- a/sdks/go/pkg/beam/core/runtime/harness/sampler.go
+++ b/sdks/go/pkg/beam/core/runtime/harness/sampler.go
@@ -17,9 +17,9 @@ package harness
 
 import (
 	"context"
-	"time"
-
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/metrics"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+	"time"
 )
 
 type stateSampler struct {
@@ -27,21 +27,24 @@ type stateSampler struct {
 	sampler metrics.StateSampler
 }
 
-func newSampler(store *metrics.Store) *stateSampler {
-	return &stateSampler{sampler: metrics.NewSampler(store), done: make(chan int)}
+func newSampler(store *metrics.Store, elementProcessingTimeout time.Duration) *stateSampler {
+	return &stateSampler{sampler: metrics.NewSampler(store, elementProcessingTimeout), done: make(chan int)}
 }
 
-func (s *stateSampler) start(ctx context.Context, t time.Duration) {
+func (s *stateSampler) start(ctx context.Context, t time.Duration) error {
 	ticker := time.NewTicker(t)
 	defer ticker.Stop()
 	for {
 		select {
 		case <-s.done:
-			return
+			return nil
 		case <-ctx.Done():
-			return
+			return nil
 		case <-ticker.C:
-			s.sampler.Sample(ctx, t)
+			err := s.sampler.Sample(ctx, t)
+			if err != nil {
+				return errors.Errorf("Failed to sample: %v", err)
+			}
 		}
 	}
 }
diff --git a/sdks/go/pkg/beam/options/jobopts/options.go b/sdks/go/pkg/beam/options/jobopts/options.go
index 0652fe24e86..04348b1d1ae 100644
--- a/sdks/go/pkg/beam/options/jobopts/options.go
+++ b/sdks/go/pkg/beam/options/jobopts/options.go
@@ -22,9 +22,8 @@ import (
 	"flag"
 	"fmt"
 	"strings"
-	"time"
-
 	"sync/atomic"
+	"time"
 
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/core"
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
@@ -101,6 +100,9 @@ var (
 
 	// ResourceHints flag takes whole pipeline hints for resources.
 	ResourceHints stringSlice
+
+	// Flag to set the timeout for processing an element in a PTransform operation. If set to -1, there is no timeout.
+	ElementProcessingTimeout = flag.Duration("element_processing_timeout", -1, "The timeout for processing an element in a PTransform operation. If set to -1, there is no timeout.")
 )
 
 type missingFlagError error
@@ -179,6 +181,15 @@ func GetExperiments() []string {
 	return strings.Split(*Experiments, ",")
 }
 
+// GetElementProcessingTimeout returns the element processing timeout. If the flag is set to -1,
+// there is no timeout.
+func GetElementProcessingTimeout() time.Duration {
+	if *ElementProcessingTimeout == -1 {
+		return 0 * time.Minute
+	}
+	return *ElementProcessingTimeout
+}
+
 // GetPipelineResourceHints parses known standard hints and returns the flag set hints for the pipeline.
 // In case of duplicate hint URNs, the last value specified will be used.
 func GetPipelineResourceHints() resource.Hints {
diff --git a/sdks/go/pkg/beam/options/jobopts/options_test.go b/sdks/go/pkg/beam/options/jobopts/options_test.go
index dc23be95415..89e2d5a721d 100644
--- a/sdks/go/pkg/beam/options/jobopts/options_test.go
+++ b/sdks/go/pkg/beam/options/jobopts/options_test.go
@@ -20,6 +20,7 @@ import (
 	"reflect"
 	"strings"
 	"testing"
+	"time"
 
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/core"
 	"github.com/apache/beam/sdks/v2/go/pkg/beam/options/resource"
@@ -169,3 +170,18 @@ func TestGetExperiements(t *testing.T) {
 		t.Errorf("GetExperiments(\"\") = %v, want %v", got, want)
 	}
 }
+
+func TestGetElementProcessingTimeout(t *testing.T) {
+	*ElementProcessingTimeout = -1
+	if got, want := GetElementProcessingTimeout(), 0*time.Minute; got != want {
+		t.Errorf("getElementProcessingTimeout() = %v, want %v", got, want)
+	}
+	*ElementProcessingTimeout = 10 * time.Second
+	if got, want := GetElementProcessingTimeout(), 10*time.Second; got != want {
+		t.Errorf("getElementProcessingTimeout() = %v, want %v", got, want)
+	}
+	*ElementProcessingTimeout = 10 * time.Minute
+	if got, want := GetElementProcessingTimeout(), 10*time.Minute; got != want {
+		t.Errorf("getElementProcessingTimeout() = %v, want %v", got, want)
+	}
+}