@Override
 public boolean isCurrent() {
   if (Hosts.isCoordinator()) {
     syncState.set(this);
   }
   return super.isCurrent();
 }
 @Override
 void isLocked() {
   File dbLockFile = this.getLockFile();
   if (dbLockFile.exists() && Hosts.isCoordinator()) {
     this.failStop();
   }
 }
    @Override
    public boolean load() throws Exception {
      Hosts.awaitDatabases();
      Locks.DISABLED.isLocked();
      Locks.PARTITIONED.isLocked();

      Groovyness.run("setup_dbpool.groovy");
      OrderedShutdown.registerShutdownHook(
          Empyrean.class,
          new Runnable() {

            @Override
            public void run() {
              try {
                for (String ctx : PersistenceContexts.list()) {
                  try {
                    DatabaseClusterMBean db = Databases.lookup(ctx, TimeUnit.SECONDS.toMillis(5));
                    for (String host : db.getinactiveDatabases()) {
                      Databases.disable(host);
                    }
                    for (String host : db.getactiveDatabases()) {
                      Databases.disable(host);
                    }
                  } catch (Exception ex) {
                    LOG.error(ex);
                  }
                }
              } catch (NoSuchElementException ex) {
                LOG.error(ex);
              }
            }
          });
      TimeUnit.SECONDS.sleep(INITIAL_DB_SYNC_RETRY_WAIT);
      if (!Hosts.isCoordinator() && Hosts.localHost().hasDatabase()) {
        while (!Databases.enable(Hosts.localHost())) {
          LOG.warn(
              LogUtil.subheader("Synchronization of the database failed: " + Hosts.localHost()));
          if (counter.decrementAndGet() == 0) {
            LOG.fatal("Restarting process to force re-synchronization.");
            System.exit(123);
          } else {
            LOG.warn(
                "Sleeping for " + INITIAL_DB_SYNC_RETRY_WAIT + " seconds before trying again.");
            TimeUnit.SECONDS.sleep(INITIAL_DB_SYNC_RETRY_WAIT);
          }
        }

        Locks.DISABLED.create();

        Hosts.UpdateEntry.INSTANCE.apply(Hosts.localHost());
        LOG.info(LogUtil.subheader("Database synchronization complete: " + Hosts.localHost()));
      }
      return true;
    }
 public static Boolean isVolatile() {
   if (!Bootstrap.isFinished() || BootstrapArgs.isInitializeSystem()) {
     return false;
   } else if (!Hosts.isCoordinator() && BootstrapArgs.isCloudController()) {
     return !isSynchronized() || !activeHosts.get().containsAll(hostDatabases.get());
   } else if (!activeHosts.get().equals(hostDatabases.get())) {
     return true;
   } else {
     return !Hosts.list(FILTER_SYNCING_DBS).isEmpty();
   }
 }
 private static void prepareConnections(final Host host, final String contextName)
     throws NoSuchElementException {
   final String dbUrl =
       "jdbc:" + ServiceUris.remote(Database.class, host.getBindAddress(), contextName);
   final String hostName = host.getDisplayName();
   final DriverDatabaseMBean database = Databases.lookupDatabase(contextName, hostName);
   database.setuser(getUserName());
   database.setpassword(getPassword());
   database.setweight(
       Hosts.isCoordinator(host) ? DATABASE_WEIGHT_PRIMARY : DATABASE_WEIGHT_SECONDARY);
   database.setlocal(host.isLocalHost());
   database.setlocation(dbUrl);
 }
 private static void resetDatabaseWeights(final String contextName) {
   for (final Host host : Hosts.listActiveDatabases())
     try {
       lookupDatabase(contextName, host.getDisplayName())
           .setweight(
               Hosts.isCoordinator(host) ? DATABASE_WEIGHT_PRIMARY : DATABASE_WEIGHT_SECONDARY);
     } catch (NoSuchElementException e) {
       // Weight will be set on activation
     } catch (Exception e) {
       LOG.error(
           "Error resetting weight for host " + host.getDisplayName() + " context " + contextName,
           e);
     }
 }