lostluck commented on code in PR #29590:
URL: https://github.com/apache/beam/pull/29590#discussion_r1418070997


##########
sdks/go/pkg/beam/core/runtime/exec/datasampler.go:
##########
@@ -0,0 +1,156 @@
+// 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 exec
+
+import (
+       "context"
+       "sync"
+       "time"
+)
+
+// DataSample contains property for sampled element
+type DataSample struct {
+       PCollectionID string
+       Timestamp     time.Time
+       Element       []byte
+}
+
+// DataSampler manages sampled elements based on PCollectionID
+type DataSampler struct {
+       sampleChannel chan *DataSample
+       samplesMap    sync.Map // Key: PCollectionID string, Value: 
*OutputSamples pointer
+       ctx           context.Context
+}
+
+// NewDataSampler inits a new Data Sampler object and returns pointer to it.
+func NewDataSampler(ctx context.Context) *DataSampler {
+       return &DataSampler{
+               sampleChannel: make(chan *DataSample, 1000),
+               ctx:           ctx,
+       }
+}
+
+// Process processes sampled element.
+func (d *DataSampler) Process() {
+       for {
+               select {
+               case <-d.ctx.Done():
+                       return
+               case sample := <-d.sampleChannel:
+                       d.addSample(sample)
+               }
+       }
+}
+
+// GetSamples returns samples for given pCollectionID.
+// If no pCollectionID is provided, return all available samples
+func (d *DataSampler) GetSamples(pids []string) map[string][]*DataSample {
+       if len(pids) == 0 {
+               return d.getAllSamples()
+       }
+       return d.getSamplesForPCollections(pids)
+}
+
+// SendSample is called by PCollection Node to send sampled element to Data 
Sampler async
+func (d *DataSampler) SendSample(pCollectionID string, element []byte, 
timestamp time.Time) {
+       sample := DataSample{
+               PCollectionID: pCollectionID,
+               Element:       element,
+               Timestamp:     timestamp,
+       }
+       d.sampleChannel <- &sample
+}
+
+func (d *DataSampler) getAllSamples() map[string][]*DataSample {
+       var res = make(map[string][]*DataSample)
+       d.samplesMap.Range(func(key any, value any) bool {
+               pid := key.(string)
+               samples := d.getSamples(pid)
+               if len(samples) > 0 {
+                       res[pid] = samples
+               }
+               return true
+       })
+       return res
+}
+
+func (d *DataSampler) getSamplesForPCollections(pids []string) 
map[string][]*DataSample {
+       var res = make(map[string][]*DataSample)
+       for _, pid := range pids {
+               samples := d.getSamples(pid)
+               if len(samples) > 0 {
+                       res[pid] = samples
+               }
+       }
+       return res
+}
+
+func (d *DataSampler) addSample(sample *DataSample) {
+       p, ok := d.samplesMap.Load(sample.PCollectionID)
+       if !ok {
+               p = &outputSamples{maxElements: 10, numSamples: 0, sampleIndex: 
0}
+               d.samplesMap.Store(sample.PCollectionID, p)
+       }
+       outputSamples := p.(*outputSamples)
+       outputSamples.addSample(sample)
+}
+
+func (d *DataSampler) getSamples(pCollectionID string) []*DataSample {
+       p, ok := d.samplesMap.Load(pCollectionID)
+       if !ok {
+               return nil
+       }
+       outputSamples := p.(*outputSamples)
+       return outputSamples.getSamples()
+}
+
+type outputSamples struct {
+       elements    []*DataSample
+       mu          sync.Mutex
+       maxElements int
+       numSamples  int

Review Comment:
   Per the code below, it is.
   
   In `addSample`, until there are maxElements, we append to the slice. When 
it's at max, we turn it into a circular buffer with the sampleIndex.
   
   When getSamples is called, we return the slice, but then zero and nil 
everything anyways.
   
   So, as a result, your code is already repeatedly creating the `elements 
[]*dataSample` sample slice. I'll note that this is fine and expected at the 
moment.



##########
sdks/go/pkg/beam/core/runtime/exec/translate.go:
##########
@@ -411,11 +413,11 @@ func (b *builder) makePCollection(id string) 
(*PCollection, error) {
 }
 
 func (b *builder) newPCollectionNode(id string, out Node) (*PCollection, 
error) {
-       ec, _, err := b.makeCoderForPCollection(id)
+       ec, wc, err := b.makeCoderForPCollection(id)
        if err != nil {
                return nil, err
        }
-       u := &PCollection{UID: b.idgen.New(), Out: out, PColID: id, Coder: ec, 
Seed: rand.Int63()}
+       u := &PCollection{UID: b.idgen.New(), Out: out, PColID: id, Coder: ec, 
WindowCoder: wc, Seed: rand.Int63(), dataSampler: b.dataSampler}

Review Comment:
   Scratch that: Due to how the SDK is implemented, we currently never sample 
anything from the DataSource with your code. We simply count and size all the 
elements since we have the raw bytes already.
   
   
https://github.com/apache/beam/blob/5c6955ad00b2fdef412b4b7325b94ac78db1dc20/sdks/go/pkg/beam/core/runtime/exec/datasource.go#L244
   
   I'd say we don't block on fixing this issue, but we should add a TODO around 
there, with a note about validating CoGBKs and similar types with the sampling 
implementation. We'd also just want to copy the bytes "raw" for a sampled 
windowed value element element (the PCollection sampling logic, which we'd want 
to then also co-opt) instead of decoding an element, and then re-encoding it. 
That might avoid some questions around Large Iterables, or large GBKs too.



##########
sdks/go/pkg/beam/core/runtime/exec/datasampler_test.go:
##########
@@ -0,0 +1,84 @@
+// 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 exec
+
+import (
+       "context"
+       "testing"
+       "time"
+)
+
+// TestDataSampler verifies that the DataSampler works correctly.
+func TestDataSampler(t *testing.T) {
+       tests := []struct {
+               name    string
+               samples []dataSample
+               pids    []string
+               want    int
+       }{
+               {
+                       name: "GetAllSamples",
+                       samples: []dataSample{
+                               {PCollectionID: "pid1", Element: 
[]byte("element1"), Timestamp: time.Now()},
+                               {PCollectionID: "pid2", Element: 
[]byte("element2"), Timestamp: time.Now()},
+                       },
+                       pids: []string{},
+                       want: 2,
+               },
+               {
+                       name: "GetSamplesForPCollections",
+                       samples: []dataSample{
+                               {PCollectionID: "pid1", Element: 
[]byte("element1"), Timestamp: time.Now()},
+                               {PCollectionID: "pid2", Element: 
[]byte("element2"), Timestamp: time.Now()},
+                       },
+                       pids: []string{"pid1"},
+                       want: 1,
+               },
+               {
+                       name: "GetSamplesForPCollectionsWithNoResult",
+                       samples: []dataSample{
+                               {PCollectionID: "pid1", Element: 
[]byte("element1"), Timestamp: time.Now()},
+                               {PCollectionID: "pid2", Element: 
[]byte("element2"), Timestamp: time.Now()},
+                       },
+                       pids: []string{"pid3"},
+                       want: 0,
+               },

Review Comment:
   Please add a test case that validates the circular buffer logic in 
outputSamples. (either actually create 10 elements, or reduce max to 3, or 
similar).



##########
sdks/go/pkg/beam/core/runtime/exec/translate.go:
##########
@@ -411,11 +413,11 @@ func (b *builder) makePCollection(id string) 
(*PCollection, error) {
 }
 
 func (b *builder) newPCollectionNode(id string, out Node) (*PCollection, 
error) {
-       ec, _, err := b.makeCoderForPCollection(id)
+       ec, wc, err := b.makeCoderForPCollection(id)
        if err != nil {
                return nil, err
        }
-       u := &PCollection{UID: b.idgen.New(), Out: out, PColID: id, Coder: ec, 
Seed: rand.Int63()}
+       u := &PCollection{UID: b.idgen.New(), Out: out, PColID: id, Coder: ec, 
WindowCoder: wc, Seed: rand.Int63(), dataSampler: b.dataSampler}

Review Comment:
   I'll note that there's at least one location where we *do not* want to have 
Data Sampling behavior, and that's for Large Iterables.
   
   The `mayFixDataSourceCoder` function has 2 cases where we convert the coder 
to a CoGBK handler. We should nil the DataSource's PCollection dataSampler in 
those cases.
   
   The reasoning is values grouped by key are typically large, so we probably 
shouldn't sample them. We can always evaluate this more specifically later if 
there's a problem or customer demand. 
   
   Also, since the iterator Types are anonymous functions being passed in, I 
don't think it's going to print anything meaningful, if at all. (It'll at best, 
encode the key, and then the "iterators" would print their function pointers, 
if it prints anything for the function pointers at all).
   
   I guess that's really the question: What gets sampled currently for GBKs in 
your tests?
   



-- 
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]

Reply via email to