1818package org .apache .beam .runners .dataflow .worker ;
1919
2020import static org .apache .beam .runners .dataflow .util .Structs .getString ;
21- import static org .apache .beam .vendor .guava .v32_1_2_jre .com .google .common .base .Preconditions .checkState ;
2221
2322import com .google .auto .service .AutoService ;
2423import java .io .IOException ;
5453 "nullness" // TODO(https://github.com/apache/beam/issues/20497)
5554})
5655class WindmillSink <T > extends Sink <WindowedValue <T >> {
56+
5757 private WindmillStreamWriter writer ;
5858 private final Coder <T > valueCoder ;
5959 private final Coder <Collection <? extends BoundedWindow >> windowsCoder ;
@@ -71,15 +71,29 @@ class WindmillSink<T> extends Sink<WindowedValue<T>> {
7171 this .context = context ;
7272 }
7373
74+ private static ByteString encodeMetadata (
75+ ByteStringOutputStream stream ,
76+ Coder <Collection <? extends BoundedWindow >> windowsCoder ,
77+ Collection <? extends BoundedWindow > windows ,
78+ PaneInfo paneInfo )
79+ throws IOException {
80+ try {
81+ PaneInfoCoder .INSTANCE .encode (paneInfo , stream );
82+ windowsCoder .encode (windows , stream , Coder .Context .OUTER );
83+ return stream .toByteStringAndReset ();
84+ } catch (Throwable e ) {
85+ stream .toByteStringAndReset ();
86+ throw e ;
87+ }
88+ }
89+
7490 public static ByteString encodeMetadata (
7591 Coder <Collection <? extends BoundedWindow >> windowsCoder ,
7692 Collection <? extends BoundedWindow > windows ,
7793 PaneInfo paneInfo )
7894 throws IOException {
7995 ByteStringOutputStream stream = new ByteStringOutputStream ();
80- PaneInfoCoder .INSTANCE .encode (paneInfo , stream );
81- windowsCoder .encode (windows , stream , Coder .Context .OUTER );
82- return stream .toByteString ();
96+ return encodeMetadata (stream , windowsCoder , windows , paneInfo );
8397 }
8498
8599 public static PaneInfo decodeMetadataPane (ByteString metadata ) throws IOException {
@@ -109,6 +123,7 @@ public Map<String, SinkFactory> factories() {
109123 }
110124
111125 public static class Factory implements SinkFactory {
126+
112127 @ Override
113128 public WindmillSink <?> create (
114129 CloudObject spec ,
@@ -133,6 +148,7 @@ public SinkWriter<WindowedValue<T>> writer() {
133148 }
134149
135150 class WindmillStreamWriter implements SinkWriter <WindowedValue <T >> {
151+
136152 private Map <ByteString , Windmill .KeyedMessageBundle .Builder > productionMap ;
137153 private final String destinationName ;
138154 private final ByteStringOutputStream stream ; // Kept across encodes for buffer reuse.
@@ -144,10 +160,10 @@ private WindmillStreamWriter(String destinationName) {
144160 }
145161
146162 private <EncodeT > ByteString encode (Coder <EncodeT > coder , EncodeT object ) throws IOException {
147- checkState (
148- stream . size () == 0 ,
149- "Expected output stream to be empty but had %s" ,
150- stream . toByteString ());
163+ if ( stream . size () != 0 ) {
164+ throw new IllegalStateException (
165+ "Expected output stream to be empty but had " + stream . toByteString ());
166+ }
151167 try {
152168 coder .encode (object , stream , Coder .Context .OUTER );
153169 return stream .toByteStringAndReset ();
@@ -162,7 +178,8 @@ private <EncodeT> ByteString encode(Coder<EncodeT> coder, EncodeT object) throws
162178 public long add (WindowedValue <T > data ) throws IOException {
163179 ByteString key , value ;
164180 ByteString id = ByteString .EMPTY ;
165- ByteString metadata = encodeMetadata (windowsCoder , data .getWindows (), data .getPaneInfo ());
181+ ByteString metadata =
182+ encodeMetadata (stream , windowsCoder , data .getWindows (), data .getPaneInfo ());
166183 if (valueCoder instanceof KvCoder ) {
167184 KvCoder kvCoder = (KvCoder ) valueCoder ;
168185 KV kv = (KV ) data .getValue ();
0 commit comments