You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2022/07/06 07:11:49 UTC

[flink] branch master updated (78a96a4b1e8 -> 342dfd16e57)

This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


    from 78a96a4b1e8 [FLINK-26323][tests] Allow CREATED state
     new 8d548d683db [FLINK-26979][core] Make Transformation#getNewNodeId() thread-safe
     new 342dfd16e57 [FLINK-26979][core][tests] Add thread-safety test

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/flink/api/dag/Transformation.java   |  6 +--
 .../apache/flink/api/dag/TransformationTest.java   | 45 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 3 deletions(-)


[flink] 01/02: [FLINK-26979][core] Make Transformation#getNewNodeId() thread-safe

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 8d548d683dbb005ee75cf07f99fc014901c0c94a
Author: Shubham Bansal <il...@gmail.com>
AuthorDate: Sat Jun 18 00:19:02 2022 -0700

    [FLINK-26979][core] Make Transformation#getNewNodeId() thread-safe
---
 .../src/main/java/org/apache/flink/api/dag/Transformation.java      | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flink-core/src/main/java/org/apache/flink/api/dag/Transformation.java b/flink-core/src/main/java/org/apache/flink/api/dag/Transformation.java
index 0d30b17e155..afb21fbbbc8 100644
--- a/flink-core/src/main/java/org/apache/flink/api/dag/Transformation.java
+++ b/flink-core/src/main/java/org/apache/flink/api/dag/Transformation.java
@@ -37,6 +37,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.apache.flink.util.Preconditions.checkArgument;
 import static org.apache.flink.util.Preconditions.checkNotNull;
@@ -108,11 +109,10 @@ public abstract class Transformation<T> {
     public static final int UPPER_BOUND_MAX_PARALLELISM = 1 << 15;
 
     // This is used to assign a unique ID to every Transformation
-    protected static Integer idCounter = 0;
+    private static final AtomicInteger ID_COUNTER = new AtomicInteger(0);
 
     public static int getNewNodeId() {
-        idCounter++;
-        return idCounter;
+        return ID_COUNTER.incrementAndGet();
     }
 
     protected final int id;


[flink] 02/02: [FLINK-26979][core][tests] Add thread-safety test

Posted by ch...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit 342dfd16e575ee23ef6058869a93b44deb0b4681
Author: Chesnay Schepler <ch...@apache.org>
AuthorDate: Tue Jul 5 10:46:40 2022 +0200

    [FLINK-26979][core][tests] Add thread-safety test
---
 .../apache/flink/api/dag/TransformationTest.java   | 45 ++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/flink-core/src/test/java/org/apache/flink/api/dag/TransformationTest.java b/flink-core/src/test/java/org/apache/flink/api/dag/TransformationTest.java
index 494fc9d3465..c14ab0d8e3d 100644
--- a/flink-core/src/test/java/org/apache/flink/api/dag/TransformationTest.java
+++ b/flink-core/src/test/java/org/apache/flink/api/dag/TransformationTest.java
@@ -20,16 +20,22 @@ package org.apache.flink.api.dag;
 
 import org.apache.flink.api.common.typeinfo.TypeInformation;
 import org.apache.flink.core.memory.ManagedMemoryUseCase;
+import org.apache.flink.core.testutils.CheckedThread;
+import org.apache.flink.core.testutils.OneShotLatch;
 import org.apache.flink.util.TestLogger;
 
 import org.junit.Before;
 import org.junit.Test;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 
 /** Tests for {@link Transformation}. */
@@ -42,6 +48,45 @@ public class TransformationTest extends TestLogger {
         transformation = new TestTransformation<>("t", null, 1);
     }
 
+    @Test
+    public void testGetNewNodeIdIsThreadSafe() throws Exception {
+        final int numThreads = 10;
+        final int numIdsPerThread = 100;
+
+        final List<CheckedThread> threads = new ArrayList<>();
+
+        final OneShotLatch startLatch = new OneShotLatch();
+
+        final List<List<Integer>> idLists = Collections.synchronizedList(new ArrayList<>());
+        for (int x = 0; x < numThreads; x++) {
+            threads.add(
+                    new CheckedThread() {
+                        @Override
+                        public void go() throws Exception {
+                            startLatch.await();
+
+                            final List<Integer> ids = new ArrayList<>();
+                            for (int c = 0; c < numIdsPerThread; c++) {
+                                ids.add(Transformation.getNewNodeId());
+                            }
+                            idLists.add(ids);
+                        }
+                    });
+        }
+        threads.forEach(Thread::start);
+
+        startLatch.trigger();
+
+        for (CheckedThread thread : threads) {
+            thread.sync();
+        }
+
+        final Set<Integer> deduplicatedIds =
+                idLists.stream().flatMap(List::stream).collect(Collectors.toSet());
+
+        assertEquals(numThreads * numIdsPerThread, deduplicatedIds.size());
+    }
+
     @Test
     public void testDeclareManagedMemoryUseCase() {
         transformation.declareManagedMemoryUseCaseAtOperatorScope(