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.hbtop;
019
020import static org.hamcrest.CoreMatchers.hasItems;
021import static org.hamcrest.CoreMatchers.is;
022import static org.hamcrest.CoreMatchers.notNullValue;
023import static org.hamcrest.CoreMatchers.nullValue;
024import static org.hamcrest.MatcherAssert.assertThat;
025
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.Collections;
029import java.util.List;
030import java.util.stream.Collectors;
031import org.apache.hadoop.hbase.Size;
032import org.apache.hadoop.hbase.hbtop.field.Field;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037@Tag(SmallTests.TAG)
038public class TestRecordFilter {
039
040  @Test
041  public void testParseAndBuilder() {
042    testParseAndBuilder("REGION=region1", false,
043      RecordFilter.newBuilder(Field.REGION).equal("region1"));
044
045    testParseAndBuilder("REGION=", false, RecordFilter.newBuilder(Field.REGION).equal(""));
046
047    testParseAndBuilder("!REGION=region1", false,
048      RecordFilter.newBuilder(Field.REGION).notEqual("region1"));
049
050    testParseAndBuilder("REGION==region2", true,
051      RecordFilter.newBuilder(Field.REGION, true).doubleEquals("region2"));
052
053    testParseAndBuilder("!REGION==region2", true,
054      RecordFilter.newBuilder(Field.REGION, true).notDoubleEquals("region2"));
055
056    testParseAndBuilder("#REQ/S>100", false,
057      RecordFilter.newBuilder(Field.REQUEST_COUNT_PER_SECOND).greater(100L));
058
059    testParseAndBuilder("!#REQ/S>100", false,
060      RecordFilter.newBuilder(Field.REQUEST_COUNT_PER_SECOND).notGreater(100L));
061
062    testParseAndBuilder("SF>=50MB", true,
063      RecordFilter.newBuilder(Field.STORE_FILE_SIZE, true).greaterOrEqual("50MB"));
064
065    testParseAndBuilder("!SF>=50MB", true,
066      RecordFilter.newBuilder(Field.STORE_FILE_SIZE, true).notGreaterOrEqual("50MB"));
067
068    testParseAndBuilder("#REQ/S<20", false,
069      RecordFilter.newBuilder(Field.REQUEST_COUNT_PER_SECOND).less(20L));
070
071    testParseAndBuilder("!#REQ/S<20", false,
072      RecordFilter.newBuilder(Field.REQUEST_COUNT_PER_SECOND).notLess(20L));
073
074    testParseAndBuilder("%COMP<=50%", true,
075      RecordFilter.newBuilder(Field.COMPACTION_PROGRESS, true).lessOrEqual("50%"));
076
077    testParseAndBuilder("!%COMP<=50%", true,
078      RecordFilter.newBuilder(Field.COMPACTION_PROGRESS, true).notLessOrEqual("50%"));
079  }
080
081  private void testParseAndBuilder(String filterString, boolean ignoreCase, RecordFilter expected) {
082    RecordFilter actual = RecordFilter.parse(filterString, ignoreCase);
083    assertThat(expected, is(actual));
084  }
085
086  @Test
087  public void testParseFailure() {
088    RecordFilter filter = RecordFilter.parse("REGIO=region1", false);
089    assertThat(filter, is(nullValue()));
090
091    filter = RecordFilter.parse("", false);
092    assertThat(filter, is(nullValue()));
093
094    filter = RecordFilter.parse("#REQ/S==aaa", false);
095    assertThat(filter, is(nullValue()));
096
097    filter = RecordFilter.parse("SF>=50", false);
098    assertThat(filter, is(nullValue()));
099  }
100
101  @Test
102  public void testToString() {
103    testToString("REGION=region1");
104    testToString("!REGION=region1");
105    testToString("REGION==region2");
106    testToString("!REGION==region2");
107    testToString("#REQ/S>100");
108    testToString("!#REQ/S>100");
109    testToString("SF>=50.0MB");
110    testToString("!SF>=50.0MB");
111    testToString("#REQ/S<20");
112    testToString("!#REQ/S<20");
113    testToString("%COMP<=50.00%");
114    testToString("!%COMP<=50.00%");
115  }
116
117  private void testToString(String filterString) {
118    RecordFilter filter = RecordFilter.parse(filterString, false);
119    assertThat(filter, is(notNullValue()));
120    assertThat(filterString, is(filter.toString()));
121  }
122
123  @Test
124  public void testFilters() {
125    List<Record> records = createTestRecords();
126
127    testFilter(records, "REGION=region", false, "region1", "region2", "region3", "region4",
128      "region5");
129    testFilter(records, "!REGION=region", false);
130    testFilter(records, "REGION=Region", false);
131
132    testFilter(records, "REGION==region", false);
133    testFilter(records, "REGION==region1", false, "region1");
134    testFilter(records, "!REGION==region1", false, "region2", "region3", "region4", "region5");
135
136    testFilter(records, "#REQ/S==100", false, "region1");
137    testFilter(records, "#REQ/S>100", false, "region2", "region5");
138    testFilter(records, "SF>=100MB", false, "region1", "region2", "region4", "region5");
139    testFilter(records, "!#SF>=10", false, "region1", "region4");
140    testFilter(records, "LOCALITY<0.5", false, "region5");
141    testFilter(records, "%COMP<=50%", false, "region2", "region3", "region4", "region5");
142
143    testFilters(records, Arrays.asList("SF>=100MB", "#REQ/S>100"), false, "region2", "region5");
144    testFilters(records, Arrays.asList("%COMP<=50%", "!#SF>=10"), false, "region4");
145    testFilters(records, Arrays.asList("!REGION==region1", "LOCALITY<0.5", "#REQ/S>100"), false,
146      "region5");
147  }
148
149  @Test
150  public void testFiltersIgnoreCase() {
151    List<Record> records = createTestRecords();
152
153    testFilter(records, "REGION=Region", true, "region1", "region2", "region3", "region4",
154      "region5");
155    testFilter(records, "REGION=REGION", true, "region1", "region2", "region3", "region4",
156      "region5");
157  }
158
159  private List<Record> createTestRecords() {
160    List<Record> ret = new ArrayList<>();
161    ret.add(createTestRecord("region1", 100L, new Size(100, Size.Unit.MEGABYTE), 2, 1.0f, 80f));
162    ret.add(createTestRecord("region2", 120L, new Size(100, Size.Unit.GIGABYTE), 10, 0.5f, 20f));
163    ret.add(createTestRecord("region3", 50L, new Size(500, Size.Unit.KILOBYTE), 15, 0.8f, 50f));
164    ret.add(createTestRecord("region4", 90L, new Size(10, Size.Unit.TERABYTE), 5, 0.9f, 30f));
165    ret.add(createTestRecord("region5", 200L, new Size(1, Size.Unit.PETABYTE), 13, 0.1f, 40f));
166    return ret;
167  }
168
169  private Record createTestRecord(String region, long requestCountPerSecond, Size storeFileSize,
170    int numStoreFiles, float locality, float compactionProgress) {
171    Record.Builder builder = Record.builder();
172    builder.put(Field.REGION, region);
173    builder.put(Field.REQUEST_COUNT_PER_SECOND, requestCountPerSecond);
174    builder.put(Field.STORE_FILE_SIZE, storeFileSize);
175    builder.put(Field.NUM_STORE_FILES, numStoreFiles);
176    builder.put(Field.LOCALITY, locality);
177    builder.put(Field.COMPACTION_PROGRESS, compactionProgress);
178    return builder.build();
179  }
180
181  private void testFilter(List<Record> records, String filterString, boolean ignoreCase,
182    String... expectedRegions) {
183    testFilters(records, Collections.singletonList(filterString), ignoreCase, expectedRegions);
184  }
185
186  private void testFilters(List<Record> records, List<String> filterStrings, boolean ignoreCase,
187    String... expectedRegions) {
188    List<String> actual = records.stream()
189      .filter(r -> filterStrings.stream().map(f -> RecordFilter.parse(f, ignoreCase))
190        .allMatch(f -> f.execute(r)))
191      .map(r -> r.get(Field.REGION).asString()).collect(Collectors.toList());
192    assertThat(actual, hasItems(expectedRegions));
193    assertThat(actual.size(), is(expectedRegions.length));
194  }
195}