@@ -1331,64 +1331,53 @@ private TableEntry refreshTableEntry(TableEntry tableEntry, String tableName)
13311331
13321332 public TableEntry refreshTableLocationByTabletId (TableEntry tableEntry , String tableName , Long tabletId ) throws ObTableAuthException {
13331333 TableEntryKey tableEntryKey = new TableEntryKey (clusterName , tenantName , database , tableName );
1334- Lock lock = null ;
13351334 try {
1336- // if table entry is null, throw exception
13371335 if (tableEntry == null ) {
1338- throw new ObTableEntryRefreshException ("table entry is null, tableName=" + tableName );
1336+ throw new ObTableEntryRefreshException ("Table entry is null, tableName=" + tableName );
13391337 }
1340-
1341- long lastRefreshTime = tableEntry .getTabletLocationLastRefreshTimeMills ( tabletId );
1338+
1339+ long lastRefreshTime = tableEntry .getPartitionEntry (). getPartitionInfo ( tabletId ). getLastUpdateTime ( );
13421340 long currentTime = System .currentTimeMillis ();
1343- // 100ms
1344- if (currentTime - lastRefreshTime < 100 ) {
1341+ if (currentTime - lastRefreshTime < 200 ) {
13451342 return tableEntry ;
13461343 }
1347- Lock tempLock = new ReentrantLock ();
1348- lock = tableEntry .refreshLockMap .putIfAbsent (tabletId , tempLock );
1349- lock = (lock == null ) ? tempLock : lock ; // check the first lock
1350-
1351- boolean acquired = lock .tryLock (tableEntryRefreshLockTimeout , TimeUnit .MILLISECONDS );
1352- tableEntry .setTableLocationLastRefreshTimeMills (tabletId , currentTime );
13531344
1354- if (!acquired ) {
1355- String errMsg = "try to lock table-entry refreshing timeout " + "dataSource:"
1356- + dataSourceName + " ,tableName:" + tableName + ", refresh:" +
1357- " , timeout:" + tableEntryRefreshLockTimeout + "." ;
1345+ Lock lock = tableEntry .refreshLockMap .computeIfAbsent (tabletId , k -> new ReentrantLock ());
1346+
1347+ if (!lock .tryLock (tableEntryRefreshLockTimeout , TimeUnit .MILLISECONDS )) {
1348+ String errMsg = String .format ("Try to lock table-entry refreshing timeout. DataSource: %s, TableName: %s, Timeout: %d." ,
1349+ dataSourceName , tableName , tableEntryRefreshLockTimeout );
13581350 RUNTIME .error (errMsg );
13591351 throw new ObTableEntryRefreshException (errMsg );
13601352 }
1361- tableEntry = loadTableEntryLocationWithPriority (serverRoster , //
1362- tableEntryKey ,//
1363- tableEntry ,//
1364- tabletId ,
1365- tableEntryAcquireConnectTimeout ,//
1366- tableEntryAcquireSocketTimeout ,//
1367- serverAddressPriorityTimeout , //
1368- serverAddressCachingTimeout , sysUA );
1369- // prepare the table entry for weak read.
1370- tableEntry .prepareForWeakRead (serverRoster .getServerLdcLocation ());
1353+
1354+ try {
1355+ tableEntry = loadTableEntryLocationWithPriority (serverRoster , tableEntryKey , tableEntry , tabletId ,
1356+ tableEntryAcquireConnectTimeout , tableEntryAcquireSocketTimeout ,
1357+ serverAddressPriorityTimeout , serverAddressCachingTimeout , sysUA );
1358+
1359+ tableEntry .prepareForWeakRead (serverRoster .getServerLdcLocation ());
1360+ } finally {
1361+ lock .unlock ();
1362+ }
1363+
13711364 } catch (ObTableNotExistException | ObTableServerCacheExpiredException e ) {
1372- RUNTIME .error ("refreshTableEntry meet exception" , e );
1365+ RUNTIME .error ("RefreshTableEntry encountered an exception" , e );
13731366 throw e ;
13741367 } catch (Exception e ) {
1368+ String errorMsg = String .format ("Failed to get table entry. Key=%s, Original TableEntry=%s" , tableEntryKey , tableEntry );
13751369 RUNTIME .error (LCD .convert ("01-00020" ), tableEntryKey , tableEntry , e );
1376- throw new ObTableEntryRefreshException (String .format (
1377- "failed to get table entry key=%s original tableEntry=%s " , tableEntryKey ,
1378- tableEntry ), e );
1379- } finally {
1380- if (lock != null ) {
1381- lock .unlock ();
1382- }
1370+ throw new ObTableEntryRefreshException (errorMsg , e );
13831371 }
1384- tableLocations .put (tableName , tableEntry );
13851372
1373+ tableLocations .put (tableName , tableEntry );
13861374 tableEntryRefreshContinuousFailureCount .set (0 );
1375+
13871376 if (logger .isInfoEnabled ()) {
1388- logger .info (
1389- "refresh table entry, dataSource: {}, tableName: {}, refresh: {} key:{} entry:{} " ,
1390- dataSourceName , tableName , true , tableEntryKey , JSON .toJSON (tableEntry ));
1377+ logger .info ("Refreshed table entry. DataSource: {}, TableName: {}, Key: {}, Entry: {}" ,
1378+ dataSourceName , tableName , tableEntryKey , JSON .toJSON (tableEntry ));
13911379 }
1380+
13921381 return tableEntry ;
13931382 }
13941383
@@ -1607,13 +1596,6 @@ private ReplicaLocation getPartitionLocation(TableEntry tableEntry, long partId,
16071596
16081597 private ReplicaLocation getPartitionLocation (ObPartitionLocationInfo obPartitionLocationInfo ,
16091598 ObServerRoute route ) {
1610- while (obPartitionLocationInfo .getPartitionLocation () == null ) {
1611- try {
1612- Thread .sleep (10 );
1613- } catch (InterruptedException e ) {
1614- e .printStackTrace ();
1615- }
1616- }
16171599 return obPartitionLocationInfo .getPartitionLocation ().getReplica (route );
16181600 }
16191601
@@ -1854,48 +1836,61 @@ public ObPair<Long, ObTableParam> getTableInternal(String tableName, TableEntry
18541836 long partId , boolean waitForRefresh ,
18551837 ObServerRoute route ) throws Exception {
18561838 long tabletId = getTabletIdByPartId (tableEntry , partId );
1857- ObPartitionLocationInfo obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1858- if (!obPartitionLocationInfo .initialized .get ()) {
1859- tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1860- obPartitionLocationInfo .initialized .compareAndSet (false , true );
1861- }
1862-
1839+ ObPartitionLocationInfo obPartitionLocationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
1840+
18631841 ReplicaLocation replica = getPartitionLocation (obPartitionLocationInfo , route );
18641842 ObServerAddr addr = replica .getAddr ();
18651843 ObTable obTable = tableRoster .get (addr );
1866- boolean addrExpired = addr .isExpired (serverAddressCachingTimeout );
1867- if (obTable == null ) {
1868- logger .warn ("can not get ObTable by addr {}, refresh metadata." , addr );
1869- syncRefreshMetadata ();
1870- }
1871- if (addrExpired || obTable == null ) {
1872- if (logger .isInfoEnabled () && addrExpired ) {
1873- logger .info ("server addr {} is expired, refresh tableEntry." , addr );
1874- }
18751844
1876- tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1877- obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1845+ if (obTable == null || addr .isExpired (serverAddressCachingTimeout )) {
1846+ if (obTable == null ) {
1847+ logger .warn ("Cannot get ObTable by addr {}, refreshing metadata." , addr );
1848+ syncRefreshMetadata ();
1849+ }
1850+ if (addr .isExpired (serverAddressCachingTimeout )) {
1851+ logger .info ("Server addr {} is expired, refreshing tableEntry." , addr );
1852+ }
1853+
1854+ obPartitionLocationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
18781855 replica = getPartitionLocation (obPartitionLocationInfo , route );
18791856 addr = replica .getAddr ();
18801857 obTable = tableRoster .get (addr );
1858+
1859+ if (obTable == null ) {
1860+ RUNTIME .error ("Cannot get table by addr: " + addr );
1861+ throw new ObTableGetException ("Cannot get table by addr: " + addr );
1862+ }
18811863 }
18821864
1883- if (obTable == null ) {
1884- RUNTIME .error ("cannot get table by addr: " + addr );
1885- throw new ObTableGetException ("cannot get table by addr: " + addr );
1865+ ObTableParam param = createTableParam (obTable , tableEntry , obPartitionLocationInfo , partId , tabletId );
1866+ addr .recordAccess ();
1867+ return new ObPair <>(tabletId , param );
1868+ }
1869+
1870+ private ObPartitionLocationInfo getOrRefreshPartitionInfo (TableEntry tableEntry ,
1871+ String tableName , long tabletId )
1872+ throws Exception {
1873+ ObPartitionLocationInfo obPartitionLocationInfo = tableEntry .getPartitionEntry ()
1874+ .getPartitionInfo (tabletId );
1875+ if (!obPartitionLocationInfo .initialized .get ()) {
1876+ tableEntry = refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1877+ obPartitionLocationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1878+ obPartitionLocationInfo .initializationLatch .await ();
18861879 }
1880+ return obPartitionLocationInfo ;
1881+ }
18871882
1883+ private ObTableParam createTableParam (ObTable obTable , TableEntry tableEntry ,
1884+ ObPartitionLocationInfo obPartitionLocationInfo ,
1885+ long partId , long tabletId ) {
18881886 ObTableParam param = new ObTableParam (obTable );
1889- param .setPartId (partId ); // used in getTable(), 4.x may change the origin partId
1887+ param .setPartId (partId );
18901888 if (ObGlobal .obVsnMajor () >= 4 && tableEntry != null ) {
18911889 param .setLsId (obPartitionLocationInfo .getTabletLsId ());
18921890 }
1893-
18941891 param .setTableId (tableEntry .getTableId ());
18951892 param .setPartitionId (tabletId );
1896-
1897- addr .recordAccess ();
1898- return new ObPair <>(tabletId , param );
1893+ return param ;
18991894 }
19001895
19011896 /**
@@ -1920,11 +1915,7 @@ private List<ObPair<Long, ReplicaLocation>> getPartitionReplica(TableEntry table
19201915
19211916 if (!tableEntry .isPartitionTable () || tableEntry .getPartitionInfo ().getLevel () == ObPartitionLevel .LEVEL_ZERO ) {
19221917 long tabletId = getTabletIdByPartId (tableEntry , 0L );
1923- ObPartitionLocationInfo locationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1924- if (!locationInfo .initialized .get ()) {
1925- refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1926- locationInfo .initialized .compareAndSet (false , true );
1927- }
1918+ ObPartitionLocationInfo locationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
19281919 replicas .add (new ObPair <>(tabletId , getPartitionLocation (locationInfo , route )));
19291920 return replicas ;
19301921 }
@@ -1934,11 +1925,7 @@ private List<ObPair<Long, ReplicaLocation>> getPartitionReplica(TableEntry table
19341925
19351926 for (Long partId : partIds ) {
19361927 long tabletId = getTabletIdByPartId (tableEntry , partId );
1937- ObPartitionLocationInfo locationInfo = tableEntry .getPartitionEntry ().getPartitionInfo (tabletId );
1938- if (!locationInfo .initialized .get ()) {
1939- refreshTableLocationByTabletId (tableEntry , tableName , tabletId );
1940- locationInfo .initialized .compareAndSet (false , true );
1941- }
1928+ ObPartitionLocationInfo locationInfo = getOrRefreshPartitionInfo (tableEntry , tableName , tabletId );
19421929 replicas .add (new ObPair <>(tabletId , getPartitionLocation (locationInfo , route )));
19431930 }
19441931
0 commit comments