@PostConstruct protected void postConstruct() { DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest().withDomainNames(getDomainName()); List<DomainStatus> list = amazonCloudSearch.describeDomains(describeDomainsRequest).getDomainStatusList(); if (list.isEmpty()) { throw new InternalServerErrorException("Could not find CloudSearch domain: " + getDomainName()); } ServiceEndpoint searchService = list.get(0).getSearchService(); if (searchService == null || searchService.getEndpoint() == null) { throw new InternalServerErrorException("Could not find SearchService for: " + getDomainName()); } domain = createDomain(); domain.setEndpoint(searchService.getEndpoint()); }
@Test public void testThrowsExecptionWhenSearchServiceDoesNotExist() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint())))); try { // TODO suppress exception stacktrace getService(ModelIndexer.class); } catch (MultiException e) { assertEquals("Could not find SearchService for: test-model", e.getCause().getMessage()); return; } fail("Was expection an exception"); }
@Test public void testSearchCenter() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); HashMap<String, List<String>> map = Maps.newHashMap(); map.put("property", Lists.newArrayList("value")); SearchResult expected = new SearchResult().withHits(new Hits().withHit(new Hit().withFields(map))); ArgumentCaptor<SearchRequest> requestCaptor = ArgumentCaptor.forClass(SearchRequest.class); when(domain.search(requestCaptor.capture())).thenReturn(expected); List<ObjectNode> result = getService(ModelIndexer.class).searchCenter("0,0"); SearchRequest request = requestCaptor.getValue(); assertEquals("value", result.get(0).get("property").asText()); assertEquals("latlon:['0.1,-0.1','-0.1,0.1']", request.getQuery()); assertEquals("{\"distance\":\"haversin(0.0,0.0,latlon.latitude,latlon.longitude)\"}", request.getExpr()); assertEquals("distance asc", request.getSort()); assertEquals(Long.valueOf(30L), request.getSize()); }
@Bean public Map<String, AmazonCloudSearchDomainAsyncClient> cloudSearchDomainAsyncClients( AmazonCloudSearchClient cloudSearchClient, AWSCredentialsProvider awsCredentialsProvider) { DescribeDomainsResult describeDomainsResult = cloudSearchClient.describeDomains(); List<DomainStatus> domainStatusList = describeDomainsResult.getDomainStatusList(); Map<String, AmazonCloudSearchDomainAsyncClient> domainClients = new HashMap<>(domainStatusList.size()); for (DomainStatus domainStatus : domainStatusList) { log.debug("domainStatus: {}", domainStatus); String domainName = domainStatus.getDomainName(); if (domainStatus.isCreated() && !domainStatus.isDeleted()) { log.info("creating AmazonCloudSearchDomainClient for {} domain", domainName); ServiceEndpoint serviceEndpoint = domainStatus.getDocService(); AmazonCloudSearchDomainAsyncClient domainClient = new AmazonCloudSearchDomainAsyncClient( awsCredentialsProvider, client) .withEndpoint(serviceEndpoint.getEndpoint()); domainClients.put(domainName, domainClient); } else { log.info("skipping domain {}: created = {}, deleted = {}", domainName, domainStatus.isCreated(), domainStatus.isDeleted()); } } return domainClients; }
public CloudSearchIndexer(AWSCredentialsProvider creds, String index) { // Find the Cloud Search Domain endpoint AmazonCloudSearchClient cloudsearch = new AmazonCloudSearchClient(creds); for (DomainStatus domain : cloudsearch.describeDomains().getDomainStatusList()) { Logger.Info(domain.getDomainName()); if (domain.getDomainName().equals(index)) searchClient = new AmazonCloudSearchDomainClient(creds) .withEndpoint(domain.getDocService().getEndpoint()); } if (searchClient == null) { Logger.Info("Could not find Cloud Search index %s, aborting.", index); throw new IllegalArgumentException("Unrecognized index."); } }
@Test public void testCreateDomain() { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); AmazonCloudSearchDomain domain = getService(ModelIndexer2.class).createDomain(); assertNotNull(domain); assertTrue(domain instanceof AmazonCloudSearchDomain); }
/** * Collect data for CloudSearch. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanCloudSearch(AwsStats stats, AwsAccount account, Regions region) { LOG.debug("Scan for CloudSearch in region " + region.getName() + " in account " + account.getAccountId()); try { AmazonCloudSearchClient cs = new AmazonCloudSearchClient(account.getCredentials()); cs.setRegion(Region.getRegion(region)); int totalDomains = 0; for (DomainStatus ds : cs.describeDomains().getDomainStatusList()) { AwsResource res = new AwsResource(ds.getDomainName(), account.getAccountId(), AwsResourceType.CloudSearch, region); res.addInfo("Endpoint", ds.getSearchService().getEndpoint()); res.addInfo("SearchInstanceType", ds.getSearchInstanceType()); res.addInfo("SearchInstanceCount", ds.getSearchInstanceCount()); res.addInfo("ARN", ds.getARN()); stats.add(res); totalDomains++; } LOG.info(totalDomains + " CloudSearch domains in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { if (ase.getErrorCode().contains("AccessDenied")) { LOG.info("Access denied for CloudSearch in region " + region.getName() + " in account " + account.getAccountId()); } else { LOG.error("Exception of CloudSearch: " + ase.getMessage()); } } catch (Exception ex) { LOG.error("Exception of CloudSearch: " + ex.getMessage()); } }
private void cacheDomainEndpoints() { if (cloudSearchClient != null && !domainEndpointsCached) { final Set<String> managedDomains = new HashSet<>(); for (final DocumentConfiguration documentConfiguration : documentConfigurations.values()) { final String domainName = documentConfigurationHolder.schemaName() + "-" + documentConfiguration.namespace(); managedDomains.add(domainName); } final DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest(); describeDomainsRequest.setDomainNames(managedDomains); final DescribeDomainsResult describeDomainsResult = cloudSearchClient .describeDomains(describeDomainsRequest); final List<DomainStatus> domainStatusList = describeDomainsResult.getDomainStatusList(); if (domainStatusList.size() != managedDomains.size()) { logger.info("Unable to cache CloudSearch document/search endpoints for: " + managedDomains); } else { for (final DomainStatus domainStatus : domainStatusList) { if (domainStatus.isCreated() && !domainStatus.isDeleted()) { final String documentServiceEndpoint = domainStatus.getDocService().getEndpoint(); final String searchServiceEndpoint = domainStatus.getSearchService().getEndpoint(); if (documentServiceEndpoint == null || searchServiceEndpoint == null) { domainEndpointsCached = false; return; } final AmazonCloudSearchDomain documentServiceClient = AmazonCloudSearchDomainClientBuilder .build(documentServiceEndpoint); final AmazonCloudSearchDomain searchServiceClient = AmazonCloudSearchDomainClientBuilder .build(searchServiceEndpoint); documentServiceClients.put(domainStatus.getDomainName(), documentServiceClient); searchServiceClients.put(domainStatus.getDomainName(), searchServiceClient); } } domainEndpointsCached = true; } } }
private DescribeDomainsResult getDescribeDomainsResult(final String domainName, final String documentServiceEndpoint, final String searchServiceEndpoint) { return new DescribeDomainsResult() .withDomainStatusList(new DomainStatus().withDomainName(domainName).withCreated(true).withDeleted(false) .withDocService(new ServiceEndpoint().withEndpoint(documentServiceEndpoint)) .withSearchService(new ServiceEndpoint().withEndpoint(searchServiceEndpoint))); }
@Override public void open(JobConf job, String name) throws IOException { LOG.debug("CloudSearchIndexWriter.open() name={} ", name); maxDocsInBatch = job.getInt(CloudSearchConstants.MAX_DOCS_BATCH, -1); buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('['); dumpBatchFilesToTemp = job.getBoolean(CloudSearchConstants.BATCH_DUMP, false); if (dumpBatchFilesToTemp) { // only dumping to local file // no more config required return; } String endpoint = job.get(CloudSearchConstants.ENDPOINT); if (StringUtils.isBlank(endpoint)) { throw new RuntimeException("endpoint not set for CloudSearch"); } AmazonCloudSearchClient cl = new AmazonCloudSearchClient(); if (StringUtils.isNotBlank(regionName)) { cl.setRegion(RegionUtils.getRegion(regionName)); } String domainName = null; // retrieve the domain name DescribeDomainsResult domains = cl .describeDomains(new DescribeDomainsRequest()); Iterator<DomainStatus> dsiter = domains.getDomainStatusList().iterator(); while (dsiter.hasNext()) { DomainStatus ds = dsiter.next(); if (ds.getDocService().getEndpoint().equals(endpoint)) { domainName = ds.getDomainName(); break; } } // check domain name if (StringUtils.isBlank(domainName)) { throw new RuntimeException( "No domain name found for CloudSearch endpoint"); } DescribeIndexFieldsResult indexDescription = cl.describeIndexFields( new DescribeIndexFieldsRequest().withDomainName(domainName)); for (IndexFieldStatus ifs : indexDescription.getIndexFields()) { String indexname = ifs.getOptions().getIndexFieldName(); String indextype = ifs.getOptions().getIndexFieldType(); LOG.info("CloudSearch index name {} of type {}", indexname, indextype); csfields.put(indexname, indextype); } client = new AmazonCloudSearchDomainClient(); client.setEndpoint(endpoint); }
@Test public void testIndexJsonProcessingException() throws Exception { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); Model model = new Model(); model.setGuid("00000000-0000-0000-0000-000000000000"); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); getService(ModelIndexer.class).index(Lists.newArrayList(model)); verify(executor).submit(runnableCaptor.capture()); when(objectMapper.writeValueAsString(any())).thenThrow(new StubJsonProcessingException()); runnableCaptor.getValue().run(); verify(domain, never()).uploadDocuments(any()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void prepare(Map conf, TopologyContext context, OutputCollector collector) { super.prepare(conf, context, collector); _collector = collector; this.eventCounter = context.registerMetric("CloudSearchIndexer", new MultiCountMetric(), 10); maxTimeBuffered = ConfUtils.getInt(conf, CloudSearchConstants.MAX_TIME_BUFFERED, 10); maxDocsInBatch = ConfUtils.getInt(conf, CloudSearchConstants.MAX_DOCS_BATCH, -1); buffer = new StringBuffer(MAX_SIZE_BATCH_BYTES).append('['); dumpBatchFilesToTemp = ConfUtils.getBoolean(conf, "cloudsearch.batch.dump", false); if (dumpBatchFilesToTemp) { // only dumping to local file // no more config required return; } String endpoint = ConfUtils.getString(conf, "cloudsearch.endpoint"); if (StringUtils.isBlank(endpoint)) { String message = "Missing CloudSearch endpoint"; LOG.error(message); throw new RuntimeException(message); } String regionName = ConfUtils.getString(conf, CloudSearchConstants.REGION); AmazonCloudSearchClient cl = new AmazonCloudSearchClient(); if (StringUtils.isNotBlank(regionName)) { cl.setRegion(RegionUtils.getRegion(regionName)); } String domainName = null; // retrieve the domain name DescribeDomainsResult domains = cl .describeDomains(new DescribeDomainsRequest()); Iterator<DomainStatus> dsiter = domains.getDomainStatusList() .iterator(); while (dsiter.hasNext()) { DomainStatus ds = dsiter.next(); if (ds.getDocService().getEndpoint().equals(endpoint)) { domainName = ds.getDomainName(); break; } } // check domain name if (StringUtils.isBlank(domainName)) { throw new RuntimeException( "No domain name found for CloudSearch endpoint"); } DescribeIndexFieldsResult indexDescription = cl .describeIndexFields(new DescribeIndexFieldsRequest() .withDomainName(domainName)); for (IndexFieldStatus ifs : indexDescription.getIndexFields()) { String indexname = ifs.getOptions().getIndexFieldName(); String indextype = ifs.getOptions().getIndexFieldType(); LOG.info("CloudSearch index name {} of type {}", indexname, indextype); csfields.put(indexname, indextype); } client = new AmazonCloudSearchDomainClient(); client.setEndpoint(endpoint); }
@Test public void testIndex() throws Exception { when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult() .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost"))))); objectMapper = new ObjectMapper(); Model model = new Model(); model.setGuid("00000000-0000-0000-0000-000000000000"); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); getService(ModelIndexer.class).index(model); verify(executor).submit(runnableCaptor.capture()); runnableCaptor.getValue().run(); ArgumentCaptor<UploadDocumentsRequest> requestCaptor = ArgumentCaptor.forClass(UploadDocumentsRequest.class); verify(domain).uploadDocuments(requestCaptor.capture()); Scanner scanner = new Scanner(requestCaptor.getValue().getDocuments()); JsonNode json = new ObjectMapper().readTree(scanner.useDelimiter("\\A").next()).get(0); assertEquals("add", json.get("type").asText()); assertEquals("00000000-0000-0000-0000-000000000000", json.get("id").asText()); assertEquals("00000000-0000-0000-0000-000000000000", json.get("fields").get("guid").asText()); scanner.close(); }