2626import static org .junit .Assert .fail ;
2727
2828import java .io .IOException ;
29+ import java .net .InetAddress ;
2930import java .nio .ByteBuffer ;
3031import java .util .ArrayList ;
3132import java .util .Collection ;
4344import org .apache .hadoop .hbase .HBaseClassTestRule ;
4445import org .apache .hadoop .hbase .HBaseTestingUtil ;
4546import org .apache .hadoop .hbase .HConstants ;
47+ import org .apache .hadoop .hbase .HRegionLocation ;
4648import org .apache .hadoop .hbase .NamespaceDescriptor ;
4749import org .apache .hadoop .hbase .TableName ;
4850import org .apache .hadoop .hbase .TableNotFoundException ;
4951import org .apache .hadoop .hbase .client .AsyncClusterConnection ;
52+ import org .apache .hadoop .hbase .client .AsyncTableRegionLocator ;
5053import org .apache .hadoop .hbase .client .ColumnFamilyDescriptor ;
5154import org .apache .hadoop .hbase .client .ColumnFamilyDescriptorBuilder ;
5255import org .apache .hadoop .hbase .client .Table ;
6366import org .apache .hadoop .hbase .testclassification .MiscTests ;
6467import org .apache .hadoop .hbase .util .Bytes ;
6568import org .apache .hadoop .hbase .util .CommonFSUtils ;
69+ import org .apache .hadoop .hbase .util .FutureUtils ;
6670import org .apache .hadoop .hbase .util .HFileTestUtil ;
71+ import org .apache .hadoop .hdfs .DistributedFileSystem ;
72+ import org .apache .hadoop .hdfs .protocol .DatanodeInfo ;
73+ import org .apache .hadoop .hdfs .protocol .LocatedBlock ;
74+ import org .apache .hadoop .hdfs .protocol .LocatedBlocks ;
6775import org .hamcrest .MatcherAssert ;
6876import org .junit .AfterClass ;
6977import org .junit .BeforeClass ;
@@ -555,15 +563,48 @@ public void testSplitStoreFile() throws IOException {
555563 FileSystem fs = util .getTestFileSystem ();
556564 Path testIn = new Path (dir , "testhfile" );
557565 ColumnFamilyDescriptor familyDesc = ColumnFamilyDescriptorBuilder .of (FAMILY );
566+ String tableName = tn .getMethodName ();
567+ util .createTable (TableName .valueOf (tableName ), familyDesc .getNameAsString ());
558568 HFileTestUtil .createHFile (util .getConfiguration (), fs , testIn , FAMILY , QUALIFIER ,
559569 Bytes .toBytes ("aaa" ), Bytes .toBytes ("zzz" ), 1000 );
560570
561571 Path bottomOut = new Path (dir , "bottom.out" );
562572 Path topOut = new Path (dir , "top.out" );
563573
564- BulkLoadHFilesTool .splitStoreFile (util .getConfiguration (), testIn , familyDesc ,
565- Bytes .toBytes ("ggg" ), bottomOut , topOut );
574+ BulkLoadHFilesTool .splitStoreFile (
575+ util .getAsyncConnection ().getRegionLocator (TableName .valueOf (tableName )),
576+ util .getConfiguration (), testIn , familyDesc , Bytes .toBytes ("ggg" ), bottomOut , topOut );
577+
578+ int rowCount = verifyHFile (bottomOut );
579+ rowCount += verifyHFile (topOut );
580+ assertEquals (1000 , rowCount );
581+ }
582+
583+ /**
584+ * Test hfile splits with the favored nodes
585+ */
586+ @ Test
587+ public void testSplitStoreFileWithFavoriteNodes () throws IOException {
566588
589+ Path dir = new Path (util .getDefaultRootDirPath (), "testhfile" );
590+ FileSystem fs = util .getDFSCluster ().getFileSystem ();
591+
592+ Path testIn = new Path (dir , "testSplitStoreFileWithFavoriteNodes" );
593+ ColumnFamilyDescriptor familyDesc = ColumnFamilyDescriptorBuilder .of (FAMILY );
594+ String tableName = tn .getMethodName ();
595+ Table table = util .createTable (TableName .valueOf (tableName ), familyDesc .getNameAsString ());
596+ HFileTestUtil .createHFile (util .getConfiguration (), fs , testIn , FAMILY , QUALIFIER ,
597+ Bytes .toBytes ("aaa" ), Bytes .toBytes ("zzz" ), 1000 );
598+
599+ Path bottomOut = new Path (dir , "bottom.out" );
600+ Path topOut = new Path (dir , "top.out" );
601+
602+ final AsyncTableRegionLocator regionLocator =
603+ util .getAsyncConnection ().getRegionLocator (TableName .valueOf (tableName ));
604+ BulkLoadHFilesTool .splitStoreFile (regionLocator , util .getConfiguration (), testIn , familyDesc ,
605+ Bytes .toBytes ("ggg" ), bottomOut , topOut );
606+ verifyHFileFavoriteNode (topOut , regionLocator , fs );
607+ verifyHFileFavoriteNode (bottomOut , regionLocator , fs );
567608 int rowCount = verifyHFile (bottomOut );
568609 rowCount += verifyHFile (topOut );
569610 assertEquals (1000 , rowCount );
@@ -575,14 +616,17 @@ public void testSplitStoreFileWithCreateTimeTS() throws IOException {
575616 FileSystem fs = util .getTestFileSystem ();
576617 Path testIn = new Path (dir , "testhfile" );
577618 ColumnFamilyDescriptor familyDesc = ColumnFamilyDescriptorBuilder .of (FAMILY );
619+ String tableName = tn .getMethodName ();
620+ util .createTable (TableName .valueOf (tableName ), familyDesc .getNameAsString ());
578621 HFileTestUtil .createHFile (util .getConfiguration (), fs , testIn , FAMILY , QUALIFIER ,
579622 Bytes .toBytes ("aaa" ), Bytes .toBytes ("zzz" ), 1000 );
580623
581624 Path bottomOut = new Path (dir , "bottom.out" );
582625 Path topOut = new Path (dir , "top.out" );
583626
584- BulkLoadHFilesTool .splitStoreFile (util .getConfiguration (), testIn , familyDesc ,
585- Bytes .toBytes ("ggg" ), bottomOut , topOut );
627+ BulkLoadHFilesTool .splitStoreFile (
628+ util .getAsyncConnection ().getRegionLocator (TableName .valueOf (tableName )),
629+ util .getConfiguration (), testIn , familyDesc , Bytes .toBytes ("ggg" ), bottomOut , topOut );
586630
587631 verifyHFileCreateTimeTS (bottomOut );
588632 verifyHFileCreateTimeTS (topOut );
@@ -615,14 +659,17 @@ private void testSplitStoreFileWithDifferentEncoding(DataBlockEncoding bulkloadE
615659 Path testIn = new Path (dir , "testhfile" );
616660 ColumnFamilyDescriptor familyDesc =
617661 ColumnFamilyDescriptorBuilder .newBuilder (FAMILY ).setDataBlockEncoding (cfEncoding ).build ();
662+ String tableName = tn .getMethodName ();
663+ util .createTable (TableName .valueOf (tableName ), familyDesc .getNameAsString ());
618664 HFileTestUtil .createHFileWithDataBlockEncoding (util .getConfiguration (), fs , testIn ,
619665 bulkloadEncoding , FAMILY , QUALIFIER , Bytes .toBytes ("aaa" ), Bytes .toBytes ("zzz" ), 1000 );
620666
621667 Path bottomOut = new Path (dir , "bottom.out" );
622668 Path topOut = new Path (dir , "top.out" );
623669
624- BulkLoadHFilesTool .splitStoreFile (util .getConfiguration (), testIn , familyDesc ,
625- Bytes .toBytes ("ggg" ), bottomOut , topOut );
670+ BulkLoadHFilesTool .splitStoreFile (
671+ util .getAsyncConnection ().getRegionLocator (TableName .valueOf (tableName )),
672+ util .getConfiguration (), testIn , familyDesc , Bytes .toBytes ("ggg" ), bottomOut , topOut );
626673
627674 int rowCount = verifyHFile (bottomOut );
628675 rowCount += verifyHFile (topOut );
@@ -654,6 +701,61 @@ private void verifyHFileCreateTimeTS(Path p) throws IOException {
654701 }
655702 }
656703
704+ /**
705+ * test split storefile with favorite node information
706+ */
707+ private void verifyHFileFavoriteNode (Path p , AsyncTableRegionLocator regionLocator , FileSystem fs )
708+ throws IOException {
709+ Configuration conf = util .getConfiguration ();
710+
711+ try (HFile .Reader reader = HFile .createReader (fs , p , new CacheConfig (conf ), true , conf );) {
712+
713+ final byte [] firstRowkey = reader .getFirstRowKey ().get ();
714+ final HRegionLocation hRegionLocation =
715+ FutureUtils .get (regionLocator .getRegionLocation (firstRowkey ));
716+
717+ final String targetHostName = hRegionLocation .getHostname ();
718+
719+ if (fs instanceof DistributedFileSystem ) {
720+ String pathStr = p .toUri ().getPath ();
721+ LocatedBlocks blocks =
722+ ((DistributedFileSystem ) fs ).getClient ().getLocatedBlocks (pathStr , 0L );
723+
724+ boolean isFavoriteNode = false ;
725+ List <LocatedBlock > locatedBlocks = blocks .getLocatedBlocks ();
726+ int index = 0 ;
727+ do {
728+ if (index > 0 ) {
729+ assertTrue ("failed use favored nodes" , isFavoriteNode );
730+ }
731+ isFavoriteNode = false ;
732+ final LocatedBlock block = locatedBlocks .get (index );
733+
734+ final DatanodeInfo [] locations = block .getLocations ();
735+ for (DatanodeInfo location : locations ) {
736+
737+ final String hostName = location .getHostName ();
738+ if (
739+ targetHostName .equals (hostName .equals ("127.0.0.1" )
740+ ? InetAddress .getLocalHost ().getHostName ()
741+ : "127.0.0.1" ) || targetHostName .equals (hostName )
742+ ) {
743+ isFavoriteNode = true ;
744+ break ;
745+ }
746+ }
747+
748+ index ++;
749+ } while (index < locatedBlocks .size ());
750+ if (index > 0 ) {
751+ assertTrue ("failed use favored nodes" , isFavoriteNode );
752+ }
753+
754+ }
755+
756+ }
757+ }
758+
657759 private void addStartEndKeysForTest (TreeMap <byte [], Integer > map , byte [] first , byte [] last ) {
658760 Integer value = map .containsKey (first ) ? map .get (first ) : 0 ;
659761 map .put (first , value + 1 );
0 commit comments