<commit_before>package me.prettyprint.cassandra.service;

import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPoolFactory;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableSet;

/*package*/ class CassandraClientPoolByHostImpl implements CassandraClientPoolByHost {

  private static final Logger log = LoggerFactory.getLogger(CassandraClientPoolByHostImpl.class);

  private final CassandraClientFactory clientFactory;
  private final String url;
  private final String name;
  private final int port;
  private final int maxActive;
  private final int maxIdle;
  private final ExhaustedPolicy exhaustedPolicy;
  private final long maxWaitTimeWhenExhausted;
  private final GenericObjectPool pool;

  /**
   * Number of currently blocked threads.
   * This includes the number of threads waiting for an idle connection, as well as threads
   * wanting for connection initialization after they won a free slot.
   */
  private final AtomicInteger blockedThreadsCount;

  /**
   * The set of live clients created by the pool.
   * This set includes both the active clients currently used by active threads as well as idle
   * clients waiting in the pool.
   */
  private final Set<CassandraClient> liveClientsFromPool;

  public CassandraClientPoolByHostImpl(CassandraHost cassandraHost,
      CassandraClientPool pools,
      CassandraClientMonitor cassandraClientMonitor) {
    this(cassandraHost, pools, cassandraClientMonitor, new CassandraClientFactory(pools, cassandraHost, cassandraClientMonitor));
  }

  public CassandraClientPoolByHostImpl(CassandraHost cassandraHost,
      CassandraClientPool pools,
      CassandraClientMonitor cassandraClientMonitor,
      CassandraClientFactory cassandraClientFactory) {
    log.debug("Creating new connection pool for {}", cassandraHost.getUrlPort());
    url = cassandraHost.getUrl();
    port = cassandraHost.getPort();
    this.name = cassandraHost.getName();
    this.maxActive = cassandraHost.getMaxActive();
    this.maxIdle = cassandraHost.getMaxIdle();
    this.maxWaitTimeWhenExhausted = cassandraHost.getMaxWaitTimeWhenExhausted();
    this.exhaustedPolicy = cassandraHost.getExhaustedPolicy();
    this.clientFactory = cassandraClientFactory;

    blockedThreadsCount = new AtomicInteger(0);
    // Create a set implemented as a ConcurrentHashMap for performance and concurrency.
    liveClientsFromPool =
        Collections.newSetFromMap(new ConcurrentHashMap<CassandraClient,Boolean>());
    pool = createPool();
  }

  @Override
  public CassandraClient borrowClient() throws Exception, PoolExhaustedException,
      IllegalStateException {
    try {
      blockedThreadsCount.incrementAndGet();
      CassandraClient client = (CassandraClient) pool.borrowObject();
      liveClientsFromPool.add(client);
      return client;
    } catch (NoSuchElementException e) {
      throw new PoolExhaustedException(e.getMessage());
    } finally {
      blockedThreadsCount.decrementAndGet();
    }
  }

  @Override
  public void close() {
    try {
      pool.close();
    } catch (Exception e) {
      log.error("Unable to close pool", e);
    }
  }

  @Override
  public int getNumIdle() {
    return pool.getNumIdle();
  }

  @Override
  public int getNumActive() {
    return pool.getNumActive();
  }

  @Override
  public int getNumBeforeExhausted() {
    return maxActive - pool.getNumActive();
  }

  @Override
  public void releaseClient(CassandraClient client) throws Exception {
    pool.returnObject(client);
  }

  private GenericObjectPool createPool() {
    GenericObjectPoolFactory poolFactory = new GenericObjectPoolFactory(clientFactory, maxActive,
        getObjectPoolExhaustedAction(exhaustedPolicy),
        maxWaitTimeWhenExhausted, maxIdle);
    GenericObjectPool p = (GenericObjectPool) poolFactory.createPool();
    p.setTestOnBorrow(true);
    // maxIdle controls the maximum number of objects that can sit idle in the pool at any time.
    // When negative, there is no limit to the number of objects that may be idle at one time.
    p.setMaxIdle(-1);
    return p;
  }

  public static byte getObjectPoolExhaustedAction(ExhaustedPolicy exhaustedAction){
    switch(exhaustedAction){
      case WHEN_EXHAUSTED_FAIL:
        return GenericObjectPool.WHEN_EXHAUSTED_FAIL;
      case WHEN_EXHAUSTED_BLOCK:<commit_msg>now using maxIdle property provided<commit_after>package me.prettyprint.cassandra.service;

import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPoolFactory;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableSet;

/*package*/ class CassandraClientPoolByHostImpl implements CassandraClientPoolByHost {

  private static final Logger log = LoggerFactory.getLogger(CassandraClientPoolByHostImpl.class);

  private final CassandraClientFactory clientFactory;
  private final String url;
  private final String name;
  private final int port;
  private final int maxActive;
  private final int maxIdle;
  private final ExhaustedPolicy exhaustedPolicy;
  private final long maxWaitTimeWhenExhausted;
  private final GenericObjectPool pool;

  /**
   * Number of currently blocked threads.
   * This includes the number of threads waiting for an idle connection, as well as threads
   * wanting for connection initialization after they won a free slot.
   */
  private final AtomicInteger blockedThreadsCount;

  /**
   * The set of live clients created by the pool.
   * This set includes both the active clients currently used by active threads as well as idle
   * clients waiting in the pool.
   */
  private final Set<CassandraClient> liveClientsFromPool;

  public CassandraClientPoolByHostImpl(CassandraHost cassandraHost,
      CassandraClientPool pools,
      CassandraClientMonitor cassandraClientMonitor) {
    this(cassandraHost, pools, cassandraClientMonitor, new CassandraClientFactory(pools, cassandraHost, cassandraClientMonitor));
  }

  public CassandraClientPoolByHostImpl(CassandraHost cassandraHost,
      CassandraClientPool pools,
      CassandraClientMonitor cassandraClientMonitor,
      CassandraClientFactory cassandraClientFactory) {
    log.debug("Creating new connection pool for {}", cassandraHost.getUrlPort());
    url = cassandraHost.getUrl();
    port = cassandraHost.getPort();
    this.name = cassandraHost.getName();
    this.maxActive = cassandraHost.getMaxActive();
    this.maxIdle = cassandraHost.getMaxIdle();
    this.maxWaitTimeWhenExhausted = cassandraHost.getMaxWaitTimeWhenExhausted();
    this.exhaustedPolicy = cassandraHost.getExhaustedPolicy();
    this.clientFactory = cassandraClientFactory;

    blockedThreadsCount = new AtomicInteger(0);
    // Create a set implemented as a ConcurrentHashMap for performance and concurrency.
    liveClientsFromPool =
        Collections.newSetFromMap(new ConcurrentHashMap<CassandraClient,Boolean>());
    pool = createPool();
  }

  @Override
  public CassandraClient borrowClient() throws Exception, PoolExhaustedException,
      IllegalStateException {
    try {
      blockedThreadsCount.incrementAndGet();
      CassandraClient client = (CassandraClient) pool.borrowObject();
      liveClientsFromPool.add(client);
      return client;
    } catch (NoSuchElementException e) {
      throw new PoolExhaustedException(e.getMessage());
    } finally {
      blockedThreadsCount.decrementAndGet();
    }
  }

  @Override
  public void close() {
    try {
      pool.close();
    } catch (Exception e) {
      log.error("Unable to close pool", e);
    }
  }

  @Override
  public int getNumIdle() {
    return pool.getNumIdle();
  }

  @Override
  public int getNumActive() {
    return pool.getNumActive();
  }

  @Override
  public int getNumBeforeExhausted() {
    return maxActive - pool.getNumActive();
  }

  @Override
  public void releaseClient(CassandraClient client) throws Exception {
    pool.returnObject(client);
  }

  private GenericObjectPool createPool() {
    GenericObjectPoolFactory poolFactory = new GenericObjectPoolFactory(clientFactory, maxActive,
        getObjectPoolExhaustedAction(exhaustedPolicy),
        maxWaitTimeWhenExhausted, maxIdle);
    GenericObjectPool p = (GenericObjectPool) poolFactory.createPool();
    p.setTestOnBorrow(true);
    // maxIdle controls the maximum number of objects that can sit idle in the pool at any time.
    // When negative, there is no limit to the number of objects that may be idle at one time.
    p.setMaxIdle(maxIdle);
    return p;
  }

  public static byte getObjectPoolExhaustedAction(ExhaustedPolicy exhaustedAction){
    switch(exhaustedAction){
      case WHEN_EXHAUSTED_FAIL:
        return GenericObjectPool.WHEN_EXHAUSTED_FAIL;
      case WHEN_EXHAUSTED_BLOCK:
{'fraction_non_alphanumeric': 0.07422084181214522, 'fraction_numerical': 0.0007497054728499518, 'mean_word_length': 4.360505166475316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1119752', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2562, 'n_words': 677}