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.client;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.Collections;
025import java.util.Map;
026import java.util.Set;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032
033import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
034import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;
035
036@Tag(ClientTests.TAG)
037@Tag(SmallTests.TAG)
038public class TestOnlineLogRecord {
039
040  @Test
041  public void itSerializesScan() {
042    Scan scan = new Scan();
043    scan.withStartRow(Bytes.toBytes(123));
044    scan.withStopRow(Bytes.toBytes(456));
045    String expectedOutput = "{\n" + "  \"startTime\": 1,\n" + "  \"processingTime\": 2,\n"
046      + "  \"queueTime\": 3,\n" + "  \"responseSize\": 4,\n" + "  \"blockBytesScanned\": 5,\n"
047      + "  \"fsReadTime\": 6,\n" + "  \"multiGetsCount\": 6,\n" + "  \"multiMutationsCount\": 7,\n"
048      + "  \"scan\": {\n" + "    \"totalColumns\": 0,\n" + "    \"maxResultSize\": -1,\n"
049      + "    \"caching\": -1,\n" + "    \"includeStopRow\": false,\n"
050      + "    \"consistency\": \"STRONG\",\n" + "    \"maxVersions\": 1,\n"
051      + "    \"mvccReadPoint\": -1,\n" + "    \"includeStartRow\": true,\n"
052      + "    \"stopRow\": \"\\\\x00\\\\x00\\\\x01\\\\xC8\",\n" + "    \"limit\": -1,\n"
053      + "    \"timeRange\": [\n" + "      0,\n" + "      9223372036854775807\n" + "    ],\n"
054      + "    \"startRow\": \"\\\\x00\\\\x00\\\\x00{\",\n" + "    \"targetReplicaId\": -1,\n"
055      + "    \"batch\": -1,\n" + "    \"families\": {},\n" + "    \"priority\": -1,\n"
056      + "    \"storeOffset\": 0,\n" + "    \"queryMetricsEnabled\": false,\n"
057      + "    \"needCursorResult\": false,\n" + "    \"storeLimit\": -1,\n"
058      + "    \"cacheBlocks\": true,\n" + "    \"readType\": \"DEFAULT\",\n"
059      + "    \"allowPartialResults\": false,\n" + "    \"reversed\": false\n" + "  }\n" + "}";
060    OnlineLogRecord o = new OnlineLogRecord(1, 2, 3, 4, 5, 6, null, null, null, null, null, null,
061      null, 6, 7, 0, scan, Collections.emptyMap(), Collections.emptyMap());
062    String actualOutput = o.toJsonPrettyPrint();
063    System.out.println(actualOutput);
064    assertEquals(expectedOutput, actualOutput);
065  }
066
067  @Test
068  public void itSerializesRequestAttributes() {
069    Map<String, byte[]> requestAttributes = ImmutableMap.<String, byte[]> builder()
070      .put("r", Bytes.toBytes("1")).put("2", Bytes.toBytes(0.0)).build();
071    Set<String> expectedOutputs =
072      ImmutableSet.<String> builder().add("requestAttributes").add("\"r\": \"1\"")
073        .add("\"2\": \"\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\"").build();
074    OnlineLogRecord o = new OnlineLogRecord(1, 2, 3, 4, 5, 6, null, null, null, null, null, null,
075      null, 6, 7, 0, null, requestAttributes, Collections.emptyMap());
076    String actualOutput = o.toJsonPrettyPrint();
077    System.out.println(actualOutput);
078    expectedOutputs.forEach(expected -> assertTrue(actualOutput.contains(expected)));
079  }
080
081  @Test
082  public void itOmitsEmptyRequestAttributes() {
083    OnlineLogRecord o = new OnlineLogRecord(1, 2, 3, 4, 5, 6, null, null, null, null, null, null,
084      null, 6, 7, 0, null, Collections.emptyMap(), Collections.emptyMap());
085    String actualOutput = o.toJsonPrettyPrint();
086    System.out.println(actualOutput);
087    assertFalse(actualOutput.contains("requestAttributes"));
088  }
089
090  @Test
091  public void itSerializesConnectionAttributes() {
092    Map<String, byte[]> connectionAttributes = ImmutableMap.<String, byte[]> builder()
093      .put("c", Bytes.toBytes("1")).put("2", Bytes.toBytes(0.0)).build();
094    Set<String> expectedOutputs =
095      ImmutableSet.<String> builder().add("connectionAttributes").add("\"c\": \"1\"")
096        .add("\"2\": \"\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\\\\x00\"").build();
097    OnlineLogRecord o = new OnlineLogRecord(1, 2, 3, 4, 5, 6, null, null, null, null, null, null,
098      null, 6, 7, 0, null, Collections.emptyMap(), connectionAttributes);
099    String actualOutput = o.toJsonPrettyPrint();
100    System.out.println(actualOutput);
101    expectedOutputs.forEach(expected -> assertTrue(actualOutput.contains(expected)));
102  }
103
104  @Test
105  public void itOmitsEmptyConnectionAttributes() {
106    OnlineLogRecord o = new OnlineLogRecord(1, 2, 3, 4, 5, 6, null, null, null, null, null, null,
107      null, 6, 7, 0, null, Collections.emptyMap(), Collections.emptyMap());
108    String actualOutput = o.toJsonPrettyPrint();
109    System.out.println(actualOutput);
110    assertFalse(actualOutput.contains("connectionAttributes"));
111  }
112}