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