Skip to content

Commit

Permalink
Merge pull request #114 from emartinez-usgs/configure-timeouts
Browse files Browse the repository at this point in the history
Configure timeouts
  • Loading branch information
jmfee-usgs authored May 8, 2020
2 parents a0135ce + 392d643 commit 8175b7e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,31 @@

public class GeoservePlacesService implements GeoservePlaces {
/** Default URL for GeoServe Places service. */
public static final String DEFAULT_GEOSERVE_PLACES_URL = "https://earthquake.usgs.gov/ws/geoserve/places.json";
public static final String DEFAULT_ENDPOINT_URL = "https://earthquake.usgs.gov/ws/geoserve/places.json";
public static final int DEFAULT_CONNECT_TIMEOUT = 300; // ms
public static final int DEFAULT_READ_TIMEOUT = 1700; // ms

/** Configured URL for GeoServe Places service. */
private String endpointUrl;
private int connectTimeout;
private int readTimeout;

public GeoservePlacesService() {
this(DEFAULT_GEOSERVE_PLACES_URL);
this(DEFAULT_ENDPOINT_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
}

public GeoservePlacesService(final String endpointUrl) {
this(endpointUrl, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
}

public GeoservePlacesService(final int connectTimeout, final int readTimeout) {
this(DEFAULT_ENDPOINT_URL, connectTimeout, readTimeout);
}

public GeoservePlacesService(final String endpointUrl, final int connectTimeout, final int readTimeout) {
this.endpointUrl = endpointUrl;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}

/**
Expand Down Expand Up @@ -70,6 +84,10 @@ public String formatEventTitle(JsonObject feature) {
return String.format("%d km %s of %s, %s", distance, direction, name, admin);
}

public int getConnectTimeout() {
return this.connectTimeout;
}

public String getEndpointURL() {
return this.endpointUrl;
}
Expand All @@ -79,7 +97,7 @@ public JsonObject getEventPlaces(BigDecimal latitude, BigDecimal longitude)
final URL url = new URL(String.format("%s?type=event&latitude=%s&longitude=%s", this.endpointUrl,
URLEncoder.encode(latitude.toString(), "UTF-8"), URLEncoder.encode(longitude.toString(), "UTF-8")));

try (InputStream in = StreamUtils.getURLInputStream(url, 250, 250)) {
try (InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout)) {
JsonReader reader = Json.createReader(in);
JsonObject json = reader.readObject();
reader.close();
Expand All @@ -95,9 +113,21 @@ public String getEventTitle(BigDecimal latitude, BigDecimal longitude) throws IO
return this.formatEventTitle(feature);
}

public int getReadTimeout() {
return this.readTimeout;
}

public void setConnectTimeout(final int connectTimeout) {
this.connectTimeout = connectTimeout;
}

public void setEndpointURL(final String endpointUrl) {
this.endpointUrl = endpointUrl;
}

public void setReadTimeout(final int readTimeout) {
this.readTimeout = readTimeout;
}

// TODO as needed, implement full GeoServe places API options
}
69 changes: 29 additions & 40 deletions src/main/java/gov/usgs/earthquake/indexer/DefaultIndexerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
import gov.usgs.earthquake.product.Product;
import gov.usgs.earthquake.qdm.Point;
import gov.usgs.earthquake.qdm.Regions;
import gov.usgs.util.DefaultConfigurable;

import java.math.BigDecimal;
import java.util.logging.Logger;


/**
* Default implementation of the IndexerModule interface,
* implements ANSS Authoritative Region logic.
*
* Default implementation of the IndexerModule interface, implements ANSS
* Authoritative Region logic.
*
* Provides a basic level of support for any type of product. Creates a
* ProductSummary using the ProductSummary(product) constructor, which copies
* all properties, and links from the product.
*/
public class DefaultIndexerModule implements IndexerModule {
public class DefaultIndexerModule extends DefaultConfigurable implements IndexerModule {

private static final Logger LOGGER = Logger
.getLogger(DefaultIndexerModule.class.getName());
private static final Logger LOGGER = Logger.getLogger(DefaultIndexerModule.class.getName());

/** Initial preferred weight. */
public static final long DEFAULT_PREFERRED_WEIGHT = 1;
Expand All @@ -44,25 +43,21 @@ public class DefaultIndexerModule implements IndexerModule {

/**
* Create a ProductSummary from a Product.
*
*
* Uses the ProductSummary(Product) constructor, which copies product
* information. Checks whether product is within its authoritative region,
* and if so boosts preferredWeight by AUTHORITATIVE_WEIGHT.
*
* @param product
* the product to summarize.
* information. Checks whether product is within its authoritative region, and
* if so boosts preferredWeight by AUTHORITATIVE_WEIGHT.
*
* @param product the product to summarize.
* @return ProductSummary for Product object.
*/
public ProductSummary getProductSummary(final Product product)
throws Exception {
public ProductSummary getProductSummary(final Product product) throws Exception {
ProductSummary summary = new ProductSummary(product);

// allow sender to assign preferredWeight if we add them to the keychain
String preferredWeight = product.getProperties().get("preferredWeight");
if (preferredWeight != null
&& signatureVerifier.verifySignature(product)) {
LOGGER.fine("Signature verified, using sender assigned preferredWeight "
+ preferredWeight);
if (preferredWeight != null && signatureVerifier.verifySignature(product)) {
LOGGER.fine("Signature verified, using sender assigned preferredWeight " + preferredWeight);
summary.setPreferredWeight(Long.valueOf(preferredWeight));
} else {
summary.setPreferredWeight(getPreferredWeight(summary));
Expand All @@ -72,24 +67,21 @@ public ProductSummary getProductSummary(final Product product)

/**
* Calculate the preferred weight for a product summary.
*
* This method is called after creating a product summary, but before
* returning the created summary. It's return value is used to assign the
* product summary preferred weight.
*
* Within each type of product, the summary with the largest preferred
* weight is considered preferred.
*
* @param summary
* the summary to calculate a preferred weight.
*
* This method is called after creating a product summary, but before returning
* the created summary. It's return value is used to assign the product summary
* preferred weight.
*
* Within each type of product, the summary with the largest preferred weight is
* considered preferred.
*
* @param summary the summary to calculate a preferred weight.
* @return the absolute preferred weight.
*/
protected long getPreferredWeight(final ProductSummary summary)
throws Exception {
protected long getPreferredWeight(final ProductSummary summary) throws Exception {
Regions regions = ANSSRegionsFactory.getFactory().getRegions();
if (regions == null) {
throw new ContinuableListenerException(
"Unable to load ANSS Authoritative Regions");
throw new ContinuableListenerException("Unable to load ANSS Authoritative Regions");
}

long preferredWeight = DEFAULT_PREFERRED_WEIGHT;
Expand All @@ -100,8 +92,7 @@ protected long getPreferredWeight(final ProductSummary summary)
BigDecimal longitude = summary.getEventLongitude();
Point location = null;
if (latitude != null && longitude != null) {
location = new Point(longitude.doubleValue(),
latitude.doubleValue());
location = new Point(longitude.doubleValue(), latitude.doubleValue());
}

// authoritative check
Expand All @@ -127,8 +118,7 @@ protected long getPreferredWeight(final ProductSummary summary)
/**
* Remove "internal-" prefix and "-scenario" suffix from product type".
*
* @param type
* product type.
* @param type product type.
* @return base product type (without any known prefix or suffix).
*/
public String getBaseProductType(String type) {
Expand All @@ -145,9 +135,8 @@ public String getBaseProductType(String type) {

/**
* This module provides a default level of support for any type of product.
*
* @param product
* the product to test.
*
* @param product the product to test.
* @return IndexerModule.LEVEL_DEFAULT.
*/
public int getSupportLevel(final Product product) {
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/gov/usgs/earthquake/origin/OriginIndexerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,32 @@
import gov.usgs.earthquake.indexer.ProductSummary;
import gov.usgs.earthquake.product.Product;

import gov.usgs.util.Config;

/**
* Class for summarizing "origin" type products during the indexing process.
* Specifically this implementation uses a GeoservePlacesService to augment the
* properties on the product to include a "title" property if one is not already
* present.
*
* This module may be configured with the following properties: `endpointUrl`
* `connectTimeout`, and `readTimeout`.
*/
public class OriginIndexerModule extends DefaultIndexerModule {
private static final Logger LOGGER = Logger.getLogger(OriginIndexerModule.class.getName());

private GeoservePlaces geoservePlaces;

public static final String ENDPOINT_URL_PROPERTY = "endpointUrl";
public static final String CONNECT_TIMEOUT_PROPERTY = "connectTimeout";
public static final String READ_TIMEOUT_PROPERTY = "readTimeout";

public OriginIndexerModule() {
this.geoservePlaces = new GeoservePlacesService();
// Do nothing, must be configured through bootstrapping before use
}

public OriginIndexerModule(final GeoservePlacesService geoservePlaces) {
this.setPlacesService(geoservePlaces);
}

/**
Expand All @@ -42,7 +61,8 @@ public ProductSummary getProductSummary(Product product) throws Exception {
title = this.geoservePlaces.getEventTitle(latitude, longitude);
summaryProperties.put("title", title);
} catch (Exception ex) {
LOGGER.fine(ex.getMessage());
LOGGER
.fine(String.format("[%s] %s for product %s", this.getName(), ex.getMessage(), product.getId().toString()));
// Do nothing, value-added failed. Move on.
}
}
Expand Down Expand Up @@ -71,4 +91,17 @@ public int getSupportLevel(Product product) {
public void setPlacesService(GeoservePlaces geoservePlaces) {
this.geoservePlaces = geoservePlaces;
}

@Override
public void configure(Config config) throws Exception {
String endpointUrl = config.getProperty(ENDPOINT_URL_PROPERTY, GeoservePlacesService.DEFAULT_ENDPOINT_URL);
int connectTimeout = Integer.parseInt(
config.getProperty(CONNECT_TIMEOUT_PROPERTY, Integer.toString(GeoservePlacesService.DEFAULT_CONNECT_TIMEOUT)));
int readTimeout = Integer.parseInt(
config.getProperty(READ_TIMEOUT_PROPERTY, Integer.toString(GeoservePlacesService.DEFAULT_READ_TIMEOUT)));

LOGGER.config(String.format("[%s] GeoservePlacesService(%s, %d, %d)", this.getName(), endpointUrl, connectTimeout,
readTimeout));
this.setPlacesService(new GeoservePlacesService(endpointUrl, connectTimeout, readTimeout));
}
}

0 comments on commit 8175b7e

Please sign in to comment.