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

[JENKINS-65530] Add dbIndex support for Redis. #100

Open
wants to merge 1 commit 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 @@ -17,6 +17,7 @@ public class Redis extends HostBasedLogstashIndexer<RedisDao>

protected String key;
protected Secret password;
protected int dbindex;

@DataBoundConstructor
public Redis()
Expand Down Expand Up @@ -45,6 +46,17 @@ public void setPassword(Secret password)
this.password = password;
}

public int getDbindex()
{
return dbindex;
}

@DataBoundSetter
public void setDbindex(int dbindex)
{
this.dbindex = dbindex;
}

@Override
public boolean equals(Object obj)
{
Expand Down Expand Up @@ -85,7 +97,7 @@ public int hashCode()
@Override
public RedisDao createIndexerInstance()
{
return new RedisDao(getHost(), getPort(), key, Secret.toString(password));
return new RedisDao(getHost(), getPort(), key, Secret.toString(password), getDbindex());
}

@Extension
Expand All @@ -105,6 +117,11 @@ public int getDefaultPort()
return 6379;
}

public int getDefaultDbindex()
{
return 0;
}

public FormValidation doCheckKey(@QueryParameter("value") String value)
{
if (StringUtils.isBlank(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ public class RedisDao extends HostBasedLogstashIndexerDao {

private final String password;
private final String key;
private final int dbindex;

//primary constructor used by indexer factory
public RedisDao(String host, int port, String key, String password) {
this(null, host, port, key, password);
public RedisDao(String host, int port, String key, String password, int dbindex) {
this(null, host, port, key, password, dbindex);
}

/*
* TODO: this constructor is only for testing so one can inject a mocked JedisPool.
* With Powermock we can intercept the creation of the JedisPool and replace with a mock
* making this constructor obsolete
*/
RedisDao(JedisPool factory, String host, int port, String key, String password) {
RedisDao(JedisPool factory, String host, int port, String key, String password, int dbindex) {
super(host, port);

this.key = key;
this.password = password;
this.dbindex = dbindex;

if (StringUtils.isBlank(key)) {
throw new IllegalArgumentException("redis key is required");
Expand All @@ -90,6 +92,11 @@ public String getKey()
return key;
}

public int getDbindex()
{
return dbindex;
}

@Override
public void push(String data) throws IOException {
Jedis jedis = null;
Expand All @@ -100,6 +107,9 @@ public void push(String data) throws IOException {
if (!StringUtils.isBlank(password)) {
jedis.auth(password);
}
if (dbindex > 0) {
jedis.select(dbindex);
}

jedis.connect();
long result = jedis.rpush(key, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%Dbindex}" field="dbindex">
<f:textbox default="${descriptor.defaultDbindex}"/>
</f:entry>
<f:entry title="${%Password}" field="password">
<f:password/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
The Redis database index to store data in.
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class RedisDaoTest {
@Mock JedisPool mockPool;
@Mock Jedis mockJedis;

RedisDao createDao(String host, int port, String key, String username, String password) {
return new RedisDao(mockPool, host, port, key, password);
RedisDao createDao(String host, int port, String key, String username, String password, int dbindex) {
return new RedisDao(mockPool, host, port, key, password, dbindex);
}

@Before
public void before() throws Exception {
int port = (int) (Math.random() * 1000);
dao = createDao("localhost", port, "logstash", "username", "password");
dao = createDao("localhost", port, "logstash", "username", "password", 0);

when(mockPool.getResource()).thenReturn(mockJedis);
}
Expand All @@ -47,7 +47,7 @@ public void after() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void constructorFailNullHost() throws Exception {
try {
createDao(null, 6379, "logstash", "username", "password");
createDao(null, 6379, "logstash", "username", "password", 0);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "host name is required", e.getMessage());
throw e;
Expand All @@ -57,7 +57,7 @@ public void constructorFailNullHost() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void constructorFailEmptyHost() throws Exception {
try {
createDao(" ", 6379, "logstash", "username", "password");
createDao(" ", 6379, "logstash", "username", "password", 0);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "host name is required", e.getMessage());
throw e;
Expand All @@ -67,7 +67,7 @@ public void constructorFailEmptyHost() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void constructorFailNullKey() throws Exception {
try {
createDao("localhost", 6379, null, "username", "password");
createDao("localhost", 6379, null, "username", "password", 0);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "redis key is required", e.getMessage());
throw e;
Expand All @@ -77,7 +77,7 @@ public void constructorFailNullKey() throws Exception {
@Test(expected = IllegalArgumentException.class)
public void constructorFailEmptyKey() throws Exception {
try {
createDao("localhost", 6379, " ", "username", "password");
createDao("localhost", 6379, " ", "username", "password", 0);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "redis key is required", e.getMessage());
throw e;
Expand All @@ -87,7 +87,7 @@ public void constructorFailEmptyKey() throws Exception {
@Test
public void constructorSuccess() throws Exception {
// Unit under test
dao = createDao("localhost", 6379, "logstash", "username", "password");
dao = createDao("localhost", 6379, "logstash", "username", "password", 0);

// Verify results
assertEquals("Wrong host name", "localhost", dao.getHost());
Expand Down Expand Up @@ -182,7 +182,7 @@ public void pushSuccessNoAuth() throws Exception {
String json = "{ 'foo': 'bar' }";

// Initialize mocks
dao = createDao("localhost", 6379, "logstash", null, null);
dao = createDao("localhost", 6379, "logstash", null, null, 0);
when(mockJedis.rpush("logstash", json)).thenReturn(1L);

// Unit under test
Expand Down