-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-6086] [py] Clean up PythonSender/-Streamer generics
- Loading branch information
Showing
11 changed files
with
479 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ython/src/main/java/org/apache/flink/python/api/streaming/data/PythonDualInputSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.python.api.streaming.data; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* This class is a {@link PythonSender} for operations with two input streams. | ||
* | ||
* @param <IN1> first input type | ||
* @param <IN2> second input type | ||
*/ | ||
public class PythonDualInputSender<IN1, IN2> extends PythonSender { | ||
|
||
private static final long serialVersionUID = 614115041181108878L; | ||
|
||
private transient Serializer<IN1> serializer1; | ||
private transient Serializer<IN2> serializer2; | ||
|
||
/** | ||
* Extracts records from an iterator and writes them to the memory-mapped file. This method assumes that all values | ||
* in the iterator are of the same type. This method does NOT take care of synchronization. The caller must | ||
* guarantee that the file may be written to before calling this method. | ||
* | ||
* @param input iterator containing records | ||
* @return size of the written buffer | ||
* @throws IOException | ||
*/ | ||
public int sendBuffer1(SingleElementPushBackIterator<IN1> input) throws IOException { | ||
if (serializer1 == null) { | ||
IN1 value = input.next(); | ||
serializer1 = getSerializer(value); | ||
input.pushBack(value); | ||
} | ||
return sendBuffer(input, serializer1); | ||
} | ||
|
||
/** | ||
* Extracts records from an iterator and writes them to the memory-mapped file. This method assumes that all values | ||
* in the iterator are of the same type. This method does NOT take care of synchronization. The caller must | ||
* guarantee that the file may be written to before calling this method. | ||
* | ||
* @param input iterator containing records | ||
* @return size of the written buffer | ||
* @throws IOException | ||
*/ | ||
public int sendBuffer2(SingleElementPushBackIterator<IN2> input) throws IOException { | ||
if (serializer2 == null) { | ||
IN2 value = input.next(); | ||
serializer2 = getSerializer(value); | ||
input.pushBack(value); | ||
} | ||
return sendBuffer(input, serializer2); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...hon/src/main/java/org/apache/flink/python/api/streaming/data/PythonDualInputStreamer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.python.api.streaming.data; | ||
|
||
import org.apache.flink.api.common.functions.AbstractRichFunction; | ||
import org.apache.flink.util.Collector; | ||
|
||
import java.io.IOException; | ||
import java.net.SocketTimeoutException; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* This class is a {@link PythonStreamer} for operations with two input stream. | ||
* | ||
* @param <IN1> first input type | ||
* @param <IN2> second input type | ||
* @param <OUT> output type | ||
*/ | ||
public class PythonDualInputStreamer<IN1, IN2, OUT> extends PythonStreamer<PythonDualInputSender<IN1, IN2>, OUT> { | ||
|
||
private static final long serialVersionUID = -607175070491761873L; | ||
|
||
public PythonDualInputStreamer(AbstractRichFunction function, int envID, int setID, boolean usesByteArray) { | ||
super(function, envID, setID, usesByteArray, new PythonDualInputSender<IN1, IN2>()); | ||
} | ||
|
||
/** | ||
* Sends all values contained in both iterators to the external process and collects all results. | ||
* | ||
* @param iterator1 first input stream | ||
* @param iterator2 second input stream | ||
* @param c collector | ||
* @throws IOException | ||
*/ | ||
public final void streamBufferWithGroups(Iterator<IN1> iterator1, Iterator<IN2> iterator2, Collector<OUT> c) throws IOException { | ||
SingleElementPushBackIterator<IN1> i1 = new SingleElementPushBackIterator<>(iterator1); | ||
SingleElementPushBackIterator<IN2> i2 = new SingleElementPushBackIterator<>(iterator2); | ||
try { | ||
int size; | ||
if (i1.hasNext() || i2.hasNext()) { | ||
while (true) { | ||
int sig = in.readInt(); | ||
switch (sig) { | ||
case SIGNAL_BUFFER_REQUEST_G0: | ||
if (i1.hasNext()) { | ||
size = sender.sendBuffer1(i1); | ||
sendWriteNotification(size, i1.hasNext()); | ||
} | ||
break; | ||
case SIGNAL_BUFFER_REQUEST_G1: | ||
if (i2.hasNext()) { | ||
size = sender.sendBuffer2(i2); | ||
sendWriteNotification(size, i2.hasNext()); | ||
} | ||
break; | ||
case SIGNAL_FINISHED: | ||
return; | ||
case SIGNAL_ERROR: | ||
try { | ||
outPrinter.join(); | ||
} catch (InterruptedException e) { | ||
outPrinter.interrupt(); | ||
} | ||
try { | ||
errorPrinter.join(); | ||
} catch (InterruptedException e) { | ||
errorPrinter.interrupt(); | ||
} | ||
throw new RuntimeException( | ||
"External process for task " + function.getRuntimeContext().getTaskName() + " terminated prematurely due to an error." + msg); | ||
default: | ||
receiver.collectBuffer(c, sig); | ||
sendReadConfirmation(); | ||
break; | ||
} | ||
} | ||
} | ||
} catch (SocketTimeoutException ignored) { | ||
throw new RuntimeException("External process for task " + function.getRuntimeContext().getTaskName() + " stopped responding." + msg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.