JinkunLiu commented on code in PR #950: URL: https://github.com/apache/flink-agents/pull/950#discussion_r3791296469
########## runtime/src/main/java/org/apache/flink/agents/runtime/memory/EventAttachmentUtils.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.flink.agents.runtime.memory; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.OutputEvent; +import org.apache.flink.agents.api.context.MemoryObject; +import org.apache.flink.agents.api.context.MemoryRef; +import org.apache.flink.agents.api.context.RunnerContext; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +/** Stores event attachments in sensory memory while events cross action boundaries. */ +public final class EventAttachmentUtils { + + private static final String ATTACHMENT_ROOT = "__event_attachments__"; + + private EventAttachmentUtils() {} + + /** Stores concrete attachment values and replaces them with sensory-memory references. */ + public static void storeEventAttachments(Event event, RunnerContext context) throws Exception { + if (event.getAttachments().isEmpty()) { + return; + } + + if (OutputEvent.EVENT_TYPE.equals(event.getType())) { + String keys = + event.getAttachments().keySet().stream() + .sorted() + .collect(Collectors.joining(", ")); + throw new IllegalArgumentException( + "Output events cannot carry attachments: event_id=" + + event.getId() + + ", event_type=" + + event.getType() + + ", key=" + + keys); + } + + for (Map.Entry<String, Object> entry : event.getAttachments().entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + if (value instanceof MemoryRef) { + continue; + } + + MemoryRef reference = + context.getSensoryMemory().set(buildAttachmentPath(event.getId(), key), value); + + event.getAttachments().put(key, reference); + } + } + + /** Loads sensory-memory references in place before a Java action is invoked. */ + public static void loadEventAttachments(Event event, RunnerContext context) throws Exception { + for (Map.Entry<String, Object> entry : event.getAttachments().entrySet()) { + Object value = entry.getValue(); + if (!(value instanceof MemoryRef)) { + continue; + } + MemoryRef reference = (MemoryRef) value; + + MemoryObject attachment = context.getSensoryMemory().get(reference); Review Comment: Thanks for you point that,Fix it in [e1242310](https://github.com/apache/flink-agents/pull/950/changes/e124231097987aca583715211c4504d0757c4d60) -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
