Add support for uncaught exceptions for json log format 63/140963/2
authorFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 27 May 2025 12:56:59 +0000 (13:56 +0100)
committerFrancesco Fiora <francesco.fiora@est.tech>
Tue, 27 May 2025 14:12:16 +0000 (14:12 +0000)
Issue-ID: POLICY-5376
Change-Id: Ice44f9588dcd81e956049e549c94d58186320c90
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/MessageProvider.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactory.java [new file with mode: 0644]
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java
runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAspect.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactoryTest.java [new file with mode: 0644]

index fee830f..9354ce2 100644 (file)
@@ -30,7 +30,6 @@ import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
 import lombok.AllArgsConstructor;
-import org.hibernate.exception.ConstraintViolationException;
 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
 import org.onap.policy.clamp.models.acm.document.concepts.DocMessage;
 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeployAck;
@@ -237,7 +236,7 @@ public class MessageProvider {
         try {
             var result = messageJobRepository.save(job);
             return Optional.of(result.getJobId());
-        } catch (ConstraintViolationException ex) {
+        } catch (RuntimeException ex) {
             // already exist a job with this identificationId
             LOGGER.warn(ex.getMessage());
         }
index c8ddb43..edb3386 100644 (file)
@@ -30,6 +30,7 @@ import java.util.concurrent.Executors;
 import lombok.NonNull;
 import lombok.RequiredArgsConstructor;
 import org.onap.policy.clamp.acm.runtime.main.parameters.AcRuntimeParameterGroup;
+import org.onap.policy.clamp.acm.runtime.supervision.AcmThreadFactory;
 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantPrimePublisher;
 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
@@ -63,7 +64,8 @@ public class CommissioningProvider {
     private final ParticipantPrimePublisher participantPrimePublisher;
     private final AcRuntimeParameterGroup acRuntimeParameterGroup;
 
-    private final ExecutorService executor = Context.taskWrapping(Executors.newFixedThreadPool(1));
+    private final ExecutorService executor =
+            Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
 
     private CommissioningResponse createCommissioningResponse(UUID compositionId,
             ToscaServiceTemplate serviceTemplate) {
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactory.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactory.java
new file mode 100644 (file)
index 0000000..12fcb7c
--- /dev/null
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.acm.runtime.supervision;
+
+import java.util.concurrent.ThreadFactory;
+import lombok.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class AcmThreadFactory implements ThreadFactory {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(AcmThreadFactory.class);
+
+    protected void uncaughtException(Thread t, Throwable e) {
+        LOGGER.error("Uncaught Exception: {}", e.getMessage(), e);
+    }
+
+    @Override
+    public Thread newThread(@NonNull Runnable r) {
+        final var thread = new Thread(r);
+        thread.setUncaughtExceptionHandler(this::uncaughtException);
+        return thread;
+    }
+}
index a42dca8..21b6c1f 100644 (file)
@@ -69,7 +69,8 @@ public class SupervisionAcHandler {
     private final MessageProvider messageProvider;
     private final EncryptionUtils encryptionUtils;
 
-    private final ExecutorService executor = Context.taskWrapping(Executors.newFixedThreadPool(1));
+    private final ExecutorService executor =
+            Context.taskWrapping(Executors.newFixedThreadPool(1, new AcmThreadFactory()));
 
     /**
      * Handle Deploy an AutomationComposition instance.
index b52e47d..e74d5b8 100644 (file)
@@ -43,7 +43,8 @@ public class SupervisionAspect implements Closeable {
     private final SupervisionParticipantScanner participantScanner;
 
     private final ThreadPoolExecutor executor =
-            new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
+            new ThreadPoolExecutor(1, 1, 0L,
+                    TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new AcmThreadFactory());
 
     @Scheduled(
             fixedRateString = "${runtime.participantParameters.heartBeatMs}",
diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactoryTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactoryTest.java
new file mode 100644 (file)
index 0000000..18356be
--- /dev/null
@@ -0,0 +1,41 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ * ================================================================================
+ * Licensed 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.acm.runtime.supervision;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import org.junit.jupiter.api.Test;
+
+class AcmThreadFactoryTest {
+
+    @Test
+    void test() throws InterruptedException {
+        var threadFactory = spy(new AcmThreadFactory());
+        var thread = threadFactory.newThread(new Thread(() -> {
+            throw new RuntimeException("Error");
+        }));
+        thread.start();
+        thread.join();
+        verify(threadFactory).uncaughtException(any(), any());
+    }
+}