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