@@ -158,41 +158,45 @@ public boolean isInitialized() {
158158 return initialized ;
159159 }
160160
161- public final synchronized void makeSureInitialized () {
162- if (isInitializing ) {
163- return ;
164- }
165- isInitializing = true ;
166- try {
167- extCatalog .makeSureInitialized ();
168- if (!initialized ) {
169- if (extCatalog .getUseMetaCache ().get ()) {
170- buildMetaCache ();
171- setLastUpdateTime (System .currentTimeMillis ());
172- } else {
173- if (!Env .getCurrentEnv ().isMaster ()) {
174- // Forward to master and wait the journal to replay.
175- int waitTimeOut = ConnectContext .get () == null ? 300 : ConnectContext .get ().getExecTimeout ();
176- MasterCatalogExecutor remoteExecutor = new MasterCatalogExecutor (waitTimeOut * 1000 );
177- try {
178- remoteExecutor .forward (extCatalog .getId (), id );
179- } catch (Exception e ) {
180- Util .logAndThrowRuntimeException (LOG ,
181- String .format ("failed to forward init external db %s operation to master" , name ),
182- e );
161+ public final void makeSureInitialized () {
162+ // Must call this method before any operation on the database to avoid deadlock of synchronized block
163+ extCatalog .makeSureInitialized ();
164+ synchronized (this ) {
165+ if (isInitializing ) {
166+ return ;
167+ }
168+ isInitializing = true ;
169+ try {
170+ if (!initialized ) {
171+ if (extCatalog .getUseMetaCache ().get ()) {
172+ buildMetaCache ();
173+ setLastUpdateTime (System .currentTimeMillis ());
174+ } else {
175+ if (!Env .getCurrentEnv ().isMaster ()) {
176+ // Forward to master and wait the journal to replay.
177+ int waitTimeOut = ConnectContext .get () == null ? 300
178+ : ConnectContext .get ().getExecTimeout ();
179+ MasterCatalogExecutor remoteExecutor = new MasterCatalogExecutor (waitTimeOut * 1000 );
180+ try {
181+ remoteExecutor .forward (extCatalog .getId (), id );
182+ } catch (Exception e ) {
183+ Util .logAndThrowRuntimeException (LOG ,
184+ String .format ("failed to forward init external db %s operation to master" ,
185+ name ), e );
186+ }
187+ return ;
183188 }
184- return ;
189+ init () ;
185190 }
186- init () ;
191+ initialized = true ;
187192 }
188- initialized = true ;
193+ } catch (Exception e ) {
194+ LOG .warn ("failed to init db {}, id {}, isInitializing: {}, initialized: {}" ,
195+ this .name , this .id , isInitializing , initialized , e );
196+ initialized = false ;
197+ } finally {
198+ isInitializing = false ;
189199 }
190- } catch (Exception e ) {
191- LOG .warn ("failed to init db {}, id {}, isInitializing: {}, initialized: {}" ,
192- this .name , this .id , isInitializing , initialized , e );
193- initialized = false ;
194- } finally {
195- isInitializing = false ;
196200 }
197201 }
198202
0 commit comments