1   /**
2    * Copyright 2008 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;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.client.HTable;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.Scan;
29  import org.apache.hadoop.hbase.client.ResultScanner;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  /**
33   * Tests master cleanup of rows in meta table where there is no HRegionInfo
34   */
35  public class TestEmptyMetaInfo extends HBaseClusterTestCase {
36    /**
37     * Insert some bogus rows in meta. Master should clean them up.
38     * @throws IOException
39     */
40    public void testEmptyMetaInfo() throws IOException {
41      HTable t = new HTable(conf, HConstants.META_TABLE_NAME);
42      final int COUNT = 5;
43      final byte [] tableName = Bytes.toBytes(getName());
44      for (int i = 0; i < COUNT; i++) {
45        byte [] regionName = HRegionInfo.createRegionName(tableName,
46          Bytes.toBytes(i == 0? "": Integer.toString(i)),
47          Long.toString(System.currentTimeMillis()), true);
48        Put put = new Put(regionName);
49        put.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
50            Bytes.toBytes("localhost:1234"));
51        t.put(put);
52      }
53      long sleepTime =
54        conf.getLong("hbase.master.meta.thread.rescanfrequency", 10000);
55      int tries = conf.getInt("hbase.client.retries.number", 5);
56      int count = 0;
57      do {
58        tries -= 1;
59        try {
60          Thread.sleep(sleepTime);
61        } catch (InterruptedException e) {
62          // ignore
63        }
64        Scan scan = new Scan();
65        scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
66        scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
67        scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER);
68        scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER);
69        scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER);
70        ResultScanner scanner = t.getScanner(scan);
71        try {
72          count = 0;
73          Result r;
74          while((r = scanner.next()) != null) {
75            if (!r.isEmpty()) {
76              count += 1;
77            }
78          }
79        } finally {
80          scanner.close();
81        }
82      } while (count != 0 && tries >= 0);
83      assertTrue(tries >= 0);
84      assertEquals(0, count);
85    }
86  }