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

fix: NPE when udf metrics enabled #5960

Merged
merged 2 commits into from
Aug 10, 2020

Conversation

big-andy-coates
Copy link
Contributor

Description

fixes: #5890

Fixes an NPE thrown when the second instance of a UDAF is initialized. The NPE was being thrown due to a bug in the code that failed to initialise the sensor fields in the UdafAggregateFunction class if the sensor already existed.

The commit also refactors the code used to add function invocation metrics such that all three function types: UDF, UDTF and UDAF, use the same function to register metrics.

Unit tests added to ensure this single code path doesn't throw an NPE or return null on the subsequent invocation.

Also added ksql.udf.collect.metrics metric to the server config file, so users are more likely to be able to find it and try it out.

Moved UDTF functions out of the ksql-udf group and into ksql-udtf group. Having them in the former was another bug.

Testing done

Added unit tests plus manual validation that metric names didn't change with new code, (except intentional move of UDTF metrics into own group).

Reviewer checklist

  • Ensure docs are updated if necessary. (eg. if a user visible feature is being added or changed).
  • Ensure relevant issues are linked (description should include text like "Fixes #")

fixes: confluentinc#5890

Fixes an NPE thrown when the second instance of a UDAF is initialized. The NPE was being thrown due to a bug in the code that failed to initialise the sensor fields in the `UdafAggregateFunction` class if the sensor already existed.

The commit also refactors the code used to add function invocation metrics such that all three function types: UDF, UDTF and UDAF, use the same function to register metrics.

Unit tests added to ensure this single code path doesn't throw an NPE or return null on the subsequent invocation.

Also added `ksql.udf.collect.metrics` metric to the server config file, so users are more likely to be able to find it and try it out.

Moved UDTF functions out of the `ksql-udf` group and into `ksql-udtf` group. Having them in the former was another bug.
@big-andy-coates big-andy-coates requested a review from a team as a code owner August 6, 2020 19:00
@big-andy-coates big-andy-coates added this to the 0.12.0 milestone Aug 6, 2020
Copy link
Contributor

@agavra agavra left a comment

Choose a reason for hiding this comment

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

LGTM, with two points of consideration

Comment on lines 74 to 79
final Sensor existing = metrics.getSensor(sensorName);
if (existing != null) {
return existing;
}

final Sensor newSensor = metrics.sensor(sensorName);
Copy link
Contributor

Choose a reason for hiding this comment

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

there's a race here that is possible here where two calls for the same metrics.getSensor(sensorName) don't find the sensor and they both end up registering the extra metrics. I don't know of a good way around it...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I know, but I was just fixing the NPE and being lazy ;). Fixed it.

@@ -67,7 +68,8 @@ public void loadUdtfFromClass(
}
final String functionName = udtfDescriptionAnnotation.name();
final String sensorName = "ksql-udtf-" + functionName;
FunctionLoaderUtils.addSensor(sensorName, functionName, metrics);

FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udtf", functionName + " udtf");
Copy link
Contributor

Choose a reason for hiding this comment

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

if anyone was relying on the old metrics, their alerts for udtfs will break with this change. probably ok as I'd be shocked if anyone was actually writing their own UDTFs and using metrics on them at this point but something to call out 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm aware, but ... meh. turning metrics on caused an NPE, so likely no one is using them, and better to change it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned in the description, I see this as a bug fix.

@big-andy-coates big-andy-coates merged commit e32183f into confluentinc:master Aug 10, 2020
@big-andy-coates big-andy-coates deleted the udaf_metrics branch August 10, 2020 10:27
sarwarbhuiyan pushed a commit to sarwarbhuiyan/ksql that referenced this pull request Sep 4, 2020
* fix: NPE when udf metrics enabled

fixes: confluentinc#5890

Fixes an NPE thrown when the second instance of a UDAF is initialized. The NPE was being thrown due to a bug in the code that failed to initialise the sensor fields in the `UdafAggregateFunction` class if the sensor already existed.

The commit also refactors the code used to add function invocation metrics such that all three function types: UDF, UDTF and UDAF, use the same function to register metrics.

Unit tests added to ensure this single code path doesn't throw an NPE or return null on the subsequent invocation.

Also added `ksql.udf.collect.metrics` metric to the server config file, so users are more likely to be able to find it and try it out.

Moved UDTF functions out of the `ksql-udf` group and into `ksql-udtf` group. Having them in the former was another bug.

Co-authored-by: Andy Coates <big-andy-coates@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UDAFs throw NPEs when UDF metrics are enabled
2 participants