wuchong commented on a change in pull request #15439:
URL: https://github.com/apache/flink/pull/15439#discussion_r604694635



##########
File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/operators/aggregate/window/LocalSlicingWindowAggOperator.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.table.runtime.operators.aggregate.window;
+
+import org.apache.flink.core.memory.ManagedMemoryUseCase;
+import org.apache.flink.runtime.execution.Environment;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.api.operators.TimestampedCollector;
+import org.apache.flink.streaming.api.watermark.Watermark;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.table.data.RowData;
+import 
org.apache.flink.table.runtime.generated.GeneratedNamespaceAggsHandleFunction;
+import org.apache.flink.table.runtime.keyselector.RowDataKeySelector;
+import 
org.apache.flink.table.runtime.operators.aggregate.window.buffers.RecordsWindowBuffer;
+import 
org.apache.flink.table.runtime.operators.aggregate.window.buffers.WindowBuffer;
+import 
org.apache.flink.table.runtime.operators.aggregate.window.combines.LocalAggRecordsCombiner;
+import org.apache.flink.table.runtime.operators.window.TimeWindow;
+import 
org.apache.flink.table.runtime.operators.window.combines.WindowCombineFunction;
+import org.apache.flink.table.runtime.operators.window.slicing.ClockService;
+import org.apache.flink.table.runtime.operators.window.slicing.SliceAssigner;
+import org.apache.flink.table.runtime.typeutils.AbstractRowDataSerializer;
+import org.apache.flink.table.runtime.typeutils.PagedTypeSerializer;
+
+/**
+ * The operator used for local window aggregation.
+ *
+ * <p>Note: this only supports event-time window.
+ */
+public class LocalSlicingWindowAggOperator extends 
AbstractStreamOperator<RowData>
+        implements OneInputStreamOperator<RowData, RowData> {
+    private static final long serialVersionUID = 1L;
+    private static final ClockService CLOCK_SERVICE = ClockService.ofSystem();
+
+    private final RowDataKeySelector keySelector;
+    private final SliceAssigner sliceAssigner;
+    private final long windowInterval;
+    private final WindowBuffer.Factory windowBufferFactory;
+    private final WindowCombineFunction.LocalFactory combinerFactory;
+
+    // ------------------------------------------------------------------------
+
+    /** This is used for emitting elements with a given timestamp. */
+    protected transient TimestampedCollector<RowData> collector;
+
+    /** Flag to prevent duplicate function.close() calls in close() and 
dispose(). */
+    private transient boolean functionsClosed = false;
+
+    /** current watermark of this operator. */
+    private transient long currentWatermark;
+
+    /** The next watermark to trigger windows. */
+    private transient long nextTriggerWatermark;
+
+    /** A buffer to buffers window data in memory and may flush to output. */
+    private transient WindowBuffer windowBuffer;
+
+    public LocalSlicingWindowAggOperator(
+            RowDataKeySelector keySelector,
+            SliceAssigner sliceAssigner,
+            PagedTypeSerializer<RowData> keySer,
+            AbstractRowDataSerializer<RowData> inputSer,
+            GeneratedNamespaceAggsHandleFunction<Long> genAggsHandler) {
+        this(
+                keySelector,
+                sliceAssigner,
+                new RecordsWindowBuffer.Factory(keySer, inputSer),
+                new LocalAggRecordsCombiner.Factory(genAggsHandler, keySer));
+    }
+
+    public LocalSlicingWindowAggOperator(
+            RowDataKeySelector keySelector,
+            SliceAssigner sliceAssigner,
+            WindowBuffer.Factory windowBufferFactory,
+            WindowCombineFunction.LocalFactory combinerFactory) {
+        this.keySelector = keySelector;
+        this.sliceAssigner = sliceAssigner;
+        this.windowInterval = sliceAssigner.getSliceEndInterval();
+        this.windowBufferFactory = windowBufferFactory;
+        this.combinerFactory = combinerFactory;
+    }
+
+    @Override
+    public void open() throws Exception {
+        super.open();
+        functionsClosed = false;
+
+        collector = new TimestampedCollector<>(output);
+        collector.eraseTimestamp();
+
+        final WindowCombineFunction localCombiner =
+                combinerFactory.create(getRuntimeContext(), collector);
+        this.windowBuffer =
+                windowBufferFactory.create(
+                        getContainingTask(),
+                        
getContainingTask().getEnvironment().getMemoryManager(),
+                        computeMemorySize(),
+                        localCombiner);
+    }
+
+    @Override
+    public void processElement(StreamRecord<RowData> element) throws Exception 
{
+        RowData inputRow = element.getValue();
+        RowData key = keySelector.getKey(inputRow);
+        long sliceEnd = sliceAssigner.assignSliceEnd(inputRow, CLOCK_SERVICE);
+        // may flush to output if buffer is full
+        windowBuffer.addElement(key, sliceEnd, inputRow);

Review comment:
       Use memory buffer can help to reduce GC problems and avoid OOM.




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to