[
https://issues.apache.org/jira/browse/BEAM-9775?focusedWorklogId=426854&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-426854
]
ASF GitHub Bot logged work on BEAM-9775:
----------------------------------------
Author: ASF GitHub Bot
Created on: 24/Apr/20 04:16
Start Date: 24/Apr/20 04:16
Worklog Time Spent: 10m
Work Description: youngoli commented on a change in pull request #11499:
URL: https://github.com/apache/beam/pull/11499#discussion_r414280113
##########
File path: sdks/go/examples/stringsplit/stringsplit.go
##########
@@ -0,0 +1,249 @@
+// 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.
+
+// An example of using a Splittable DoFn in the Go SDK with a portable runner.
+//
+// The following instructions describe how to execute this example in the
+// Flink local runner.
+//
+// 1. From a command line, navigate to the top-level beam/ directory and run
+// the Flink job server:
+// ./gradlew :runners:flink:1.10:job-server:runShadow -Djob-host=localhost
-Dflink-master=local
+//
+// 2. The job server is ready to receive jobs once it outputs a log like the
+// following: `JobService started on localhost:8099`. Take note of the endpoint
+// in that log message.
+//
+// 3. While the job server is running in one command line window, create a
+// second one in the same directory and run this example with the following
+// command, using the endpoint you noted from step 2:
+// go run sdks/go/examples/stringsplit/stringsplit.go --runner=universal
--endpoint=localhost:8099
+//
+// 4. Once the pipeline is complete, the job server can be closed with ctrl+C.
+// To check the output of the pipeline, search the job server logs for the
+// phrase "StringSplit Output".
+package main
+
+import (
+ "context"
+ "flag"
+ "reflect"
+ "time"
+
+ "github.com/apache/beam/sdks/go/examples/stringsplit/offsetrange"
+ "github.com/apache/beam/sdks/go/pkg/beam"
+ "github.com/apache/beam/sdks/go/pkg/beam/log"
+ "github.com/apache/beam/sdks/go/pkg/beam/x/beamx"
+)
+
+func init() {
+ beam.RegisterType(reflect.TypeOf((*StringSplitFn)(nil)).Elem())
+ beam.RegisterType(reflect.TypeOf((*LogFn)(nil)).Elem())
+}
+
+// StringSplitFn is a Splittable DoFn that splits strings into substrings of
the
+// specified size (for example, to be able to fit them in a small buffer).
+// See ProcessElement for more details.
+type StringSplitFn struct {
+ BufSize int64
+}
+
+// CreateInitialRestriction creates an offset range restriction for each
element
+// with the size of the restriction corresponding to the length of the string.
+func (fn *StringSplitFn) CreateInitialRestriction(s string)
offsetrange.Restriction {
+ rest := offsetrange.Restriction{Start: 0, End: int64(len(s))}
+ log.Debugf(context.Background(), "StringSplit CreateInitialRestriction:
%v", rest)
+ return rest
+}
+
+// SplitRestriction performs initial splits so that each restriction is split
+// into 5.
+func (fn *StringSplitFn) SplitRestriction(s string, rest
offsetrange.Restriction) []offsetrange.Restriction {
+ size := rest.End - rest.Start
+ splitPts := []int64{
+ rest.Start,
+ rest.Start + (size / 5),
+ rest.Start + (size * 2 / 5),
+ rest.Start + (size * 3 / 5),
+ rest.Start + (size * 4 / 5),
+ rest.End,
+ }
+ var splits []offsetrange.Restriction
+ for i := 0; i < len(splitPts)-1; i++ {
+ splits = append(splits, offsetrange.Restriction{Start:
splitPts[i], End: splitPts[i+1]})
+ }
+ log.Debugf(context.Background(), "StringSplit SplitRestrictions: %v ->
%v", rest, splits)
+ return splits
+}
+
+// RestrictionSize returns the size as the difference between the restriction's
+// start and end.
+func (fn *StringSplitFn) RestrictionSize(s string, rest
offsetrange.Restriction) float64 {
+ size := float64(rest.End - rest.Start)
+ log.Debugf(context.Background(), "StringSplit RestrictionSize: %v ->
%v", rest, size)
+ return size
+}
+
+// CreateTracker creates an offset range restriction tracker out of the offset
+// range restriction.
+func (fn *StringSplitFn) CreateTracker(rest offsetrange.Restriction)
*offsetrange.Tracker {
+ return offsetrange.NewTracker(rest)
+}
+
+// ProcessElement splits a string into substrings of a specified size (set in
+// StringSplitFn.BufSize).
+//
+// Note that the substring blocks are not guaranteed to line up with the
+// restriction boundaries. ProcessElement is expected to emit any substring
+// block that begins in its restriction, even if it extends past the end of the
+// restriction.
+//
+// Example: If BufSize is 100, then a restriction of 75 to 325 should emit the
+// following substrings: [100, 200], [200, 300], [300, 400]
+func (fn *StringSplitFn) ProcessElement(rt *offsetrange.Tracker, elem string,
emit func(string)) error {
+ log.Debugf(context.Background(), "StringSplit ProcessElement: Tracker =
%v", rt)
+ i := rt.Rest.Start
+ if rem := i % fn.BufSize; rem != 0 {
+ i += fn.BufSize - rem // Skip to next multiple of BufSize.
+ }
+ strEnd := int64(len(elem))
+
+ for ok := rt.TryClaim(i); ok == true; ok = rt.TryClaim(i) {
+ if i+fn.BufSize > strEnd {
+ emit(elem[i:])
+ } else {
+ emit(elem[i : i+fn.BufSize])
+ }
+ i += fn.BufSize
+ }
+ // TODO(BEAM-9799): Remove this check once error checking is automatic.
+ if err := rt.GetError(); err != nil {
+ return err
+ }
+ return nil
Review comment:
Done. I'll hold off on having it automatically checked for the moment,
just because I wanna add a test along with that when I get to it.
##########
File path: sdks/go/examples/stringsplit/offsetrange/offsetrange.go
##########
@@ -0,0 +1,107 @@
+package offsetrange
+
+import (
+ "errors"
+ "math"
+ "reflect"
+
+ "github.com/apache/beam/sdks/go/pkg/beam"
+)
+
+func init() {
+ beam.RegisterType(reflect.TypeOf((*Tracker)(nil)))
+ beam.RegisterType(reflect.TypeOf((*Restriction)(nil)))
+}
+
+type Restriction struct {
+ Start, End int64 // Half-closed interval with boundaries [start, end).
+}
+
+// Tracker tracks a restriction that can be represented as a range of integer
values,
+// for example for byte offsets in a file, or indices in an array. Note that
this tracker makes
+// no assumptions about the positions of blocks within the range, so users
must handle validation
+// of block positions if needed.
+type Tracker struct {
+ Rest Restriction
+ Claimed int64 // Tracks the last claimed position.
+ Stopped bool // Tracks whether TryClaim has already indicated to stop
processing elements for
+ // any reason.
+ Err error
+}
+
+// NewTracker is a constructor for an Tracker given a start and end range.
+func NewTracker(rest Restriction) *Tracker {
+ return &Tracker{
+ Rest: rest,
+ Claimed: rest.Start - 1,
+ Stopped: false,
+ Err: nil,
+ }
+}
+
+// TryClaim accepts an int64 position and successfully claims it if that
position is greater than
+// the previously claimed position and less than the end of the restriction.
Note that the
+// Tracker is not considered done until a position >= tracker.end tries to be
claimed,
+// at which point this method signals to end processing.
+func (tracker *Tracker) TryClaim(rawPos interface{}) bool {
+ if tracker.Stopped == true {
+ tracker.Err = errors.New("cannot claim work after restriction
tracker returns false")
+ return false
+ }
+
+ pos := rawPos.(int64)
+
+ if pos < tracker.Rest.Start {
+ tracker.Stopped = true
+ tracker.Err = errors.New("position claimed is out of bounds of
the restriction")
+ return false
+ }
+ if pos <= tracker.Claimed {
+ tracker.Stopped = true
+ tracker.Err = errors.New("cannot claim a position lower than
the previously claimed position")
+ return false
+ }
+
+ tracker.Claimed = pos
+ if pos >= tracker.Rest.End {
+ tracker.Stopped = true
+ return false
+ }
+ return true
+}
+
+// IsDone returns true if the most recent claimed element is past the end of
the restriction.
Review comment:
I was planning on doing it in a seperate PR, but thinking about it, it's
a really small change, so why not? Done
##########
File path: sdks/go/examples/stringsplit/offsetrange/offsetrange.go
##########
@@ -0,0 +1,107 @@
+package offsetrange
Review comment:
Whoops, fixed.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 426854)
Time Spent: 2h 40m (was: 2.5h)
> Add capability to run SDFs on Flink Runner and Python FnApiRunner.
> ------------------------------------------------------------------
>
> Key: BEAM-9775
> URL: https://issues.apache.org/jira/browse/BEAM-9775
> Project: Beam
> Issue Type: Sub-task
> Components: sdk-go
> Reporter: Daniel Oliveira
> Assignee: Daniel Oliveira
> Priority: Major
> Time Spent: 2h 40m
> Remaining Estimate: 0h
>
> This is pretty simple: Add any missing requirements for being able to
> actually execute SDFs, and _maybe_ an example SDF or something that actually
> works. This can be marked completed when we can run a simple SDF with the Go
> SDK.
> I'm still hesitant on the example SDF because it may imply that the feature
> is ready for general usage, so it might need to be bundled with the
> documentation PR, but we'll see.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)