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.wal;
019
020import static org.junit.Assert.assertEquals;
021
022import com.google.protobuf.ServiceException;
023import java.io.IOException;
024import java.util.List;
025import java.util.Random;
026import java.util.SortedMap;
027import java.util.TreeMap;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Delete;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.master.HMaster;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.regionserver.Region;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.RegionServerTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.After;
041import org.junit.Before;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045
046import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
047
048import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.FlushRegionRequest;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.GetLastFlushedSequenceIdRequest;
051
052@Category({RegionServerTests.class, MediumTests.class})
053public class TestWALFiltering {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057      HBaseClassTestRule.forClass(TestWALFiltering.class);
058
059  private static final int NUM_RS = 4;
060
061  private static final TableName TABLE_NAME =
062      TableName.valueOf("TestWALFiltering");
063  private static final byte[] CF1 = Bytes.toBytes("MyCF1");
064  private static final byte[] CF2 = Bytes.toBytes("MyCF2");
065  private static final byte[][] FAMILIES = { CF1, CF2 };
066
067  private HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
068
069  @Before
070  public void setUp() throws Exception {
071    TEST_UTIL.startMiniCluster(NUM_RS);
072    fillTable();
073  }
074
075  @After
076  public void tearDown() throws Exception {
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  private void fillTable() throws IOException, InterruptedException {
081    Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILIES, 3,
082        Bytes.toBytes("row0"), Bytes.toBytes("row99"), NUM_RS);
083    Random rand = new Random(19387129L);
084    for (int iStoreFile = 0; iStoreFile < 4; ++iStoreFile) {
085      for (int iRow = 0; iRow < 100; ++iRow) {
086        final byte[] row = Bytes.toBytes("row" + iRow);
087        Put put = new Put(row);
088        Delete del = new Delete(row);
089        for (int iCol = 0; iCol < 10; ++iCol) {
090          final byte[] cf = rand.nextBoolean() ? CF1 : CF2;
091          final long ts = Math.abs(rand.nextInt());
092          final byte[] qual = Bytes.toBytes("col" + iCol);
093          if (rand.nextBoolean()) {
094            final byte[] value = Bytes.toBytes("value_for_row_" + iRow +
095                "_cf_" + Bytes.toStringBinary(cf) + "_col_" + iCol + "_ts_" +
096                ts + "_random_" + rand.nextLong());
097            put.addColumn(cf, qual, ts, value);
098          } else if (rand.nextDouble() < 0.8) {
099            del.addColumn(cf, qual, ts);
100          } else {
101            del.addColumn(cf, qual, ts);
102          }
103        }
104        table.put(put);
105        table.delete(del);
106      }
107    }
108    TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
109  }
110
111  @Test
112  public void testFlushedSequenceIdsSentToHMaster()
113  throws IOException, InterruptedException,
114  org.apache.hbase.thirdparty.com.google.protobuf.ServiceException, ServiceException {
115    SortedMap<byte[], Long> allFlushedSequenceIds = new TreeMap<>(Bytes.BYTES_COMPARATOR);
116    for (int i = 0; i < NUM_RS; ++i) {
117      flushAllRegions(i);
118    }
119    Thread.sleep(10000);
120    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
121    for (int i = 0; i < NUM_RS; ++i) {
122      for (byte[] regionName : getRegionsByServer(i)) {
123        if (allFlushedSequenceIds.containsKey(regionName)) {
124          GetLastFlushedSequenceIdRequest req =
125            RequestConverter.buildGetLastFlushedSequenceIdRequest(regionName);
126
127          assertEquals((long)allFlushedSequenceIds.get(regionName),
128            master.getMasterRpcServices().getLastFlushedSequenceId(
129              null, req).getLastFlushedSequenceId());
130        }
131      }
132    }
133  }
134
135  private List<byte[]> getRegionsByServer(int rsId) throws IOException {
136    List<byte[]> regionNames = Lists.newArrayList();
137    HRegionServer hrs = getRegionServer(rsId);
138    for (Region r : hrs.getRegions(TABLE_NAME)) {
139      regionNames.add(r.getRegionInfo().getRegionName());
140    }
141    return regionNames;
142  }
143
144  private HRegionServer getRegionServer(int rsId) {
145    return TEST_UTIL.getMiniHBaseCluster().getRegionServer(rsId);
146  }
147
148  private void flushAllRegions(int rsId)
149  throws ServiceException,
150  org.apache.hbase.thirdparty.com.google.protobuf.ServiceException, IOException {
151    HRegionServer hrs = getRegionServer(rsId);
152    for (byte[] regionName : getRegionsByServer(rsId)) {
153      FlushRegionRequest request =
154        RequestConverter.buildFlushRegionRequest(regionName);
155      hrs.getRSRpcServices().flushRegion(null, request);
156    }
157  }
158
159}