From f6b0293299cf7be3dd56397954c2c12f8e64e188 Mon Sep 17 00:00:00 2001 From: linlinnn Date: Mon, 19 Apr 2021 02:04:58 +0800 Subject: [PATCH] Fix log format and swallow exception (#10252) Co-authored-by: linjunhua --- .../org/apache/pulsar/broker/PulsarService.java | 5 ++--- .../resourcegroup/ResourceGroupService.java | 2 +- .../ResourceUsageTransportManager.java | 17 ++++++++++------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java index 8db0b1d14322e..86630bb6bcb81 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/PulsarService.java @@ -772,11 +772,10 @@ public Boolean get() { } // Start the task to publish resource usage, if necessary - if (config.getResourceUsageTransportClassName() != null - && config.getResourceUsageTransportClassName() != "") { + if (isNotBlank(config.getResourceUsageTransportClassName())) { Class clazz = Class.forName(config.getResourceUsageTransportClassName()); Constructor ctor = clazz.getConstructor(PulsarService.class); - Object object = ctor.newInstance(new Object[] { this }); + Object object = ctor.newInstance(new Object[]{this}); this.resourceUsageTransportManager = (ResourceUsageTransportManager) object; } diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java index 568807411ad77..a711a805f5915 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceGroupService.java @@ -292,7 +292,7 @@ public boolean incrementUsage(String tenantName, String nsName, // Expect stats to increase monotonically. if (incStats.bytes < 0 || incStats.messages < 0) { - String errMesg = String.format("incrementUsage on tenant={}, NS={}: bytes ({}) or mesgs ({}) is negative", + String errMesg = String.format("incrementUsage on tenant=%s, NS=%s: bytes (%s) or mesgs (%s) is negative", tenantName, nsName, incStats.bytes, incStats.messages); throw new PulsarAdminException(errMesg); } diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceUsageTransportManager.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceUsageTransportManager.java index 227bce0658ee9..7cebe43a435d7 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceUsageTransportManager.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/resourcegroup/ResourceUsageTransportManager.java @@ -97,7 +97,7 @@ public void run() { byte[] bytes = buf.array(); producer.sendAsync(bytes).whenComplete((id, ex) -> { if (null != ex) { - LOG.error("Resource usage publisher: sending message ID {} error {}", id, ex); + LOG.error("Resource usage publisher: sending message ID {} error", id, ex); } buf.release(); }); @@ -152,9 +152,11 @@ public void received(Reader reader, Message msg) { }); } catch (IllegalStateException exception) { - LOG.error("Resource usage reader: Error parsing incoming message {}", exception); + LOG.error("Resource usage reader: Error parsing incoming message", exception); + throw exception; } catch (Exception exception) { - LOG.error("Resource usage reader: Unknown exception while parsing message {}", exception); + LOG.error("Resource usage reader: Unknown exception while parsing message", exception); + throw exception; } } @@ -192,8 +194,8 @@ private void createTenantAndNamespace() throws PulsarServerException, PulsarAdmi } catch (PulsarAdminException ex1) { if (!(ex1 instanceof PulsarAdminException.ConflictException)) { LOG.error("Unexpected exception {} when creating tenant {}", ex1, tenant); - throw ex1; } + throw ex1; } } List nsList = admin.namespaces().getNamespaces(tenant); @@ -203,8 +205,8 @@ private void createTenantAndNamespace() throws PulsarServerException, PulsarAdmi } catch (PulsarAdminException ex1) { if (!(ex1 instanceof PulsarAdminException.ConflictException)) { LOG.error("Unexpected exception {} when creating namespace {}", ex1, namespace); - throw ex1; } + throw ex1; } } } @@ -218,7 +220,7 @@ public ResourceUsageTransportManager(PulsarService pulsarService) throws Excepti consumer = new ResourceUsageReader(); pTask = new ResourceUsageWriterTask(); } catch (Exception ex) { - LOG.error("Error initializing resource usage transport manager: {}", ex); + LOG.error("Error initializing resource usage transport manager", ex); throw ex; } } @@ -265,7 +267,8 @@ public void close() throws Exception { pTask.close(); consumer.close(); } catch (Exception ex1) { - LOG.error("Error closing producer/consumer for resource-usage topic {}", ex1); + LOG.error("Error closing producer/consumer for resource-usage topic", ex1); + throw ex1; } } }