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