Esempio n. 1
0
  private static GraphiteWriter getGraphiteWriter(OutputStream out, List<String> typeNames)
      throws Exception {
    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);

    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer =
        GraphiteWriter.builder().setHost("localhost").setPort(2003).addTypeNames(typeNames).build();
    writer.setPool(pool);

    return writer;
  }
  @Override
  public GPCatalogConnectorStore build() throws Exception {
    Preconditions.checkNotNull(serverUrl, "CSW Server URL must " + "not be null.");

    GPPoolConnectorKey keyConnector =
        super.proxyConfiguration != null
            ? new GPPoolConnectorKey(serverUrl, securityConnector, proxyConfiguration, version)
            : new GPPoolConnectorKey(serverUrl, securityConnector, version);

    GPCatalogConnectorStore catalogStore = catalogConnectoPool.borrowObject(keyConnector);

    catalogConnectoPool.returnObject(keyConnector, catalogStore);

    return catalogStore;
  }
Esempio n. 3
0
  @Test
  public void socketInvalidatedWhenError() throws Exception {
    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);
    UnflushableByteArrayOutputStream out = new UnflushableByteArrayOutputStream();
    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer = GraphiteWriter.builder().setHost("localhost").setPort(2003).build();
    writer.setPool(pool);

    writer.doWrite(dummyServer(), dummyQuery(), dummyResults());
    Mockito.verify(pool)
        .invalidateObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket));
    Mockito.verify(pool, Mockito.never())
        .returnObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket));
  }
 @Override
 public void invalidateObject(RConnection connection) throws Exception {
   Field hostField = connection.getClass().getDeclaredField("host");
   hostField.setAccessible(true);
   String host = (String) hostField.get(connection);
   keyedConnectionPool.invalidateObject(host, connection);
   hostQueue.put(host);
 }
 public synchronized String getDebugInfo() {
   StringBuilder builder = new StringBuilder();
   builder.append("Hosts: ").append(StringUtils.join(hosts, ", ")).append("\n");
   builder.append("Num connections per host: ").append(numConnections).append("\n");
   builder.append("\n");
   for (String host : hosts) {
     builder.append(host).append(": ");
     builder.append(keyedConnectionPool.getNumActive(host)).append(" active; ");
     builder.append(keyedConnectionPool.getNumIdle(host)).append(" idle.\n");
   }
   builder.append("\n");
   builder.append("Total: ");
   builder.append(this.getNumActive()).append(" active; ");
   builder.append(this.getNumIdle()).append(" idle.\n");
   builder.append("\n");
   builder.append("Queue: ").append(StringUtils.join(hostQueue.toArray(), ", "));
   return builder.toString();
 }
