Skip to content

Commit cef2b97

Browse files
tteofilianishasthananjhill
authored
Let RemotePayloadProcessor send also gRPC call Metadata (opendatahub-io#96)
#### Motivation The `RemotePayloadProcessor` currently doesn't send gRPC call `Metadata` as part of its `PayloadContent` (to a remote endpoint). Such information might be important for many reasons, including debugging purposes. #### Modifications `RemotePayloadProcessor`'s `PayloadContent` now also features a `Map` that contains such `Metadata` information (when available). #### Result `RemotePayloadProcessor` sends also `Metadata` information (to a remote endpoint). Signed-off-by: Tommaso Teofili <[email protected]> Signed-off-by: Tommaso Teofili <[email protected]> Co-authored-by: Anish Asthana <[email protected]> Co-authored-by: Nick Hill <[email protected]>
1 parent dde5ca8 commit cef2b97

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/main/java/com/ibm/watson/modelmesh/payload/RemotePayloadProcessor.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import java.net.http.HttpRequest;
2222
import java.net.http.HttpResponse;
2323
import java.nio.charset.StandardCharsets;
24+
import java.util.HashMap;
25+
import java.util.Map;
2426

2527
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import io.grpc.Metadata;
2629
import io.netty.buffer.ByteBuf;
2730
import io.netty.handler.codec.base64.Base64;
2831
import org.slf4j.Logger;
@@ -58,16 +61,32 @@ private static PayloadContent prepareContentBody(Payload payload) {
5861
ByteBuf byteBuf = payload.getData();
5962
String data;
6063
if (byteBuf != null) {
61-
ByteBuf encoded = Base64.encode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), false);
62-
//TODO custom jackson serialization for this field to avoid round-tripping to string
63-
data = encoded.toString(StandardCharsets.US_ASCII);
64+
data = encodeBinaryToString(byteBuf);
6465
} else {
6566
data = "";
6667
}
68+
Metadata metadata = payload.getMetadata();
69+
Map<String, String> metadataMap = new HashMap<>();
70+
if (metadata != null) {
71+
for (String key : metadata.keys()) {
72+
if (key.endsWith("-bin")) {
73+
byte[] bytes = metadata.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
74+
metadataMap.put(key, java.util.Base64.getEncoder().encodeToString(bytes));
75+
} else {
76+
String value = metadata.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
77+
metadataMap.put(key, value);
78+
}
79+
}
80+
}
6781
String status = payload.getStatus() != null ? payload.getStatus().getCode().toString() : "";
68-
return new PayloadContent(id, modelId, data, kind, status);
82+
return new PayloadContent(id, modelId, data, kind, status, metadataMap);
6983
}
7084

85+
private static String encodeBinaryToString(ByteBuf byteBuf) {
86+
ByteBuf encodedBinary = Base64.encode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), false);
87+
//TODO custom jackson serialization for this field to avoid round-tripping to string
88+
return encodedBinary.toString(StandardCharsets.US_ASCII);
89+
}
7190

7291
private boolean sendPayload(Payload payload) {
7392
try {
@@ -94,18 +113,22 @@ public String getName() {
94113
}
95114

96115
private static class PayloadContent {
116+
97117
private final String id;
98118
private final String modelid;
99119
private final String data;
100120
private final String kind;
101121
private final String status;
122+
private final Map<String, String> metadata;
102123

103-
private PayloadContent(String id, String modelid, String data, String kind, String status) {
124+
private PayloadContent(String id, String modelid, String data, String kind, String status,
125+
Map<String, String> metadata) {
104126
this.id = id;
105127
this.modelid = modelid;
106128
this.data = data;
107129
this.kind = kind;
108130
this.status = status;
131+
this.metadata = metadata;
109132
}
110133

111134
public String getId() {
@@ -128,6 +151,10 @@ public String getStatus() {
128151
return status;
129152
}
130153

154+
public Map<String, String> getMetadata() {
155+
return metadata;
156+
}
157+
131158
@Override
132159
public String toString() {
133160
return "PayloadContent{" +
@@ -136,6 +163,7 @@ public String toString() {
136163
", data='" + data + '\'' +
137164
", kind='" + kind + '\'' +
138165
", status='" + status + '\'' +
166+
", metadata='" + metadata + '\'' +
139167
'}';
140168
}
141169
}

src/test/java/com/ibm/watson/modelmesh/SidecarModelMeshPayloadProcessingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class SidecarModelMeshPayloadProcessingTest extends SingleInstanceModelMe
4242

4343
@BeforeEach
4444
public void initialize() throws Exception {
45-
System.setProperty(ModelMeshEnvVars.MM_PAYLOAD_PROCESSORS, "logger://*");
45+
System.setProperty(ModelMeshEnvVars.MM_PAYLOAD_PROCESSORS, "http://localhost:8080/consumer/kserve/v2");
4646
super.initialize();
4747
}
4848

src/test/java/com/ibm/watson/modelmesh/payload/RemotePayloadProcessorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void testDestinationUnreachable() {
3636
String method = "predict";
3737
Status kind = Status.INVALID_ARGUMENT;
3838
Metadata metadata = new Metadata();
39+
metadata.put(Metadata.Key.of("foo", Metadata.ASCII_STRING_MARSHALLER), "bar");
40+
metadata.put(Metadata.Key.of("binary-bin", Metadata.BINARY_BYTE_MARSHALLER), "string".getBytes());
3941
ByteBuf data = Unpooled.buffer(4);
4042
Payload payload = new Payload(id, modelId, method, metadata, data, kind);
4143
assertFalse(remotePayloadProcessor.process(payload));

0 commit comments

Comments
 (0)