You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2020/10/28 12:36:37 UTC

[flink-statefun] branch release-2.2 updated (e10cdd3 -> 8823b27)

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

tzulitai pushed a change to branch release-2.2
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git.


    from e10cdd3  [FLINK-19515][python] InvocationContext use after dispose fix
     new a7efe0a  [FLINK-19692][core] Expose the list of assigned key groups
     new 3a5b541  [FLINK-19692][core] Write all the assigned KeyGroups into the raw keyed stream
     new 8823b27  [FLINK-19692][core] Add a header for each keyGroup

The 3 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:
 .../core/logger/CheckpointedStreamOperations.java  |  2 +
 .../statefun/flink/core/logger/KeyGroupStream.java |  4 ++
 .../flink/statefun/flink/core/logger/Loggers.java  |  5 ++
 .../flink/core/logger/UnboundedFeedbackLogger.java | 61 +++++++++++++++--
 .../core/logger/UnboundedFeedbackLoggerTest.java   | 76 ++++++++++++++++++++--
 5 files changed, 137 insertions(+), 11 deletions(-)


[flink-statefun] 01/03: [FLINK-19692][core] Expose the list of assigned key groups

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

tzulitai pushed a commit to branch release-2.2
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit a7efe0a3b50f2651afff405582ca1efd27330cf1
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Wed Oct 21 14:32:03 2020 +0200

    [FLINK-19692][core] Expose the list of assigned key groups
    
    This commit exposes the list of key groups that can be written
    into the raw keyed stream.
