hlteoh37 commented on code in PR #49: URL: https://github.com/apache/flink-connector-aws/pull/49#discussion_r1182458530
########## flink-connector-aws-kinesis-streams/src/main/java/org/apache/flink/connector/kinesis/source/enumerator/KinesisStreamsSourceEnumeratorStateSerializer.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.connector.kinesis.source.enumerator; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.kinesis.source.split.KinesisShardSplit; +import org.apache.flink.connector.kinesis.source.split.KinesisShardSplitSerializer; +import org.apache.flink.core.io.SimpleVersionedSerializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +/** TODO: Add JAvadoc. */ +@Internal +public class KinesisStreamsSourceEnumeratorStateSerializer + implements SimpleVersionedSerializer<KinesisStreamsSourceEnumeratorState> { + + private static final int CURRENT_VERSION = 0; + + private final KinesisShardSplitSerializer splitSerializer; + + public KinesisStreamsSourceEnumeratorStateSerializer(KinesisShardSplitSerializer splitSerializer) { + this.splitSerializer = splitSerializer; + } + + @Override + public int getVersion() { + return CURRENT_VERSION; + } + + @Override + public byte[] serialize(KinesisStreamsSourceEnumeratorState kinesisStreamsSourceEnumeratorState) + throws IOException { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(baos)) { + + out.writeInt(getVersion()); + + boolean hasLastSeenShardId = + kinesisStreamsSourceEnumeratorState.getLastSeenShardId() != null; + out.writeBoolean(hasLastSeenShardId); + if (hasLastSeenShardId) { + out.writeUTF(kinesisStreamsSourceEnumeratorState.getLastSeenShardId()); + } + + out.writeInt(kinesisStreamsSourceEnumeratorState.getCompletedShardIds().size()); + for (String shardId : kinesisStreamsSourceEnumeratorState.getCompletedShardIds()) { + out.writeUTF(shardId); + } + + out.writeInt(kinesisStreamsSourceEnumeratorState.getUnassignedSplits().size()); + out.writeInt(splitSerializer.getVersion()); + for (KinesisShardSplit split : kinesisStreamsSourceEnumeratorState.getUnassignedSplits()) { + byte[] serializedSplit = splitSerializer.serialize(split); + out.writeInt(serializedSplit.length); + out.write(serializedSplit); + } + + out.flush(); + + return baos.toByteArray(); + } + } + + @Override + public KinesisStreamsSourceEnumeratorState deserialize( + int version, byte[] serializedEnumeratorState) throws IOException { + try (ByteArrayInputStream bais = new ByteArrayInputStream(serializedEnumeratorState); + DataInputStream in = new DataInputStream(bais)) { + + final int serializerVersion = in.readInt(); + + if (serializerVersion != getVersion()) { + throw new IOException("Trying to deserialize KinesisStreamsSourceEnumeratorState serialized with unsupported version of " + getVersion()); Review Comment: Great shout. Changed the two version exceptions into `VersionMismatchException`, but others left as IOException because they are -- 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