Exemplo n.º 1
0
 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();
   }
 }
Exemplo n.º 2
0
 public static Hosts rndHosts(int max) {
   int nhosts = rndInt(0, max);
   Hosts hosts = new Hosts();
   hosts.setSticky((Boolean) StubFactory.rndChoice((new Boolean[] {true, false})));
   for (int i = nhosts; i <= StubFactory.rndInt(1, 5); i++) {
     Host h = StubFactory.rndHostPost();
     h.setId(rndInt(0, 10000));
     hosts.getHosts().add(h);
   }
   return hosts;
 }
Exemplo n.º 3
0
 @Override
 public boolean check() throws Exception {
   if (Bootstrap.isOperational()
       && !Hosts.localHost().hasDatabase()) { // This check is redundant on hosts with DBs
     final List<Host.DBStatus> statusList = Hosts.localHost().getDatabaseStatus();
     for (final Host.DBStatus status : statusList) {
       for (String error : status.getError().asSet()) {
         LOG.error(error);
       }
     }
   }
   return super.check();
 }
Exemplo n.º 4
0
 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);
     }
 }
Exemplo n.º 5
0
 static boolean shouldInitialize() { // GRZE:WARNING:HACKHACKHACK do not duplicate pls thanks.
   for (final Host h : Hosts.listActiveDatabases()) {
     final String url =
         String.format(
             "jdbc:%s",
             ServiceUris.remote(Database.class, h.getBindAddress(), "eucalyptus_config"));
     try {
       final Connection conn =
           DriverManager.getConnection(url, Databases.getUserName(), Databases.getPassword());
       try {
         final PreparedStatement statement =
             conn.prepareStatement(
                 "select config_component_hostname from config_component_base where config_component_partition='eucalyptus';");
         final ResultSet result = statement.executeQuery();
         while (result.next()) {
           final Object columnValue = result.getObject(1);
           if (Internets.testLocal(columnValue.toString())) {
             return true;
           }
         }
       } finally {
         conn.close();
       }
     } catch (final Exception ex) {
       LOG.error(ex, ex);
     }
   }
   return false;
 }
Exemplo n.º 6
0
 @Override
 public boolean isCurrent() {
   if (Hosts.isCoordinator()) {
     syncState.set(this);
   }
   return super.isCurrent();
 }
Exemplo n.º 7
0
 @Override
 void isLocked() {
   File dbLockFile = this.getLockFile();
   if (dbLockFile.exists() && Hosts.isCoordinator()) {
     this.failStop();
   }
 }
Exemplo n.º 8
0
 public static void check() {
   for (String ctx : PersistenceContexts.list()) {
     try {
       DatabaseClusterMBean db = lookup(ctx, TimeUnit.SECONDS.toMillis(5));
       for (String host : db.getactiveDatabases()) {
         Host hostEntry = Hosts.lookup(host);
         if (hostEntry == null) {
           disable(host);
         } else if (!Hosts.contains(hostEntry.getGroupsId())) {
           Hosts.remove(host); // GRZE: this will clean up group state and de-activate db.
         }
       }
     } catch (NoSuchElementException ex) {
       LOG.error(ex, ex);
     }
     return;
   }
 }
Exemplo n.º 9
0
 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);
 }
Exemplo n.º 10
0
    @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;
    }
Exemplo n.º 11
0
  /** List all known databases. */
  static Set<String> listDatabases() {
    final Set<String> dbNames = Sets.newHashSet();
    final Predicate<String> dbNamePredicate =
        Predicates.or(Strings.startsWith("eucalyptus_"), Predicates.equalTo("database_events"));

    for (final Host h : Hosts.listActiveDatabases()) {
      Iterables.addAll(
          dbNames,
          Iterables.filter(
              Databases.getBootstrapper().listDatabases(h.getBindAddress()), dbNamePredicate));
    }

    return dbNames;
  }
Exemplo n.º 12
0
 public static void awaitSynchronized() {
   if (!isVolatile()) {
     return;
   } else {
     Collection<StackTraceElement> stack = Threads.filteredStack(stackFilter);
     String caller = (stack.isEmpty() ? "" : stack.iterator().next().toString());
     for (int i = 0; i < MAX_TX_START_SYNC_RETRIES && isVolatile(); i++) {
       try {
         TimeUnit.MILLISECONDS.sleep(1000);
         LOG.debug("Transaction blocked on sync: " + caller);
       } catch (InterruptedException ex) {
         Exceptions.maybeInterrupted(ex);
         return;
       }
     }
     if (isVolatile()) {
       throw new DatabaseStateException(
           "Transaction begin failed due to concurrent database synchronization: "
               + Hosts.listDatabases()
               + " for caller:\n"
               + Joiner.on("\n\tat ").join(stack));
     }
   }
 }
