markap14 commented on a change in pull request #5101: URL: https://github.com/apache/nifi/pull/5101#discussion_r640012388
########## File path: nifi-stateless/nifi-stateless-bundle/nifi-stateless-engine/src/main/java/org/apache/nifi/controller/reporting/LogComponentStatuses.java ########## @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +package org.apache.nifi.controller.reporting; + +import org.apache.nifi.controller.Counter; +import org.apache.nifi.controller.ProcessorNode; +import org.apache.nifi.controller.flow.FlowManager; +import org.apache.nifi.controller.repository.CounterRepository; +import org.apache.nifi.controller.repository.FlowFileEvent; +import org.apache.nifi.controller.repository.FlowFileEventRepository; +import org.apache.nifi.groups.ProcessGroup; +import org.apache.nifi.util.FormatUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class LogComponentStatuses implements Runnable { + private static final Logger logger = LoggerFactory.getLogger(LogComponentStatuses.class); + + private static final String PROCESSOR_LINE_FORMAT = "| %1$-30.30s | %2$-36.36s | %3$-30.30s | %4$28.28s | %5$30.30s | %6$14.14s | %714.14s | %8$28.28s |\n"; + private static final String COUNTER_LINE_FORMAT = "| %1$-36.36s | %2$-36.36s | %3$28.28s | %4$28.28s |\n"; + + private final FlowFileEventRepository flowFileEventRepository; + private final CounterRepository counterRepository; + private final FlowManager flowManager; + + private final String processorHeader; + private final String processorBorderLine; + private final String counterHeader; + private final String counterBorderLine; + + private final Map<String, Long> previousCounterValues = new ConcurrentHashMap<>(); + private volatile long lastTriggerTime = System.currentTimeMillis(); + + public LogComponentStatuses(final FlowFileEventRepository flowFileEventRepository, final CounterRepository counterRepository, final FlowManager flowManager) { + this.flowFileEventRepository = flowFileEventRepository; + this.counterRepository = counterRepository; + this.flowManager = flowManager; + + processorHeader = String.format(PROCESSOR_LINE_FORMAT, "Processor Name", "Processor ID", "Processor Type", "Bytes Read/sec", "Bytes Written/sec", "Tasks/sec", "Nanos/Task", + "Percent of Processing Time"); + processorBorderLine = createLine(processorHeader); + + counterHeader = String.format(COUNTER_LINE_FORMAT, "Counter Context", "Counter Name", "Counter Value", "Increase/sec"); + counterBorderLine = createLine(counterHeader); + } + + private String createLine(final String valueToUnderscore) { + final StringBuilder processorBorderBuilder = new StringBuilder(valueToUnderscore.length()); + for (int i = 0; i < valueToUnderscore.length(); i++) { + processorBorderBuilder.append('-'); + } + return processorBorderBuilder.toString(); + } + + @Override + public void run() { + try { + if (!logger.isInfoEnabled()) { + return; + } + + logFlowFileEvents(); + logCounters(); Review comment: I don't think so. I am a big proponent of the fail-fast or escape-fast approach. The original here conveys the meaning more clearly: "if logger's INFO level is not enabled, we're done. Do nothing." The refactored approach says "if the logger is enabled, here are the different things I want to do." As the code is refactored/updated in the future, the logic within the if conditional is likely to grow, to the point of it not being as obvious what the intent is. This is a general practice that I try to always adhere to, as you'll see all throughout the codebase and tends to lead to cleaner code over time, I think. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
