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

Change RedisBackedJobService to use a connection pool #439

Merged
merged 6 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions serving/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,40 @@ public RedisBackedJobService(JedisPool jedisPool) {

@Override
public Optional<Job> get(String id) {
Jedis jedis = jedisPool.getResource();
String json = jedis.get(id);
if (json == null) {
return Optional.empty();
}
Jedis jedis = null;
Job job = null;
Builder builder = Job.newBuilder();
try {
jedis = jedisPool.getResource();
String json = jedis.get(id);
if (json == null) {
return Optional.empty();
}
Builder builder = Job.newBuilder();
JsonFormat.parser().merge(json, builder);
job = builder.build();
} catch (Exception e) {
log.error(String.format("Failed to parse JSON for Feast job: %s", e.getMessage()));
zhilingc marked this conversation as resolved.
Show resolved Hide resolved
} finally {
jedis.close();
if (jedis != null) {
jedis.close();
}
}
return Optional.ofNullable(job);
}

@Override
public void upsert(Job job) {
Jedis jedis = jedisPool.getResource();
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(job.getId(), JsonFormat.printer().omittingInsignificantWhitespace().print(job));
jedis.expire(job.getId(), defaultExpirySeconds);
} catch (Exception e) {
log.error(String.format("Failed to upsert job: %s", e.getMessage()));
} finally {
jedis.close();
if (jedis != null) {
jedis.close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package feast.serving.service;

import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.embedded.RedisServer;

public class RedisBackedJobServiceTest {
private static String REDIS_HOST = "localhost";
private static int REDIS_PORT = 51235;
private RedisServer redis;

@Before
public void setUp() throws IOException {
redis = new RedisServer(REDIS_PORT);
redis.start();
}

@After
public void teardown() {
redis.stop();
}

@Test
public void shouldRecoverIfRedisConnectionIsLost() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(1);
jedisPoolConfig.setMaxWaitMillis(10);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, REDIS_HOST, REDIS_PORT);
RedisBackedJobService jobService = new RedisBackedJobService(jedisPool);
jobService.get("does not exist");
redis.stop();
try {
jobService.get("does not exist");
} catch (Exception e) {
// pass, this should fail, and return a broken connection to the pool
}
redis.start();
jobService.get("does not exist");
}
}