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 (expected=IOException.class)
47    public void testAddCoprocessorTwice() throws IOException {
48      HTableDescriptor htd = new HTableDescriptor(TableName.META_TABLE_NAME);
49      String cpName = "a.b.c.d";
50      htd.addCoprocessor(cpName);
51      htd.addCoprocessor(cpName);
52    }
53  
54    @Test
55    public void testAddCoprocessorWithSpecStr() throws IOException {
56      HTableDescriptor htd = new HTableDescriptor(TableName.META_TABLE_NAME);
57      String cpName = "a.b.c.d";
58      boolean expected = false;
59      try {
60        htd.addCoprocessorWithSpec(cpName);
61      } catch (IllegalArgumentException iae) {
62        expected = true;
63      }
64      if (!expected) fail();
65      // Try minimal spec.
66      try {
67        htd.addCoprocessorWithSpec("file:///some/path" + "|" + cpName);
68      } catch (IllegalArgumentException iae) {
69        expected = false;
70      }
71      if (expected) fail();
72      // Try more spec.
73      String spec = "hdfs:///foo.jar|com.foo.FooRegionObserver|1001|arg1=1,arg2=2";
74      try {
75        htd.addCoprocessorWithSpec(spec);
76      } catch (IllegalArgumentException iae) {
77        expected = false;
78      }
79      if (expected) fail();
80      // Try double add of same coprocessor
81      try {
82        htd.addCoprocessorWithSpec(spec);
83      } catch (IOException ioe) {
84        expected = true;
85      }
86      if (!expected) fail();
87    }
88  
89    @Test
90    public void testPb() throws DeserializationException, IOException {
91      HTableDescriptor htd = new HTableDescriptor(TableName.META_TABLE_NAME);
92      final int v = 123;
93      htd.setMaxFileSize(v);
94      htd.setDurability(Durability.ASYNC_WAL);
95      htd.setReadOnly(true);
96      byte [] bytes = htd.toByteArray();
97      HTableDescriptor deserializedHtd = HTableDescriptor.parseFrom(bytes);
98      assertEquals(htd, deserializedHtd);
99      assertEquals(v, deserializedHtd.getMaxFileSize());
100     assertTrue(deserializedHtd.isReadOnly());
101     assertEquals(Durability.ASYNC_WAL, deserializedHtd.getDurability());
102   }
103 
104   /**
105    * Test cps in the table description
106    * @throws Exception
107    */
108   @Test
109   public void testGetSetRemoveCP() throws Exception {
110     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
111     // simple CP
112     String className = BaseRegionObserver.class.getName();
113     // add and check that it is present
114     desc.addCoprocessor(className);
115     assertTrue(desc.hasCoprocessor(className));
116     // remove it and check that it is gone
117     desc.removeCoprocessor(className);
118     assertFalse(desc.hasCoprocessor(className));
119   }
120 
121   /**
122    * Test cps in the table description
123    * @throws Exception
124    */
125   @Test
126   public void testSetListRemoveCP() throws Exception {
127     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("testGetSetRemoveCP"));
128     // simple CP
129     String className1 = BaseRegionObserver.class.getName();
130     String className2 = SampleRegionWALObserver.class.getName();
131     // Check that any coprocessor is present.
132     assertTrue(desc.getCoprocessors().size() == 0);
133 
134     // Add the 1 coprocessor and check if present.
135     desc.addCoprocessor(className1);
136     assertTrue(desc.getCoprocessors().size() == 1);
137     assertTrue(desc.getCoprocessors().contains(className1));
138 
139     // Add the 2nd coprocessor and check if present.
140     // remove it and check that it is gone
141     desc.addCoprocessor(className2);
142     assertTrue(desc.getCoprocessors().size() == 2);
143     assertTrue(desc.getCoprocessors().contains(className2));
144 
145     // Remove one and check
146     desc.removeCoprocessor(className1);
147     assertTrue(desc.getCoprocessors().size() == 1);
148     assertFalse(desc.getCoprocessors().contains(className1));
149     assertTrue(desc.getCoprocessors().contains(className2));
150 
151     // Remove the last and check
152     desc.removeCoprocessor(className2);
153     assertTrue(desc.getCoprocessors().size() == 0);
154     assertFalse(desc.getCoprocessors().contains(className1));
155     assertFalse(desc.getCoprocessors().contains(className2));
156   }
157 
158   /**
159    * Test that we add and remove strings from settings properly.
160    * @throws Exception
161    */
162   @Test
163   public void testRemoveString() throws Exception {
164     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
165     String key = "Some";
166     String value = "value";
167     desc.setValue(key, value);
168     assertEquals(value, desc.getValue(key));
169     desc.remove(key);
170     assertEquals(null, desc.getValue(key));
171   }
172 
173   String legalTableNames[] = { "foo", "with-dash_under.dot", "_under_start_ok",
174       "with-dash.with_underscore", "02-01-2012.my_table_01-02", "xyz._mytable_", "9_9_0.table_02"
175       , "dot1.dot2.table", "new.-mytable", "with-dash.with.dot", "legal..t2", "legal..legal.t2",
176       "trailingdots..", "trailing.dots...", "ns:mytable", "ns:_mytable_", "ns:my_table_01-02"};
177   String illegalTableNames[] = { ".dot_start_illegal", "-dash_start_illegal", "spaces not ok",
178       "-dash-.start_illegal", "new.table with space", "01 .table", "ns:-illegaldash",
179       "new:.illegaldot", "new:illegalcolon1:", "new:illegalcolon1:2"};
180 
181   @Test
182   public void testLegalHTableNames() {
183     for (String tn : legalTableNames) {
184       TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
185     }
186   }
187 
188   @Test
189   public void testIllegalHTableNames() {
190     for (String tn : illegalTableNames) {
191       try {
192         TableName.isLegalFullyQualifiedTableName(Bytes.toBytes(tn));
193         fail("invalid tablename " + tn + " should have failed");
194       } catch (Exception e) {
195         // expected
196       }
197     }
198   }
199 
200   @Test
201   public void testLegalHTableNamesRegex() {
202     for (String tn : legalTableNames) {
203       TableName tName = TableName.valueOf(tn);
204       assertTrue("Testing: '" + tn + "'", Pattern.matches(TableName.VALID_USER_TABLE_REGEX,
205           tName.getNameAsString()));
206     }
207   }
208 
209   @Test
210   public void testIllegalHTableNamesRegex() {
211     for (String tn : illegalTableNames) {
212       LOG.info("Testing: '" + tn + "'");
213       assertFalse(Pattern.matches(TableName.VALID_USER_TABLE_REGEX, tn));
214     }
215   }
216 
217     /**
218    * Test default value handling for maxFileSize
219    */
220   @Test
221   public void testGetMaxFileSize() {
222     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
223     assertEquals(-1, desc.getMaxFileSize());
224     desc.setMaxFileSize(1111L);
225     assertEquals(1111L, desc.getMaxFileSize());
226   }
227 
228   /**
229    * Test default value handling for memStoreFlushSize
230    */
231   @Test
232   public void testGetMemStoreFlushSize() {
233     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
234     assertEquals(-1, desc.getMemStoreFlushSize());
235     desc.setMemStoreFlushSize(1111L);
236     assertEquals(1111L, desc.getMemStoreFlushSize());
237   }
238 
239   /**
240    * Test that we add and remove strings from configuration properly.
241    */
242   @Test
243   public void testAddGetRemoveConfiguration() throws Exception {
244     HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("table"));
245     String key = "Some";
246     String value = "value";
247     desc.setConfiguration(key, value);
248     assertEquals(value, desc.getConfigurationValue(key));
249     desc.removeConfiguration(key);
250     assertEquals(null, desc.getConfigurationValue(key));
251   }
252 }