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.coprocessor;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import java.io.IOException;
023import java.util.Optional;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.Coprocessor;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
030import org.apache.hadoop.hbase.client.Durability;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
036import org.apache.hadoop.hbase.regionserver.ChunkCreator;
037import org.apache.hadoop.hbase.regionserver.HRegion;
038import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
039import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
040import org.apache.hadoop.hbase.regionserver.RegionServerServices;
041import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
042import org.apache.hadoop.hbase.testclassification.SmallTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
045import org.apache.hadoop.hbase.util.Threads;
046import org.apache.hadoop.hbase.wal.WALEdit;
047import org.junit.jupiter.api.Tag;
048import org.junit.jupiter.api.Test;
049import org.mockito.Mockito;
050
051@Tag(CoprocessorTests.TAG)
052@Tag(SmallTests.TAG)
053public class TestRegionObserverStacking {
054
055  private static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
056  static final Path DIR = TEST_UTIL.getDataTestDir();
057
058  public static class ObserverA implements RegionCoprocessor, RegionObserver {
059    long id;
060
061    @Override
062    public Optional<RegionObserver> getRegionObserver() {
063      return Optional.of(this);
064    }
065
066    @Override
067    public void postPut(final ObserverContext<? extends RegionCoprocessorEnvironment> c,
068      final Put put, final WALEdit edit, final Durability durability) throws IOException {
069      id = EnvironmentEdgeManager.currentTime();
070      Threads.sleepWithoutInterrupt(10);
071    }
072  }
073
074  public static class ObserverB implements RegionCoprocessor, RegionObserver {
075    long id;
076
077    @Override
078    public Optional<RegionObserver> getRegionObserver() {
079      return Optional.of(this);
080    }
081
082    @Override
083    public void postPut(final ObserverContext<? extends RegionCoprocessorEnvironment> c,
084      final Put put, final WALEdit edit, final Durability durability) throws IOException {
085      id = EnvironmentEdgeManager.currentTime();
086      Threads.sleepWithoutInterrupt(10);
087    }
088  }
089
090  public static class ObserverC implements RegionCoprocessor, RegionObserver {
091    long id;
092
093    @Override
094    public Optional<RegionObserver> getRegionObserver() {
095      return Optional.of(this);
096    }
097
098    @Override
099    public void postPut(final ObserverContext<? extends RegionCoprocessorEnvironment> c,
100      final Put put, final WALEdit edit, final Durability durability) throws IOException {
101      id = EnvironmentEdgeManager.currentTime();
102      Threads.sleepWithoutInterrupt(10);
103    }
104  }
105
106  HRegion initHRegion(byte[] tableName, String callingMethod, Configuration conf,
107    byte[]... families) throws IOException {
108    TableDescriptorBuilder builder =
109      TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
110    for (byte[] family : families) {
111      builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family));
112    }
113    TableDescriptor tableDescriptor = builder.build();
114    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
115      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
116    RegionInfo info = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();
117    Path path = new Path(DIR + callingMethod);
118    HRegion r = HBaseTestingUtil.createRegionAndWAL(info, path, conf, tableDescriptor);
119    // this following piece is a hack. currently a coprocessorHost
120    // is secretly loaded at OpenRegionHandler. we don't really
121    // start a region server here, so just manually create cphost
122    // and set it to region.
123    RegionCoprocessorHost host =
124      new RegionCoprocessorHost(r, Mockito.mock(RegionServerServices.class), conf);
125    r.setCoprocessorHost(host);
126    return r;
127  }
128
129  @Test
130  public void testRegionObserverStacking() throws Exception {
131    byte[] ROW = Bytes.toBytes("testRow");
132    byte[] TABLE = Bytes.toBytes(this.getClass().getSimpleName());
133    byte[] A = Bytes.toBytes("A");
134    byte[][] FAMILIES = new byte[][] { A };
135
136    Configuration conf = TEST_UTIL.getConfiguration();
137    HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);
138    RegionCoprocessorHost h = region.getCoprocessorHost();
139    h.load(ObserverA.class, Coprocessor.PRIORITY_HIGHEST, conf);
140    h.load(ObserverB.class, Coprocessor.PRIORITY_USER, conf);
141    h.load(ObserverC.class, Coprocessor.PRIORITY_LOWEST, conf);
142
143    Put put = new Put(ROW);
144    put.addColumn(A, A, A);
145    region.put(put);
146
147    Coprocessor c = h.findCoprocessor(ObserverA.class.getName());
148    long idA = ((ObserverA) c).id;
149    c = h.findCoprocessor(ObserverB.class.getName());
150    long idB = ((ObserverB) c).id;
151    c = h.findCoprocessor(ObserverC.class.getName());
152    long idC = ((ObserverC) c).id;
153
154    assertTrue(idA < idB);
155    assertTrue(idB < idC);
156    HBaseTestingUtil.closeRegionAndWAL(region);
157  }
158}