001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.namequeues;
019
020import java.util.ArrayList;
021import java.util.List;
022import org.apache.commons.lang3.StringUtils;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos;
026import org.apache.hadoop.hbase.shaded.protobuf.generated.TooSlowLog;
027
028/**
029 * Event Handler utility class
030 */
031@InterfaceAudience.Private
032public class LogHandlerUtils {
033
034  private static int getTotalFiltersCount(AdminProtos.SlowLogResponseRequest request) {
035    int totalFilters = 0;
036    if (StringUtils.isNotEmpty(request.getRegionName())) {
037      totalFilters++;
038    }
039    if (StringUtils.isNotEmpty(request.getTableName())) {
040      totalFilters++;
041    }
042    if (StringUtils.isNotEmpty(request.getClientAddress())) {
043      totalFilters++;
044    }
045    if (StringUtils.isNotEmpty(request.getUserName())) {
046      totalFilters++;
047    }
048    return totalFilters;
049  }
050
051  private static List<TooSlowLog.SlowLogPayload> filterLogs(
052    AdminProtos.SlowLogResponseRequest request, List<TooSlowLog.SlowLogPayload> slowLogPayloadList,
053    int totalFilters) {
054    List<TooSlowLog.SlowLogPayload> filteredSlowLogPayloads = new ArrayList<>();
055    final String regionName =
056      StringUtils.isNotEmpty(request.getRegionName()) ? request.getRegionName() : null;
057    final String tableName =
058      StringUtils.isNotEmpty(request.getTableName()) ? request.getTableName() : null;
059    final String clientAddress =
060      StringUtils.isNotEmpty(request.getClientAddress()) ? request.getClientAddress() : null;
061    final String userName =
062      StringUtils.isNotEmpty(request.getUserName()) ? request.getUserName() : null;
063    for (TooSlowLog.SlowLogPayload slowLogPayload : slowLogPayloadList) {
064      int totalFilterMatches = 0;
065      if (slowLogPayload.getRegionName().equals(regionName)) {
066        totalFilterMatches++;
067      }
068      if (tableName != null && slowLogPayload.getRegionName().startsWith(tableName)) {
069        totalFilterMatches++;
070      }
071      if (slowLogPayload.getClientAddress().equals(clientAddress)) {
072        totalFilterMatches++;
073      }
074      if (slowLogPayload.getUserName().equals(userName)) {
075        totalFilterMatches++;
076      }
077      if (
078        request.hasFilterByOperator() && request.getFilterByOperator()
079          .equals(AdminProtos.SlowLogResponseRequest.FilterByOperator.AND)
080      ) {
081        // Filter by AND operator
082        if (totalFilterMatches == totalFilters) {
083          filteredSlowLogPayloads.add(slowLogPayload);
084        }
085      } else {
086        // Filter by OR operator
087        if (totalFilterMatches > 0) {
088          filteredSlowLogPayloads.add(slowLogPayload);
089        }
090      }
091    }
092    return filteredSlowLogPayloads;
093  }
094
095  public static List<TooSlowLog.SlowLogPayload> getFilteredLogs(
096    AdminProtos.SlowLogResponseRequest request, List<TooSlowLog.SlowLogPayload> logPayloadList) {
097    int totalFilters = getTotalFiltersCount(request);
098    if (totalFilters > 0) {
099      logPayloadList = filterLogs(request, logPayloadList, totalFilters);
100    }
101    int limit = Math.min(request.getLimit(), logPayloadList.size());
102    return logPayloadList.subList(0, limit);
103  }
104
105}