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