Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use semaphore in ObjectPool #426

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public RetryingPooledBookkeeperClient createBookKeeperClient(String host, Config
}
else {
Poolable<TTransport> obj;
obj = pool.borrowObject(host, conf);
obj = pool.borrowObject(host);
RetryingPooledBookkeeperClient retryingBookkeeperClient = new RetryingPooledBookkeeperClient(obj, host, conf);
return retryingBookkeeperClient;
}
Expand Down
6 changes: 2 additions & 4 deletions rubix-spi/src/main/java/com/qubole/rubix/spi/CacheConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
*/
package com.qubole.rubix.spi;

import com.google.common.collect.ImmutableList;
import org.apache.hadoop.conf.Configuration;

import java.util.List;

import static com.qubole.rubix.spi.utils.DataSizeUnits.MEGABYTES;

/**
Expand Down Expand Up @@ -52,6 +49,7 @@ public class CacheConfig
private static final String KEY_POOL_MIN_SIZE = "rubix.pool.size.min";
private static final String KEY_POOL_DELTA_SIZE = "rubix.pool.delta.size";
private static final String KEY_POOL_MAX_WAIT_TIMEOUT = "rubix.pool.wait.timeout";
private static final String KEY_POOL_SCAVENGER_INTERVAL = "rubix.pool.scavenger.interval";
private static final String KEY_DATA_CACHE_EXPIRY_AFTER_WRITE = "rubix.cache.expiration.after-write";
private static final String KEY_DATA_CACHE_DIR_PREFIX = "rubix.cache.dirprefix.list";
private static final String KEY_DATA_CACHE_DIR_SUFFIX = "rubix.cache.dirsuffix";
Expand Down Expand Up @@ -310,7 +308,7 @@ public static int getTransportPoolMaxWait(Configuration conf)

public static int getScavengeInterval(Configuration conf)
{
return conf.getInt(KEY_POOL_MAX_WAIT_TIMEOUT, DEFAULT_SCAVENGE_INTERVAL);
return conf.getInt(KEY_POOL_SCAVENGER_INTERVAL, DEFAULT_SCAVENGE_INTERVAL);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please separate out commits. Have one separate for changing config name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a separate commit.

}

public static int get(Configuration conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.hadoop.conf.Configuration;

import java.io.Closeable;
import java.io.IOException;
import java.nio.channels.SocketChannel;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -53,7 +52,7 @@ public static DataTransferClient getClient(String host, Configuration conf)
}
}
}
Poolable<SocketChannel> socketChannelPoolable = pool.borrowObject(host, conf);
Poolable<SocketChannel> socketChannelPoolable = pool.borrowObject(host);
return new DataTransferClient(socketChannelPoolable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.qubole.rubix.spi;

import com.google.common.annotations.VisibleForTesting;
import com.qubole.rubix.spi.fop.ObjectPool;
import com.qubole.rubix.spi.fop.Poolable;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -76,14 +77,20 @@ protected <V> V retryConnection(Callable<V> callable)

// unset transportPoolable so that close() doesnt return it again to pool if borrowObject hits an exception
transportPoolable = null;
transportPoolable = objectPool.borrowObject(host, conf);
transportPoolable = objectPool.borrowObject(host);
updateClient(transportPoolable);
}
}

throw new TException();
}

@VisibleForTesting
public Poolable<TTransport> getTransportPoolable()
{
return transportPoolable;
}

@Override
public void close()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
*/
public interface ObjectFactory<T>
{
T create(String host, int socketTimeout, int connectTimeout);
T create(String host, int socketTimeout, int connectTimeout)
throws Exception;

void destroy(T t);

Expand Down
27 changes: 12 additions & 15 deletions rubix-spi/src/main/java/com/qubole/rubix/spi/fop/ObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
import com.google.common.util.concurrent.AbstractScheduledService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.currentThread;
Expand Down Expand Up @@ -67,7 +65,7 @@ protected BlockingQueue<Poolable<T>> createBlockingQueue(PoolConfig poolConfig)
return new ArrayBlockingQueue<>(poolConfig.getMaxSize());
}

public Poolable<T> borrowObject(String host, Configuration conf)
public Poolable<T> borrowObject(String host)
{
if (!hostToPoolMap.containsKey(host)) {
synchronized (hostToPoolMap) {
Expand All @@ -77,22 +75,21 @@ public Poolable<T> borrowObject(String host, Configuration conf)
}
}
log.debug(this.name + " : Borrowing object for partition: " + host);
for (int i = 0; i < 3; i++) { // try at most three times
Poolable<T> result = getObject(false, host);
if (factory.validate(result.getObject())) {
return result;
}
else {
this.hostToPoolMap.get(host).decreaseObject(result);
}
Poolable<T> result = getObject(host);
if (result == null) {
throw new RuntimeException("Unable to find a free object from connection pool: " + this.name);
}
else if (!factory.validate(result.getObject())) {
this.hostToPoolMap.get(host).decreaseObject(result);
throw new RuntimeException("Cannot find a valid object from connection pool: " + this.name);
}
throw new RuntimeException("Cannot find a valid object");
return result;
}

private Poolable<T> getObject(boolean blocking, String host)
private Poolable<T> getObject(String host)
{
ObjectPoolPartition<T> subPool = this.hostToPoolMap.get(host);
return subPool.getObject(blocking);
return subPool.getObject();
}

public void returnObject(Poolable<T> obj)
Expand All @@ -105,7 +102,7 @@ public int getSize()
{
int size = 0;
for (ObjectPoolPartition<T> subPool : hostToPoolMap.values()) {
size += subPool.getTotalCount();
size += subPool.getAliveObjectCount();
}
return size;
}
Expand Down
Loading