@@ -1330,64 +1330,53 @@ private TableEntry refreshTableEntry(TableEntry tableEntry, String tableName)
13301330
13311331 public TableEntry refreshTableLocationByTabletId (TableEntry tableEntry , String tableName , Long tabletId ) throws ObTableAuthException {
13321332 TableEntryKey tableEntryKey = new TableEntryKey (clusterName , tenantName , database , tableName );
1333- Lock lock = null ;
13341333 try {
1335- // if table entry is null, throw exception
13361334 if (tableEntry == null ) {
1337- throw new ObTableEntryRefreshException ("table entry is null, tableName=" + tableName );
1335+ throw new ObTableEntryRefreshException ("Table entry is null, tableName=" + tableName );
13381336 }
1339-
1340- long lastRefreshTime = tableEntry .getTabletLocationLastRefreshTimeMills ( tabletId );
1337+
1338+ long lastRefreshTime = tableEntry .getPartitionEntry (). getPartitionInfo ( tabletId ). getLastUpdateTime ( );
13411339 long currentTime = System .currentTimeMillis ();
1342- // 100ms
1343- if (currentTime - lastRefreshTime < 100 ) {
1340+ if (currentTime - lastRefreshTime < 200 ) {
13441341 return tableEntry ;
13451342 }
1346- Lock tempLock = new ReentrantLock ();
1347- lock = tableEntry .refreshLockMap .putIfAbsent (tabletId , tempLock );
1348- lock = (lock == null ) ? tempLock : lock ; // check the first lock
1349-
1350- boolean acquired = lock .tryLock (tableEntryRefreshLockTimeout , TimeUnit .MILLISECONDS );
1351- tableEntry .setTableLocationLastRefreshTimeMills (tabletId , currentTime );
13521343
1353- if (!acquired ) {
1354- String errMsg = "try to lock table-entry refreshing timeout " + "dataSource:"
1355- + dataSourceName + " ,tableName:" + tableName + ", refresh:" +
1356- " , timeout:" + tableEntryRefreshLockTimeout + "." ;
1344+ Lock lock = tableEntry .refreshLockMap .computeIfAbsent (tabletId , k -> new ReentrantLock ());
1345+
1346+ if (!lock .tryLock (tableEntryRefreshLockTimeout , TimeUnit .MILLISECONDS )) {
1347+ String errMsg = String .format ("Try to lock table-entry refreshing timeout. DataSource: %s, TableName: %s, Timeout: %d." ,
1348+ dataSourceName , tableName , tableEntryRefreshLockTimeout );
13571349 RUNTIME .error (errMsg );
13581350 throw new ObTableEntryRefreshException (errMsg );
13591351 }
1360- tableEntry = loadTableEntryLocationWithPriority (serverRoster , //
1361- tableEntryKey ,//
1362- tableEntry ,//
1363- tabletId ,
1364- tableEntryAcquireConnectTimeout ,//
1365- tableEntryAcquireSocketTimeout ,//
1366- serverAddressPriorityTimeout , //
1367- serverAddressCachingTimeout , sysUA );
1368- // prepare the table entry for weak read.
1369- tableEntry .prepareForWeakRead (serverRoster .getServerLdcLocation ());
1352+
1353+ try {
1354+ tableEntry = loadTableEntryLocationWithPriority (serverRoster , tableEntryKey , tableEntry , tabletId ,
1355+ tableEntryAcquireConnectTimeout , tableEntryAcquireSocketTimeout ,
1356+ serverAddressPriorityTimeout , serverAddressCachingTimeout , sysUA );
1357+
1358+ tableEntry .prepareForWeakRead (serverRoster .getServerLdcLocation ());
1359+ } finally {
1360+ lock .unlock ();
1361+ }
1362+
13701363 } catch (ObTableNotExistException | ObTableServerCacheExpiredException e ) {
1371- RUNTIME .error ("refreshTableEntry meet exception" , e );
1364+ RUNTIME .error ("RefreshTableEntry encountered an exception" , e );
13721365 throw e ;
13731366 } catch (Exception e ) {
1367+ String errorMsg = String .format ("Failed to get table entry. Key=%s, Original TableEntry=%s" , tableEntryKey , tableEntry );
13741368 RUNTIME .error (LCD .convert ("01-00020" ), tableEntryKey , tableEntry , e );
1375- throw new ObTableEntryRefreshException (String .format (
1376- "failed to get table entry key=%s original tableEntry=%s " , tableEntryKey ,
1377- tableEntry ), e );
1378- } finally {
1379- if (lock != null ) {
1380- lock .unlock ();
1381- }
1369+ throw new ObTableEntryRefreshException (errorMsg , e );
13821370 }
1383- tableLocations .put (tableName , tableEntry );
13841371
1372+ tableLocations .put (tableName , tableEntry );
13851373 tableEntryRefreshContinuousFailureCount .set (0 );
1374+
13861375 if (logger .isInfoEnabled ()) {
1387- logger .info (
1388- "refresh table entry, dataSource: {}, tableName: {}, refresh: {} key:{} entry:{} " ,
1389- dataSourceName , tableName , true , tableEntryKey , JSON .toJSON (tableEntry ));
1376+ logger .info ("Refreshed table entry. DataSource: {}, TableName: {}, Key: {}, Entry: {}" ,
1377+ dataSourceName , tableName , tableEntryKey , JSON .toJSON (tableEntry ));
13901378 }
1379+
13911380 return tableEntry ;
13921381 }
13931382
@@ -1606,13 +1595,6 @@ private ReplicaLocation getPartitionLocation(TableEntry tableEntry, long partId,
16061595
16071596 private ReplicaLocation getPartitionLocation (ObPartitionLocationInfo obPartitionLocationInfo ,
16081597 ObServerRoute route ) {
1609- while (obPartitionLocationInfo .getPartitionLocation () == null ) {
1610- try {
1611- Thread .sleep (10 );
1612- } catch (InterruptedException e ) {
1613- e .printStackTrace ();
1614- }
1615- }
16161598 return obPartitionLocationInfo .getPartitionLocation ().getReplica (route );
16171599 }
16181600
@@ -1844,48 +1826,61 @@ public ObPair<Long, ObTableParam> getTableInternal(String tableName, TableEntry
18441826 long partId , boolean waitForRefresh ,
18451827 ObServerRoute route ) throws Exception {
18461828 long tabletId = getTabletIdByPartId (tableEntry , partId );
1847- ObPartitionLocationInfo obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1848- if (!obPartitionLocationInfo .initialized .get ()) {
1849- tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1850- obPartitionLocationInfo .initialized .compareAndSet (false , true );
1851- }
1852-
1829+ ObPartitionLocationInfo obPartitionLocationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
1830+
18531831 ReplicaLocation replica = getPartitionLocation (obPartitionLocationInfo , route );
18541832 ObServerAddr addr = replica .getAddr ();
18551833 ObTable obTable = tableRoster .get (addr );
1856- boolean addrExpired = addr .isExpired (serverAddressCachingTimeout );
1857- if (obTable == null ) {
1858- logger .warn ("can not get ObTable by addr {}, refresh metadata." , addr );
1859- syncRefreshMetadata ();
1860- }
1861- if (addrExpired || obTable == null ) {
1862- if (logger .isInfoEnabled () && addrExpired ) {
1863- logger .info ("server addr {} is expired, refresh tableEntry." , addr );
1864- }
18651834
1866- tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1867- obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1835+ if (obTable == null || addr .isExpired (serverAddressCachingTimeout )) {
1836+ if (obTable == null ) {
1837+ logger .warn ("Cannot get ObTable by addr {}, refreshing metadata." , addr );
1838+ syncRefreshMetadata ();
1839+ }
1840+ if (addr .isExpired (serverAddressCachingTimeout )) {
1841+ logger .info ("Server addr {} is expired, refreshing tableEntry." , addr );
1842+ }
1843+
1844+ obPartitionLocationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
18681845 replica = getPartitionLocation (obPartitionLocationInfo , route );
18691846 addr = replica .getAddr ();
18701847 obTable = tableRoster .get (addr );
1848+
1849+ if (obTable == null ) {
1850+ RUNTIME .error ("Cannot get table by addr: " + addr );
1851+ throw new ObTableGetException ("Cannot get table by addr: " + addr );
1852+ }
18711853 }
18721854
1873- if (obTable == null ) {
1874- RUNTIME .error ("cannot get table by addr: " + addr );
1875- throw new ObTableGetException ("cannot get table by addr: " + addr );
1855+ ObTableParam param = createTableParam (obTable , tableEntry , obPartitionLocationInfo , partId , tabletId );
1856+ addr .recordAccess ();
1857+ return new ObPair <>(tabletId , param );
1858+ }
1859+
1860+ private ObPartitionLocationInfo getOrRefreshPartitionInfo (TableEntry tableEntry ,
1861+ String tableName , long tabletId )
1862+ throws Exception {
1863+ ObPartitionLocationInfo obPartitionLocationInfo = tableEntry .getPartitionEntry ()
1864+ .getPartitionInfo (tabletId );
1865+ if (!obPartitionLocationInfo .initialized .get ()) {
1866+ tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1867+ obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1868+ obPartitionLocationInfo .initializationLatch .await ();
18761869 }
1870+ return obPartitionLocationInfo ;
1871+ }
18771872
1873+ private ObTableParam createTableParam (ObTable obTable , TableEntry tableEntry ,
1874+ ObPartitionLocationInfo obPartitionLocationInfo ,
1875+ long partId , long tabletId ) {
18781876 ObTableParam param = new ObTableParam (obTable );
1879- param .setPartId (partId ); // used in getTable(), 4.x may change the origin partId
1877+ param .setPartId (partId );
18801878 if (ObGlobal .obVsnMajor () >= 4 && tableEntry != null ) {
18811879 param .setLsId (obPartitionLocationInfo .getTabletLsId ());
18821880 }
1883-
18841881 param .setTableId (tableEntry .getTableId ());
18851882 param .setPartitionId (tabletId );
1886-
1887- addr .recordAccess ();
1888- return new ObPair <>(tabletId , param );
1883+ return param ;
18891884 }
18901885
18911886 /**
@@ -1910,11 +1905,7 @@ private List<ObPair<Long, ReplicaLocation>> getPartitionReplica(TableEntry table
19101905
19111906 if (!tableEntry .isPartitionTable () || tableEntry .getPartitionInfo ().getLevel () == ObPartitionLevel .LEVEL_ZERO ) {
19121907 long tabletId = getTabletIdByPartId (tableEntry , 0L );
1913- ObPartitionLocationInfo locationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1914- if (!locationInfo .initialized .get ()) {
1915- refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1916- locationInfo .initialized .compareAndSet (false , true );
1917- }
1908+ ObPartitionLocationInfo locationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
19181909 replicas .add (new ObPair <>(tabletId , getPartitionLocation (locationInfo , route )));
19191910 return replicas ;
19201911 }
@@ -1924,11 +1915,7 @@ private List<ObPair<Long, ReplicaLocation>> getPartitionReplica(TableEntry table
19241915
19251916 for (Long partId : partIds ) {
19261917 long tabletId = getTabletIdByPartId (tableEntry , partId );
1927- ObPartitionLocationInfo locationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1928- if (!locationInfo .initialized .get ()) {
1929- refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1930- locationInfo .initialized .compareAndSet (false , true );
1931- }
1918+ ObPartitionLocationInfo locationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
19321919 replicas .add (new ObPair <>(tabletId , getPartitionLocation (locationInfo , route )));
19331920 }
19341921
0 commit comments