1   /*
2    * Copyright 2009 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 org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.client.Get;
26  import org.apache.hadoop.hbase.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.client.HTable;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  public class TestMultiParallelPut extends MultiRegionTable {
36    final Log LOG = LogFactory.getLog(getClass());
37    private static final byte[] VALUE = Bytes.toBytes("value");
38    private static final byte[] QUALIFIER = Bytes.toBytes("qual");
39    private static final String FAMILY = "family";
40    private static final String TEST_TABLE = "test_table";
41    private static final byte[] BYTES_FAMILY = Bytes.toBytes(FAMILY);
42  
43  
44    public TestMultiParallelPut() {
45      super(2, FAMILY);
46      desc = new HTableDescriptor(TEST_TABLE);
47      desc.addFamily(new HColumnDescriptor(FAMILY));
48      makeKeys();
49    }
50  
51    private void makeKeys() {
52      for (byte [] k : KEYS) {
53        byte [] cp = new byte[k.length+1];
54        System.arraycopy(k, 0, cp, 0, k.length);
55        cp[k.length] = 1;
56        keys.add(cp);
57      }
58    }
59  
60    List<byte[]> keys = new ArrayList<byte[]>();
61  
62    public void testParallelPut() throws Exception {
63      LOG.info("Starting testParallelPut");
64      doATest(false);
65    }
66  
67    public void testParallelPutWithRSAbort() throws Exception {
68      LOG.info("Starting testParallelPutWithRSAbort");
69      doATest(true);
70    }
71  
72    public void doATest(boolean doAbort) throws Exception {
73      HTable table = new HTable(TEST_TABLE);
74      table.setAutoFlush(false);
75      table.setWriteBufferSize(10 * 1024 * 1024);
76      for ( byte [] k : keys ) {
77        Put put = new Put(k);
78        put.add(BYTES_FAMILY, QUALIFIER, VALUE);
79        table.put(put);
80      }
81      table.flushCommits();
82  
83      if (doAbort) {
84        LOG.info("Aborting...");
85        cluster.abortRegionServer(0);
86        // try putting more keys after the abort.
87        for ( byte [] k : keys ) {
88          Put put = new Put(k);
89          put.add(BYTES_FAMILY, QUALIFIER, VALUE);
90          table.put(put);
91        }
92        table.flushCommits();
93      }
94  
95      for (byte [] k : keys ) {
96        Get get = new Get(k);
97        get.addColumn(BYTES_FAMILY, QUALIFIER);
98        Result r = table.get(get);
99        assertTrue(r.containsColumn(BYTES_FAMILY, QUALIFIER));
100       assertEquals(0,
101           Bytes.compareTo(VALUE,
102               r.getValue(BYTES_FAMILY, QUALIFIER)));
103     }
104 
105     HBaseAdmin admin = new HBaseAdmin(conf);
106     ClusterStatus cs = admin.getClusterStatus();
107     int expectedServerCount = 2;
108     if (doAbort)  expectedServerCount = 1;
109     LOG.info("Clusterstatus servers count " + cs.getServers());
110     assertEquals(expectedServerCount, cs.getServers());
111     for ( HServerInfo info : cs.getServerInfo()) {
112       LOG.info("Info from clusterstatus=" + info);
113       assertTrue(info.getLoad().getNumberOfRegions() > 8);
114     }
115   }
116 }