Esempio n. 6
0
  @Test
  public void booleanAsNumberWorks() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket);

    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer =
        GraphiteWriter.builder().setHost("localhost").setPort(123).setBooleanAsNumber(true).build();
    writer.setPool(pool);

    writer.doWrite(dummyServer(), dummyQuery(), singleTrueResult());

    // check that the booleanAsNumber property was picked up from the JSON
    assertThat(out.toString())
        .startsWith("servers.host_example_net_4321.VerboseMemory.Verbose 1 0");
  }
  public RserveConnectionPool(String[] hosts, int numConnections) {
    this.hosts = hosts;
    this.numConnections = numConnections;

    final RserveConnectionFactory keyedConnectionFactory = new RserveConnectionFactory();

    final GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
    config.maxActive = numConnections;
    config.maxIdle = numConnections;
    config.minIdle = numConnections;
    config.whenExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK;
    config.testOnBorrow = true;
    config.testOnReturn = true;
    config.testWhileIdle = true;
    config.timeBetweenEvictionRunsMillis = 1000; // enables eviction thread
    config.minEvictableIdleTimeMillis = -1; // disable eviction due to idle time
    config.numTestsPerEvictionRun = -1; // test all idle objects
    config.lifo = false;

    keyedConnectionPool =
        new GenericKeyedObjectPool<String, RConnection>(keyedConnectionFactory, config);

    // Initialise keyed object pool with each host
    for (String host : hosts) {
      keyedConnectionPool.preparePool(host, false);
    }

    // Initialise blocking queue with elements representing potential host connections.
    // There are n elements per host, where n is the number of connections permitted per host.
    // When connections are obtained, we take the host from the head of the queue and use this as
    // the key to the pool.
    // When connections are released, we return the host to the tail of the queue so future
    // connections can be obtained.
    // This implements a first-available-first-used (or first-in-first-out) model for host
    // connections.
    hostQueue = new ArrayBlockingQueue<String>(hosts.length * numConnections, true);
    for (String host : hosts) {
      for (int i = 0; i < numConnections; i++) {
        hostQueue.add(host);
      }
    }
  }
  private synchronized void registerPool(String username, String password)
      throws javax.naming.NamingException {
    Map pools = (Map) dsInstanceMap.get(instanceKey);
    PoolKey key = getPoolKey(username);
    if (!pools.containsKey(key)) {
      int maxActive = getDefaultMaxActive();
      int maxIdle = getDefaultMaxIdle();
      int maxWait = getDefaultMaxWait();

      // The source of physical db connections
      ConnectionPoolDataSource cpds = this.cpds;
      if (cpds == null) {
        Context ctx = null;
        if (jndiEnvironment == null) {
          ctx = new InitialContext();
        } else {
          ctx = new InitialContext(jndiEnvironment);
        }
        cpds = (ConnectionPoolDataSource) ctx.lookup(dataSourceName);
      }

      Object whicheverPool = null;
      if (perUserMaxActive != null && perUserMaxActive.containsKey(username)) {
        Integer userMax = getPerUserMaxActive(username);
        if (userMax != null) {
          maxActive = userMax.intValue();
        }
        userMax = getPerUserMaxIdle(username);
        if (userMax != null) {
          maxIdle = userMax.intValue();
        }
        userMax = getPerUserMaxWait(username);
        if (userMax != null) {
          maxWait = userMax.intValue();
        }

        // Create an object pool to contain our PooledConnections
        GenericObjectPool pool = new GenericObjectPool(null);
        pool.setMaxActive(maxActive);
        pool.setMaxIdle(maxIdle);
        pool.setMaxWait(maxWait);
        pool.setWhenExhaustedAction(getWhenExhausted(maxActive, maxWait));
        pool.setTestOnBorrow(getTestOnBorrow());
        pool.setTestOnReturn(getTestOnReturn());
        pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
        pool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
        pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        pool.setTestWhileIdle(getTestWhileIdle());

        // Set up the factory we will use (passing the pool associates
        // the factory with the pool, so we do not have to do so
        // explicitly)
        new CPDSConnectionFactory(cpds, pool, validationQuery, username, password);
        whicheverPool = pool;
      } else {
        // use default pool
        // Create an object pool to contain our PooledConnections
        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(null);
        pool.setMaxActive(maxActive);
        pool.setMaxIdle(maxIdle);
        pool.setMaxWait(maxWait);
        pool.setWhenExhaustedAction(getWhenExhausted(maxActive, maxWait));
        pool.setTestOnBorrow(getTestOnBorrow());
        pool.setTestOnReturn(getTestOnReturn());
        pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
        pool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
        pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        pool.setTestWhileIdle(getTestWhileIdle());

        // Set up the factory we will use (passing the pool associates
        // the factory with the pool, so we do not have to do so
        // explicitly)
        new KeyedCPDSConnectionFactory(cpds, pool, validationQuery);
        whicheverPool = pool;
      }

      // pools is a FastHashMap set to put the pool in a thread-safe way
      pools.put(key, whicheverPool);
    }
  }
 @Override
 public int getNumActive() throws UnsupportedOperationException {
   return keyedConnectionPool.getNumActive();
 }
 @Override
 public RConnection borrowObject()
     throws Exception, NoSuchElementException, IllegalStateException {
   String host = hostQueue.take();
   return keyedConnectionPool.borrowObject(host);
 }
 @Override
 public void close() throws Exception {
   keyedConnectionPool.close();
 }
 @Override
 public void clear() throws Exception, UnsupportedOperationException {
   keyedConnectionPool.clear();
 }