HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / TestHBaseMetaEdit.java
blobfb3ea93fe5b3391fe6659b2d4a57d9392fa6ddac
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
25 import java.io.IOException;
26 import java.util.Collections;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
29 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
30 import org.apache.hadoop.hbase.client.RegionInfoBuilder;
31 import org.apache.hadoop.hbase.client.TableDescriptor;
32 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
33 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
34 import org.apache.hadoop.hbase.regionserver.Region;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.apache.hadoop.hbase.testclassification.MiscTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.ClassRule;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.junit.rules.TestName;
46 /**
47 * Test being able to edit hbase:meta.
49 @Category({ MiscTests.class, MediumTests.class })
50 public class TestHBaseMetaEdit {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestHBaseMetaEdit.class);
54 @Rule
55 public TestName name = new TestName();
56 private final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
58 @Before
59 public void before() throws Exception {
60 UTIL.startMiniCluster();
63 @After
64 public void after() throws Exception {
65 UTIL.shutdownMiniCluster();
68 // make sure that with every possible way, we get the same meta table descriptor.
69 private TableDescriptor getMetaDescriptor() throws TableNotFoundException, IOException {
70 Admin admin = UTIL.getAdmin();
71 TableDescriptor get = admin.getDescriptor(TableName.META_TABLE_NAME);
72 TableDescriptor list =
73 admin.listTableDescriptors(true).stream().filter(td -> td.isMetaTable()).findAny().get();
74 TableDescriptor listByName =
75 admin.listTableDescriptors(Collections.singletonList(TableName.META_TABLE_NAME)).get(0);
76 TableDescriptor listByNs =
77 admin.listTableDescriptorsByNamespace(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME).stream()
78 .filter(td -> td.isMetaTable()).findAny().get();
79 assertEquals(get, list);
80 assertEquals(get, listByName);
81 assertEquals(get, listByNs);
82 return get;
85 /**
86 * Set versions, set HBASE-16213 indexed block encoding, and add a column family.
87 * Delete the column family. Then try to delete a core hbase:meta family (should fail).
88 * Verify they are all in place by looking at TableDescriptor AND by checking
89 * what the RegionServer sees after opening Region.
91 @Test
92 public void testEditMeta() throws IOException {
93 Admin admin = UTIL.getAdmin();
94 admin.tableExists(TableName.META_TABLE_NAME);
95 TableDescriptor originalDescriptor = getMetaDescriptor();
96 ColumnFamilyDescriptor cfd = originalDescriptor.getColumnFamily(HConstants.CATALOG_FAMILY);
97 int oldVersions = cfd.getMaxVersions();
98 // Add '1' to current versions count. Set encoding too.
99 cfd = ColumnFamilyDescriptorBuilder.newBuilder(cfd).setMaxVersions(oldVersions + 1).
100 setConfiguration(ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING,
101 DataBlockEncoding.ROW_INDEX_V1.toString()).build();
102 admin.modifyColumnFamily(TableName.META_TABLE_NAME, cfd);
103 byte [] extraColumnFamilyName = Bytes.toBytes("xtra");
104 ColumnFamilyDescriptor newCfd =
105 ColumnFamilyDescriptorBuilder.newBuilder(extraColumnFamilyName).build();
106 admin.addColumnFamily(TableName.META_TABLE_NAME, newCfd);
107 TableDescriptor descriptor = getMetaDescriptor();
108 // Assert new max versions is == old versions plus 1.
109 assertEquals(oldVersions + 1,
110 descriptor.getColumnFamily(HConstants.CATALOG_FAMILY).getMaxVersions());
111 descriptor = getMetaDescriptor();
112 // Assert new max versions is == old versions plus 1.
113 assertEquals(oldVersions + 1,
114 descriptor.getColumnFamily(HConstants.CATALOG_FAMILY).getMaxVersions());
115 assertTrue(descriptor.getColumnFamily(newCfd.getName()) != null);
116 String encoding = descriptor.getColumnFamily(HConstants.CATALOG_FAMILY).getConfiguration().
117 get(ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING);
118 assertEquals(encoding, DataBlockEncoding.ROW_INDEX_V1.toString());
119 Region r = UTIL.getHBaseCluster().getRegionServer(0).
120 getRegion(RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedName());
121 assertEquals(oldVersions + 1,
122 r.getStore(HConstants.CATALOG_FAMILY).getColumnFamilyDescriptor().getMaxVersions());
123 encoding = r.getStore(HConstants.CATALOG_FAMILY).getColumnFamilyDescriptor().
124 getConfigurationValue(ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING);
125 assertEquals(encoding, DataBlockEncoding.ROW_INDEX_V1.toString());
126 assertTrue(r.getStore(extraColumnFamilyName) != null);
127 // Assert we can't drop critical hbase:meta column family but we can drop any other.
128 admin.deleteColumnFamily(TableName.META_TABLE_NAME, newCfd.getName());
129 descriptor = getMetaDescriptor();
130 assertTrue(descriptor.getColumnFamily(newCfd.getName()) == null);
131 try {
132 admin.deleteColumnFamily(TableName.META_TABLE_NAME, HConstants.CATALOG_FAMILY);
133 fail("Should not reach here");
134 } catch (HBaseIOException hioe) {
135 assertTrue(hioe.getMessage().contains("Delete of hbase:meta"));
140 * Validate whether meta table can be altered as READ only, shouldn't be allowed otherwise it will
141 * break assignment functionalities. See HBASE-24977.
143 @Test
144 public void testAlterMetaWithReadOnly() throws IOException {
145 Admin admin = UTIL.getAdmin();
146 TableDescriptor origMetaTableDesc = admin.getDescriptor(TableName.META_TABLE_NAME);
147 assertFalse(origMetaTableDesc.isReadOnly());
148 TableDescriptor newTD =
149 TableDescriptorBuilder.newBuilder(origMetaTableDesc).setReadOnly(true).build();
150 try {
151 admin.modifyTable(newTD);
152 fail("Meta table can't be set as read only");
153 } catch (Exception e) {
154 assertFalse(admin.getDescriptor(TableName.META_TABLE_NAME).isReadOnly());
157 // Create a table to check region assignment & meta operation
158 TableName tableName = TableName.valueOf("tempTable");
159 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName).setReadOnly(true)
160 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1")).build())
161 .build();
162 UTIL.getAdmin().createTable(td);
163 UTIL.deleteTable(tableName);