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.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.Optional;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
034import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044
045@Category({ CoprocessorTests.class, MediumTests.class })
046public class TestMasterObserverToModifyTableSchema {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestMasterObserverToModifyTableSchema.class);
051
052  private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
053  private static TableName TABLENAME = TableName.valueOf("TestTable");
054
055  @Rule
056  public TestName name = new TestName();
057
058  @BeforeClass
059  public static void setupBeforeClass() throws Exception {
060    Configuration conf = UTIL.getConfiguration();
061    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
062        OnlyOneVersionAllowedMasterObserver.class.getName());
063    UTIL.startMiniCluster(1);
064  }
065
066  @AfterClass
067  public static void tearDownAfterClass() throws Exception {
068    UTIL.shutdownMiniCluster();
069  }
070
071  @Test
072  public void testMasterObserverToModifyTableSchema() throws IOException {
073    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLENAME);
074    for (int i = 1; i <= 3; i++) {
075      builder.setColumnFamily(
076          ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf" + i)).setMaxVersions(i)
077              .build());
078    }
079    try (Admin admin = UTIL.getAdmin()) {
080      admin.createTable(builder.build());
081      assertOneVersion(admin.getDescriptor(TABLENAME));
082
083      builder.modifyColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1"))
084          .setMaxVersions(Integer.MAX_VALUE).build());
085      admin.modifyTable(builder.build());
086      assertOneVersion(admin.getDescriptor(TABLENAME));
087    }
088  }
089
090  private void assertOneVersion(TableDescriptor td) {
091    for (ColumnFamilyDescriptor cfd : td.getColumnFamilies()) {
092      assertEquals(1, cfd.getMaxVersions());
093    }
094  }
095
096  public static class OnlyOneVersionAllowedMasterObserver
097      implements MasterCoprocessor, MasterObserver {
098
099    @Override
100    public Optional<MasterObserver> getMasterObserver() {
101      return Optional.of(this);
102    }
103
104    @Override
105    public TableDescriptor preCreateTableRegionsInfos(
106        ObserverContext<MasterCoprocessorEnvironment> ctx, TableDescriptor desc)
107        throws IOException {
108      TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(desc);
109      for (ColumnFamilyDescriptor cfd : desc.getColumnFamilies()) {
110        builder.modifyColumnFamily(
111            ColumnFamilyDescriptorBuilder.newBuilder(cfd).setMaxVersions(1).build());
112      }
113      return builder.build();
114    }
115
116    @Override
117    public TableDescriptor preModifyTable(ObserverContext<MasterCoprocessorEnvironment> env,
118        TableName tableName, final TableDescriptor currentDescriptor,
119        final TableDescriptor newDescriptor) throws IOException {
120      TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(newDescriptor);
121      for (ColumnFamilyDescriptor cfd : newDescriptor.getColumnFamilies()) {
122        builder.modifyColumnFamily(
123            ColumnFamilyDescriptorBuilder.newBuilder(cfd).setMaxVersions(1).build());
124      }
125      return builder.build();
126    }
127  }
128}