@Override protected Cache createInstance() throws Exception { DescribeCacheClustersRequest describeCacheClustersRequest = new DescribeCacheClustersRequest().withCacheClusterId(getCacheClusterName()); describeCacheClustersRequest.setShowCacheNodeInfo(true); DescribeCacheClustersResult describeCacheClustersResult = this.amazonElastiCache.describeCacheClusters(describeCacheClustersRequest); CacheCluster cacheCluster = describeCacheClustersResult.getCacheClusters().get(0); if (!"available".equals(cacheCluster.getCacheClusterStatus())) { LOGGER.warn("Cache cluster is not available now. Connection may fail during cache access. Current status is {}", cacheCluster.getCacheClusterStatus()); } Endpoint configurationEndpoint = getEndpointForCache(cacheCluster); for (CacheFactory cacheFactory : this.cacheFactories) { if (cacheFactory.isSupportingCacheArchitecture(cacheCluster.getEngine())) { return cacheFactory.createCache(this.cacheClusterId, configurationEndpoint.getAddress(), configurationEndpoint.getPort()); } } throw new IllegalArgumentException("No CacheFactory configured for engine: " + cacheCluster.getEngine()); }
@Test public void getObject_availableCluster_returnsConfiguredMemcachedClient() throws Exception { // Arrange AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache"); testCache.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(testCache)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint( new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached"))); ElastiCacheFactoryBean elasticCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "testCache", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678))); // Act elasticCacheFactoryBean.afterPropertiesSet(); Cache cache = elasticCacheFactoryBean.getObject(); // Assert assertNotNull(cache); }
@Test public void getObject_availableClusterWithLogicalName_returnsConfigurationMemcachedClientWithPhysicalName() throws Exception { // Arrange AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest testCache = new DescribeCacheClustersRequest().withCacheClusterId("testCache"); testCache.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(testCache)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withConfigurationEndpoint( new Endpoint().withAddress("localhost").withPort(45678)).withCacheClusterStatus("available").withEngine("memcached"))); ResourceIdResolver resourceIdResolver = mock(ResourceIdResolver.class); when(resourceIdResolver.resolveToPhysicalResourceId("test")).thenReturn("testCache"); ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "test", resourceIdResolver, Collections.<CacheFactory>singletonList(new TestCacheFactory("test", "localhost", 45678))); // Act elastiCacheFactoryBean.afterPropertiesSet(); Cache cache = elastiCacheFactoryBean.getObject(); // Assert assertNotNull(cache); }
@Test public void getObject_clusterWithRedisEngineConfigured_reportsError() throws Exception { //Arrange this.expectedException.expect(IllegalArgumentException.class); this.expectedException.expectMessage("engine"); AmazonElastiCache amazonElastiCache = mock(AmazonElastiCacheClient.class); DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(amazonElastiCache.describeCacheClusters(memcached)). thenReturn(new DescribeCacheClustersResult().withCacheClusters(new CacheCluster().withEngine("redis").withCacheNodes(new CacheNode(). withEndpoint(new Endpoint().withAddress("localhost").withPort(45678))))); ElastiCacheFactoryBean elastiCacheFactoryBean = new ElastiCacheFactoryBean(amazonElastiCache, "memcached", Collections.singletonList(new TestCacheFactory("testCache", "localhost", 45678))); // Act elastiCacheFactoryBean.afterPropertiesSet(); // Assert }
/** * Collect data for ElastiCache. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanElastiCache(AwsStats stats, AwsAccount account, Regions region) { if (region == Regions.EU_CENTRAL_1) return; LOG.debug("Scan for ElastiCache in region " + region.getName() + " in account " + account.getAccountId()); /* * Amazon ElastiCache */ try { AmazonElastiCache elastiCache = new AmazonElastiCacheClient(account.getCredentials()); elastiCache.setRegion(Region.getRegion(region)); List<CacheCluster> list = elastiCache.describeCacheClusters().getCacheClusters(); int totalItems = list.size(); for (CacheCluster cluster : list) { AwsResource res = new AwsResource(cluster.getCacheClusterId(), account.getAccountId(), AwsResourceType.ElastiCache, region); res.addInfo("Engine", cluster.getEngine()); res.addInfo("EngineVersion", cluster.getEngineVersion()); res.addInfo("NumCacheNodes", cluster.getNumCacheNodes()); stats.add(res); } LOG.info(totalItems + " ElastiCache in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of ElastiCache: " + ase.getMessage()); } }
private static Endpoint getEndpointForCache(CacheCluster cacheCluster) { if (cacheCluster.getConfigurationEndpoint() != null) { return cacheCluster.getConfigurationEndpoint(); } if (!cacheCluster.getCacheNodes().isEmpty()) { return cacheCluster.getCacheNodes().get(0).getEndpoint(); } throw new IllegalArgumentException("No Configuration Endpoint or Cache Node available to " + "receive address information for cluster:'" + cacheCluster.getCacheClusterId() + "'"); }
@Test public void parseInternal_cacheConfigWithExpiration_returnsConfiguredCacheThatRespectExpiration() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-cacheConfigWithExpiration.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("memcached"); cache.put("foo", "bar"); Thread.sleep(2000); //Assert assertNotNull(cacheManager); assertNotNull(cache); assertNull(cache.get("foo")); }
@Test public void parseInternal_clusterCacheConfiguration_returnsConfiguredClusterCache() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfig.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("memcached"); cache.put("foo", "bar"); cache.evict("foo"); //Assert assertNotNull(cacheManager); assertNotNull(cache); }
@Test public void parseInternal_clusterCacheConfigurationWithCustomElastiCacheClient_returnsConfigurationWithCustomClient() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigWithCustomElastiCacheClient.xml", getClass())); AmazonElastiCache amazonElastiCacheMock = beanFactory.getBean("customClient", AmazonElastiCache.class); DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(amazonElastiCacheMock.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("memcached"); cache.put("foo", "bar"); cache.evict("foo"); //Assert assertNotNull(cacheManager); assertNotNull(cache); }
public ElasticacheCluster(CacheCluster cacheCluster) { this.cacheClusterId = cacheCluster.getCacheClusterId(); this.clientDownloadLandingPage = cacheCluster.getClientDownloadLandingPage(); this.cacheNodeType = cacheCluster.getCacheNodeType(); this.engine = cacheCluster.getEngine(); this.engineVersion = cacheCluster.getEngineVersion(); this.cacheClusterStatus = cacheCluster.getCacheClusterStatus(); this.numCacheNodes = cacheCluster.getNumCacheNodes(); this.preferredAvailabilityZone = cacheCluster.getPreferredAvailabilityZone(); this.cacheClusterCreateTime = cacheCluster.getCacheClusterCreateTime(); this.preferredMaintenanceWindow = cacheCluster.getPreferredMaintenanceWindow(); this.pendingModifiedValues = cacheCluster.getPendingModifiedValues().toString(); if (cacheCluster.getNotificationConfiguration() != null) { this.notificationConfiguration = cacheCluster.getNotificationConfiguration().toString(); } else { this.notificationConfiguration = "empty"; } this.cacheSecurityGroups = cacheCluster.getSecurityGroups().toString(); this.cacheParameterGroup = cacheCluster.getCacheParameterGroup().toString(); this.cacheSubnetGroupName = cacheCluster.getCacheSubnetGroupName(); this.cacheNodes = cacheCluster.getCacheNodes().toString(); this.autoMinorVersionUpgrade = cacheCluster.getAutoMinorVersionUpgrade(); this.securityGroups = cacheCluster.getSecurityGroups().toString(); this.replicationGroupId = cacheCluster.getReplicationGroupId(); this.snapshotRetentionLimit = cacheCluster.getSnapshotRetentionLimit(); this.snapshotWindow = cacheCluster.getSnapshotWindow(); }
@Test public void parseInternal_mixedCacheConfig_returnsBothCaches() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); BeanDefinitionBuilder cacheBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); cacheBuilder.setFactoryMethod("mock"); cacheBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), cacheBuilder.getBeanDefinition()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-mixedCacheConfig.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); //Replay invocation that will be called DescribeCacheClustersRequest memcached1 = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached1.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached1)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); Cache cache = beanFactory.getBean("memc", Cache.class); when(cache.getName()).thenReturn("memc"); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache memc = cacheManager.getCache("memc"); Cache memcached = cacheManager.getCache("memcached"); //Assert assertNotNull(cacheManager); assertNotNull(memcached); memc.put("foo", "bar"); memc.evict("foo"); memcached.put("foo", "bar"); memcached.evict("foo"); }
@Test public void parseInternal_clusterCacheConfigurationWithLogicalName_returnsConfiguredClusterCacheWithPhysicalName() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); //Register a mock object which will be used to replay service calls BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); beanDefinitionBuilder.setFactoryMethod("mock"); beanDefinitionBuilder.addConstructorArgValue(AmazonElastiCache.class); beanFactory.registerBeanDefinition(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), beanDefinitionBuilder.getBeanDefinition()); BeanDefinitionBuilder resourceIdBuilder = BeanDefinitionBuilder.rootBeanDefinition(Mockito.class); resourceIdBuilder.setFactoryMethod("mock"); resourceIdBuilder.addConstructorArgValue(ResourceIdResolver.class); beanFactory.registerBeanDefinition(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, resourceIdBuilder.getBeanDefinition()); //Load xml file XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-elastiCacheConfigStackConfigured.xml", getClass())); AmazonElastiCache client = beanFactory.getBean(AmazonWebserviceClientConfigurationUtils.getBeanName(AmazonElastiCacheClient.class.getName()), AmazonElastiCache.class); ResourceIdResolver resourceIdResolver = beanFactory.getBean(GlobalBeanDefinitionUtils.RESOURCE_ID_RESOLVER_BEAN_NAME, ResourceIdResolver.class); when(resourceIdResolver.resolveToPhysicalResourceId("testMemcached")).thenReturn("memcached"); //Replay invocation that will be called DescribeCacheClustersRequest memcached = new DescribeCacheClustersRequest().withCacheClusterId("memcached"); memcached.setShowCacheNodeInfo(true); when(client.describeCacheClusters(memcached)).thenReturn( new DescribeCacheClustersResult().withCacheClusters( new CacheCluster().withCacheClusterId("memcached"). withConfigurationEndpoint(new Endpoint().withAddress("localhost").withPort(Integer.parseInt(System.getProperty("memcachedPort")))). withCacheClusterStatus("available").withEngine("memcached") ) ); //Act CacheManager cacheManager = beanFactory.getBean(CacheManager.class); Cache cache = cacheManager.getCache("testMemcached"); cache.put("foo", "bar"); cache.evict("foo"); //Assert assertNotNull(cacheManager); assertNotNull(cache); }