Skip to content

Commit

Permalink
Institute - Cache institute api data into Redis Cluster instead of st…
Browse files Browse the repository at this point in the history
…andalone (#336)

* GRAD2-2840 - Institute - Cache institute api data into Redis Cluster instead of standalone

* GRAD2-2840 - Institute - Cache institute api data into Redis Cluster instead of standalone

* GRAD2-2840 - Institute - Cache institute api data into Redis Cluster instead of standalone
  • Loading branch information
kamal-mohammed authored Jul 4, 2024
1 parent 02b8b32 commit 066e125
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 119 deletions.
6 changes: 4 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.oauth2.client.*;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -80,18 +78,6 @@ public WebClient getInstituteWebClient(OAuth2AuthorizedClientManager authorizedC
.apply(filter.oauth2Configuration())
.build();
}
/*@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository) {
OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}*/

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
Expand Down
62 changes: 31 additions & 31 deletions api/src/main/java/ca/bc/gov/educ/api/trax/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package ca.bc.gov.educ.api.trax.config;

import ca.bc.gov.educ.api.trax.util.EducGradTraxApiConstants;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableRedisRepositories("ca.bc.gov.educ.api.trax.repository.redis")
public class RedisConfig {
@Autowired
private EducGradTraxApiConstants constants;

@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(constants.getRedisUrl());
redisStandaloneConfiguration.setPort(Integer.parseInt(constants.getRedisPort()));
redisStandaloneConfiguration.setPassword(constants.getRedisSecret());

//Cluster Configuration
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
RedisNode node0 = new RedisNode(constants.getRedisUrl(), Integer.parseInt(constants.getRedisPort()));
redisClusterConfiguration.addClusterNode(node0);

RedisClusterNode rcn = new RedisClusterNode(constants.getRedisUrl(), Integer.parseInt(constants.getRedisPort()));

return new JedisConnectionFactory(redisStandaloneConfiguration);
redisClusterConfiguration.addClusterNode(new RedisClusterNode(
constants.getRedisUrl(),
Integer.parseInt(constants.getRedisPort())));
redisClusterConfiguration.setPassword(constants.getRedisSecret());
return new JedisConnectionFactory(redisClusterConfiguration);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();

return template;
public JedisCluster jedisCluster() {
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
jedisClusterNodes.add(
new HostAndPort(constants.getRedisUrl(),
Integer.parseInt(constants.getRedisPort())));

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setMaxIdle(8);
poolConfig.setMinIdle(0);
poolConfig.setBlockWhenExhausted(true);
poolConfig.setMaxWait(Duration.ofSeconds(1));
poolConfig.setTestWhileIdle(true);
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));

return new JedisCluster(jedisClusterNodes, 5000, 5000, 3,
constants.getRedisUser(), constants.getRedisSecret(),
"educ-grad-trax-api", poolConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import ca.bc.gov.educ.api.trax.constant.CacheStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

@Slf4j
@Component
public class ServiceHelper<T> {

@Autowired
RedisTemplate<String, String> redisTemplate;
JedisCluster jedisCluster;

public void initializeCache(boolean force, CacheKey cacheKey, T service) {
String cacheStatus = redisTemplate.opsForValue().get(cacheKey.name());
String cacheStatus = jedisCluster.get(cacheKey.name());
cacheStatus = cacheStatus == null ? "" : cacheStatus;
if (CacheStatus.LOADING.name().compareToIgnoreCase(cacheStatus) == 0
|| CacheStatus.READY.name().compareToIgnoreCase(cacheStatus) == 0) {
Expand All @@ -33,9 +33,9 @@ public void initializeCache(boolean force, CacheKey cacheKey, T service) {
}

private void loadCache(CacheKey cacheKey, T service) {
redisTemplate.opsForValue().set(cacheKey.name(), CacheStatus.LOADING.name());
jedisCluster.set(cacheKey.name(), CacheStatus.LOADING.name());
loadDataIntoRedisCache(cacheKey, service);
redisTemplate.opsForValue().set(cacheKey.name(), CacheStatus.READY.name());
jedisCluster.set(cacheKey.name(), CacheStatus.READY.name());
log.info(String.format("Success! - %s is now READY", cacheKey));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public class EducGradTraxApiConstants {
@Value("${redis.port}")
private String redisPort;

@Value("${redis.user}")
private String redisUser;

@Value("${redis.secret}")
private String redisSecret;

Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ nats:
redis:
url: ${REDIS_URL}
port: ${REDIS_PORT}
user: ${REDIS_USER}
secret: ${REDIS_SECRET}

#Scheduler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {EducGradTraxApiApplication.class})
Expand All @@ -42,6 +44,10 @@ public abstract class BaseReplicationServiceTest {
public OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepository;
@MockBean
public OAuth2AuthorizedClientService oAuth2AuthorizedClientService;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@Before
public void resetState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -53,6 +55,11 @@ public class ChoreographedEventPersistenceServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactory;
@MockBean
private JedisCluster jedisClusterMock;


@TestConfiguration
static class TestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -58,6 +60,10 @@ public class CodeServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@TestConfiguration
static class TestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.WebClient;
import redis.clients.jedis.JedisCluster;

import java.util.List;
import java.util.Optional;
Expand All @@ -51,6 +53,10 @@ public class DistrictServiceTest {
@Mock WebClient.RequestBodySpec requestBodyMock;
@Mock WebClient.RequestBodyUriSpec requestBodyUriMock;
@Mock WebClient.ResponseSpec responseMock;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@MockBean
@Qualifier("default")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import java.math.BigDecimal;
import java.util.Arrays;
Expand Down Expand Up @@ -51,6 +53,10 @@ public class EdwServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;


@TestConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import static org.mockito.MockitoAnnotations.openMocks;

Expand All @@ -47,6 +49,10 @@ public class GradStudentGraduatedServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@TestConfiguration
static class TestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import static org.mockito.MockitoAnnotations.openMocks;

Expand All @@ -47,6 +49,10 @@ public class GradStudentUndoCompletionServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@TestConfiguration
static class TestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import static org.mockito.MockitoAnnotations.openMocks;

Expand All @@ -47,6 +49,10 @@ public class GradStudentUpdatedServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@TestConfiguration
static class TestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -64,6 +66,10 @@ public class PsiServiceTest {

@MockBean
private Subscriber subscriber;
@MockBean
private JedisConnectionFactory jedisConnectionFactoryMock;
@MockBean
private JedisCluster jedisClusterMock;

@TestConfiguration
static class TestConfig {
Expand Down
Loading

0 comments on commit 066e125

Please sign in to comment.