From: FrancescoFioraEst Date: Tue, 27 May 2025 12:56:59 +0000 (+0100) Subject: Add support for uncaught exceptions for json log format X-Git-Tag: 8.2.0~3 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F63%2F140963%2F2;p=policy%2Fclamp.git Add support for uncaught exceptions for json log format Issue-ID: POLICY-5376 Change-Id: Ice44f9588dcd81e956049e549c94d58186320c90 Signed-off-by: FrancescoFioraEst --- diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/MessageProvider.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/MessageProvider.java index fee830fb8..9354ce2c6 100644 --- a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/MessageProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/MessageProvider.java @@ -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()); } diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java index c8ddb4374..edb3386e2 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProvider.java @@ -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 index 000000000..12fcb7c64 --- /dev/null +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactory.java @@ -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; + } +} diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java index a42dca8f1..21b6c1fbf 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAcHandler.java @@ -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. diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAspect.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAspect.java index b52e47d22..e74d5b810 100644 --- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAspect.java +++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionAspect.java @@ -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 index 000000000..18356becf --- /dev/null +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/AcmThreadFactoryTest.java @@ -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()); + } +}