View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase;
19  
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;
24  
25  import java.io.IOException;
26  import java.util.regex.Pattern;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.client.Durability;
31  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
32  import org.apache.hadoop.hbase.coprocessor.SampleRegionWALObserver;
33  import org.apache.hadoop.hbase.exceptions.DeserializationException;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   * Test setting values in the descriptor
40   */
41  @Category(SmallTests.class)
42  public class TestHTableDescriptor {
43    final static Log LOG = LogFactory.getLog(TestHTableDescriptor.class);
44  
45    @Test
46    public void testPb() throws DeserializationException, IOException {
47      HTableDescriptor htd = new HTableDescriptor(HTableDescriptor.META_TABLEDESC);
48      final int v = 123;
49      htd.setMaxFileSize(v);
50      htd.setDurability(Durability.ASYNC_WAL);
51      htd.setReadOnly(true);
52      byte [] bytes = htd.toByteArray();
53      HTableDescriptor deserializedHtd = HTableDescriptor.parseFrom(bytes);
54      assertEquals(htd, deserializedHtd);
55      assertEquals(v, deserializedHtd.getMaxFileSize());
56      assertTrue(deserializedHtd.isReadOnly());
57      assertEquals(Durability.ASYNC_WAL, deserializedHtd.getDurability());
58    }
59  
60    /**
61     * Test cps in the table description
62     * @throws Exception
63     */
64    @Test
65    public void testGetSetRemoveCP() throws Exception {
66      HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
67      // simple CP
68      String className = BaseRegionObserver.class.getName();
69      // add and check that it is present
70      desc.addCoprocessor(className);
71      assertTrue(desc.hasCoprocessor(className));
72      // remove it and check that it is gone
73      desc.removeCoprocessor(className);
74      assertFalse(desc.hasCoprocessor(className));
75    }
76  
77    /**
78     * Test cps in the table description
79     * @throws Exception
80     */
81    @Test
82    public void testSetListRemoveCP() throws Exception {
83      HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("testGetSetRemoveCP"));
84      // simple CP
85      String className1 = BaseRegionObserver.class.getName();
86      String className2 = SampleRegionWALObserver.class.getName();
87      // Check that any coprocessor is present.
88      assertTrue(desc.getCoprocessors().size() == 0);
89  
90      // Add the 1 coprocessor and check if present.
91      desc.addCoprocessor(className1);
92      assertTrue(desc.getCoprocessors().size() == 1);
93      assertTrue(desc.getCoprocessors().contains(className1));
94  
95      // Add the 2nd coprocessor and check if present.
96      // remove it and check that it is gone
97      desc.addCoprocessor(className2);
98      assertTrue(desc.getCoprocessors().size() == 2);
99      assertTrue(desc.getCoprocessors().contains(className2));
100 
101     // Remove one and check
102     desc.removeCoprocessor(className1);
103     assertTrue(desc.getCoprocessors().size() == 1);
104     assertFalse(desc.getCoprocessors().contains(className1));
105     assertTrue(desc.getCoprocessors().contains(className2));
106 
107     // Remove the last and check
108     desc.removeCoprocessor(className2);
109     assertTrue(desc.getCoprocessors().size() == 0);
110     assertFalse(desc.getCoprocessors().contains(className1));
111     assertFalse(desc.getCoprocessors().contains(className2));
112   }
113 
114   /**
115    * Test that we add and remove strings from settings properly.
116    * @throws Exception
117    */
118   @Test
119   public void testRemoveString() throws Exception {
120     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
121     String key = "Some";
122     String value = "value";
123     desc.setValue(key, value);
124     assertEquals(value, desc.getValue(key));
125     desc.remove(key);
126     assertEquals(null, desc.getValue(key));
127   }
128 
129   String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
130       "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
131       , "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
132       "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
133   String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
134       "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
135       "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
136 
137   @Test
138   public void testLegalHTableNames() {
139     for (String tn : legalTableNames) {
140       TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
141     }
142   }
143 
144   @Test
145   public void testIllegalHTableNames() {
146     for (String tn : illegalTableNames) {
147       try {
148         TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
149         fail("invalid tablename " + tn + " should have failed");
150       } catch (Exception e) {
151         // expected
152       }
153     }
154   }
155 
156   @Test
157   public void testLegalHTableNamesRegex() {
158     for (String tn : legalTableNames) {
159       TableName tName = TableName.valueOf(tn);
160       assertTrue("Testing: '" + tn + "'", Pattern.matches(TableName.VALID_USER_TABLE_REGEX,
161           tName.getNameAsString()));
162     }
163   }
164 
165   @Test
166   public void testIllegalHTableNamesRegex() {
167     for (String tn : illegalTableNames) {
168       LOG.info("Testing: '" + tn + "'");
169       assertFalse(Pattern.matches(TableName.VALID_USER_TABLE_REGEX, tn));
170     }
171   }
172 
173     /**
174    * Test default value handling for maxFileSize
175    */
176   @Test
177   public void testGetMaxFileSize() {
178     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
179     assertEquals(-1, desc.getMaxFileSize());
180     desc.setMaxFileSize(1111L);
181     assertEquals(1111L, desc.getMaxFileSize());
182   }
183 
184   /**
185    * Test default value handling for memStoreFlushSize
186    */
187   @Test
188   public void testGetMemStoreFlushSize() {
189     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
190     assertEquals(-1, desc.getMemStoreFlushSize());
191     desc.setMemStoreFlushSize(1111L);
192     assertEquals(1111L, desc.getMemStoreFlushSize());
193   }
194 
195   /**
196    * Test that we add and remove strings from configuration properly.
197    */
198   @Test
199   public void testAddGetRemoveConfiguration() throws Exception {
200     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
201     String key = "Some";
202     String value = "value";
203     desc.setConfiguration(key, value);
204     assertEquals(value, desc.getConfigurationValue(key));
205     desc.removeConfiguration(key);
206     assertEquals(null, desc.getConfigurationValue(key));
207   }
208 }