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

Handle exceptions on close in JedisSentinelPool #2102

Merged
merged 1 commit into from Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/redis/clients/jedis/JedisSentinelPool.java
Expand Up @@ -237,8 +237,13 @@ protected void returnBrokenResource(final Jedis resource) {
@Override
protected void returnResource(final Jedis resource) {
if (resource != null) {
resource.resetState();
returnResourceObject(resource);
try {
resource.resetState();
returnResourceObject(resource);
} catch (Exception e) {
returnBrokenResource(resource);
throw new JedisException("Resource is returned to the pool as broken", e);
}
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/test/java/redis/clients/jedis/tests/JedisSentinelPoolTest.java
Expand Up @@ -5,7 +5,11 @@

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -175,6 +179,61 @@ public void ensureSafeTwiceFailover() throws InterruptedException {
// you can test failover as much as possible
}

@Test
public void returnResourceDestroysResourceOnException() {
class CrashingJedis extends Jedis {
public CrashingJedis(final HostAndPort hp) {
super(hp);
}
@Override
public void resetState() {
throw new RuntimeException();
}
}

final AtomicInteger destroyed = new AtomicInteger(0);

class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

@Override
public PooledObject<Jedis> makeObject() throws Exception {
return new DefaultPooledObject<Jedis>(new CrashingJedis(master));
}

@Override
public void destroyObject(PooledObject<Jedis> p) throws Exception {
destroyed.incrementAndGet();
}

@Override
public boolean validateObject(PooledObject<Jedis> p) {
return true;
}

@Override
public void activateObject(PooledObject<Jedis> p) throws Exception {
}

@Override
public void passivateObject(PooledObject<Jedis> p) throws Exception {
}
}

GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
pool.initPool(config, new CrashingJedisPooledObjectFactory());
Jedis crashingJedis = pool.getResource();

try {
crashingJedis.close();
} catch (Exception ignored) {
}

assertEquals(1, destroyed.get());
}

private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
HostAndPort oldMaster = pool.getCurrentHostMaster();

Expand Down