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

[FLINK-30998] Add optional exception handler to flink-connector-opensearch #11

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

lilyevsky
Copy link
Contributor

No description provided.

@reta
Copy link
Member

reta commented Feb 15, 2023

@lilyevsky the relevant changes looks good, but your branch is out of sync with https://github.com/apache/flink-connector-opensearch, you need to sync + rebase

@lilyevsky
Copy link
Contributor Author

lilyevsky commented Feb 15, 2023

@reta I see. Please disregard my previous message.
Do I sync + rebase on my desktop or I can do it in github directly?

@lilyevsky
Copy link
Contributor Author

@lilyevsky
Copy link
Contributor Author

Done

this.hosts = checkNotNull(hosts);
checkArgument(!hosts.isEmpty(), "Hosts cannot be empty.");
this.emitter = checkNotNull(emitter);
this.deliveryGuarantee = checkNotNull(deliveryGuarantee);
this.buildBulkProcessorConfig = checkNotNull(buildBulkProcessorConfig);
this.networkClientConfig = checkNotNull(networkClientConfig);
this.failureHandler = failureHandler;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.failureHandler = failureHandler;
this.failureHandler = checkNotNull(failureHandler);

@@ -72,6 +72,7 @@
private Integer connectionRequestTimeout;
private Integer socketTimeout;
private Boolean allowInsecure;
private FailureHandler failureHandler;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private FailureHandler failureHandler;
private FailureHandler failureHandler = DEFAULT_FAILURE_HANDLER;

