pnowojski commented on code in PR #24119:
URL: https://github.com/apache/flink/pull/24119#discussion_r1457322520


##########
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/StateHandleDownloadSpec.java:
##########
@@ -53,7 +53,9 @@ public IncrementalLocalKeyedStateHandle 
createLocalStateHandleForDownloadedState
         return new IncrementalLocalKeyedStateHandle(
                 stateHandle.getBackendIdentifier(),
                 stateHandle.getCheckpointId(),
-                new DirectoryStateHandle(downloadDestination),
+                // This handle is just temporary and will the directory size 
will never be used for
+                // metrics, so we just fill it in as 0.
+                new DirectoryStateHandle(downloadDestination, 0L),

Review Comment:
   For completeness, why don't you pass here `stateHandle.getStateSize()`?



##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/StateObject.java:
##########
@@ -63,4 +64,60 @@ public interface StateObject extends Serializable {
      * @return Size of the state in bytes.
      */
     long getStateSize();
+
+    /**
+     * Collects statistics about state size and location from the state object.
+     *
+     * @implNote default implementation reports {@link 
StateObject#getStateSize()} as size and
+     *     {@link StateObjectLocation#UNKNOWN} as location.
+     * @param collector the statistics collector.
+     */
+    default void collectSizeStats(StateObjectSizeStatsCollector collector) {
+        collector.add(StateObjectLocation.UNKNOWN, getStateSize());
+    }
+
+    /** Enum for state locations. */
+    enum StateObjectLocation {
+        LOCAL_MEMORY,
+        LOCAL_DISK,
+        REMOTE,
+        UNKNOWN,
+    }
+
+    /**
+     * Collector for size and location stats from a state object via {@link
+     * StateObject#collectSizeStats(StateObjectSizeStatsCollector)}.
+     */
+    final class StateObjectSizeStatsCollector {
+        private final EnumMap<StateObjectLocation, Long> stats;
+
+        private StateObjectSizeStatsCollector() {
+            stats = new EnumMap<>(StateObjectLocation.class);
+        }
+
+        public void add(StateObjectLocation key, long value) {
+            stats.compute(
+                    key,
+                    (k, v) -> {
+                        if (v != null) {
+                            return v + value;
+                        } else {
+                            return value;
+                        }
+                    });
+        }
+
+        public EnumMap<StateObjectLocation, Long> getStats() {
+            return stats;
+        }
+
+        public static StateObjectSizeStatsCollector create() {
+            return new StateObjectSizeStatsCollector();
+        }
+
+        @Override
+        public String toString() {
+            return "StateObjectSizeStatsCollector{" + "stats=" + stats + '}';

Review Comment:
   Will this print to a human readable map representation?



##########
docs/content.zh/docs/ops/traces.md:
##########
@@ -159,6 +159,14 @@ Flink reports a single span trace for the whole checkpoint 
and job initializatio
       <td>(Max/Sum)DownloadStateDurationMs<br><br>(optional - currently only 
supported by RocksDB Incremental)</td>
       <td>The aggregated (max and sum) across all subtasks duration of 
downloading state files from the DFS.</td>
     </tr>
+    <tr>
+      <td>(Max/Sum)RestoreStateSizeBytes.[location]<br><br>(optional - only 
reported if metrics.trace-restore-state-size is true)</td>

Review Comment:
   Restore**d**StateSizeBytes ? (ditto in the actual metric name)



##########
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/OperatorSubtaskState.java:
##########
@@ -130,21 +135,26 @@ private OperatorSubtaskState(
         this.inputRescalingDescriptor = checkNotNull(inputRescalingDescriptor);
         this.outputRescalingDescriptor = 
checkNotNull(outputRescalingDescriptor);
 
-        long calculateStateSize = managedOperatorState.getStateSize();
-        calculateStateSize += rawOperatorState.getStateSize();
-        calculateStateSize += managedKeyedState.getStateSize();
-        calculateStateSize += rawKeyedState.getStateSize();
-        calculateStateSize += inputChannelState.getStateSize();
-        calculateStateSize += resultSubpartitionState.getStateSize();
-        stateSize = calculateStateSize;
-
-        long calculateCheckpointedSize = 
managedOperatorState.getCheckpointedSize();
-        calculateCheckpointedSize += rawOperatorState.getCheckpointedSize();
-        calculateCheckpointedSize += managedKeyedState.getCheckpointedSize();
-        calculateCheckpointedSize += rawKeyedState.getCheckpointedSize();
-        calculateCheckpointedSize += inputChannelState.getCheckpointedSize();
-        calculateCheckpointedSize += 
resultSubpartitionState.getCheckpointedSize();
-        this.checkpointedSize = calculateCheckpointedSize;
+        this.stateSize = 
streamSubCollections().mapToLong(StateObject::getStateSize).sum();
+        this.checkpointedSize =
+                
streamSubCollections().mapToLong(StateObjectCollection::getCheckpointedSize).sum();
+    }
+
+    private Stream<StateObjectCollection<?>> streamSubCollections() {
+        return Stream.of(streamOperatorAndKeyedStates(), streamChannelStates())
+                .flatMap(Function.identity());
+    }
+
+    private Stream<StateObjectCollection<? extends StateObject>> 
streamOperatorAndKeyedStates() {
+        return Stream.of(managedOperatorState, rawOperatorState, 
managedKeyedState, rawKeyedState)
+                .filter(Objects::nonNull);
+    }
+
+    private Stream<StateObjectCollection<? extends 
AbstractChannelStateHandle<?>>>
+            streamChannelStates() {
+        return Stream.<StateObjectCollection<? extends 
AbstractChannelStateHandle<?>>>of(
+                        inputChannelState, resultSubpartitionState)
+                .filter(Objects::nonNull);

Review Comment:
   Why do we need two methods, can not we have just a single one returning both 
op and channel state?
   
   edit: I see `collectUniqueDelegates`. Note, `collectUniqueDelegates` could 
be refactored to also support but ignore non channel state, and then we 
wouldn't need to differentiate between two types of handles? But I'm not sure 
which option is better so 🤷 



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/StreamTaskStateInitializerImpl.java:
##########
@@ -227,6 +238,24 @@ public StreamOperatorStateContext 
streamOperatorStateContext(
                 timeServiceManager = null;
             }
 
+            // Add stats for input channel and result partition state
+            Stream.concat(
+                            
prioritizedOperatorSubtaskStates.getPrioritizedInputChannelState()
+                                    .stream(),
+                            
prioritizedOperatorSubtaskStates.getPrioritizedResultSubpartitionState()
+                                    .stream())
+                    .filter(Objects::nonNull)
+                    .forEach(channelHandle -> 
channelHandle.collectSizeStats(statsCollector));
+
+            // Report collected stats to metrics
+            statsCollector
+                    .getStats()
+                    .forEach(

Review Comment:
   If you are going all in with streams, why don't you merge those two?



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

Reply via email to