/** * Manages refreshing the forests and hosts and retrying events after a host * becomes unavailable. * @param moveMgr the DataMovementManager (used to call readForestConfig to reset after black-listing an unavailable host) */ public HostAvailabilityListener(DataMovementManager moveMgr) { if (moveMgr == null) throw new IllegalArgumentException("moveMgr must not be null"); this.moveMgr = moveMgr; if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.DIRECT) { hostUnavailableExceptions.add(SocketException.class); hostUnavailableExceptions.add(SSLException.class); hostUnavailableExceptions.add(UnknownHostException.class); } }
private synchronized boolean processException(Batcher batcher, Throwable throwable, String host) { return (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) ? processGatewayException(batcher, throwable, host) : processForestHostException(batcher, throwable, host); }
/** If less than minHosts are left, calls stopJob. * * @param numHosts the minimum number of hosts before this will call dataMovementMangaer.stopJob(batcher) * * @return this instance (for method chaining) */ public HostAvailabilityListener withMinHosts(int numHosts) { if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) { if (numHosts != 1) { throw new IllegalArgumentException("numHosts must be 1 when using only the primary host for the connection"); } } else { if (numHosts <= 0) throw new IllegalArgumentException("numHosts must be > 0"); // TODO: use existing forest configuration instead of refreshing? int numConfigHosts = moveMgr.readForestConfig().getPreferredHosts().length; if (numHosts > numConfigHosts) throw new IllegalArgumentException ("numHosts must be less than or equal to the number of hosts in the cluster"); } this.minHosts = numHosts; return this; }
@Test public void testWithWriteAndQueryBatcher() throws Exception{ if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return; ForestConfiguration forestConfig = moveMgr.readForestConfig(); long hostNum = Stream.of(forestConfig.listForests()).map(forest->forest.getPreferredHost()).distinct().count(); if ( hostNum <= 1 ) return; // we're not in a cluster, so this test isn't valid String host1 = forestConfig.listForests()[0].getPreferredHost(); FilteredForestConfiguration ffg = new FilteredForestConfiguration(forestConfig) .withRenamedHost(host1, Inet4Address.getByName(host1).getHostAddress()); runWithWriteAndQueryBatcher(ffg); ffg = new FilteredForestConfiguration(forestConfig) .withWhiteList(host1); runWithWriteAndQueryBatcher(ffg); ffg = new FilteredForestConfiguration(forestConfig) .withBlackList(host1); runWithWriteAndQueryBatcher(ffg); }
if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) {
@Test public void testArgs() throws Exception { if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return; int defaultPort = client.getPort(); Class<?> defaultAuthContext = client.getSecurityContext().getClass(); ForestConfiguration forestConfig = moveMgr.readForestConfig(); Forest[] forests = forestConfig.listForests(); String defaultDatabase = forests[0].getDatabaseName(); // expect three forests per node assertTrue(forests.length % 3 == 0); for ( Forest forest : forests ) { DatabaseClient forestClient = ((DataMovementManagerImpl) moveMgr).getForestClient(forest); // not all forests for a database are on the same host, so all we // can check is that the hostname is not null assertNotNull(forest.getHost()); // not all hosts have the original REST server, but all hosts have the uber port assertEquals(defaultPort, forestClient.getPort()); assertEquals(defaultDatabase, forest.getDatabaseName()); assertEquals(defaultAuthContext, forestClient.getSecurityContext().getClass()); assertEquals(true, forest.isUpdateable()); if ( forest.getForestName() == null || ! forest.getForestName().startsWith("java-unittest-") ) { fail("Unexpected forestName \"" + forest.getForestName() + "\""); } } } }
@Test public void testWithInvalidHosts() throws Exception{ if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return; ForestConfiguration forestConfig = moveMgr.readForestConfig(); String host1 = forestConfig.listForests()[0].getPreferredHost(); FilteredForestConfiguration ffg = new FilteredForestConfiguration(forestConfig) .withRenamedHost("someInvalidHostName", "anotherInvalidHostName"); runWithWriteAndQueryBatcher(ffg); ffg = new FilteredForestConfiguration(forestConfig) .withBlackList("someInvalidHostName"); runWithWriteAndQueryBatcher(ffg); ffg = new FilteredForestConfiguration(forestConfig) .withWhiteList("someInvalidHostName") .withWhiteList(host1); runWithWriteAndQueryBatcher(ffg); }
@Test public void testBlackList() { if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return;
@Test public void testWhiteList() { if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return;
@Test public void testRename() { if (moveMgr.getConnectionType() == DatabaseClient.ConnectionType.GATEWAY) return;