Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-draft][flink] The FinkCommitter's commit info class could not be inferred. #2086

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.IOException;
import java.io.Serializable;

public interface Serializer<T> extends Serializable{
public interface Serializer<T> extends Serializable {

/**
* Serializes the given object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.seatunnel.translation.flink.serialization;

import org.apache.seatunnel.api.serialization.Serializer;
import org.apache.seatunnel.translation.flink.sink.CommitWrapper;

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;

public class CommitWrapperSerializer<T> implements SimpleVersionedSerializer<CommitWrapper<T>> {
private final Serializer<T> serializer;

public CommitWrapperSerializer(Serializer<T> serializer) {
this.serializer = serializer;
}

@Override
public int getVersion() {
return 0;
}

@Override
public byte[] serialize(CommitWrapper<T> commitWrapper) throws IOException {
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream out = new DataOutputStream(baos)) {
byte[] serialize = serializer.serialize(commitWrapper.getCommit());
out.writeInt(serialize.length);
out.write(serialize);
out.flush();
return baos.toByteArray();
}
}

@Override
public CommitWrapper<T> deserialize(int version, byte[] serialized) throws IOException {
try (final ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
final DataInputStream in = new DataInputStream(bais)) {
final int size = in.readInt();
final byte[] stateBytes = new byte[size];
in.read(stateBytes);
T commitT = serializer.deserialize(stateBytes);
return new CommitWrapper<>(commitT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.seatunnel.translation.flink.sink;

public class CommitWrapper<CommitT> {
private final CommitT commit;

public CommitWrapper(CommitT commit) {
this.commit = commit;
}

public CommitT getCommit() {
return commit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FlinkCommitter<CommT> implements Committer<CommT> {
public class FlinkCommitter<CommT> implements Committer<CommitWrapper<CommT>> {

private static final Logger LOGGER = LoggerFactory.getLogger(FlinkCommitter.class);

Expand All @@ -38,8 +39,10 @@ public class FlinkCommitter<CommT> implements Committer<CommT> {
}

@Override
public List<CommT> commit(List<CommT> committables) throws IOException {
List<CommT> reCommittable = sinkCommitter.commit(committables);
public List<CommitWrapper<CommT>> commit(List<CommitWrapper<CommT>> committables) throws IOException {
List<CommT> reCommittable = sinkCommitter.commit(committables.stream()
.map(CommitWrapper::getCommit)
.collect(Collectors.toList()));
if (reCommittable != null && !reCommittable.isEmpty()) {
LOGGER.warn("this version not support re-commit when use flink engine");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class FlinkGlobalCommitter<CommT, GlobalCommT> implements GlobalCommitter<CommT, GlobalCommT> {
public class FlinkGlobalCommitter<CommT, GlobalCommT> implements GlobalCommitter<CommitWrapper<CommT>, GlobalCommT> {

private static final Logger LOGGER = LoggerFactory.getLogger(FlinkGlobalCommitter.class);

Expand All @@ -44,8 +45,10 @@ public List<GlobalCommT> filterRecoveredCommittables(List globalCommittables) th
}

@Override
public GlobalCommT combine(List<CommT> committables) throws IOException {
return aggregatedCommitter.combine(committables);
public GlobalCommT combine(List<CommitWrapper<CommT>> committables) throws IOException {
return aggregatedCommitter.combine(committables.stream()
.map(CommitWrapper::getCommit)
.collect(Collectors.toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.seatunnel.api.sink.DefaultSinkWriterContext;
import org.apache.seatunnel.api.sink.SeaTunnelSink;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.translation.flink.serialization.CommitWrapperSerializer;
import org.apache.seatunnel.translation.flink.serialization.FlinkSimpleVersionedSerializer;
import org.apache.seatunnel.translation.flink.serialization.FlinkWriterStateSerializer;

Expand All @@ -36,7 +37,7 @@
import java.util.stream.Collectors;

@SuppressWarnings("unchecked")
public class FlinkSink<InputT, CommT, WriterStateT, GlobalCommT> implements Sink<InputT, CommT,
public class FlinkSink<InputT, CommT, WriterStateT, GlobalCommT> implements Sink<InputT, CommitWrapper<CommT>,
FlinkWriterState<WriterStateT>, GlobalCommT> {

private final SeaTunnelSink<SeaTunnelRow, WriterStateT, CommT, GlobalCommT> sink;
Expand All @@ -49,7 +50,7 @@ public FlinkSink(SeaTunnelSink<SeaTunnelRow, WriterStateT, CommT, GlobalCommT> s
}

@Override
public SinkWriter<InputT, CommT, FlinkWriterState<WriterStateT>> createWriter(org.apache.flink.api.connector.sink.Sink.InitContext context, List<FlinkWriterState<WriterStateT>> states) throws IOException {
public SinkWriter<InputT, CommitWrapper<CommT>, FlinkWriterState<WriterStateT>> createWriter(org.apache.flink.api.connector.sink.Sink.InitContext context, List<FlinkWriterState<WriterStateT>> states) throws IOException {
// TODO add subtask and parallelism.
org.apache.seatunnel.api.sink.SinkWriter.Context stContext =
new DefaultSinkWriterContext(configuration, 0, 0);
Expand All @@ -63,18 +64,18 @@ public SinkWriter<InputT, CommT, FlinkWriterState<WriterStateT>> createWriter(or
}

@Override
public Optional<Committer<CommT>> createCommitter() throws IOException {
public Optional<Committer<CommitWrapper<CommT>>> createCommitter() throws IOException {
return sink.createCommitter().map(FlinkCommitter::new);
}

@Override
public Optional<GlobalCommitter<CommT, GlobalCommT>> createGlobalCommitter() throws IOException {
public Optional<GlobalCommitter<CommitWrapper<CommT>, GlobalCommT>> createGlobalCommitter() throws IOException {
return sink.createAggregatedCommitter().map(FlinkGlobalCommitter::new);
}

@Override
public Optional<SimpleVersionedSerializer<CommT>> getCommittableSerializer() {
return sink.getCommitInfoSerializer().map(FlinkSimpleVersionedSerializer::new);
public Optional<SimpleVersionedSerializer<CommitWrapper<CommT>>> getCommittableSerializer() {
return sink.getCommitInfoSerializer().map(CommitWrapperSerializer::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

public class FlinkSinkWriter<InputT, CommT, WriterStateT> implements SinkWriter<InputT, CommT, FlinkWriterState<WriterStateT>> {
public class FlinkSinkWriter<InputT, CommT, WriterStateT> implements SinkWriter<InputT, CommitWrapper<CommT>, FlinkWriterState<WriterStateT>> {

private final org.apache.seatunnel.api.sink.SinkWriter<SeaTunnelRow, CommT, WriterStateT> sinkWriter;
private final FlinkRowConverter rowSerialization;
Expand All @@ -55,9 +55,9 @@ public void write(InputT element, org.apache.flink.api.connector.sink.SinkWriter
}

@Override
public List<CommT> prepareCommit(boolean flush) throws IOException {
public List<CommitWrapper<CommT>> prepareCommit(boolean flush) throws IOException {
Optional<CommT> commTOptional = sinkWriter.prepareCommit();
return commTOptional.map(Collections::singletonList).orElse(Collections.emptyList());
return commTOptional.map(CommitWrapper::new).map(Collections::singletonList).orElse(Collections.emptyList());
}

@Override
Expand Down