@@ -343,7 +348,11 @@ private void extractFailures(BulkRequest request, BulkResponse response) {
if (chainedFailures == null) {
return;
}
throw new FlinkRuntimeException(chainedFailures);
if (failureHandler == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check is not needed anymore (the builder would supply the default handler), just failureHandler.onFailure(chainedFailures);

@reta
Copy link
Member

reta commented Feb 16, 2023

Thanks @lilyevsky just a few minor comments, I think the only part that is missed are tests, and probably you may need to run mvn spotless:apply to apply the formatting, thank you!

@lilyevsky
Copy link
Contributor Author

@reta Did all the changes you suggested, also ran the mvn spotless:apply .
Btw., my IntelliJ showed a list of warnings in places not relevant to my changes. Even one error (which is not really an error because it is in the comments), in OpensearchWriter.java:96.
Do you have any suggestions for the tests? As I mentioned before, I actually tested this in real environment; on the other hand, I understand that unit tests are very important to have.

@reta
Copy link
Member

reta commented Feb 16, 2023

@reta Did all the changes you suggested, also ran the mvn spotless:apply .

👍 thank you!

Even one error (which is not really an error because it is in the comments), in OpensearchWriter.java:96.

Hm .. could you share the error please?

Do you have any suggestions for the tests?

Yes, sure, I think OpensearchWriterITCase could be a good candidate to add a test case with the simulated failure (fe we could pause / shutdown Opensearch container, just an idea). It may be a bit difficult, please let me know if help needed, thank you.

@lilyevsky
Copy link
Contributor Author

The error is about this line:

  • @param bulkProcessorBuilderFactory configuring the {@link BulkProcessor}'s builder

Apparently that parameter is no longer present.
Also 10 warnings, like "Return value ... is never used" or "Exception ... is never thrown".
One warning is actually about failureHandler, missing description.

I actually can fix all of that if it is OK, then you can review.

Also will look into the tests tomorrow.

@reta
Copy link
Member

reta commented Feb 16, 2023

I actually can fix all of that if it is OK, then you can review.

Thank you, cleanup would be much appreciated, I would suggest let's finish up with tests first so to have the scope finalized, and we could fix javadocs after. Thanks!

@lilyevsky
Copy link
Contributor Author

Sure.
I will fix that bulkProcessorBuilderFactory thing, everything else is less important at the moment. I got that list of warnings while committing. When I just run 'analyze', I get even more stuff.

@lilyevsky
Copy link
Contributor Author

@reta I fixed some warnings, pushed as a separate commit, so you can review.
Now looking at the tests. We need to somehow simulate the Opensearch server that can get the request and then send back the response. Is there currently such thing in the test suite? Is it what you do in flink-connector-opensearch-e2e-tests?
In any case, I think you would know better than I how to do it.

@reta
Copy link
Member

reta commented Feb 16, 2023

@reta I fixed some warnings, pushed as a separate commit, so you can review. Now looking at the tests. We need to somehow simulate the Opensearch server that can get the request and then send back the response.

I shared the hints with you here #11 (comment)

Here is sample test case:

    @Test
    void testWriteErrorOnUpdate() throws Exception {
        final String index = "test-bulk-flush-with-error";
        final int flushAfterNActions = 1;
        final BulkProcessorConfig bulkProcessorConfig =
                new BulkProcessorConfig(flushAfterNActions, -1, -1, FlushBackoffType.NONE, 0, 0);

        try (final OpensearchWriter<Tuple2<Integer, String>> writer =
                createWriter(index, true, bulkProcessorConfig)) {
            // Trigger an error by updating non-existing document
            writer.write(Tuple2.of(1, "u" + buildMessage(1)), null);
            context.assertThatIdsAreNotWritten(index, 1);
        }
    }

You need to attach the failure handler and assert afterwards it has been called.

@lilyevsky
Copy link
Contributor Author

@reta Trying to add the test. For some reason the tests from OpensearchWriterITCase class are not executed at all.
I run the command:

mvn clean test

in flink-connector-opensearch directory. I see that some tests from org.apache.flink.connector.opensearch.sink.OpensearchSinkBuilderTest are executed, but not from other 4 files in the same directory.
What could be wrong?

maventest.txt

@reta
Copy link
Member

reta commented Feb 16, 2023

@lilyevsky OpensearchWriterITCase is integration test, mvn clean verify will run it

@lilyevsky
Copy link
Contributor Author

Thanks @reta . One more step. It looks like I need to have a "valid Docker environment". I install the docker things:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

What else do I need to do?

@reta
Copy link
Member

reta commented Feb 16, 2023

@lilyevsky I assume you are on Ubuntu [1]

[1] https://docs.docker.com/engine/install/ubuntu/

@lilyevsky
Copy link
Contributor Author

lilyevsky commented Feb 16, 2023

@reta I installed docker, the daemon is running.
However, the 'mvn verify' still fails with "OpensearchSinkITCase » IllegalState Could not find a valid Docker environment...." ....
I see that I have permissions problem:
docker run hello-world
docker: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.

I think I can fix it. Will let you know.

@lilyevsky
Copy link
Contributor Author

@reta Added the test. I think now it is good.
I also tested the test itself, by simulating the "bad" handler, making the test fail, just to make sure it checks it properly.

@@ -103,7 +107,8 @@
BulkProcessorConfig bulkProcessorConfig,
NetworkClientConfig networkClientConfig,
SinkWriterMetricGroup metricGroup,
MailboxExecutor mailboxExecutor) {
MailboxExecutor mailboxExecutor,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update javadocs with failureHandler arg

return failed;
}

@java.lang.Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit, just @Override

@@ -238,19 +240,68 @@ void testCurrentSendTime() throws Exception {
}
}

private class TestHandler implements FailureHandler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably private static is better

