1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.master;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.HBaseClusterTestCase;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.client.ResultScanner;
31  
32  public class TestMinimumServerCount extends HBaseClusterTestCase {
33  
34    static final String TABLE_NAME = "TestTable";
35  
36    public TestMinimumServerCount() {
37      // start cluster with one region server only
38      super(1, true);
39    }
40  
41    boolean isTableAvailable(String tableName) throws IOException {
42      boolean available = true;
43      HTable meta = new HTable(conf, ".META.");
44      ResultScanner scanner = meta.getScanner(HConstants.CATALOG_FAMILY);
45      Result result;
46      while ((result = scanner.next()) != null) {
47        // set available to false if a region of the table is found with no
48        // assigned server
49        byte[] value = result.getValue(HConstants.CATALOG_FAMILY,
50          HConstants.SERVER_QUALIFIER);
51        if (value == null) {
52          available = false;
53          break;
54        }
55      }
56      return available;
57    }
58  
59    public void testMinimumServerCount() throws Exception {
60      HBaseAdmin admin = new HBaseAdmin(conf);
61  
62      // create and disable table
63      admin.createTable(createTableDescriptor(TABLE_NAME));
64      admin.disableTable(TABLE_NAME);
65      assertFalse(admin.isTableEnabled(TABLE_NAME));
66  
67      // reach in and set minimum server count
68      cluster.hbaseCluster.getMaster().getServerManager()
69        .setMinimumServerCount(2);
70  
71      // now try to enable the table
72      try {
73        admin.enableTable(TABLE_NAME);
74      } catch (IOException ex) {
75        // ignore
76      }
77      Thread.sleep(10 * 1000);
78      assertFalse(admin.isTableAvailable(TABLE_NAME));
79  
80      // now start another region server
81      cluster.startRegionServer();
82  
83      // sleep a bit for assignment
84      Thread.sleep(10 * 1000);
85      assertTrue(admin.isTableAvailable(TABLE_NAME));
86    }
87  
88  }