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