rosemarYuan commented on code in PR #887: URL: https://github.com/apache/flink-agents/pull/887#discussion_r3632672992
########## runtime/src/main/java/org/apache/flink/agents/runtime/memory/MemoryEventBuilder.java: ########## @@ -0,0 +1,154 @@ +/* + * 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 com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.flink.agents.api.context.MemoryUpdate; +import org.apache.flink.agents.api.event.LongTermGetEvent; +import org.apache.flink.agents.api.event.LongTermSearchEvent; +import org.apache.flink.agents.api.event.LongTermUpdateEvent; +import org.apache.flink.agents.api.event.MemoryEvent; +import org.apache.flink.agents.runtime.memory.MemoryEventSettings.MemoryOp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Converts per-action memory records into {@link MemoryEvent}s at the action finish boundary. + * + * <p>Values are dot-key flat maps; multiple operations on the same field within one action fold to + * the last value (net effect). LTM search values map each query to its (last) hit list. + */ +public final class MemoryEventBuilder { + + private static final Logger LOG = LoggerFactory.getLogger(MemoryEventBuilder.class); + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private MemoryEventBuilder() {} + + public static List<MemoryEvent> buildWriteEvents( + String key, + List<MemoryUpdate> sensoryUpdates, + List<MemoryUpdate> shortTermUpdates, + MemoryEventSettings settings) { + List<MemoryEvent> events = new ArrayList<>(2); + addFolded(events, key, MemoryOp.SENSORY_WRITE, sensoryUpdates, settings); + addFolded(events, key, MemoryOp.SHORT_TERM_WRITE, shortTermUpdates, settings); + return events; + } + + public static List<MemoryEvent> buildReadEvents( + String key, + List<MemoryUpdate> sensoryReads, + List<MemoryUpdate> shortTermReads, + MemoryEventSettings settings) { + List<MemoryEvent> events = new ArrayList<>(2); + addFolded(events, key, MemoryOp.SENSORY_READ, sensoryReads, settings); + addFolded(events, key, MemoryOp.SHORT_TERM_READ, shortTermReads, settings); + return events; + } + + /** + * Builds LTM events from drained Python-side records ({@code {op,set,id,value,key}} maps). The + * record-level {@code key} is a hashed partition key used only for drain filtering; the emitted + * events carry the finishing action's readable {@code key} passed in here. + */ + public static List<MemoryEvent> buildLtmEvents( + String key, List<Map<String, Object>> records, MemoryEventSettings settings) { + Map<String, Object> updates = new LinkedHashMap<>(); + Map<String, Object> gets = new LinkedHashMap<>(); + Map<String, Object> searches = new LinkedHashMap<>(); + for (Map<String, Object> record : records) { + Object op = record.get("op"); + String set = record.get("set") != null ? record.get("set").toString() : null; + if (set == null) { + continue; + } + Object id = record.get("id"); + Object value = record.get("value"); + if ("ADD".equals(op)) { + updates.put(set + "." + id, value); + } else if ("DELETE".equals(op)) { + updates.put(set + "." + id, null); + } else if ("DELETE_SET".equals(op)) { + updates.put(set, null); Review Comment: Hi @weiqingy, sorry for the delayed response. I was working on the Action Condition changes last week, so I didn’t get a chance to address this PR sooner. I’ve now addressed all the review comments and added the corresponding tests. Thanks again for the thorough review. For `begin-event`, I hadn’t fully considered the cost you pointed out: enabling it by default makes every input scan the full STM state, even when log-based reconstruction is not needed. Given that overhead, I changed `begin-event` to be opt-in (`false` by default) and restored the original `ON_READ_AND_WRITE` TTL default. Users who need an STM reconstruction anchor can still enable the event explicitly. -- 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]
