/
buffer.go
174 lines (143 loc) · 4.52 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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 direct
import (
"context"
"fmt"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex"
"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
)
// buffer buffers all input and notifies on FinishBundle. It is also a SideInputAdapter.
// It is used as a guard for the wait node to buffer data used as side input.
type buffer struct {
uid exec.UnitID
next exec.UnitID // debug only
read exec.UnitID // debug only
notify func(ctx context.Context) error
buf []exec.FullValue
done bool
}
func (n *buffer) ID() exec.UnitID {
return n.uid
}
func (n *buffer) Up(ctx context.Context) error {
return nil
}
func (n *buffer) StartBundle(ctx context.Context, id string, data exec.DataContext) error {
n.buf = nil
n.done = false
return nil
}
func (n *buffer) ProcessElement(ctx context.Context, elm *exec.FullValue, values ...exec.ReStream) error {
n.buf = append(n.buf, *elm)
return nil
}
func (n *buffer) FinishBundle(ctx context.Context) error {
n.done = true
return n.notify(ctx)
}
func (n *buffer) Down(ctx context.Context) error {
return nil
}
func (n *buffer) NewIterable(ctx context.Context, reader exec.StateReader, w typex.Window) (exec.ReStream, error) {
if !n.done {
panic(fmt.Sprintf("buffer[%v] incomplete: %v", n.uid, len(n.buf)))
}
return &exec.FixedReStream{Buf: n.buf}, nil
}
func (n *buffer) NewKeyedIterable(ctx context.Context, reader exec.StateReader, w typex.Window, iterKey interface{}) (exec.ReStream, error) {
return n.NewIterable(ctx, reader, w)
}
func (n *buffer) String() string {
return fmt.Sprintf("buffer[%v]. wait:%v Out:%v", n.uid, n.next, n.read)
}
// wait buffers all input until the guard condition is triggered. It then
// proceeds normally. The main purpose is to delay bundle processing until side input
// is ready.
type wait struct {
UID exec.UnitID
need int // guards needed
next exec.Node
instID string
mgr exec.DataContext
buf []bufElement
ready int // guards ready
done bool // FinishBundle called for main input?
}
type bufElement struct {
elm exec.FullValue
values []exec.ReStream
}
func (w *wait) ID() exec.UnitID {
return w.UID
}
func (w *wait) notify(ctx context.Context) error {
if w.ready == w.need {
panic("Too many notify")
}
w.ready++
if w.ready < w.need {
return nil
}
// All ready: continue the processing. We may or may not have buffered
// all the data. If not, wait is a pass-through going forward.
log.Debugf(ctx, "wait[%v] unblocked w/ %v [%v]", w.UID, len(w.buf), w.done)
if err := w.next.StartBundle(ctx, w.instID, w.mgr); err != nil {
return err
}
for _, element := range w.buf {
if err := w.next.ProcessElement(ctx, &element.elm, element.values...); err != nil {
return err
}
}
w.buf = nil
if w.done {
if err := w.next.FinishBundle(ctx); err != nil {
return err
}
}
log.Debugf(ctx, "wait[%v] done", w.UID)
return nil
}
func (w *wait) Up(ctx context.Context) error {
return nil
}
func (w *wait) StartBundle(ctx context.Context, id string, data exec.DataContext) error {
return nil // done in notify
}
func (w *wait) ProcessElement(ctx context.Context, elm *exec.FullValue, values ...exec.ReStream) error {
if w.ready < w.need {
// log.Printf("buffer[%v]: %v", w.UID, elm)
w.buf = append(w.buf, bufElement{elm: *elm, values: values})
return nil
}
// log.Printf("NOT buffer[%v]: %v", w.UID, elm)
return w.next.ProcessElement(ctx, elm, values...)
}
func (w *wait) FinishBundle(ctx context.Context) error {
if w.ready < w.need || w.done {
w.done = true
return nil
}
w.done = true
return w.next.FinishBundle(ctx)
}
func (w *wait) Down(ctx context.Context) error {
return nil
}
func (w *wait) String() string {
return fmt.Sprintf("wait[%v] Out:%v", w.need, w.next.ID())
}