You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by mb...@apache.org on 2012/04/21 03:37:16 UTC
svn commit: r1328562 [9/9] - in /hbase/branches/0.89-fb: ./
src/main/java/org/apache/hadoop/hbase/
src/main/java/org/apache/hadoop/hbase/filter/
src/main/java/org/apache/hadoop/hbase/regionserver/
src/main/java/org/apache/hadoop/hbase/thrift/ src/main/...
Copied: hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServerLegacy.java (from r1328561, hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java)
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServerLegacy.java?p2=hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServerLegacy.java&p1=hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java&r1=1328561&r2=1328562&rev=1328562&view=diff
==============================================================================
--- hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServer.java (original)
+++ hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/TestThriftServerLegacy.java Sat Apr 21 01:37:15 2012
@@ -19,44 +19,45 @@
*/
package org.apache.hadoop.hbase.thrift;
-import java.io.IOException;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.assertBufferEquals;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.closeScanner;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.columnAname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.columnBname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getBatchMutations;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getColumnDescriptors;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getColumnList;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getHandlerForMetricsTest;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getMetrics;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.getMutations;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.rowAname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.rowBname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.tableAname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.tableBname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.valueAname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.valueBname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.valueCname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.valueDname;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.verifyMetricRange;
+import static org.apache.hadoop.hbase.thrift.TestThriftServer.verifyMetrics;
+
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
-import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseClusterTestCase;
-import org.apache.hadoop.hbase.thrift.generated.BatchMutation;
import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
import org.apache.hadoop.hbase.thrift.generated.Hbase;
-import org.apache.hadoop.hbase.thrift.generated.IOError;
import org.apache.hadoop.hbase.thrift.generated.Mutation;
import org.apache.hadoop.hbase.thrift.generated.TCell;
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.hadoop.hbase.util.Threads;
-import org.apache.hadoop.metrics.ContextFactory;
-import org.apache.hadoop.metrics.MetricsContext;
-import org.apache.hadoop.metrics.MetricsUtil;
-import org.apache.hadoop.metrics.spi.NoEmitMetricsContext;
-import org.apache.hadoop.metrics.spi.OutputRecord;
/**
- * Unit testing for ThriftServer.HBaseHandler, a part of the
- * org.apache.hadoop.hbase.thrift package.
+ * Unit testing for ThriftServerRunner.HBaseHandler, a part of the org.apache.hadoop.hbase.thrift
+ * package. Most of this stuff exists in {@link TestThriftServer} but there might be differences in
+ * details, so leaving this around for better test coverage.
*/
-public class TestThriftServer extends HBaseClusterTestCase {
-
- // Static names for tables, columns, rows, and values
- private static byte[] tableAname = Bytes.toBytes("tableA");
- private static byte[] tableBname = Bytes.toBytes("tableB");
- private static byte[] columnAname = Bytes.toBytes("columnA:");
- private static byte[] columnBname = Bytes.toBytes("columnB:");
- private static byte[] rowAname = Bytes.toBytes("rowA");
- private static byte[] rowBname = Bytes.toBytes("rowB");
- private static byte[] valueAname = Bytes.toBytes("valueA");
- private static byte[] valueBname = Bytes.toBytes("valueB");
- private static byte[] valueCname = Bytes.toBytes("valueC");
- private static byte[] valueDname = Bytes.toBytes("valueD");
+public class TestThriftServerLegacy extends HBaseClusterTestCase {
/**
* Runs all of the tests under a single JUnit test method. We
@@ -85,7 +86,7 @@ public class TestThriftServer extends HB
* @throws Exception
*/
public void doTestTableCreateDrop() throws Exception {
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
// Create/enable/disable/delete tables, ensure methods act correctly
assertEquals(handler.getTableNames().size(), 0);
@@ -107,21 +108,6 @@ public class TestThriftServer extends HB
handler.deleteTable(tableAname);
}
- public static final class MySlowHBaseHandler extends ThriftServer.HBaseHandler
- implements Hbase.Iface {
-
- protected MySlowHBaseHandler(Configuration c, ThriftMetrics metrics)
- throws IOException {
- super(c, metrics);
- }
-
- @Override
- public List<byte[]> getTableNames() throws IOError {
- Threads.sleepWithoutInterrupt(3000);
- return super.getTableNames();
- }
- }
-
/**
* Tests if the metrics for thrift handler work correctly
*/
@@ -144,51 +130,6 @@ public class TestThriftServer extends HB
6L * 1000 * 1000 * 1000);
}
- private static Hbase.Iface getHandlerForMetricsTest(ThriftMetrics metrics, Configuration conf)
- throws Exception {
- Hbase.Iface handler = new MySlowHBaseHandler(conf, metrics);
- return HbaseHandlerMetricsProxy.newInstance(handler, metrics, conf);
- }
-
- private static ThriftMetrics getMetrics(Configuration conf) throws Exception {
- setupMetricsContext();
- return new ThriftMetrics(
- Integer.parseInt(ThriftServer.DEFAULT_LISTEN_PORT), conf);
- }
-
- private static void setupMetricsContext() throws IOException {
- ContextFactory factory = ContextFactory.getFactory();
- factory.setAttribute(ThriftMetrics.CONTEXT_NAME + ".class",
- NoEmitMetricsContext.class.getName());
- MetricsUtil.getContext(ThriftMetrics.CONTEXT_NAME)
- .createRecord(ThriftMetrics.CONTEXT_NAME).remove();
- }
-
- private static void verifyMetrics(ThriftMetrics metrics, String name, long expectValue)
- throws Exception {
- long metricVal = getMetricValue(metrics, name);
- assertEquals(expectValue, metricVal);
- }
-
- private static void verifyMetricRange(ThriftMetrics metrics, String name,
- long minValue, long maxValue)
- throws Exception {
- long metricVal = getMetricValue(metrics, name);
- if (metricVal < minValue || metricVal > maxValue) {
- throw new AssertionError("Value of metric " + name + " is outside of the expected " +
- "range [" + minValue + ", " + maxValue + "]: " + metricVal);
- }
- }
-
- private static long getMetricValue(ThriftMetrics metrics, String name) {
- MetricsContext context = MetricsUtil.getContext(
- ThriftMetrics.CONTEXT_NAME);
- metrics.doUpdates(context);
- OutputRecord record = context.getAllRecords().get(
- ThriftMetrics.CONTEXT_NAME).iterator().next();
- return record.getMetric(name).longValue();
- }
-
/**
* Tests adding a series of Mutations and BatchMutations, including a
* delete mutation. Also tests data retrieval, and getting back multiple
@@ -198,7 +139,7 @@ public class TestThriftServer extends HB
*/
public void doTestTableMutations() throws Exception {
// Setup
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
handler.createTable(tableAname, getColumnDescriptors());
// Apply a few Mutations to rowA
@@ -207,12 +148,10 @@ public class TestThriftServer extends HB
handler.mutateRow(tableAname, rowAname, getMutations());
// Assert that the changes were made
- assertTrue(Bytes.equals(valueAname,
- handler.get(tableAname, rowAname, columnAname).get(0).value));
+ assertBufferEquals(valueAname, handler.get(tableAname, rowAname, columnAname).get(0).value);
TRowResult rowResult1 = handler.getRow(tableAname, rowAname).get(0);
- assertTrue(Bytes.equals(rowAname, rowResult1.row));
- assertTrue(Bytes.equals(valueBname,
- rowResult1.columns.get(columnBname).value));
+ assertBufferEquals(rowAname, rowResult1.row);
+ assertBufferEquals(valueBname, rowResult1.columns.get(columnBname).value);
// Apply a few BatchMutations for rowA and rowB
// rowAmutations.add(new Mutation(true, columnAname, null));
@@ -227,16 +166,16 @@ public class TestThriftServer extends HB
// Assert that changes were made to rowA
List<TCell> cells = handler.get(tableAname, rowAname, columnAname);
assertFalse(cells.size() > 0);
- assertTrue(Bytes.equals(valueCname, handler.get(tableAname, rowAname, columnBname).get(0).value));
+ assertBufferEquals(valueCname, handler.get(tableAname, rowAname, columnBname).get(0).value);
List<TCell> versions = handler.getVer(tableAname, rowAname, columnBname, MAXVERSIONS);
- assertTrue(Bytes.equals(valueCname, versions.get(0).value));
- assertTrue(Bytes.equals(valueBname, versions.get(1).value));
+ assertBufferEquals(valueCname, versions.get(0).value);
+ assertBufferEquals(valueBname, versions.get(1).value);
// Assert that changes were made to rowB
TRowResult rowResult2 = handler.getRow(tableAname, rowBname).get(0);
- assertTrue(Bytes.equals(rowBname, rowResult2.row));
- assertTrue(Bytes.equals(valueCname, rowResult2.columns.get(columnAname).value));
- assertTrue(Bytes.equals(valueDname, rowResult2.columns.get(columnBname).value));
+ assertBufferEquals(rowBname, rowResult2.row);
+ assertBufferEquals(valueCname, rowResult2.columns.get(columnAname).value);
+ assertBufferEquals(valueDname, rowResult2.columns.get(columnBname).value);
// Apply some deletes
handler.deleteAll(tableAname, rowAname, columnBname);
@@ -254,7 +193,7 @@ public class TestThriftServer extends HB
handler.mutateRow(tableAname, rowAname, mutations);
TRowResult rowResult3 = handler.getRow(tableAname, rowAname).get(0);
assertEquals(rowAname, rowResult3.row);
- assertEquals(0, rowResult3.columns.get(columnAname).value.length);
+ assertEquals(0, rowResult3.columns.get(columnAname).value.remaining());
// Teardown
handler.disableTable(tableAname);
@@ -270,7 +209,7 @@ public class TestThriftServer extends HB
*/
public void doTestTableTimestampsAndColumns() throws Exception {
// Setup
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
@@ -299,22 +238,22 @@ public class TestThriftServer extends HB
TRowResult rowResult1 = handler.getRowTs(tableAname, rowAname, time1).get(0);
TRowResult rowResult2 = handler.getRowTs(tableAname, rowAname, time2).get(0);
// columnA was completely deleted
- //assertTrue(Bytes.equals(rowResult1.columns.get(columnAname).value, valueAname));
- assertTrue(Bytes.equals(rowResult1.columns.get(columnBname).value, valueBname));
- assertTrue(Bytes.equals(rowResult2.columns.get(columnBname).value, valueCname));
+ //assertBufferEquals(rowResult1.columns.get(columnAname).value, valueAname);
+ assertBufferEquals(rowResult1.columns.get(columnBname).value, valueBname);
+ assertBufferEquals(rowResult2.columns.get(columnBname).value, valueCname);
// ColumnAname has been deleted, and will never be visible even with a getRowTs()
assertFalse(rowResult2.columns.containsKey(columnAname));
- List<byte[]> columns = new ArrayList<byte[]>();
+ List<ByteBuffer> columns = new ArrayList<ByteBuffer>();
columns.add(columnBname);
rowResult1 = handler.getRowWithColumns(tableAname, rowAname, columns).get(0);
- assertTrue(Bytes.equals(rowResult1.columns.get(columnBname).value, valueCname));
+ assertBufferEquals(rowResult1.columns.get(columnBname).value, valueCname);
assertFalse(rowResult1.columns.containsKey(columnAname));
rowResult1 = handler.getRowWithColumnsTs(tableAname, rowAname, columns, time1).get(0);
- assertTrue(Bytes.equals(rowResult1.columns.get(columnBname).value, valueBname));
+ assertBufferEquals(rowResult1.columns.get(columnBname).value, valueBname);
assertFalse(rowResult1.columns.containsKey(columnAname));
// Apply some timestamped deletes
@@ -331,7 +270,7 @@ public class TestThriftServer extends HB
assertEquals(1, size);
// should be available....
- assertTrue(Bytes.equals(handler.get(tableAname, rowAname, columnBname).get(0).value, valueCname));
+ assertBufferEquals(handler.get(tableAname, rowAname, columnBname).get(0).value, valueCname);
assertEquals(0, handler.getRow(tableAname, rowBname).size());
@@ -348,7 +287,7 @@ public class TestThriftServer extends HB
*/
public void doTestTableScanners() throws Exception {
// Setup
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
@@ -368,18 +307,18 @@ public class TestThriftServer extends HB
// Test a scanner on all rows and all columns, no timestamp
int scanner1 = handler.scannerOpen(tableAname, rowAname, getColumnList(true, true));
TRowResult rowResult1a = handler.scannerGet(scanner1).get(0);
- assertTrue(Bytes.equals(rowResult1a.row, rowAname));
+ assertBufferEquals(rowResult1a.row, rowAname);
// This used to be '1'. I don't know why when we are asking for two columns
// and when the mutations above would seem to add two columns to the row.
// -- St.Ack 05/12/2009
assertEquals(rowResult1a.columns.size(), 1);
- assertTrue(Bytes.equals(rowResult1a.columns.get(columnBname).value, valueCname));
+ assertBufferEquals(rowResult1a.columns.get(columnBname).value, valueCname);
TRowResult rowResult1b = handler.scannerGet(scanner1).get(0);
- assertTrue(Bytes.equals(rowResult1b.row, rowBname));
+ assertBufferEquals(rowResult1b.row, rowBname);
assertEquals(rowResult1b.columns.size(), 2);
- assertTrue(Bytes.equals(rowResult1b.columns.get(columnAname).value, valueCname));
- assertTrue(Bytes.equals(rowResult1b.columns.get(columnBname).value, valueDname));
+ assertBufferEquals(rowResult1b.columns.get(columnAname).value, valueCname);
+ assertBufferEquals(rowResult1b.columns.get(columnBname).value, valueDname);
closeScanner(scanner1, handler);
// Test a scanner on all rows and all columns, with timestamp
@@ -387,8 +326,8 @@ public class TestThriftServer extends HB
TRowResult rowResult2a = handler.scannerGet(scanner2).get(0);
assertEquals(rowResult2a.columns.size(), 1);
// column A deleted, does not exist.
- //assertTrue(Bytes.equals(rowResult2a.columns.get(columnAname).value, valueAname));
- assertTrue(Bytes.equals(rowResult2a.columns.get(columnBname).value, valueBname));
+ //assertBufferEquals(rowResult2a.columns.get(columnAname).value, valueAname);
+ assertBufferEquals(rowResult2a.columns.get(columnBname).value, valueBname);
closeScanner(scanner2, handler);
// Test a scanner on the first row and first column only, no timestamp
@@ -401,7 +340,7 @@ public class TestThriftServer extends HB
getColumnList(false, true), time1);
TRowResult rowResult4a = handler.scannerGet(scanner4).get(0);
assertEquals(rowResult4a.columns.size(), 1);
- assertTrue(Bytes.equals(rowResult4a.columns.get(columnBname).value, valueBname));
+ assertBufferEquals(rowResult4a.columns.get(columnBname).value, valueBname);
// Teardown
handler.disableTable(tableAname);
@@ -415,7 +354,7 @@ public class TestThriftServer extends HB
*/
public void doTestTableMultiGet() throws Exception {
// Setup
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
@@ -433,7 +372,7 @@ public class TestThriftServer extends HB
time1 += 1;
// test getting the two rows with a multiget
- List<byte[]> rows = new ArrayList<byte[]>();
+ List<ByteBuffer> rows = new ArrayList<ByteBuffer>();
rows.add(rowAname);
rows.add(rowBname);
@@ -457,7 +396,7 @@ public class TestThriftServer extends HB
*/
public void doTestTableCheckAndMutate() throws Exception {
// Setup
- ThriftServer.HBaseHandler handler = new ThriftServer.HBaseHandler(conf);
+ ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(conf);
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
@@ -489,9 +428,9 @@ public class TestThriftServer extends HB
// Mess stuff up; shouldn't pass, wrong check value
handler.checkAndMutateRowTs(tableAname, rowAname, columnAname,
- Bytes.toBytes("randovalue1"), getMutations2(), time2);
+ ByteBuffer.wrap(Bytes.toBytes("randovalue1")), getMutations2(), time2);
handler.checkAndMutateRowTs(tableAname, rowAname, columnBname,
- Bytes.toBytes("randovalue2"), getMutations2(), time2);
+ ByteBuffer.wrap(Bytes.toBytes("randovalue2")), getMutations2(), time2);
// Check that all still the same!
assertEquals(res1.get(0).columns.get(columnAname).value, valueAname);
@@ -513,53 +452,6 @@ public class TestThriftServer extends HB
/**
*
- * @return a List of ColumnDescriptors for use in creating a table. Has one
- * default ColumnDescriptor and one ColumnDescriptor with fewer
- * versions
- */
- private List<ColumnDescriptor> getColumnDescriptors() {
- ArrayList<ColumnDescriptor> cDescriptors = new ArrayList<ColumnDescriptor>();
-
- // A default ColumnDescriptor
- ColumnDescriptor cDescA = new ColumnDescriptor();
- cDescA.name = columnAname;
- cDescriptors.add(cDescA);
-
- // A slightly customized ColumnDescriptor (only 2 versions)
- ColumnDescriptor cDescB = new ColumnDescriptor(columnBname, 2, "NONE",
- false, "NONE", 0, 0, false, -1);
- cDescriptors.add(cDescB);
-
- return cDescriptors;
- }
-
- /**
- *
- * @param includeA whether or not to include columnA
- * @param includeB whether or not to include columnB
- * @return a List of column names for use in retrieving a scanner
- */
- private List<byte[]> getColumnList(boolean includeA, boolean includeB) {
- List<byte[]> columnList = new ArrayList<byte[]>();
- if (includeA) columnList.add(columnAname);
- if (includeB) columnList.add(columnBname);
- return columnList;
- }
-
- /**
- *
- * @return a List of Mutations for a row, with columnA having valueA
- * and columnB having valueB
- */
- private List<Mutation> getMutations() {
- List<Mutation> mutations = new ArrayList<Mutation>();
- mutations.add(new Mutation(false, columnAname, valueAname));
- mutations.add(new Mutation(false, columnBname, valueBname));
- return mutations;
- }
-
- /**
- *
* @return a List of Mutations for a row, with columnA having valueC and
* columnB having valueD
*/
@@ -570,43 +462,4 @@ public class TestThriftServer extends HB
return mutations;
}
- /**
- *
- * @return a List of BatchMutations with the following effects: (rowA,
- * columnA): delete (rowA, columnB): place valueC (rowB, columnA):
- * place valueC (rowB, columnB): place valueD
- */
- private List<BatchMutation> getBatchMutations() {
- List<BatchMutation> batchMutations = new ArrayList<BatchMutation>();
-
- // Mutations to rowA. You can't mix delete and put anymore.
- List<Mutation> rowAmutations = new ArrayList<Mutation>();
- rowAmutations.add(new Mutation(true, columnAname, null));
- batchMutations.add(new BatchMutation(rowAname, rowAmutations));
-
- rowAmutations = new ArrayList<Mutation>();
- rowAmutations.add(new Mutation(false, columnBname, valueCname));
- batchMutations.add(new BatchMutation(rowAname, rowAmutations));
-
- // Mutations to rowB
- List<Mutation> rowBmutations = new ArrayList<Mutation>();
- rowBmutations.add(new Mutation(false, columnAname, valueCname));
- rowBmutations.add(new Mutation(false, columnBname, valueDname));
- batchMutations.add(new BatchMutation(rowBname, rowBmutations));
-
- return batchMutations;
- }
-
- /**
- * Asserts that the passed scanner is exhausted, and then closes
- * the scanner.
- *
- * @param scannerId the scanner to close
- * @param handler the HBaseHandler interfacing to HBase
- * @throws Exception
- */
- private void closeScanner(int scannerId, ThriftServer.HBaseHandler handler) throws Exception {
- handler.scannerGet(scannerId);
- handler.scannerClose(scannerId);
- }
}
Added: hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/ThriftServerCmdLineTestBase.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/ThriftServerCmdLineTestBase.java?rev=1328562&view=auto
==============================================================================
--- hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/ThriftServerCmdLineTestBase.java (added)
+++ hbase/branches/0.89-fb/src/test/java/org/apache/hadoop/hbase/thrift/ThriftServerCmdLineTestBase.java Sat Apr 21 01:37:15 2012
@@ -0,0 +1,235 @@
+/*
+ * Copyright The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hadoop.hbase.thrift;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.thrift.ThriftServerRunner.ImplType;
+import org.apache.hadoop.hbase.thrift.generated.Hbase;
+import org.apache.hadoop.hbase.util.Threads;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.server.TServer;
+import org.apache.thrift.transport.TFramedTransport;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.common.base.Joiner;
+
+/**
+ * Start the HBase Thrift server on a random port through the command-line
+ * interface and talk to it from client side. This itself is not a JUnit test,
+ * but a base class for subclasses testing individual thrift server types.
+ */
+public class ThriftServerCmdLineTestBase {
+
+ public static final Log LOG =
+ LogFactory.getLog(ThriftServerCmdLineTestBase.class);
+
+ private final ImplType implType;
+ private boolean specifyFramed;
+ private boolean specifyBindIP;
+ private boolean specifyCompact;
+
+ private static final HBaseTestingUtility TEST_UTIL =
+ new HBaseTestingUtility();
+
+ private Thread cmdLineThread;
+ private volatile Exception cmdLineException;
+
+ private Exception clientSideException;
+
+ private ThriftServer thriftServer;
+ private int port;
+
+ /**
+ * @param klass what thrift server class to use
+ * @param framed whether to use framed server, or null to include all configurations
+ * @return parameters for the unit test
+ */
+ static Collection<Object[]> getParameters(Class<? extends TServer> klass,
+ Boolean framed) {
+ Collection<Object[]> parameters = new ArrayList<Object[]>();
+ for (ImplType implType : ImplType.values()) {
+ if (implType.getServerClass() != klass) {
+ continue;
+ }
+
+ for (boolean specifyFramed : new boolean[] {false, true}) {
+ if (framed != null && specifyFramed != framed) {
+ continue;
+ }
+ for (boolean specifyBindIP : new boolean[] {false, true}) {
+ if (specifyBindIP && !implType.canSpecifyBindIP) {
+ continue;
+ }
+ for (boolean specifyCompact : new boolean[] {false, true}) {
+ parameters.add(new Object[]{implType, new Boolean(specifyFramed),
+ new Boolean(specifyBindIP), new Boolean(specifyCompact)});
+ }
+ }
+ }
+ }
+ return parameters;
+ }
+
+ public ThriftServerCmdLineTestBase(ImplType implType, boolean specifyFramed,
+ boolean specifyBindIP, boolean specifyCompact) {
+ this.implType = implType;
+ this.specifyFramed = specifyFramed;
+ this.specifyBindIP = specifyBindIP;
+ this.specifyCompact = specifyCompact;
+ LOG.debug(getParametersString());
+ }
+
+ private String getParametersString() {
+ return "implType=" + implType + ", " +
+ "specifyFramed=" + specifyFramed + ", " +
+ "specifyBindIP=" + specifyBindIP + ", " +
+ "specifyCompact=" + specifyCompact;
+ }
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ TEST_UTIL.startMiniCluster();
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ TEST_UTIL.shutdownMiniCluster();
+ }
+
+ private void startCmdLineThread(final String[] args) {
+ LOG.info("Starting HBase Thrift server with command line: " +
+ Joiner.on(" ").join(args));
+
+ cmdLineException = null;
+ cmdLineThread = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ thriftServer.doMain(args);
+ } catch (Exception e) {
+ cmdLineException = e;
+ }
+ }
+ });
+ cmdLineThread.setName(ThriftServer.class.getSimpleName() +
+ "-cmdline");
+ cmdLineThread.start();
+ }
+
+ @Test(timeout=90 * 1000)
+ public void testRunThriftServer() throws Exception {
+ List<String> args = new ArrayList<String>();
+ if (implType != null) {
+ String serverTypeOption = implType.toString();
+ assertTrue(serverTypeOption.startsWith("-"));
+ args.add(serverTypeOption);
+ }
+ port = HBaseTestingUtility.randomFreePort();
+ args.add("-" + ThriftServer.PORT_OPTION);
+ args.add(String.valueOf(port));
+ if (specifyFramed) {
+ args.add("-" + ThriftServer.FRAMED_OPTION);
+ }
+ if (specifyBindIP) {
+ args.add("-" + ThriftServer.BIND_OPTION);
+ args.add(InetAddress.getLocalHost().getHostName());
+ }
+ if (specifyCompact) {
+ args.add("-" + ThriftServer.COMPACT_OPTION);
+ }
+ args.add("start");
+
+ thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
+ startCmdLineThread(args.toArray(new String[0]));
+ Threads.sleepWithoutInterrupt(2000);
+
+ Class<? extends TServer> expectedClass = implType != null ?
+ implType.serverClass : TBoundedThreadPoolServer.class;
+ assertEquals(expectedClass,
+ thriftServer.serverRunner.tserver.getClass());
+
+ try {
+ talkToThriftServer();
+ } catch (Exception ex) {
+ clientSideException = ex;
+ } finally {
+ stopCmdLineThread();
+ }
+
+ if (clientSideException != null) {
+ LOG.error("Thrift client threw an exception. Parameters:" +
+ getParametersString(), clientSideException);
+ throw new Exception(clientSideException);
+ }
+ }
+
+ private void talkToThriftServer() throws Exception {
+ TSocket sock = new TSocket(InetAddress.getLocalHost().getHostName(),
+ port);
+ TTransport transport = sock;
+ if (specifyFramed || implType.isAlwaysFramed) {
+ transport = new TFramedTransport(transport);
+ }
+
+ sock.open();
+ try {
+ TProtocol prot;
+ if (specifyCompact) {
+ prot = new TCompactProtocol(transport);
+ } else {
+ prot = new TBinaryProtocol(transport);
+ }
+ Hbase.Client client = new Hbase.Client(prot);
+ TestThriftServer.doTestTableCreateDrop(client);
+ TestThriftServer.doTestGetRegionInfo(client);
+ TestThriftServer.doTestGetTableRegions(client);
+ TestThriftServer.doTestTableMutations(client);
+ } finally {
+ sock.close();
+ }
+ }
+
+ private void stopCmdLineThread() throws Exception {
+ LOG.debug("Stopping " + implType.simpleClassName() + " Thrift server");
+ thriftServer.stop();
+ cmdLineThread.join();
+ if (cmdLineException != null) {
+ LOG.error("Command-line invocation of HBase Thrift server threw an " +
+ "exception", cmdLineException);
+ throw new Exception(cmdLineException);
+ }
+ }
+
+}