Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context Propagation: KV #117

Draft
wants to merge 1 commit into
base: li_trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.TypeDescriptor;
Expand Down Expand Up @@ -52,6 +53,8 @@ public Coder<V> getValueCoder() {

private final Coder<K> keyCoder;
private final Coder<V> valueCoder;
private static final MapCoder<String, String> CONTEXT_CODER =
MapCoder.of(StringUtf8Coder.of(), StringUtf8Coder.of());

private KvCoder(Coder<K> keyCoder, Coder<V> valueCoder) {
this.keyCoder = keyCoder;
Expand All @@ -69,6 +72,7 @@ public void encode(KV<K, V> kv, OutputStream outStream, Context context)
if (kv == null) {
throw new CoderException("cannot encode a null KV");
}
CONTEXT_CODER.encode(kv.getW3cTraceContext(), outStream);
keyCoder.encode(kv.getKey(), outStream);
valueCoder.encode(kv.getValue(), outStream, context);
}
Expand All @@ -80,9 +84,12 @@ public KV<K, V> decode(InputStream inStream) throws IOException, CoderException

@Override
public KV<K, V> decode(InputStream inStream, Context context) throws IOException, CoderException {
Map<String, String> traceContext = CONTEXT_CODER.decode(inStream);
K key = keyCoder.decode(inStream);
V value = valueCoder.decode(inStream, context);
return KV.of(key, value);
KV<K, V> newKv = KV.of(key, value);
newKv.getW3cTraceContext().putAll(traceContext);
return newKv;
}

@Override
Expand Down
18 changes: 17 additions & 1 deletion sdks/java/core/src/main/java/org/apache/beam/sdk/values/KV.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package org.apache.beam.sdk.values;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.GroupByKey;
Expand All @@ -38,7 +42,7 @@
* @param <K> the type of the key
* @param <V> the type of the value
*/
public class KV<K, V> implements Serializable {
public class KV<K, V> implements Serializable, Traceable {
/** Returns a {@link KV} with the given key and value. */
public static <K, V> KV<K, V> of(K key, V value) {
return new KV<>(key, value);
Expand All @@ -56,10 +60,22 @@ public V getValue() {
return value;
}

@Override
public Map<String, String> getW3cTraceContext() {
return w3cTraceContext;
}

@Override
public List<Map<String, String>> getW3cLinkedContext() {
return w3cLinkedContext;
}

/////////////////////////////////////////////////////////////////////////////

final K key;
final V value;
final Map<String, String> w3cTraceContext = new HashMap<>();
final List<Map<String, String>> w3cLinkedContext = new ArrayList<>();

private KV(K key, V value) {
this.key = key;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 org.apache.beam.sdk.values;

import java.util.List;
import java.util.Map;

public interface Traceable {

Map<String, String> getW3cTraceContext();

List<Map<String, String>> getW3cLinkedContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testCoderIsSerializableWithWellKnownCoderType() throws Exception {
* org.apache.beam.sdk.coders.PrintBase64Encodings}.
*/
private static final List<String> TEST_ENCODINGS =
Arrays.asList("AP____8P", "BWhlbGxvAA", "B2dvb2RieWX_____Bw");
Arrays.asList("AAAAAAD_____Dw", "AAAAAAVoZWxsbwA", "AAAAAAdnb29kYnll_____wc");

@Test
public void testWireFormatEncode() throws Exception {
Expand Down