AlanConfluent commented on code in PR #21264:
URL: https://github.com/apache/flink/pull/21264#discussion_r1024790314


##########
flink-tests/src/test/java/org/apache/flink/test/state/TaskManagerWideRocksDbMemorySharingITCase.java:
##########
@@ -0,0 +1,257 @@
+/*
+ * 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.test.state;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.functions.RichMapFunction;
+import org.apache.flink.api.common.state.ListState;
+import org.apache.flink.api.common.state.ListStateDescriptor;
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.configuration.StateBackendOptions;
+import org.apache.flink.configuration.TaskManagerOptions;
+import org.apache.flink.contrib.streaming.state.RocksDBNativeMetricOptions;
+import org.apache.flink.contrib.streaming.state.RocksDBOptions;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.testutils.InMemoryReporter;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.CheckpointingMode;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.math.BigInteger;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.DoubleSummaryStatistics;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static 
org.apache.flink.api.common.restartstrategy.RestartStrategies.noRestart;
+import static 
org.apache.flink.contrib.streaming.state.RocksDBMemoryControllerUtils.calculateActualCacheCapacity;
+import static 
org.apache.flink.runtime.testutils.CommonTestUtils.waitForAllTaskRunning;
+import static org.apache.flink.util.Preconditions.checkState;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests that memory sharing scope and {@link 
TaskManagerOptions#MANAGED_MEMORY_SHARED_FRACTION}
+ * work as expected, i.e. make RocksDB use the same BlockCache and 
WriteBufferManager objects. It
+ * does so using RocksDB metrics.
+ */
+public class TaskManagerWideRocksDbMemorySharingITCase {
+    private static final int PARALLELISM = 4;
+    private static final int NUMBER_OF_JOBS = 5;
+    private static final int NUMBER_OF_TASKS = NUMBER_OF_JOBS * PARALLELISM;
+
+    private static final int MANAGED_MEMORY_SIZE_BYTES = NUMBER_OF_TASKS * 25 
* 1024 * 1024;
+    private static final double MANAGED_MEMORY_SHARED_FRACTION = .85d;
+    private static final double WRITE_BUFFER_RATIO = 0.5;
+    private static final double EXPECTED_BLOCK_CACHE_SIZE =
+            calculateActualCacheCapacity(
+                    (long) (MANAGED_MEMORY_SIZE_BYTES * 
MANAGED_MEMORY_SHARED_FRACTION),
+                    WRITE_BUFFER_RATIO);
+    // try to check that the memory usage is limited
+    // however, there is no hard limit actually
+    // because of https://issues.apache.org/jira/browse/FLINK-15532
+    private static final double EFFECTIVE_LIMIT = EXPECTED_BLOCK_CACHE_SIZE * 
1.25;
+
+    private InMemoryReporter metricsReporter;
+    private MiniClusterWithClientResource cluster;
+
+    @Before
+    public void init() throws Exception {
+        metricsReporter = InMemoryReporter.create();
+        cluster =
+                new MiniClusterWithClientResource(
+                        new MiniClusterResourceConfiguration.Builder()
+                                
.setConfiguration(getConfiguration(metricsReporter))
+                                .setNumberTaskManagers(1)
+                                .setNumberSlotsPerTaskManager(NUMBER_OF_TASKS)
+                                .build());
+        cluster.before();
+    }
+
+    @After
+    public void destroy() {
+        cluster.after();
+        metricsReporter.close();
+    }
+
+    @Test
+    public void testBlockCache() throws Exception {
+        List<JobID> jobIDs = new ArrayList<>(NUMBER_OF_JOBS);
+        try {
+            // launch jobs
+            for (int i = 0; i < NUMBER_OF_JOBS; i++) {
+                
jobIDs.add(cluster.getRestClusterClient().submitJob(dag()).get());
+            }
+
+            // wait for init
+            Deadline initDeadline = Deadline.fromNow(Duration.ofMinutes(1));
+            for (JobID jid : jobIDs) {
+                waitForAllTaskRunning(cluster.getMiniCluster(), jid, false);
+                waitForAllMetricsReported(jid, initDeadline);
+            }
+
+            // check declared capacity
+            collectGaugeValues(jobIDs, "rocksdb.block-cache-capacity")
+                    .forEach(
+                            size ->
+                                    assertEquals(
+                                            "Unexpected rocksdb block cache 
capacity",
+                                            EXPECTED_BLOCK_CACHE_SIZE,
+                                            size,
+                                            0));
+
+            // do some work and check the actual usage of memory
+            for (int i = 0; i < 10; i++) {
+                Thread.sleep(50L);
+                DoubleSummaryStatistics stats =
+                        collectGaugeValues(jobIDs, "rocksdb.block-cache-usage")
+                                
.collect(Collectors.summarizingDouble((Double::doubleValue)));
+                assertEquals(
+                        String.format(
+                                "Block cache usage reported by different tasks 
varies too much: %s\n"

Review Comment:
   What's the idea behind verifying that the shared memory metric doesn't 
change much?  If you were accidentally allocating from exclusive memory, 
wouldn't you expect that this doesn't deviate much as well?  Or does this 
measure the cache used regardless of whether it's exclusive memory or not?



##########
flink-tests/src/test/java/org/apache/flink/test/state/TaskManagerWideRocksDbMemorySharingITCase.java:
##########
@@ -0,0 +1,257 @@
+/*
+ * 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.test.state;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.api.common.functions.RichMapFunction;
+import org.apache.flink.api.common.state.ListState;
+import org.apache.flink.api.common.state.ListStateDescriptor;
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.configuration.StateBackendOptions;
+import org.apache.flink.configuration.TaskManagerOptions;
+import org.apache.flink.contrib.streaming.state.RocksDBNativeMetricOptions;
+import org.apache.flink.contrib.streaming.state.RocksDBOptions;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.testutils.InMemoryReporter;
+import org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.CheckpointingMode;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.DiscardingSink;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.math.BigInteger;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.DoubleSummaryStatistics;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static 
org.apache.flink.api.common.restartstrategy.RestartStrategies.noRestart;
+import static 
org.apache.flink.contrib.streaming.state.RocksDBMemoryControllerUtils.calculateActualCacheCapacity;
+import static 
org.apache.flink.runtime.testutils.CommonTestUtils.waitForAllTaskRunning;
+import static org.apache.flink.util.Preconditions.checkState;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests that memory sharing scope and {@link 
TaskManagerOptions#MANAGED_MEMORY_SHARED_FRACTION}
+ * work as expected, i.e. make RocksDB use the same BlockCache and 
WriteBufferManager objects. It
+ * does so using RocksDB metrics.
+ */
+public class TaskManagerWideRocksDbMemorySharingITCase {
+    private static final int PARALLELISM = 4;
+    private static final int NUMBER_OF_JOBS = 5;
+    private static final int NUMBER_OF_TASKS = NUMBER_OF_JOBS * PARALLELISM;
+
+    private static final int MANAGED_MEMORY_SIZE_BYTES = NUMBER_OF_TASKS * 25 
* 1024 * 1024;
+    private static final double MANAGED_MEMORY_SHARED_FRACTION = .85d;
+    private static final double WRITE_BUFFER_RATIO = 0.5;
+    private static final double EXPECTED_BLOCK_CACHE_SIZE =
+            calculateActualCacheCapacity(
+                    (long) (MANAGED_MEMORY_SIZE_BYTES * 
MANAGED_MEMORY_SHARED_FRACTION),
+                    WRITE_BUFFER_RATIO);
+    // try to check that the memory usage is limited
+    // however, there is no hard limit actually
+    // because of https://issues.apache.org/jira/browse/FLINK-15532
+    private static final double EFFECTIVE_LIMIT = EXPECTED_BLOCK_CACHE_SIZE * 
1.25;
+
+    private InMemoryReporter metricsReporter;
+    private MiniClusterWithClientResource cluster;
+
+    @Before
+    public void init() throws Exception {
+        metricsReporter = InMemoryReporter.create();
+        cluster =
+                new MiniClusterWithClientResource(
+                        new MiniClusterResourceConfiguration.Builder()
+                                
.setConfiguration(getConfiguration(metricsReporter))
+                                .setNumberTaskManagers(1)
+                                .setNumberSlotsPerTaskManager(NUMBER_OF_TASKS)
+                                .build());
+        cluster.before();
+    }
+
+    @After
+    public void destroy() {
+        cluster.after();
+        metricsReporter.close();
+    }
+
+    @Test
+    public void testBlockCache() throws Exception {
+        List<JobID> jobIDs = new ArrayList<>(NUMBER_OF_JOBS);
+        try {
+            // launch jobs
+            for (int i = 0; i < NUMBER_OF_JOBS; i++) {
+                
jobIDs.add(cluster.getRestClusterClient().submitJob(dag()).get());
+            }
+
+            // wait for init
+            Deadline initDeadline = Deadline.fromNow(Duration.ofMinutes(1));
+            for (JobID jid : jobIDs) {
+                waitForAllTaskRunning(cluster.getMiniCluster(), jid, false);
+                waitForAllMetricsReported(jid, initDeadline);
+            }
+
+            // check declared capacity
+            collectGaugeValues(jobIDs, "rocksdb.block-cache-capacity")

Review Comment:
   Is this metric for the total shared memory used by rocks db and it doesn't 
matter if the memory is managed or not?



##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/runtime/SavepointEnvironment.java:
##########
@@ -126,6 +128,7 @@ private SavepointEnvironment(
         this.taskStateManager = new 
SavepointTaskStateManager(prioritizedOperatorSubtaskState);
         this.ioManager = new 
IOManagerAsync(ConfigurationUtils.parseTempDirectories(configuration));
         this.memoryManager = MemoryManager.create(64 * 1024 * 1024, 
DEFAULT_PAGE_SIZE);
+        this.sharedMemoryManager = MemoryManager.create(0, DEFAULT_PAGE_SIZE);

Review Comment:
   Doesn't this create a memory manager with 0 pages? I assume this is not 
utilized in some of these environments. It seems like `TaskManagerServices` is 
where it's set to a real value.  Just curious, how is this environment used?



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to