github.com/apache/beam/sdks/v2@v2.48.2/go/test/load/sideinput/sideinput.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package main
    17  
    18  import (
    19  	"context"
    20  	"flag"
    21  
    22  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    23  	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/synthetic"
    24  	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
    25  	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
    26  	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
    27  	"github.com/apache/beam/sdks/v2/go/test/load"
    28  )
    29  
    30  func init() {
    31  	register.DoFn4x0[[]byte, []byte, func(*[]byte, *[]byte) bool, func([]byte, []byte)]((*iterSideInputFn)(nil))
    32  	register.Emitter2[[]byte, []byte]()
    33  	register.Iter2[[]byte, []byte]()
    34  	register.Function2x0(impToKV)
    35  }
    36  
    37  var (
    38  	accessPercentage = flag.Int(
    39  		"access_percentage",
    40  		100,
    41  		"Specifies the percentage of elements in the side input to be accessed.")
    42  	syntheticSourceConfig = flag.String(
    43  		"input_options",
    44  		"",
    45  		"A JSON object that describes the configuration for synthetic source")
    46  )
    47  
    48  func parseSyntheticConfig() synthetic.SourceConfig {
    49  	if *syntheticSourceConfig == "" {
    50  		panic("--input_options not provided")
    51  	} else {
    52  		encoded := []byte(*syntheticSourceConfig)
    53  		return synthetic.DefaultSourceConfig().BuildFromJSON(encoded)
    54  	}
    55  }
    56  
    57  // impToKV just turns an impulse signal into a KV instead of
    58  // adding a single value input version of RuntimeMonitor
    59  func impToKV(imp []byte, emit func([]byte, []byte)) {
    60  	emit(imp, imp)
    61  }
    62  
    63  type iterSideInputFn struct {
    64  	ElementsToAccess int64
    65  }
    66  
    67  func (fn *iterSideInputFn) ProcessElement(_, _ []byte, values func(*[]byte, *[]byte) bool, emit func([]byte, []byte)) {
    68  	var key, value []byte
    69  	var i int64
    70  	for values(&key, &value) {
    71  		if i >= fn.ElementsToAccess {
    72  			break
    73  		}
    74  		emit(key, value)
    75  		i++
    76  	}
    77  }
    78  
    79  func main() {
    80  	flag.Parse()
    81  	beam.Init()
    82  	ctx := context.Background()
    83  	p, s := beam.NewPipelineWithRoot()
    84  
    85  	syntheticConfig := parseSyntheticConfig()
    86  	elementsToAccess := syntheticConfig.NumElements * int64(float64(*accessPercentage)/float64(100))
    87  
    88  	src := synthetic.SourceSingle(s, syntheticConfig)
    89  
    90  	imp := beam.Impulse(s)
    91  	impKV := beam.ParDo(s, impToKV, imp)
    92  	monitored := beam.ParDo(s, &load.RuntimeMonitor{}, impKV)
    93  
    94  	useSide := beam.ParDo(
    95  		s,
    96  		&iterSideInputFn{ElementsToAccess: elementsToAccess},
    97  		monitored,
    98  		beam.SideInput{Input: src})
    99  
   100  	beam.ParDo(s, &load.RuntimeMonitor{}, useSide)
   101  
   102  	presult, err := beamx.RunWithMetrics(ctx, p)
   103  	if err != nil {
   104  		log.Fatalf(ctx, "Failed to execute job: %v", err)
   105  	}
   106  
   107  	if presult != nil {
   108  		metrics := presult.Metrics().AllMetrics()
   109  		load.PublishMetrics(metrics)
   110  	}
   111  }