@@ -580,6 +580,33 @@ public TableEntry refreshTabletLocationBatch(String tableName) throws Exception
580580 }
581581 }
582582
583+ public void refreshTabletLocationForAtomicQuery (String tableName , ObTableQuery query , boolean isHKV ) throws Exception {
584+ Map <Long , ObTableParam > partIdParamMap = getPartIdParamMapForQuery (tableName , query .getScanRangeColumns (), query .getKeyRanges ());
585+ if (isHKV ) {
586+ // for HBase process, if distributed function is enabled, no need to do routing refresh
587+ boolean isDistributedSupported = getServerCapacity ().isSupportDistributedExecute ();
588+ if (partIdParamMap .size () > 1 && !isDistributedSupported ) {
589+ throw new ObTablePartitionConsistentException (
590+ "query and mutate must be a atomic operation" );
591+ } else if (isDistributedSupported ) {
592+ return ;
593+ }
594+ } else {
595+ // for table process, distributed function is not supported yet, need to refresh routing
596+ // for now only support to query single tablet
597+ if (partIdParamMap .size () > 1 ) {
598+ throw new ObTablePartitionConsistentException (
599+ "query and mutate must be a atomic operation" );
600+ } else if (partIdParamMap .isEmpty ()) {
601+ throw new ObTableException ("could not find part id of range" );
602+ }
603+ }
604+ Map .Entry <Long , ObTableParam > entry = partIdParamMap .entrySet ().iterator ().next ();
605+ TableEntry tableEntry = getTableEntry (tableName );
606+ long tabletId = entry .getValue ().getTabletId ();
607+ refreshPartitionLocation (tableName , tabletId , tableEntry );
608+ }
609+
583610 private Long [] getTabletsFromTableEntry (TableEntry tableEntry ) {
584611 Long [] tablets = null ;
585612 if (tableEntry .isPartitionTable ()) {
@@ -693,6 +720,44 @@ private ObPartitionLocationInfo getOrRefreshPartitionInfo(TableEntry tableEntry,
693720 return obPartitionLocationInfo ;
694721 }
695722
723+ public Map <Long , ObTableParam > getPartIdParamMapForQuery (String tableName , List <String > scanRangeColumns ,
724+ List <ObNewRange > keyRanges ) throws Exception {
725+ Map <Long , ObTableParam > parIdParamMapObTable = new HashMap <Long , ObTableParam >();
726+ for (ObNewRange keyRange : keyRanges ) {
727+ ObRowKey startKey = keyRange .getStartKey ();
728+ int startKeySize = startKey .getObjs ().size ();
729+ ObRowKey endKey = keyRange .getEndKey ();
730+ int endKeySize = endKey .getObjs ().size ();
731+ Object [] start = new Object [startKeySize ];
732+ Object [] end = new Object [endKeySize ];
733+ for (int i = 0 ; i < startKeySize ; i ++) {
734+ ObObj curStart = startKey .getObj (i );
735+ if (curStart .isMinObj ()) {
736+ start [i ] = curStart ;
737+ } else {
738+ start [i ] = curStart .getValue ();
739+ }
740+ }
741+
742+ for (int i = 0 ; i < endKeySize ; i ++) {
743+ ObObj curEnd = endKey .getObj (i );
744+ if (curEnd .isMaxObj ()) {
745+ end [i ] = curEnd ;
746+ } else {
747+ end [i ] = curEnd .getValue ();
748+ }
749+ }
750+ ObBorderFlag borderFlag = keyRange .getBorderFlag ();
751+ List <ObTableParam > paramList = getTablesInternal (tableName , scanRangeColumns , start ,
752+ borderFlag .isInclusiveStart (), end , borderFlag .isInclusiveEnd (),
753+ tableClient .getRoute (false ));
754+ for (ObTableParam param : paramList ) {
755+ parIdParamMapObTable .put (param .getPartId (), param );
756+ }
757+ }
758+ return parIdParamMapObTable ;
759+ }
760+
696761 /**
697762 * get addr by partId
698763 * @param tableName table want to get
@@ -869,50 +934,18 @@ public ObTableParam getTableParam(String tableName, List<String> scanRangeColumn
869934 */
870935 public ObTableParam getTableParam (String tableName , List <String > scanRangeColumns ,
871936 List <ObNewRange > keyRanges ) throws Exception {
872- Map <Long , ObTableParam > tabletIdIdMapObTable = new HashMap <Long , ObTableParam >();
873- for (ObNewRange keyRange : keyRanges ) {
874- ObRowKey startKey = keyRange .getStartKey ();
875- int startKeySize = startKey .getObjs ().size ();
876- ObRowKey endKey = keyRange .getEndKey ();
877- int endKeySize = endKey .getObjs ().size ();
878- Object [] start = new Object [startKeySize ];
879- Object [] end = new Object [endKeySize ];
880- for (int i = 0 ; i < startKeySize ; i ++) {
881- ObObj curStart = startKey .getObj (i );
882- if (curStart .isMinObj ()) {
883- start [i ] = curStart ;
884- } else {
885- start [i ] = curStart .getValue ();
886- }
887- }
888-
889- for (int i = 0 ; i < endKeySize ; i ++) {
890- ObObj curEnd = endKey .getObj (i );
891- if (curEnd .isMaxObj ()) {
892- end [i ] = curEnd ;
893- } else {
894- end [i ] = curEnd .getValue ();
895- }
896- }
897- ObBorderFlag borderFlag = keyRange .getBorderFlag ();
898- List <ObTableParam > paramList = getTablesInternal (tableName , scanRangeColumns , start ,
899- borderFlag .isInclusiveStart (), end , borderFlag .isInclusiveEnd (),
900- tableClient .getRoute (false ));
901- for (ObTableParam param : paramList ) {
902- tabletIdIdMapObTable .put (param .getTabletId (), param );
903- }
904- }
937+ Map <Long , ObTableParam > partIdIdMapObTable = getPartIdParamMapForQuery (
938+ tableName , scanRangeColumns , keyRanges );
905939 // for now only support to query single tablet
906- if (tabletIdIdMapObTable .size () > 1 ) {
940+ if (partIdIdMapObTable .size () > 1 ) {
907941 throw new ObTablePartitionConsistentException (
908942 "query and mutate must be a atomic operation" );
909- } else if (tabletIdIdMapObTable . size () < 1 ) {
943+ } else if (partIdIdMapObTable . isEmpty () ) {
910944 throw new ObTableException ("could not find part id of range" );
911945 }
912946 ObTableParam ans = null ;
913- for (Long tabletId : tabletIdIdMapObTable .keySet ()) {
914- ans = tabletIdIdMapObTable .get (tabletId );
915- }
947+ Map .Entry <Long , ObTableParam > entry = partIdIdMapObTable .entrySet ().iterator ().next ();
948+ ans = entry .getValue ();
916949 return ans ;
917950 }
918951
@@ -979,45 +1012,7 @@ private List<ObTableParam> getTablesInternal(String tableName, List<String> scan
9791012 List <ObTableParam > params = new ArrayList <>();
9801013 for (ObPair <Long , ReplicaLocation > partIdWithReplica : partIdWithReplicaList ) {
9811014 long partId = partIdWithReplica .getLeft ();
982- long tabletId = getTabletIdByPartId (tableEntry , partId );
983- ReplicaLocation replica = partIdWithReplica .getRight ();
984- ObServerAddr addr = replica .getAddr ();
985- ObTable obTable = tableRoster .getTable (addr );
986- int retryTimes = 0 ;
987- while (obTable == null && retryTimes < 2 ) {
988- ++retryTimes ;
989- // need to refresh table roster to ensure the current roster is the latest
990- tableClient .syncRefreshMetadata (true );
991- // the addr is wrong, need to refresh location
992- if (logger .isInfoEnabled ()) {
993- logger .info ("Cannot get ObTable by addr {}, refreshing metadata." , addr );
994- }
995- // refresh tablet location based on the latest roster, in case that some of the observers hase been killed
996- // and used the old location
997- tableEntry = refreshPartitionLocation (tableName , tabletId , tableEntry );
998- ObPartitionLocationInfo locationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
999- replica = getPartitionLocation (locationInfo , route );
1000-
1001- if (replica == null ) {
1002- RUNTIME .error ("Cannot get replica by tableName: {}, tabletId: {}" , tableName , tabletId );
1003- throw new ObTableGetException ("Cannot get replica by tableName: " + tableName + ", tabletId: " + tabletId );
1004- }
1005- addr = replica .getAddr ();
1006- obTable = tableRoster .getTable (addr );
1007- }
1008- if (obTable == null ) {
1009- RUNTIME .error ("cannot get table by addr: " + addr );
1010- throw new ObTableGetException ("obTable is null, addr is: " + addr .getIp () + ":" + addr .getSvrPort ());
1011- }
1012-
1013- ObTableParam param = new ObTableParam (obTable );
1014- param .setLsId (tableEntry .getPartitionEntry ().getPartitionInfo (tabletId ).getTabletLsId ());
1015- param .setTableId (tableEntry .getTableId ());
1016- param .setPartId (partId );
1017- // real tablet id
1018- param .setPartitionId (tabletId );
1019-
1020- addr .recordAccess ();
1015+ ObTableParam param = getTableInternal (tableName , tableEntry , partId , route );
10211016 params .add (param );
10221017 }
10231018 return params ;
0 commit comments