2020
2121import com .google .common .annotations .VisibleForTesting ;
2222import com .google .common .base .Preconditions ;
23+ import com .google .common .base .Predicate ;
2324import com .google .common .collect .Lists ;
2425import org .apache .commons .lang3 .tuple .Pair ;
2526import org .apache .hadoop .classification .InterfaceAudience ;
4445import org .apache .hadoop .hive .metastore .utils .FilterUtils ;
4546import org .apache .hadoop .hive .metastore .utils .JavaUtils ;
4647import org .apache .hadoop .hive .metastore .utils .MetaStoreUtils ;
48+ import org .apache .hadoop .hive .metastore .utils .RetryingExecutor ;
4749import org .apache .hadoop .hive .metastore .utils .SecurityUtils ;
4850import org .apache .hadoop .security .UserGroupInformation ;
4951import org .apache .hadoop .util .ReflectionUtils ;
9294import java .util .Random ;
9395import java .util .concurrent .TimeUnit ;
9496import java .util .concurrent .atomic .AtomicInteger ;
97+ import java .util .concurrent .atomic .AtomicReference ;
9598
9699import static org .apache .hadoop .hive .metastore .utils .MetaStoreUtils .createThriftPartitionsReq ;
97100import static org .apache .hadoop .hive .metastore .utils .MetaStoreUtils .getDefaultCatalog ;
@@ -676,8 +679,6 @@ private TTransport createBinaryClient(URI store, boolean useSSL) throws TTranspo
676679
677680 private void open () throws MetaException {
678681 isConnected = false ;
679- TTransportException tte = null ;
680- MetaException recentME = null ;
681682 boolean useSSL = MetastoreConf .getBoolVar (conf , MetastoreConf .ConfVars .USE_SSL );
682683 boolean useSasl = MetastoreConf .getBoolVar (conf , MetastoreConf .ConfVars .USE_THRIFT_SASL );
683684 String clientAuthMode = MetastoreConf .getVar (conf , MetastoreConf .ConfVars .METASTORE_CLIENT_AUTH_MODE );
@@ -689,91 +690,61 @@ private void open() throws MetaException {
689690 if (clientAuthMode != null ) {
690691 usePasswordAuth = "PLAIN" .equalsIgnoreCase (clientAuthMode );
691692 }
692-
693- for (int attempt = 0 ; !isConnected && attempt < retries ; ++attempt ) {
694- for (URI store : metastoreUris ) {
695- LOG .info ("Trying to connect to metastore with URI ({}) in {} transport mode" , store ,
696- transportMode );
697- try {
698- try {
699- if (isHttpTransportMode ) {
700- transport = createHttpClient (store , useSSL );
701- } else {
702- transport = createBinaryClient (store , useSSL );
703- }
704- } catch (TTransportException te ) {
705- tte = te ;
706- throw new MetaException (te .toString ());
707- }
708-
709- final TProtocol protocol ;
710- if (useCompactProtocol ) {
711- protocol = new TCompactProtocol (transport );
712- } else {
713- protocol = new TBinaryProtocol (transport );
714- }
715- client = new ThriftHiveMetastore .Client (protocol );
716- try {
717- if (!transport .isOpen ()) {
718- transport .open ();
719- final int newCount = connCount .incrementAndGet ();
720- if (useSSL ) {
721- LOG .info (
722- "Opened an SSL connection to metastore, current connections: {}" ,
723- newCount );
724- if (LOG .isTraceEnabled ()) {
725- LOG .trace ("METASTORE SSL CONNECTION TRACE - open [{}]" ,
726- System .identityHashCode (this ), new Exception ());
727- }
728- } else {
729- LOG .info ("Opened a connection to metastore, URI ({}) "
730- + "current connections: {}" , store , newCount );
731- if (LOG .isTraceEnabled ()) {
732- LOG .trace ("METASTORE CONNECTION TRACE - open [{}]" ,
733- System .identityHashCode (this ), new Exception ());
734- }
735- }
736- }
737- isConnected = true ;
738- } catch (TTransportException e ) {
739- tte = e ;
740- String errMsg = String .format ("Failed to connect to the MetaStore Server URI (%s) in %s "
741- + "transport mode" , store , transportMode );
742- LOG .warn (errMsg );
743- LOG .debug (errMsg , e );
693+ AtomicReference <Throwable > recentEx = new AtomicReference <>();
694+ Predicate <Throwable > retryPolicy = ex -> {
695+ recentEx .set (ex );
696+ return !isConnected && (ex instanceof MetaException || ex instanceof TTransportException );
697+ };
698+ AtomicInteger inx = new AtomicInteger (0 );
699+ isConnected = new RetryingExecutor <>(retries * metastoreUris .length , () -> {
700+ URI store = metastoreUris [inx .getAndIncrement () % metastoreUris .length ];
701+ LOG .info ("Trying to connect to metastore with URI ({}) in {} transport mode" , store , transportMode );
702+ if (isHttpTransportMode ) {
703+ transport = createHttpClient (store , useSSL );
704+ } else {
705+ transport = createBinaryClient (store , useSSL );
706+ }
707+ final TProtocol protocol ;
708+ if (useCompactProtocol ) {
709+ protocol = new TCompactProtocol (transport );
710+ } else {
711+ protocol = new TBinaryProtocol (transport );
712+ }
713+ client = new ThriftHiveMetastore .Client (protocol );
714+ if (!transport .isOpen ()) {
715+ transport .open ();
716+ final int newCount = connCount .incrementAndGet ();
717+ if (useSSL ) {
718+ LOG .info (
719+ "Opened an SSL connection to metastore, current connections: {}" ,
720+ newCount );
721+ if (LOG .isTraceEnabled ()) {
722+ LOG .trace ("METASTORE SSL CONNECTION TRACE - open [{}]" ,
723+ System .identityHashCode (this ), new Exception ());
744724 }
745-
746- if (isConnected && !useSasl && !usePasswordAuth && !isHttpTransportMode &&
747- MetastoreConf .getBoolVar (conf , MetastoreConf .ConfVars .EXECUTE_SET_UGI )) {
748- // Call set_ugi, only in unsecure mode.
749- try {
750- UserGroupInformation ugi = SecurityUtils .getUGI ();
751- client .set_ugi (ugi .getUserName (), Arrays .asList (ugi .getGroupNames ()));
752- } catch (IOException e ) {
753- LOG .warn ("Failed to find ugi of client set_ugi() is not successful, Continuing without it." , e );
754- } catch (TException e ) {
755- LOG .warn ("set_ugi() not successful, Likely cause: new client talking to old server. "
756- + "Continuing without it." , e );
757- }
725+ } else {
726+ LOG .info ("Opened a connection to metastore, URI ({}) "
727+ + "current connections: {}" , store , newCount );
728+ if (LOG .isTraceEnabled ()) {
729+ LOG .trace ("METASTORE CONNECTION TRACE - open [{}]" ,
730+ System .identityHashCode (this ), new Exception ());
758731 }
759- } catch (MetaException e ) {
760- recentME = e ;
761- String errMsg = "Failed to connect to metastore with URI (" + store
762- + ") transport mode:" + transportMode + " in attempt " + attempt ;
763- LOG .error (errMsg , e );
764- }
765- if (isConnected ) {
766- // Set the beeline session modified metaConfVars for new HMS connection
767- overlaySessionModifiedMetaConf ();
768- break ;
769732 }
770733 }
771- // Wait before launching the next round of connection retries.
772- if (!isConnected && retryDelaySeconds > 0 ) {
773- try {
774- LOG .info ("Waiting " + retryDelaySeconds + " seconds before next connection attempt." );
775- Thread .sleep (retryDelaySeconds * 1000 );
776- } catch (InterruptedException ignore ) {}
734+ return true ;
735+ }).sleepInterval (retryDelaySeconds * 1000 ).onRetry (retryPolicy ).run ();
736+
737+ if (!useSasl && !usePasswordAuth && !isHttpTransportMode &&
738+ MetastoreConf .getBoolVar (conf , MetastoreConf .ConfVars .EXECUTE_SET_UGI )) {
739+ // Call set_ugi, only in unsecure mode.
740+ try {
741+ UserGroupInformation ugi = SecurityUtils .getUGI ();
742+ client .set_ugi (ugi .getUserName (), Arrays .asList (ugi .getGroupNames ()));
743+ } catch (IOException e ) {
744+ LOG .warn ("Failed to find ugi of client set_ugi() is not successful, Continuing without it." , e );
745+ } catch (TException e ) {
746+ LOG .warn ("set_ugi() not successful, Likely cause: new client talking to old server. "
747+ + "Continuing without it." , e );
777748 }
778749 }
779750
@@ -782,15 +753,14 @@ private void open() throws MetaException {
782753 // be null. When MetaException wraps TTransportException, tte will be set so stringify that
783754 // directly.
784755 String exceptionString = "Unknown exception" ;
785- if (tte != null ) {
786- exceptionString = StringUtils .stringifyException (tte );
787- } else if (recentME != null ) {
788- exceptionString = StringUtils .stringifyException (recentME );
756+ if (recentEx .get () != null ) {
757+ exceptionString = StringUtils .stringifyException (recentEx .get ());
789758 }
790759 throw new MetaException ("Could not connect to meta store using any of the URIs provided." +
791760 " Most recent failure: " + exceptionString );
792761 }
793-
762+ // Set the beeline session modified metaConfVars for new HMS connection
763+ overlaySessionModifiedMetaConf ();
794764 snapshotActiveConf ();
795765 }
796766
0 commit comments