You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/12/23 22:10:37 UTC

[GitHub] jerrypeng closed pull request #3245: Fix: Empty exception message in java functions can cause an NPE

jerrypeng closed pull request #3245: Fix: Empty exception message in java functions can cause an NPE
URL: https://github.com/apache/pulsar/pull/3245
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/FunctionStatsManager.java b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/FunctionStatsManager.java
index 72b792e861..818fdb3fe9 100644
--- a/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/FunctionStatsManager.java
+++ b/pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/FunctionStatsManager.java
@@ -320,15 +320,13 @@ public void run() {
 
     public void addUserException(Exception ex) {
         long ts = System.currentTimeMillis();
-        InstanceCommunication.FunctionStatus.ExceptionInformation info =
-                    InstanceCommunication.FunctionStatus.ExceptionInformation.newBuilder()
-                    .setExceptionString(ex.getMessage()).setMsSinceEpoch(ts).build();
+        InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
         latestUserExceptions.add(info);
 
         // report exception throw prometheus
         if (userExceptionRateLimiter.tryAcquire()) {
             String[] exceptionMetricsLabels = Arrays.copyOf(metricsLabels, metricsLabels.length + 2);
-            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage();
+            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage() != null ? ex.getMessage() : "";
             exceptionMetricsLabels[exceptionMetricsLabels.length - 1] = String.valueOf(ts);
             userExceptions.labels(exceptionMetricsLabels).set(1.0);
         }
@@ -336,15 +334,13 @@ public void addUserException(Exception ex) {
 
     public void addSystemException(Throwable ex) {
         long ts = System.currentTimeMillis();
-        InstanceCommunication.FunctionStatus.ExceptionInformation info =
-                InstanceCommunication.FunctionStatus.ExceptionInformation.newBuilder()
-                        .setExceptionString(ex.getMessage()).setMsSinceEpoch(ts).build();
+        InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
         latestSystemExceptions.add(info);
 
         // report exception throw prometheus
         if (sysExceptionRateLimiter.tryAcquire()) {
             String[] exceptionMetricsLabels = Arrays.copyOf(metricsLabels, metricsLabels.length + 2);
-            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage();
+            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage() != null ? ex.getMessage() : "";
             exceptionMetricsLabels[exceptionMetricsLabels.length - 1] = String.valueOf(ts);
             sysExceptions.labels(exceptionMetricsLabels).set(1.0);
         }
@@ -352,15 +348,13 @@ public void addSystemException(Throwable ex) {
 
     public void addSourceException(Throwable ex) {
         long ts = System.currentTimeMillis();
-        InstanceCommunication.FunctionStatus.ExceptionInformation info =
-                InstanceCommunication.FunctionStatus.ExceptionInformation.newBuilder()
-                        .setExceptionString(ex.getMessage()).setMsSinceEpoch(ts).build();
+        InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
         latestSourceExceptions.add(info);
 
         // report exception throw prometheus
         if (sourceExceptionRateLimiter.tryAcquire()) {
             String[] exceptionMetricsLabels = Arrays.copyOf(metricsLabels, metricsLabels.length + 2);
-            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage();
+            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage() != null ? ex.getMessage() : "";
             exceptionMetricsLabels[exceptionMetricsLabels.length - 1] = String.valueOf(ts);
             sourceExceptions.labels(exceptionMetricsLabels).set(1.0);
         }
@@ -368,20 +362,28 @@ public void addSourceException(Throwable ex) {
 
     public void addSinkException(Throwable ex) {
         long ts = System.currentTimeMillis();
-        InstanceCommunication.FunctionStatus.ExceptionInformation info =
-                InstanceCommunication.FunctionStatus.ExceptionInformation.newBuilder()
-                        .setExceptionString(ex.getMessage()).setMsSinceEpoch(ts).build();
+        InstanceCommunication.FunctionStatus.ExceptionInformation info = getExceptionInfo(ex, ts);
         latestSinkExceptions.add(info);
 
         // report exception throw prometheus
         if (sinkExceptionRateLimiter.tryAcquire()) {
             String[] exceptionMetricsLabels = Arrays.copyOf(metricsLabels, metricsLabels.length + 2);
-            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage();
+            exceptionMetricsLabels[exceptionMetricsLabels.length - 2] = ex.getMessage() != null ? ex.getMessage() : "";
             exceptionMetricsLabels[exceptionMetricsLabels.length - 1] = String.valueOf(ts);
             sinkExceptions.labels(exceptionMetricsLabels).set(1.0);
         }
     }
 
+    private InstanceCommunication.FunctionStatus.ExceptionInformation getExceptionInfo(Throwable th, long ts) {
+        InstanceCommunication.FunctionStatus.ExceptionInformation.Builder exceptionInfoBuilder =
+                InstanceCommunication.FunctionStatus.ExceptionInformation.newBuilder().setMsSinceEpoch(ts);
+        String msg = th.getMessage();
+        if (msg != null) {
+            exceptionInfoBuilder.setExceptionString(msg);
+        }
+        return exceptionInfoBuilder.build();
+    }
+
     public void incrTotalReceived() {
         _statTotalRecordsRecieved.inc();
         _statTotalRecordsRecieved1min.inc();


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services