---
 .../core/logger/CheckpointedStreamOperations.java    |  2 ++
 .../flink/statefun/flink/core/logger/Loggers.java    |  5 +++++
 .../core/logger/UnboundedFeedbackLoggerTest.java     | 20 +++++++++++++++++---
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/CheckpointedStreamOperations.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/CheckpointedStreamOperations.java
index 2540934..82f79c6 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/CheckpointedStreamOperations.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/CheckpointedStreamOperations.java
@@ -25,6 +25,8 @@ public interface CheckpointedStreamOperations {
 
   void requireKeyedStateCheckpointed(OutputStream keyedStateCheckpointOutputStream);
 
+  Iterable<Integer> keyGroupList(OutputStream stream);
+
   void startNewKeyGroup(OutputStream stream, int keyGroup) throws IOException;
 
   Closeable acquireLease(OutputStream keyedStateCheckpointOutputStream);
diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/Loggers.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/Loggers.java
index aee7536..d0c8db6 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/Loggers.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/Loggers.java
@@ -87,6 +87,11 @@ public final class Loggers {
     }
 
     @Override
+    public Iterable<Integer> keyGroupList(OutputStream stream) {
+      return cast(stream).getKeyGroupList();
+    }
+
+    @Override
     public void startNewKeyGroup(OutputStream stream, int keyGroup) throws IOException {
       cast(stream).startNewKeyGroup(keyGroup);
     }
diff --git a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
index dd7088f..08600ef 100644
--- a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
+++ b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertThat;
 import java.io.*;
 import java.util.ArrayList;
 import java.util.function.Function;
+import java.util.stream.IntStream;
 import org.apache.flink.api.common.typeutils.base.IntSerializer;
 import org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync;
 import org.apache.flink.statefun.flink.core.di.ObjectContainer;
@@ -114,12 +115,19 @@ public class UnboundedFeedbackLoggerTest {
         Loggers.unboundedSpillableLoggerContainer(
             IO_MANAGER, maxParallelism, totalMemory, IntSerializer.INSTANCE, Function.identity());
 
-    container.add("checkpoint-stream-ops", CheckpointedStreamOperations.class, NOOP.INSTANCE);
+    container.add(
+        "checkpoint-stream-ops",
+        CheckpointedStreamOperations.class,
+        new NoopStreamOps(maxParallelism));
     return container.get(UnboundedFeedbackLoggerFactory.class).create();
   }
 
-  enum NOOP implements CheckpointedStreamOperations {
-    INSTANCE;
+  static final class NoopStreamOps implements CheckpointedStreamOperations {
+    private final int maxParallelism;
+
+    NoopStreamOps(int maxParallelism) {
+      this.maxParallelism = maxParallelism;
+    }
 
     @Override
     public void requireKeyedStateCheckpointed(OutputStream keyedStateCheckpointOutputStream) {
@@ -127,6 +135,12 @@ public class UnboundedFeedbackLoggerTest {
     }
 
     @Override
+    public Iterable<Integer> keyGroupList(OutputStream stream) {
+      IntStream range = IntStream.range(0, maxParallelism);
+      return range::iterator;
+    }
+
+    @Override
     public void startNewKeyGroup(OutputStream stream, int keyGroup) {}
 
     @Override


[flink-statefun] 02/03: [FLINK-19692][core] Write all the assigned KeyGroups into the raw keyed stream

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

tzulitai pushed a commit to branch release-2.2
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit 3a5b54143d634ed4f0a33e541997d60459e3613e
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Wed Oct 21 15:28:39 2020 +0200

    [FLINK-19692][core] Write all the assigned KeyGroups into the raw keyed stream
---
 .../statefun/flink/core/logger/KeyGroupStream.java      |  4 ++++
 .../flink/core/logger/UnboundedFeedbackLogger.java      | 17 ++++++++++++-----
 .../flink/core/logger/UnboundedFeedbackLoggerTest.java  |  3 ++-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/KeyGroupStream.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/KeyGroupStream.java
index 6628225..5b22864 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/KeyGroupStream.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/KeyGroupStream.java
@@ -94,4 +94,8 @@ final class KeyGroupStream<T> {
       memoryPool.release(segment);
     }
   }
+
+  public static void writeEmptyTo(DataOutputView target) throws IOException {
+    target.writeInt(0);
+  }
 }
diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
index 409f714..51e8e7b 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
@@ -24,7 +24,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.TreeMap;
 import java.util.function.Supplier;
@@ -99,11 +98,19 @@ public final class UnboundedFeedbackLogger<T> implements FeedbackLogger<T> {
     checkState(keyedStateOutputStream != null, "Trying to flush envelopes not in a logging state");
 
     final DataOutputView target = new DataOutputViewStreamWrapper(keyedStateOutputStream);
-    for (Entry<Integer, KeyGroupStream<T>> entry : keyGroupStreams.entrySet()) {
-      checkpointedStreamOperations.startNewKeyGroup(keyedStateOutputStream, entry.getKey());
+    final Iterable<Integer> assignedKeyGroupIds =
+        checkpointedStreamOperations.keyGroupList(keyedStateOutputStream);
+    // the underlying checkpointed raw stream, requires that all key groups assigned
+    // to this operator must be written to the underlying stream.
+    for (Integer keyGroupId : assignedKeyGroupIds) {
+      checkpointedStreamOperations.startNewKeyGroup(keyedStateOutputStream, keyGroupId);
 
-      KeyGroupStream stream = entry.getValue();
-      stream.writeTo(target);
+      @Nullable KeyGroupStream<T> stream = keyGroupStreams.get(keyGroupId);
+      if (stream == null) {
+        KeyGroupStream.writeEmptyTo(target);
+      } else {
+        stream.writeTo(target);
+      }
     }
   }
 
diff --git a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
index 08600ef..ac7efdd 100644
--- a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
+++ b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
@@ -27,6 +27,7 @@ import java.util.stream.IntStream;
 import org.apache.flink.api.common.typeutils.base.IntSerializer;
 import org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync;
 import org.apache.flink.statefun.flink.core.di.ObjectContainer;
+import org.hamcrest.Matchers;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
@@ -57,7 +58,7 @@ public class UnboundedFeedbackLoggerTest {
     logger.startLogging(output);
     logger.commit();
 
-    assertThat(output.size(), is(0));
+    assertThat(output.size(), Matchers.greaterThan(0));
   }
 
   @Test(expected = IllegalStateException.class)


[flink-statefun] 03/03: [FLINK-19692][core] Add a header for each keyGroup

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

tzulitai pushed a commit to branch release-2.2
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git

commit 8823b27defff64fab2532fb01d1b5f8ae5cfdd72
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Wed Oct 21 20:20:45 2020 +0200

    [FLINK-19692][core] Add a header for each keyGroup
    
    This closes #168.
---
 .../flink/core/logger/UnboundedFeedbackLogger.java | 44 +++++++++++++++++-
 .../core/logger/UnboundedFeedbackLoggerTest.java   | 53 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)

diff --git a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
index 51e8e7b..7464f09 100644
--- a/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
+++ b/statefun-flink/statefun-flink-core/src/main/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLogger.java
@@ -19,10 +19,13 @@ package org.apache.flink.statefun.flink.core.logger;
 
 import static org.apache.flink.util.Preconditions.checkState;
 
+import java.io.BufferedInputStream;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PushbackInputStream;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Objects;
 import java.util.TreeMap;
@@ -30,8 +33,11 @@ import java.util.function.Supplier;
 import java.util.function.ToIntFunction;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.apache.flink.annotation.VisibleForTesting;
 import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.memory.DataInputView;
 import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputSerializer;
 import org.apache.flink.core.memory.DataOutputView;
 import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
 import org.apache.flink.statefun.flink.core.feedback.FeedbackConsumer;
@@ -104,6 +110,7 @@ public final class UnboundedFeedbackLogger<T> implements FeedbackLogger<T> {
     // to this operator must be written to the underlying stream.
     for (Integer keyGroupId : assignedKeyGroupIds) {
       checkpointedStreamOperations.startNewKeyGroup(keyedStateOutputStream, keyGroupId);
+      Header.writeHeader(target);
 
       @Nullable KeyGroupStream<T> stream = keyGroupStreams.get(keyGroupId);
       if (stream == null) {
@@ -116,8 +123,8 @@ public final class UnboundedFeedbackLogger<T> implements FeedbackLogger<T> {
 
   public void replyLoggedEnvelops(InputStream rawKeyedStateInputs, FeedbackConsumer<T> consumer)
       throws Exception {
-
-    DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(rawKeyedStateInputs);
+    DataInputView in =
+        new DataInputViewStreamWrapper(Header.skipHeaderSilently(rawKeyedStateInputs));
     KeyGroupStream.readFrom(in, serializer, consumer);
   }
 
@@ -138,4 +145,37 @@ public final class UnboundedFeedbackLogger<T> implements FeedbackLogger<T> {
     keyedStateOutputStream = null;
     keyGroupStreams.clear();
   }
+
+  @VisibleForTesting
+  static final class Header {
+    private static final int STATEFUN_VERSION = 0;
+    private static final int STATEFUN_MAGIC = 710818519;
+    private static final byte[] HEADER_BYTES = headerBytes();
+
+    public static void writeHeader(DataOutputView target) throws IOException {
+      target.write(HEADER_BYTES);
+    }
+
+    public static InputStream skipHeaderSilently(InputStream rawKeyedInput) throws IOException {
+      byte[] header = new byte[HEADER_BYTES.length];
+      PushbackInputStream input =
+          new PushbackInputStream(new BufferedInputStream(rawKeyedInput), header.length);
+      int bytesRead = input.read(header);
+      if (bytesRead > 0 && !Arrays.equals(header, HEADER_BYTES)) {
+        input.unread(header, 0, bytesRead);
+      }
+      return input;
+    }
+
+    private static byte[] headerBytes() {
+      DataOutputSerializer out = new DataOutputSerializer(8);
+      try {
+        out.writeInt(STATEFUN_VERSION);
+        out.writeInt(STATEFUN_MAGIC);
+      } catch (IOException e) {
+        throw new IllegalStateException("Unable to compute the header bytes");
+      }
+      return out.getCopyOfBuffer();
+    }
+  }
 }
diff --git a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
index ac7efdd..afe504a 100644
--- a/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
+++ b/statefun-flink/statefun-flink-core/src/test/java/org/apache/flink/statefun/flink/core/logger/UnboundedFeedbackLoggerTest.java
@@ -25,8 +25,11 @@ import java.util.ArrayList;
 import java.util.function.Function;
 import java.util.stream.IntStream;
 import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputSerializer;
 import org.apache.flink.runtime.io.disk.iomanager.IOManagerAsync;
 import org.apache.flink.statefun.flink.core.di.ObjectContainer;
+import org.apache.flink.statefun.flink.core.logger.UnboundedFeedbackLogger.Header;
 import org.hamcrest.Matchers;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -73,12 +76,62 @@ public class UnboundedFeedbackLoggerTest {
     roundTrip(100, 1024);
   }
 
+  @Test
+  public void roundTripWithoutElements() throws Exception {
+    roundTrip(0, 1024);
+  }
+
   @Ignore
   @Test
   public void roundTripWithSpill() throws Exception {
     roundTrip(1_000_000, 0);
   }
 
+  @Test
+  public void roundTripWithHeader() throws IOException {
+    DataOutputSerializer out = new DataOutputSerializer(32);
+    Header.writeHeader(out);
+    out.writeInt(123);
+    out.writeInt(456);
+    InputStream in = new ByteArrayInputStream(out.getCopyOfBuffer());
+
+    DataInputViewStreamWrapper view = new DataInputViewStreamWrapper(Header.skipHeaderSilently(in));
+
+    assertThat(view.readInt(), is(123));
+    assertThat(view.readInt(), is(456));
+  }
+
+  @Test
+  public void roundTripWithoutHeader() throws IOException {
+    DataOutputSerializer out = new DataOutputSerializer(32);
+    out.writeInt(123);
+    out.writeInt(456);
+    InputStream in = new ByteArrayInputStream(out.getCopyOfBuffer());
+
+    DataInputViewStreamWrapper view = new DataInputViewStreamWrapper(Header.skipHeaderSilently(in));
+
+    assertThat(view.readInt(), is(123));
+    assertThat(view.readInt(), is(456));
+  }
+
+  @Test
+  public void emptyKeyGroupWithHeader() throws IOException {
+    DataOutputSerializer out = new DataOutputSerializer(32);
+    Header.writeHeader(out);
+    InputStream in = new ByteArrayInputStream(out.getCopyOfBuffer());
+
+    DataInputViewStreamWrapper view = new DataInputViewStreamWrapper(Header.skipHeaderSilently(in));
+
+    assertThat(view.read(), is(-1));
+  }
+
+  @Test
+  public void emptyKeyGroupWithoutHeader() throws IOException {
+    InputStream in = new ByteArrayInputStream(new byte[0]);
+    DataInputViewStreamWrapper view = new DataInputViewStreamWrapper(Header.skipHeaderSilently(in));
+    assertThat(view.read(), is(-1));
+  }
+
   private void roundTrip(int numElements, int maxMemoryInBytes) throws Exception {
     InputStream input = serializeKeyGroup(1, maxMemoryInBytes, numElements);