Exemplo n.º 13
0
 static boolean enable(final Host host) {
   if (!host.hasDatabase() || Bootstrap.isShuttingDown()) {
     return false;
   } else if (!Hosts.contains(host.getGroupsId())) {
     Hosts.remove(host.getGroupsId());
     return false;
   } else {
     if (host.isLocalHost()) {
       if (SyncState.SYNCING.set()) {
         try {
           runDbStateChange(ActivateHostFunction.INSTANCE.apply(host));
           SyncState.SYNCED.set();
           return true;
         } catch (LockTimeoutException ex) {
           SyncState.NOTSYNCED.set();
           return false;
         } catch (Exception ex) {
           SyncState.NOTSYNCED.set();
           LOG.error(ex);
           Logs.extreme().error(ex, ex);
           return false;
         }
       } else if (!SyncState.SYNCING.isCurrent()) {
         try {
           runDbStateChange(ActivateHostFunction.INSTANCE.apply(host));
           return true;
         } catch (LockTimeoutException ex) {
           return false;
         } catch (Exception ex) {
           LOG.error(ex);
           Logs.extreme().error(ex, ex);
           return false;
         }
       } else {
         try {
           runDbStateChange(ActivateHostFunction.INSTANCE.apply(host));
           SyncState.SYNCED.set();
           return true;
         } catch (LockTimeoutException ex) {
           SyncState.NOTSYNCED.set();
           return false;
         } catch (Exception ex) {
           SyncState.NOTSYNCED.set();
           LOG.error(ex, ex);
           return false;
         }
       }
     } else if (!ActiveHostSet.ACTIVATED.get().contains(host.getDisplayName())) {
       try {
         runDbStateChange(ActivateHostFunction.INSTANCE.apply(host));
         return true;
       } catch (LockTimeoutException ex) {
         return false;
       } catch (Exception ex) {
         Logs.extreme().debug(ex);
         ActivateHostFunction.rollback(host, ex);
         return false;
       }
     } else {
       return ActiveHostSet.ACTIVATED.get().contains(host.getDisplayName());
     }
   }
 }
Exemplo n.º 14
0
 @Override
 public Set<String> get() {
   return Sets.newHashSet(
       Collections2.transform(Hosts.listDatabases(), Hosts.NameTransform.INSTANCE));
 }
Exemplo n.º 15
0
public class ProxySelectorRegex extends ProxySelector {
  public static String[] DEFAULT_EXCLUDED_HOSTS = {
    "localhost",
    "127\\.0.*",
    "192\\.168\\.1\\..*",
    ".*\\.dropbox\\.com",
    "s3\\.amazonaws\\.com",
    "s3\\.amazonaws\\.com\\.",
    ".*\\.s3\\.amazonaws\\.com",
    ".*\\.s3\\.amazonaws\\.com\\.",
    ".*\\.push\\.apple\\.com",
    ".*" + (Hosts.getHostFor("primary_prod").replace(".", "\\."))
  };

  List<Proxy> NoProxy = Arrays.asList(new Proxy[] {Proxy.NO_PROXY});
  Set<Pattern> excludedHosts = new HashSet<Pattern>();
  Map<String, List<Proxy>> schemeToProxyList = new HashMap<String, List<Proxy>>();

  @Override
  public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}

  public void excludeHost(String host) {
    excludedHosts.add(Pattern.compile(host.toLowerCase()));
  }

  public void addProxy(String scheme, Proxy proxy) {
    if (!schemeToProxyList.containsKey(scheme))
      schemeToProxyList.put(scheme, new ArrayList<Proxy>(1));

    schemeToProxyList.get(scheme).add(proxy);
  }

  @Override
  public List<Proxy> select(URI uri) {
    String host = uri.getHost().toLowerCase();
    //		System.err.println("ProxySelectorRegex.select host " + host);
    for (Pattern excluded : excludedHosts) {
      if (excluded.matcher(host).matches()) {
        //				System.err.println("ProxySelectorRegex.select host matched " + excluded.toString());
        return NoProxy;
      }
    }

    String scheme = uri.getScheme();
    //		System.err.println("ProxySelectorRegex.select scheme " + scheme);

    List<Proxy> proxyList = schemeToProxyList.get(scheme);
    if (proxyList == null) {
      //			System.err.println("ProxySelectorRegex.select did not find a proxy for " + scheme);
      return NoProxy;
    }

    System.err.println("ProxySelectorRegex.select found a proxy for " + scheme + " " + host);
    return proxyList;
  }

  public void install() {
    setDefault(this);
  }

  public void excludeHosts(String[] hosts) {
    for (String host : hosts) {
      excludeHost(host);
    }
  }
}