View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.timer;
17  
18  import java.util.stream.Collectors;
19  
20  import org.apache.commons.text.StringEscapeUtils;
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.opensearch.client.SearchEngineClient;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse;
28  import org.opensearch.common.unit.TimeValue;
29  
30  /**
31   * Monitor target for tracking hot threads in the OpenSearch cluster.
32   * This class extends MonitorTarget to provide monitoring functionality for
33   * hot threads, which helps identify performance bottlenecks and resource
34   * usage issues in the search engine cluster.
35   */
36  public class HotThreadMonitorTarget extends MonitorTarget {
37      private static final Logger logger = LogManager.getLogger(HotThreadMonitorTarget.class);
38  
39      /**
40       * Default constructor for HotThreadMonitorTarget.
41       */
42      public HotThreadMonitorTarget() {
43          super();
44      }
45  
46      @Override
47      public void expired() {
48          final StringBuilder buf = new StringBuilder(1000);
49  
50          buf.append("[HOTTHREAD MONITOR] ");
51  
52          final FessConfig fessConfig = ComponentUtil.getFessConfig();
53  
54          buf.append('{');
55  
56          final boolean ignoreIdleThreads = Constants.TRUE.equalsIgnoreCase(fessConfig.getCrawlerHotthreadIgnoreIdleThreads());
57          final TimeValue interval = TimeValue.parseTimeValue(fessConfig.getCrawlerHotthreadInterval(), "crawler.hotthread.interval");
58          final int threads = fessConfig.getCrawlerHotthreadThreadsAsInteger();
59          final String timeout = fessConfig.getCrawlerHotthreadTimeout();
60          final String type = fessConfig.getCrawlerHotthreadType();
61          try {
62              final SearchEngineClient esClient = ComponentUtil.getSearchEngineClient();
63              final NodesHotThreadsResponse response = esClient.admin()
64                      .cluster()
65                      .prepareNodesHotThreads()
66                      .setIgnoreIdleThreads(ignoreIdleThreads)
67                      .setInterval(interval)
68                      .setThreads(threads)
69                      .setTimeout(timeout)
70                      .setType(type)
71                      .execute()
72                      .actionGet(timeout);
73              append(buf, "cluster_name", () -> response.getClusterName().value()).append(',');
74              final String hotThreads = response.getNodesMap().entrySet().stream().map(e -> {
75                  final StringBuilder tempBuf = new StringBuilder();
76                  append(tempBuf, StringEscapeUtils.escapeJson(e.getKey()), () -> StringEscapeUtils.escapeJson(e.getValue().getHotThreads()));
77                  return tempBuf.toString();
78              }).collect(Collectors.joining(","));
79              buf.append(hotThreads).append(',');
80          } catch (final Exception e) {
81              appendException(buf, e).append(',');
82          }
83  
84          appendTimestamp(buf);
85          buf.append('}');
86  
87          if (logger.isInfoEnabled()) {
88              logger.info(buf.toString());
89          }
90      }
91  
92  }