1818package org .apache .hadoop .hbase .client ;
1919
2020import static org .junit .Assert .assertEquals ;
21- import static org .junit .Assert .assertNotNull ;
2221import static org .junit .Assert .assertTrue ;
2322
23+ import java .io .IOException ;
2424import java .util .List ;
25- import java .util .concurrent .CompletableFuture ;
26- import java .util .concurrent .ExecutionException ;
2725import org .apache .hadoop .hbase .DoNotRetryIOException ;
2826import org .apache .hadoop .hbase .HBaseClassTestRule ;
2927import org .apache .hadoop .hbase .HBaseTestingUtil ;
30- import org .apache .hadoop .hbase .HRegionLocation ;
3128import org .apache .hadoop .hbase .TableName ;
29+ import org .apache .hadoop .hbase .filter .PrefixFilter ;
3230import org .apache .hadoop .hbase .regionserver .HRegion ;
3331import org .apache .hadoop .hbase .testclassification .ClientTests ;
3432import org .apache .hadoop .hbase .testclassification .MediumTests ;
4644import org .junit .rules .TestName ;
4745
4846@ Category ({ MediumTests .class , ClientTests .class })
49- public class TestAsyncTableRegionLocatorWithRegionReplicaId {
47+ public class TestScanOrGetWithReplicationFromClient {
5048 @ ClassRule
5149 public static final HBaseClassTestRule CLASS_RULE =
52- HBaseClassTestRule .forClass (TestAsyncTableRegionLocatorWithRegionReplicaId .class );
50+ HBaseClassTestRule .forClass (TestScanOrGetWithReplicationFromClient .class );
5351
5452 @ Rule
5553 public TestName name = new TestName ();
@@ -63,15 +61,13 @@ public class TestAsyncTableRegionLocatorWithRegionReplicaId {
6361 // region replica id starts from 0
6462 private static final int NON_EXISTING_REGION_REPLICA_ID = REGION_REPLICATION_COUNT ;
6563 private static Connection connection ;
66- private static AsyncConnection asyncConn ;
6764 private static Admin admin ;
6865 private TableName tableName ;
6966
7067 @ BeforeClass
7168 public static void setUpBeforeClass () throws Exception {
7269 UTIL .startMiniCluster (1 );
7370 connection = UTIL .getConnection ();
74- asyncConn = ConnectionFactory .createAsyncConnection (UTIL .getConfiguration ()).get ();
7571 admin = UTIL .getAdmin ();
7672 }
7773
@@ -115,60 +111,98 @@ public void tearDown() throws Exception {
115111 }
116112
117113 @ Test
118- public void testMetaTableRegionLocatorWithRegionReplicaId ()
119- throws ExecutionException , InterruptedException {
120- AsyncTableRegionLocator locator = asyncConn .getRegionLocator (TableName .META_TABLE_NAME );
121- CompletableFuture <HRegionLocation > future =
122- locator .getRegionLocation (tableName .getName (), RegionReplicaUtil .DEFAULT_REPLICA_ID , true );
123- HRegionLocation hrl = future .get ();
124- assertNotNull (hrl );
114+ public void testScanMetaWithRegionReplicaId () throws IOException {
115+ Table metaTable = connection .getTable (TableName .META_TABLE_NAME );
116+ Scan scan = new Scan ();
117+ scan .setFilter (new PrefixFilter (tableName .getName ()));
118+ scan .setReplicaId (RegionReplicaUtil .DEFAULT_REPLICA_ID );
119+ scan .setConsistency (Consistency .TIMELINE );
120+ ResultScanner rs = metaTable .getScanner (scan );
121+ rs .forEach (r -> assertTrue (Bytes .toString (r .getRow ()).contains (tableName .getNameAsString ())));
125122 }
126123
127124 @ Test
128- public void testMetaTableRegionLocatorWithNonExistingRegionReplicaId ()
129- throws InterruptedException {
130- AsyncTableRegionLocator locator = asyncConn .getRegionLocator (TableName .META_TABLE_NAME );
131- CompletableFuture <HRegionLocation > future =
132- locator .getRegionLocation (tableName .getName (), NON_EXISTING_REGION_REPLICA_ID , true );
125+ public void testScanMetaWithNonExistingRegionReplicaId () throws IOException {
126+ Table metaTable = connection .getTable (TableName .META_TABLE_NAME );
127+ Scan scan = new Scan ();
128+ scan .setReplicaId (NON_EXISTING_REGION_REPLICA_ID );
129+ scan .setConsistency (Consistency .TIMELINE );
130+ exception .expect (DoNotRetryIOException .class );
131+ ResultScanner rs = metaTable .getScanner (scan );
133132 try {
134- future .get ();
135- } catch (ExecutionException e ) {
136- assertTrue (e .getCause () instanceof DoNotRetryIOException );
133+ rs .forEach (r -> Bytes .toString (r .getRow ()));
134+ } catch (Exception e ) {
135+ Throwable throwable = e .getCause ();
136+ assertTrue (throwable instanceof DoNotRetryIOException );
137137 String message = "The specified region replica id " + NON_EXISTING_REGION_REPLICA_ID
138138 + " does not exist, the REGION_REPLICATION of this table "
139139 + TableName .META_TABLE_NAME .getNameAsString () + " is "
140140 + TableDescriptorBuilder .DEFAULT_REGION_REPLICATION + ", "
141141 + "this means that the maximum region replica id you can specify is "
142142 + (TableDescriptorBuilder .DEFAULT_REGION_REPLICATION - 1 ) + "." ;
143- assertEquals (message , e .getCause ().getMessage ());
143+ assertEquals (message , throwable .getMessage ());
144+ }
145+ }
146+
147+ @ Test
148+ public void testScanTableWithRegionReplicaId () throws IOException {
149+ Table table = connection .getTable (tableName );
150+ Scan scan = new Scan ();
151+ scan .setReplicaId (RegionReplicaUtil .DEFAULT_REPLICA_ID );
152+ scan .setConsistency (Consistency .TIMELINE );
153+ ResultScanner rs = table .getScanner (scan );
154+ rs .forEach (r -> assertEquals (ROW , Bytes .toString (r .getRow ())));
155+ }
156+
157+ @ Test
158+ public void testScanTableWithNonExistingRegionReplicaId () throws IOException {
159+ Table table = connection .getTable (tableName );
160+ Scan scan = new Scan ();
161+ scan .setReplicaId (NON_EXISTING_REGION_REPLICA_ID );
162+ scan .setConsistency (Consistency .TIMELINE );
163+ exception .expect (DoNotRetryIOException .class );
164+ ResultScanner rs = table .getScanner (scan );
165+ try {
166+ rs .forEach (r -> Bytes .toString (r .getRow ()));
167+ } catch (Exception e ) {
168+ Throwable throwable = e .getCause ();
169+ assertTrue (throwable instanceof DoNotRetryIOException );
170+ String message = "The specified region replica id " + NON_EXISTING_REGION_REPLICA_ID
171+ + " does not exist, the REGION_REPLICATION of this table " + tableName .getNameAsString ()
172+ + " is " + REGION_REPLICATION_COUNT + ", "
173+ + "this means that the maximum region replica id you can specify is "
174+ + (REGION_REPLICATION_COUNT - 1 ) + "." ;
175+ assertEquals (message , throwable .getMessage ());
144176 }
145177 }
146178
147179 @ Test
148- public void testTableRegionLocatorWithRegionReplicaId ()
149- throws ExecutionException , InterruptedException {
150- AsyncTableRegionLocator locator = asyncConn .getRegionLocator (tableName );
151- CompletableFuture <HRegionLocation > future =
152- locator .getRegionLocation (Bytes .toBytes (ROW ), RegionReplicaUtil .DEFAULT_REPLICA_ID , true );
153- HRegionLocation hrl = future .get ();
154- assertNotNull (hrl );
180+ public void testGetTableWithRegionReplicaId () throws IOException {
181+ Table table = connection .getTable (tableName );
182+ Get get = new Get (Bytes .toBytes (ROW )).setConsistency (Consistency .TIMELINE )
183+ .setReplicaId (RegionReplicaUtil .DEFAULT_REPLICA_ID );
184+ Result result = table .get (get );
185+ assertEquals (ROW , Bytes .toString (result .getRow ()));
186+ String value = Bytes .toString (result .getValue (FAMILY , Bytes .toBytes ("q" )));
187+ assertEquals ("test_value" , value );
155188 }
156189
157190 @ Test
158- public void testTableRegionLocatorWithNonExistingRegionReplicaId () throws InterruptedException {
159- AsyncTableRegionLocator locator = asyncConn . getRegionLocator (tableName );
160- CompletableFuture < HRegionLocation > future =
161- locator . getRegionLocation ( Bytes . toBytes ( ROW ), NON_EXISTING_REGION_REPLICA_ID , true );
191+ public void testGetTableWithNonExistingRegionReplicaId () throws IOException {
192+ Table table = connection . getTable (tableName );
193+ Get get = new Get ( Bytes . toBytes ( ROW )). setConsistency ( Consistency . TIMELINE )
194+ . setReplicaId ( NON_EXISTING_REGION_REPLICA_ID );
162195 try {
163- future .get ();
164- } catch (ExecutionException e ) {
165- assertTrue (e .getCause () instanceof DoNotRetryIOException );
196+ Result result = table .get (get );
197+ result .getValue (FAMILY , Bytes .toBytes ("q" ));
198+ } catch (Exception e ) {
199+ assertTrue (e instanceof DoNotRetryIOException );
166200 String message = "The specified region replica id " + NON_EXISTING_REGION_REPLICA_ID
167201 + " does not exist, the REGION_REPLICATION of this table " + tableName .getNameAsString ()
168202 + " is " + REGION_REPLICATION_COUNT + ", "
169203 + "this means that the maximum region replica id you can specify is "
170204 + (REGION_REPLICATION_COUNT - 1 ) + "." ;
171- assertEquals (message , e .getCause (). getMessage ());
205+ assertEquals (message , e .getMessage ());
172206 }
173207 }
174208}
0 commit comments