joeyutong commented on code in PR #924: URL: https://github.com/apache/flink-agents/pull/924#discussion_r3702280480
########## runtime/src/main/java/org/apache/flink/agents/runtime/eventlog/EventLogWriter.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.eventlog; + +import org.apache.flink.agents.api.Event; +import org.apache.flink.agents.api.EventContext; +import org.apache.flink.agents.api.logger.EventLogger; +import org.apache.flink.agents.api.logger.EventLoggerConfig; +import org.apache.flink.agents.api.logger.EventLoggerFactory; +import org.apache.flink.agents.api.logger.EventLoggerOpenParams; +import org.apache.flink.agents.api.logger.LoggerType; +import org.apache.flink.agents.api.trace.ExecutionTraceContext; +import org.apache.flink.agents.plan.AgentPlan; +import org.apache.flink.agents.runtime.metrics.BuiltInMetrics; +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.streaming.api.operators.StreamingRuntimeContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.BASE_LOG_DIR; +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.EVENT_LOGGER_TYPE; +import static org.apache.flink.agents.api.configuration.AgentConfigOptions.EVENT_LOG_TRACE_ENABLED; + +/** Operator-scoped writer that owns the physical Event Log logger lifecycle. */ +@Internal +public final class EventLogWriter implements AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(EventLogWriter.class); + + @Nullable private final EventLogger eventLogger; + private final boolean traceEnabled; + + public static EventLogWriter create(AgentPlan agentPlan) { + return new EventLogWriter( + createEventLogger(agentPlan), agentPlan.getConfig().get(EVENT_LOG_TRACE_ENABLED)); + } + + @VisibleForTesting + public static EventLogWriter forEventLogger(@Nullable EventLogger eventLogger) { + return forEventLogger(eventLogger, true); + } + + @VisibleForTesting + public static EventLogWriter forEventLogger( + @Nullable EventLogger eventLogger, boolean traceEnabled) { + return new EventLogWriter(eventLogger, traceEnabled); + } + + private EventLogWriter(@Nullable EventLogger eventLogger, boolean traceEnabled) { + this.eventLogger = eventLogger; + this.traceEnabled = traceEnabled; + } + + public void open(StreamingRuntimeContext runtimeContext, BuiltInMetrics builtInMetrics) + throws Exception { + if (eventLogger == null) { + return; + } + eventLogger.open(new EventLoggerOpenParams(runtimeContext)); + if (eventLogger instanceof FileEventLogger) { + ((FileEventLogger) eventLogger) + .setTruncatedEventsCounter(builtInMetrics.getEventLogTruncatedEventsCounter()); + } else if (eventLogger instanceof Slf4jEventLogger) { + ((Slf4jEventLogger) eventLogger) + .setTruncatedEventsCounter(builtInMetrics.getEventLogTruncatedEventsCounter()); + } + } + + /** Appends and flushes a business Event best-effort. */ + public void appendBusinessEventAndFlush( + EventContext eventContext, Event event, @Nullable ExecutionTraceContext traceContext) { + appendAndFlush(eventContext, event, traceEnabled ? traceContext : null); + } + + /** Appends and flushes an execution lifecycle Event when Trace recording is enabled. */ + public void appendExecutionEventAndFlush(Event event, ExecutionTraceContext traceContext) { + if (!traceEnabled) { + return; + } + appendAndFlush(new EventContext(event), event, traceContext); + } + + private void appendAndFlush( + EventContext eventContext, Event event, @Nullable ExecutionTraceContext traceContext) { + if (eventLogger == null) { + return; + } + try { + eventLogger.append(eventContext, event, traceContext); + eventLogger.flush(); + } catch (Exception logError) { Review Comment: Making Event Log writes non-fatal is intentional. Event Log is an observability side channel, now including Trace, so a logging backend failure should not change Event processing or job success. I split `append` and `flush` into independent best-effort steps, so `flush` is still attempted after an `append` failure. Each failed write attempt increments `eventLogWriteFailures` once even if both steps fail; the first failure is logged at WARN and subsequent failures at DEBUG. `FileEventLogger` now also surfaces I/O errors otherwise swallowed by `PrintWriter`, and the compatibility change is documented. -- 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]
