You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/11/28 18:42:16 UTC

[GitHub] [flink] AlanConfluent commented on a diff in pull request #21264: [FLINK-29928][runtime, state] Share RocksDB memory across TM slots

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


##########
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:
   That makes sense.  If it works in practice, then that works.  The alternative might be separate metrics for shared vs exclusive memory -- that might be useful if they are both in use.  Or is there some other way to access the internal state?  For these integration tests, it seems like only the public APIs are used, so maybe this isn't an option.



-- 
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-unsubscribe@flink.apache.org

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