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


##########
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:
   Yes



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