Skip to content

Commit

Permalink
Initial redis config. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed Mar 2, 2017
1 parent d024f9e commit 113868c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
3 changes: 2 additions & 1 deletion kayenta-core/kayenta-core.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
compile spinnaker.dependency('bootWeb')
compile spinnaker.dependency('jedis')
compile spinnaker.dependency('lombok')
spinnaker.group('bootWeb')
spinnaker.group('jackson')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.kayenta.persistence.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Configuration
@EnableConfigurationProperties
@ConditionalOnProperty("kayenta.redis.enabled")
public class JedisConfig {

@Bean
JedisPool jedisPool(@Value("${redis.connection:redis://localhost:6379}") String connection,
@Value("${redis.timeout:2000}") int timeout) {
RedisConnectionInfo connectionInfo = RedisConnectionInfo.parseConnectionUri(connection);
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

poolConfig.setMaxTotal(100);
poolConfig.setMinIdle(25);
poolConfig.setMaxIdle(100);

return new JedisPool(poolConfig,
connectionInfo.getHost(),
connectionInfo.getPort(),
timeout,
connectionInfo.getPassword());
}

@Bean
HealthIndicator redisHealth(JedisPool jedisPool) {
return () -> {
Jedis jedis = null;
Health.Builder health = null;

try {
jedis = jedisPool.getResource();

if ("PONG".equals(jedis.ping())) {
health = Health.up();
} else {
health = Health.down();
}
} catch (Exception ex) {
health = Health.down(ex);
} finally {
if (jedis != null) {
jedis.close();
}
}

return health.build();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.kayenta.persistence.config;

import lombok.Builder;
import lombok.Data;
import redis.clients.jedis.Protocol;
import redis.clients.util.JedisURIHelper;

import java.net.URI;

@Builder
@Data
public class RedisConnectionInfo {

private String host;
private int port;
private int database;
private String password;

static RedisConnectionInfo parseConnectionUri(String connection) {
URI redisConnection = URI.create(connection);
String host = redisConnection.getHost();
int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
int database = JedisURIHelper.getDBIndex(redisConnection);
String password = JedisURIHelper.getPassword(redisConnection);

return RedisConnectionInfo.builder().host(host).port(port).database(database).password(password).build();
}
}
2 changes: 2 additions & 0 deletions kayenta-web/config/kayenta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ server:
port: 8090

kayenta:
redis:
enabled: true
gcs:
enabled: false
google:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spinnaker.kayenta.config.WebConfiguration;
import com.netflix.spinnaker.kayenta.gcs.config.GcsConfiguration;
import com.netflix.spinnaker.kayenta.google.config.GoogleConfiguration;
import com.netflix.spinnaker.kayenta.persistence.config.JedisConfig;
import com.netflix.spinnaker.kayenta.stackdriver.config.StackdriverConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand All @@ -36,6 +37,7 @@
@Import({
GcsConfiguration.class,
GoogleConfiguration.class,
JedisConfig.class,
KayentaConfiguration.class,
StackdriverConfiguration.class,
WebConfiguration.class
Expand Down

0 comments on commit 113868c

Please sign in to comment.