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  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.NamespaceDescriptor;
32  import org.apache.hadoop.hbase.NamespaceExistException;
33  import org.apache.hadoop.hbase.NamespaceNotFoundException;
34  import org.apache.hadoop.hbase.ProcedureInfo;
35  import org.apache.hadoop.hbase.constraint.ConstraintException;
36  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
37  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
38  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.CreateNamespaceState;
39  import org.apache.hadoop.hbase.testclassification.MediumTests;
40  import org.junit.After;
41  import org.junit.AfterClass;
42  import org.junit.Before;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  @Category(MediumTests.class)
48  public class TestCreateNamespaceProcedure {
49    private static final Log LOG = LogFactory.getLog(TestCreateNamespaceProcedure.class);
50  
51    protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    private static void setupConf(Configuration conf) {
54      conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
55    }
56  
57    @BeforeClass
58    public static void setupCluster() throws Exception {
59      setupConf(UTIL.getConfiguration());
60      UTIL.startMiniCluster(1);
61    }
62  
63    @AfterClass
64    public static void cleanupTest() throws Exception {
65      try {
66        UTIL.shutdownMiniCluster();
67      } catch (Exception e) {
68        LOG.warn("failure shutting down cluster", e);
69      }
70    }
71  
72    @Before
73    public void setup() throws Exception {
74      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
75    }
76  
77    @After
78    public void tearDown() throws Exception {
79      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(getMasterProcedureExecutor(), false);
80    }
81  
82    @Test(timeout = 60000)
83    public void testCreateNamespace() throws Exception {
84      final NamespaceDescriptor nsd = NamespaceDescriptor.create("testCreateNamespace").build();
85      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
86  
87      long procId = procExec.submitProcedure(
88        new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
89      // Wait the completion
90      ProcedureTestingUtility.waitProcedure(procExec, procId);
91      ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
92  
93      validateNamespaceCreated(nsd);
94    }
95  
96    @Test(timeout=60000)
97    public void testCreateSameNamespaceTwice() throws Exception {
98      final NamespaceDescriptor nsd =
99          NamespaceDescriptor.create("testCreateSameNamespaceTwice").build();
100     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
101 
102     long procId1 = procExec.submitProcedure(
103       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
104     // Wait the completion
105     ProcedureTestingUtility.waitProcedure(procExec, procId1);
106     ProcedureTestingUtility.assertProcNotFailed(procExec, procId1);
107 
108     // Create the namespace that exists
109     long procId2 = procExec.submitProcedure(
110       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
111     // Wait the completion
112     ProcedureTestingUtility.waitProcedure(procExec, procId2);
113 
114     // Second create should fail with NamespaceExistException
115     ProcedureInfo result = procExec.getResult(procId2);
116     assertTrue(result.isFailed());
117     LOG.debug("Create namespace failed with exception: " + result.getExceptionFullMessage());
118     assertTrue(
119       ProcedureTestingUtility.getExceptionCause(result) instanceof NamespaceExistException);
120   }
121 
122   @Test(timeout=60000)
123   public void testCreateSystemNamespace() throws Exception {
124     final NamespaceDescriptor nsd =
125         UTIL.getHBaseAdmin().getNamespaceDescriptor(NamespaceDescriptor.SYSTEM_NAMESPACE.getName());
126     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
127 
128     long procId = procExec.submitProcedure(
129       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
130     // Wait the completion
131     ProcedureTestingUtility.waitProcedure(procExec, procId);
132     ProcedureInfo result = procExec.getResult(procId);
133     assertTrue(result.isFailed());
134     LOG.debug("Create namespace failed with exception: " + result.getExceptionFullMessage());
135     assertTrue(
136       ProcedureTestingUtility.getExceptionCause(result) instanceof NamespaceExistException);
137   }
138 
139   @Test(timeout=60000)
140   public void testCreateNamespaceWithInvalidRegionCount() throws Exception {
141     final NamespaceDescriptor nsd =
142         NamespaceDescriptor.create("testCreateNamespaceWithInvalidRegionCount").build();
143     final String nsKey = "hbase.namespace.quota.maxregions";
144     final String nsValue = "-1";
145     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
146 
147     nsd.setConfiguration(nsKey, nsValue);
148 
149     long procId = procExec.submitProcedure(
150       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
151     // Wait the completion
152     ProcedureTestingUtility.waitProcedure(procExec, procId);
153     ProcedureInfo result = procExec.getResult(procId);
154     assertTrue(result.isFailed());
155     LOG.debug("Create namespace failed with exception: " + result.getExceptionFullMessage());
156     assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException);
157   }
158 
159   @Test(timeout=60000)
160   public void testCreateNamespaceWithInvalidTableCount() throws Exception {
161     final NamespaceDescriptor nsd =
162         NamespaceDescriptor.create("testCreateNamespaceWithInvalidTableCount").build();
163     final String nsKey = "hbase.namespace.quota.maxtables";
164     final String nsValue = "-1";
165     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
166 
167     nsd.setConfiguration(nsKey, nsValue);
168 
169     long procId = procExec.submitProcedure(
170       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
171     // Wait the completion
172     ProcedureTestingUtility.waitProcedure(procExec, procId);
173     ProcedureInfo result = procExec.getResult(procId);
174     assertTrue(result.isFailed());
175     LOG.debug("Create namespace failed with exception: " + result.getExceptionFullMessage());
176     assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof ConstraintException);
177   }
178 
179   @Test(timeout = 60000)
180   public void testRecoveryAndDoubleExecution() throws Exception {
181     final NamespaceDescriptor nsd =
182         NamespaceDescriptor.create("testRecoveryAndDoubleExecution").build();
183     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
184 
185     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
186     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
187 
188     // Start the CreateNamespace procedure && kill the executor
189     long procId = procExec.submitProcedure(
190       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
191 
192     // Restart the executor and execute the step twice
193     int numberOfSteps = CreateNamespaceState.values().length;
194     MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(
195       procExec,
196       procId,
197       numberOfSteps,
198       CreateNamespaceState.values());
199 
200     // Validate the creation of namespace
201     ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
202     validateNamespaceCreated(nsd);
203   }
204 
205   @Test(timeout = 60000)
206   public void testRollbackAndDoubleExecution() throws Exception {
207     final NamespaceDescriptor nsd =
208         NamespaceDescriptor.create("testRollbackAndDoubleExecution").build();
209     final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
210 
211     ProcedureTestingUtility.waitNoProcedureRunning(procExec);
212     ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
213 
214     // Start the CreateNamespace procedure && kill the executor
215     long procId = procExec.submitProcedure(
216       new CreateNamespaceProcedure(procExec.getEnvironment(), nsd));
217 
218     int numberOfSteps = CreateNamespaceState.values().length - 2; // failing in the middle of proc
219     MasterProcedureTestingUtility.testRollbackAndDoubleExecution(
220       procExec,
221       procId,
222       numberOfSteps,
223       CreateNamespaceState.values());
224 
225     // Validate the non-existence of namespace
226     try {
227       NamespaceDescriptor nsDescriptor = UTIL.getHBaseAdmin().getNamespaceDescriptor(nsd.getName());
228       assertNull(nsDescriptor);
229     } catch (NamespaceNotFoundException nsnfe) {
230       // Expected
231       LOG.info("The namespace " + nsd.getName() + " is not created.");
232     }
233   }
234 
235   private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
236     return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
237   }
238 
239   private void validateNamespaceCreated(NamespaceDescriptor nsd) throws IOException {
240     NamespaceDescriptor createdNsDescriptor =
241         UTIL.getHBaseAdmin().getNamespaceDescriptor(nsd.getName());
242     assertNotNull(createdNsDescriptor);
243   }
244 }