// Trigger an error by updating non-existing document
writer.write(Tuple2.of(1, "u" + buildMessage(1)), null);
context.assertThatIdsAreNotWritten(index, 1);
assertThat(testHandler.isFailed()).isEqualTo(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -174,7 +175,8 @@ void testIncrementByteOutMetric() throws Exception {
new BulkProcessorConfig(flushAfterNActions, -1, -1, FlushBackoffType.NONE, 0, 0);

try (final OpensearchWriter<Tuple2<Integer, String>> writer =
createWriter(index, false, bulkProcessorConfig, metricGroup)) {
createWriter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't need this change since createWriter has an overloaded version without failure handler

Copy link
Contributor Author

@lilyevsky lilyevsky Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it will be better not to touch that line. I did it because it needed to pass the metricGroup.
So I am fixing the situation by adding yet another constructor, with metricGroup but without failureHandler.
Also in the main constructor put the failureHandler at the end of the parameters list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for constructor, createWriter is a method in this class

import java.io.Serializable;

/** Handler to process failures. */
@Public
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Public -> @PublicEvolving would be probably better (to leave some space to evolve the API)

@reta
Copy link
Member

reta commented Feb 17, 2023

@lilyevsky a few minor comments, LGTM otherwise!

@lilyevsky
Copy link
Contributor Author

@reta I committed the changes you suggested.

}

@Override
public void write(IN element, Context context) throws IOException, InterruptedException {
public void write(IN element, Context context) throws InterruptedException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lilyevsky could you please address this and this comment? thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reta No problem, could you please clarify: you want me to put back the IOException to both write and flush methods, correct? Also, I am not sure what you mean by addressing this . Is it about removing the "@SuppressWarnings("All")"?
Please confirm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lilyevsky correct, the throws IOException, InterruptedException is the right part of the signature: the first comment is about write, the second is about flush, thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reta Done. Also removed the line with warnings suppressions that I added at some point.

@@ -134,7 +141,7 @@ public void write(IN element, Context context) throws IOException, InterruptedEx
}

@Override
public void flush(boolean endOfInput) throws IOException, InterruptedException {
public void flush(boolean endOfInput) throws InterruptedException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, please revert

@@ -148,6 +150,7 @@ public OpensearchSinkBuilder<IN> setBulkFlushMaxActions(int numMaxActions) {
* @param maxSizeMb the maximum size of buffered actions, in mb.
* @return this builder
*/
@SuppressWarnings("UnusedReturnValue")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why these suppression are needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it because compiler gave me a warning about it. I can remove that suppression if you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove, thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @reta , please let me know what are the plans for merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @reta , please let me know what are the plans for merging.

Thanks @lilyevsky , LGTM, asked Flink committers to take I look (I do not have commit rights)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @reta

@reta
Copy link
Member

reta commented Mar 4, 2023

@MartijnVisser @zentol could you please take a look guys? thank you!

@lilyevsky
Copy link
Contributor Author

I see since I finalized this request there were some other changes in main branch, and so there are conflicts.
I am going to rebase my branch and hopefully resolve the conflicts this way.
After I do that, can we merge my request sooner rather than later? Otherwise there might be other conflicts again.

@lilyevsky
Copy link
Contributor Author

I actually resolved the conflicts in the web editor, and updated my branch.
Can somebody review this?

@reta
Copy link
Member

reta commented Jul 6, 2023

@MartijnVisser @snuyanzin please help us here :-)

@lilyevsky
Copy link
Contributor Author

lilyevsky commented Jul 6, 2023

Actually, hold on for now. I was not able to compile it in my work environment, now tried in my home desktop and found some errors. Let me fix them. Will let you know.
I may still need to rebase my branch on apache:main, or merge it in. Which way it is easier?
Or maybe I will just repeat all my changes on top of the latest upstream, could be easier to make it compile clean.

@lilyevsky
Copy link
Contributor Author

I made it clean now, you can review and maybe merge already.
I got the latest from apache:main branch and re-applied all my changes on top of it, so you can see now it is just one commit.
I was able to locally build it.

@MartijnVisser MartijnVisser requested a review from reta July 7, 2023 12:07
@lilyevsky
Copy link
Contributor Author

So you can merge it now?
After you do, I am going to build locally the latest version and do my own tests.

@MartijnVisser MartijnVisser merged commit d853e3d into apache:main Jul 11, 2023
5 checks passed
@boring-cyborg
Copy link

boring-cyborg bot commented Jul 11, 2023

Awesome work, congrats on your first merged pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants