2525import com .orientechnologies .orient .core .db .OrientDBEmbedded ;
2626import com .orientechnologies .orient .core .db .document .ODatabaseDocumentEmbedded ;
2727import com .orientechnologies .orient .core .exception .ODatabaseException ;
28+ import com .orientechnologies .orient .core .metadata .security .auth .OAuthenticationInfo ;
2829import com .orientechnologies .orient .core .storage .OStorage ;
2930import com .orientechnologies .orient .core .storage .disk .OLocalPaginatedStorage ;
3031import com .orientechnologies .orient .core .storage .impl .local .OAbstractPaginatedStorage ;
@@ -267,6 +268,7 @@ public OStorage fullSync(String dbName, InputStream backupStream, OrientDBConfig
267268 @ Override
268269 public ODatabaseDocumentInternal poolOpen (
269270 String name , String user , String password , ODatabasePoolInternal pool ) {
271+ checkDbAvailableOpen (name );
270272 ODatabaseDocumentInternal session = super .poolOpen (name , user , password , pool );
271273 return session ;
272274 }
@@ -346,47 +348,34 @@ public void drop(String name, String user, String password) {
346348 }
347349
348350 private boolean checkDbAvailable (String name ) {
349- if (getPlugin () == null || ! getPlugin (). isEnabled ( )) {
351+ if (! isDistributedPluginEnabled () || OSystemDatabase . SYSTEM_DB_NAME . equals ( name )) {
350352 return true ;
351353 }
352- if ( OSystemDatabase . SYSTEM_DB_NAME . equals ( name )) return true ;
354+ waitForPluginStartup () ;
353355 DB_STATUS dbStatus = plugin .getDatabaseStatus (plugin .getLocalNodeName (), name );
354356 return dbStatus == DB_STATUS .ONLINE || dbStatus == DB_STATUS .BACKUP ;
355357 }
356358
357- private boolean checkDbAvailableOpen (String name ) {
358- if (!checkDbAvailable (name )) {
359- long waitTime =
360- getConfigurations ()
361- .getConfigurations ()
362- .getValueAsLong (OGlobalConfiguration .DISTRIBUTED_DATABASE_ONLINE_GRACE_PERIOD );
363- if (waitTime != 0 ) {
364- long retry = waitTime / 500 ;
365- // TODO: when there will be proper node online event this should attach to that with a
366- // notification instead of sleep
367- for (long i = 0 ; i < retry ; i ++) {
368- try {
369- Thread .sleep (500 );
370- } catch (InterruptedException e ) {
371- e .printStackTrace ();
372- }
373- if (checkDbAvailable (name )) {
374- return true ;
375- }
376- }
377- }
378- }
379- return false ;
380- }
359+ private void checkDbAvailableOpen (String name ) {
360+ if (checkDbAvailable (name )) return ;
381361
382- @ Override
383- public ODatabaseDocumentInternal open (String name , String user , String password ) {
384- if (checkDbAvailableOpen (name )) {
385- return super .open (name , user , password );
386- } else {
387- if (exists (name , user , password )) {
388- return super .open (name , user , password );
362+ long waitTime =
363+ getConfigurations ()
364+ .getConfigurations ()
365+ .getValueAsLong (OGlobalConfiguration .DISTRIBUTED_DATABASE_ONLINE_GRACE_PERIOD );
366+ long endTime = System .currentTimeMillis () + waitTime ;
367+ boolean dbAvailable = false ;
368+ do {
369+ // TODO: when there will be proper node online event this should attach to that with a
370+ // notification instead of sleep
371+ try {
372+ Thread .sleep (100 );
373+ } catch (InterruptedException ignored ) {
389374 }
375+ dbAvailable = checkDbAvailable (name );
376+ } while (!dbAvailable && (System .currentTimeMillis () < endTime ));
377+
378+ if (!dbAvailable ) {
390379 throw new OOfflineNodeException (
391380 "database " + name + " not online on " + plugin .getLocalNodeName ());
392381 }
@@ -395,16 +384,45 @@ public ODatabaseDocumentInternal open(String name, String user, String password)
395384 @ Override
396385 public ODatabaseDocumentInternal open (
397386 String name , String user , String password , OrientDBConfig config ) {
387+ checkDbAvailableOpen (name );
388+ return super .open (name , user , password , config );
389+ }
398390
399- if (checkDbAvailableOpen (name )) {
400- return super .open (name , user , password , config );
391+ @ Override
392+ public ODatabaseDocumentInternal open (
393+ OAuthenticationInfo authenticationInfo , OrientDBConfig config ) {
394+ if (authenticationInfo .getDatabase ().isPresent ()) {
395+ checkDbAvailableOpen (authenticationInfo .getDatabase ().get ());
401396 } else {
402- if (exists (name , user , password )) {
403- return super .open (name , user , password , config );
404- }
405- throw new OOfflineNodeException (
406- "database " + name + " not online on " + plugin .getLocalNodeName ());
397+ waitForPluginStartup ();
407398 }
399+ return super .open (authenticationInfo , config );
400+ }
401+
402+ @ Override
403+ public ODatabaseDocumentEmbedded openNoAuthenticate (String name , String user ) {
404+ checkDbAvailableOpen (name );
405+ return super .openNoAuthenticate (name , user );
406+ }
407+
408+ @ Override
409+ public ODatabaseDocumentEmbedded openNoAuthorization (String name ) {
410+ // TODO: Some paths might need protection
411+ return super .openNoAuthorization (name );
412+ }
413+
414+ @ Override
415+ public ODatabasePoolInternal openPool (
416+ String name , String user , String password , OrientDBConfig config ) {
417+ checkDbAvailableOpen (name );
418+ return super .openPool (name , user , password , config );
419+ }
420+
421+ @ Override
422+ public ODatabasePoolInternal cachedPool (
423+ String database , String user , String password , OrientDBConfig config ) {
424+ checkDbAvailableOpen (database );
425+ return super .cachedPool (database , user , password , config );
408426 }
409427
410428 @ Override
@@ -506,7 +524,7 @@ public ODistributedDatabaseImpl unregisterDatabase(final String iDatabaseName) {
506524 return db ;
507525 }
508526
509- public void registerNewDatabaseIfNeeded (
527+ private void registerNewDatabaseIfNeeded (
510528 ODatabaseDocumentInternal session , OSharedContext context ) {
511529 databases .computeIfAbsent (session .getName (), (n ) -> this .newDistributedDatabase (n , session ));
512530 }
0 commit comments