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
[FLINK-2490][FIX]Remove the retryForever check in function streamFrom… #992
Conversation
if you remove that check, retryForever is unused and can be removed completely. |
Yes, I understand you. |
If you think it was necessary why was your first step to remove it's usage... |
Hah.... |
This fix looks valid. Can it be included in an extended test for the socket function? Something that validates that the function properly tries to reconnect? |
@StephanEwen |
@HuangWHWHW |
@mxm |
@mxm if (data == -1) {
|
@HuangWHWHW A couple of things I noticed apart from the
@HuangWHWHW You asked how you could count the number of retries in a unit test. Typically, you would insert a |
@mxm |
Actually point 3 is not so bad because we're using a buffered reader that fills the buffer and does not read a character from the socket on every call to The |
Hi, there are two more questions: |
Reading by character is the way to go if we use a custom
Ok :) Here is a minimal working example where public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(12345);
final SocketAddress socketAddress = socket.getLocalSocketAddress();
new Thread(new Runnable() {
@Override
public void run() {
Socket socket = new Socket();
try {
socket.connect(socketAddress);
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println((bufferedReader.read()));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Socket channel = socket.accept();
channel.close();
} Output:
|
Thank you! |
@mxm |
Thanks for your changes. I think we should use For you test case: It's not considered good practice to mix production and test code. You're doing that by introducing the
I hope that this helps you. If not, feel free to ask more questions. |
private Socket socket; | ||
private static final int CONNECTION_TIMEOUT_TIME = 0; | ||
private static final int CONNECTION_RETRY_SLEEP = 1000; | ||
|
||
private volatile boolean isExit = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this flag necessary? We have isRunning
already.
Hi Max, |
Thanks for you changes. In addition to the "infinity" test, can you add a test that checks for a certain number of retries (e.g. 10)? Also please add a check for 1 and 0 retries. It's always good to test corner cases :) |
@mxm |
channel = serverSo.accept(); | ||
channel.close(); | ||
serverSo.close(); | ||
while(source.socketSource.retrys < 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're "busy waiting" here which consumes a lot of CPU. You could check in regular intervals and sleep in between.
Looks good to merge if we further adjust the waiting time of the tests. |
Hi, I decreased both the waiting time and the retry times since it will still cost over 10 seconds if only the waiting time is decreased due to the "Thread.sleep(CONNECTION_RETRY_SLEEP);". |
@@ -42,11 +42,13 @@ | |||
private boolean retryForever; | |||
private Socket socket; | |||
private static final int CONNECTION_TIMEOUT_TIME = 0; | |||
private static final int CONNECTION_RETRY_SLEEP = 1000; | |||
public static int CONNECTION_RETRY_SLEEP = 1000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be modifiable by everyone. Please make it just package-visible by removing the public
modifier. Also, please keep the final
modifier because the current implementation just lets the number of retries be configurable with a fixed 1 second retry rate. This is also documented in the user-facing API methods on DataStream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if I add the final, it will be a error in my test:
Cannot assign a value to final variable "CONNECTION_RETRY_SLEEP".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok no problem. Then make the variable non-final but don't expose it. So just have it static int CONNECTION_RETRY_SLEEP = 1000
.
@mxm |
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | ||
field.set(null, 200); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's quite a hack. I think it is ok to remove the final
modifier and make the field variable package-local: static int CONNECTION_RETRY_SLEEP = 1000
. Then you can set it directly here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...
Sorry for that.
I changed this in my new one. :-D
@HuangWHWHW Thanks for your changes. Adding reflection calls to the testing codes is not good practice and makes the code hard to maintain. |
Hi,@mxm |
While merging your pull request I noticed that the |
@mxm |
@mxm |
@mxm |
|
||
private volatile boolean isRunning; | ||
|
||
public SocketTextStreamFunction(String hostname, int port, char delimiter, long maxRetry) { | ||
this.retries = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialization to 0
is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
I removed this in my new commit.
Otherwise why doesn`t the CI run?
This happen in all of my new commits.
I am not sure why the CI is not retesting this. Can you try to squash your commits into one commit and force-push this branch? This always triggers CI for me... |
@HuangWHWHW Thank you for addressing my comments. Could you please squash your commits and force push to this branch again? |
@mxm @StephanEwen |
I think there is an issue with Travis at the moment. Could you force push to this branch again? |
@mxm |
1b8cbd1
to
d6e6247
Compare
@mxm |
final AtomicReference<Throwable> error = new AtomicReference<Throwable>(); | ||
private final String host = "127.0.0.1"; | ||
|
||
SourceFunction.SourceContext<String> ctx = new SourceFunction.SourceContext<String>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you replace this with a simple Mock using Mockito?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ctx is used to get the received msg from socket server.
So I override the toString() method in ctx.
I think it will be invalid if we use mockito to check the received msg is correct.
Or is it unnecessary to check the msg since this test is for the retry times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, you need to capture the input to the collect method. How about using an ArgumentCaptor
? http://mockito.googlecode.com/svn/tags/1.8.0/javadoc/org/mockito/ArgumentCaptor.html
@mxm |
Nice :) Merging... |
In the class SocketTextStreamFunction, the var retryForever only be set in the line "this.retryForever = maxRetry < 0;"(SocketTextStreamFunction.java:54).
When the program executes this “while (retry < maxRetry && !success) ” it means the maxRetry > 0 and the retryForever will always be false.
So it`s unnecessary to judge whether retryForever be false.