View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.namequeues;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
27  import org.apache.hadoop.hbase.protobuf.generated.TooSlowLog;
28  
29  /**
30   * Event Handler utility class
31   */
32  @InterfaceAudience.Private
33  public final class LogHandlerUtils {
34  
35    private LogHandlerUtils() {
36    }
37  
38    private static int getTotalFiltersCount(AdminProtos.SlowLogResponseRequest request) {
39      int totalFilters = 0;
40      if (StringUtils.isNotEmpty(request.getRegionName())) {
41        totalFilters++;
42      }
43      if (StringUtils.isNotEmpty(request.getTableName())) {
44        totalFilters++;
45      }
46      if (StringUtils.isNotEmpty(request.getClientAddress())) {
47        totalFilters++;
48      }
49      if (StringUtils.isNotEmpty(request.getUserName())) {
50        totalFilters++;
51      }
52      return totalFilters;
53    }
54  
55    private static List<TooSlowLog.SlowLogPayload> filterLogs(
56        AdminProtos.SlowLogResponseRequest request,
57        List<TooSlowLog.SlowLogPayload> slowLogPayloadList, int totalFilters) {
58      List<TooSlowLog.SlowLogPayload> filteredSlowLogPayloads = new ArrayList<>();
59      final String regionName =
60        StringUtils.isNotEmpty(request.getRegionName()) ? request.getRegionName() : null;
61      final String tableName =
62        StringUtils.isNotEmpty(request.getTableName()) ? request.getTableName() : null;
63      final String clientAddress =
64        StringUtils.isNotEmpty(request.getClientAddress()) ? request.getClientAddress() : null;
65      final String userName =
66        StringUtils.isNotEmpty(request.getUserName()) ? request.getUserName() : null;
67      for (TooSlowLog.SlowLogPayload slowLogPayload : slowLogPayloadList) {
68        int totalFilterMatches = 0;
69        if (slowLogPayload.getRegionName().equals(regionName)) {
70          totalFilterMatches++;
71        }
72        if (tableName != null && slowLogPayload.getRegionName().startsWith(tableName)) {
73          totalFilterMatches++;
74        }
75        if (slowLogPayload.getClientAddress().equals(clientAddress)) {
76          totalFilterMatches++;
77        }
78        if (slowLogPayload.getUserName().equals(userName)) {
79          totalFilterMatches++;
80        }
81        if (request.hasFilterByOperator() && request.getFilterByOperator()
82          .equals(AdminProtos.SlowLogResponseRequest.FilterByOperator.AND)) {
83          // Filter by AND operator
84          if (totalFilterMatches == totalFilters) {
85            filteredSlowLogPayloads.add(slowLogPayload);
86          }
87        } else {
88          // Filter by OR operator
89          if (totalFilterMatches > 0) {
90            filteredSlowLogPayloads.add(slowLogPayload);
91          }
92        }
93      }
94      return filteredSlowLogPayloads;
95    }
96  
97    public static List<TooSlowLog.SlowLogPayload> getFilteredLogs(
98        AdminProtos.SlowLogResponseRequest request, List<TooSlowLog.SlowLogPayload> logPayloadList) {
99      int totalFilters = getTotalFiltersCount(request);
100     if (totalFilters > 0) {
101       logPayloadList = filterLogs(request, logPayloadList, totalFilters);
102     }
103     int limit = Math.min(request.getLimit(), logPayloadList.size());
104     return logPayloadList.subList(0, limit);
105   }
106 
107 }