Skip to content

Commit

Permalink
feat(auto_discover): Add option to quit on discovery errors
Browse files Browse the repository at this point in the history
This adds a new config option, `api.targets.auto_discover_required`. If set to true, the API will quit with a fatal error if there is any problem performing the type mapping discovery.

In practice what this means is a working Elasticsearch cluster with a valid Pelias index must exist and be properly configured in `pelias.json`, or the API won't start.

This can actually be quite helpful in production environments as it can protect against misconfiguration issues such as using the wrong URL to the Elasticsearch cluster.

Because the check is only performed once at startup, it won't cause the API to quit if there's an intermittent network issue or something similar later on.

Connects #1591
Connects pelias/pelias#925
  • Loading branch information
orangejulius committed Jul 4, 2022
1 parent 5272f25 commit ade4de6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The API recognizes the following properties under the top-level `api` key in you
|`services`|*no*||Service definitions for [point-in-polygon](https://github.com/pelias/pip-service), [libpostal](https://github.com/whosonfirst/go-whosonfirst-libpostal), [placeholder](https://github.com/pelias/placeholder), and [interpolation](https://github.com/pelias/interpolation) services. For a description of when different Pelias services are recommended or required, see our [services documentation](https://github.com/pelias/documentation/blob/master/services.md).|
|`defaultParameters.focus.point.lon` <br> `defaultParameters.focus.point.lat`|no | |default coordinates for focus point
|`targets.auto_discover`|no|true|Should `sources` and `layers` be automatically discovered by querying Elasticsearch at process startup. (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below).
|`targets.auto_discover_required`|no|false|If set to `true`, type mapping discovery failures will result in a fatal error. This means a valid connection to a working Elasticsearch cluster with a Pelias index is _required_ on startup. Setting this to true can help prevent issues such as incorrect configuration in production environments
|`targets.layers_by_source` <br> `targets.source_aliases` <br> `targets.layer_aliases`|no | |custom values for which `sources` and `layers` the API accepts (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below). We recommend using the `targets.auto_discover:true` configuration instead of setting these manually.
|`customBoosts` | no | `{}` | Allows configuring boosts for specific sources and layers, in order to influence result order. See [Configurable Boosts](#custom-boosts) below for details |
|`autocomplete.exclude_address_length` | no | 0 | As a performance optimization, this optional filter excludes results from the 'address' layer for any queries where the character length of the 'subject' portion of the parsed_text is equal to or less than this many characters in length. Addresses are usually the bulk of the records in Elasticsearch, and searching across all of them for very short text inputs can be slow, with little benefit. Consider setting this to 1 or 2 if you have several million addresses in Pelias. |
Expand Down
19 changes: 15 additions & 4 deletions helper/TypeMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,14 @@ TypeMapping.prototype.loadTargets = function( targetsBlock ){

// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.load = function( done ){
// load targets from config file
this.loadFromConfig();

// load pelias config
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);

// load targets from config file
this.loadTargets( peliasConfigTargets );

// do not load values from elasticsearch
if( true !== peliasConfigTargets.auto_discover ){
if( 'function' === typeof done ){ done(); }
Expand All @@ -146,6 +144,19 @@ TypeMapping.prototype.load = function( done ){
loadFromElasticsearch(this, done);
};

// load values from either pelias config file or from elasticsearch
TypeMapping.prototype.loadFromConfig = function( done ){

// load pelias config
const peliasConfigTargets = _.get(
require('pelias-config').generate(require('../schema')),
'api.targets', {}
);

// load targets from config file
this.loadTargets( peliasConfigTargets );
};

// replace the contents of an object or array
// while maintaining the original pointer reference
function safeReplace(reference, replacement){
Expand Down
7 changes: 5 additions & 2 deletions helper/type_mapping.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const TypeMapping = require('./TypeMapping');

// instantiate a new type mapping
// instantiate a singleton type mapping
// loading normal defaults from pelias-config happens now
// updating that config from Elasticsearch happens later
// before the webserver is started
var tm = new TypeMapping();
tm.load();
tm.loadFromConfig();

// export singleton
module.exports = tm;
8 changes: 7 additions & 1 deletion helper/type_mapping_discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ module.exports = (tm, done) => {
}

// query error
if( err ){ logger.error( err ); }
if( err ){
logger.error( err );

if (peliasConfig.get('api.targets.auto_discover_required') === true) {
process.exit(1);
}
}

// invalid response
else if ( totalHits < 1 ){
Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const logger = require('pelias-logger').get('api');
const type_mapping = require('./helper/type_mapping');

const app = require('./app'),
port = ( process.env.PORT || 3100 ),
host = ( process.env.HOST || undefined );

const server = app.listen( port, host, () => {
// ask server for the actual address and port its listening on
const listenAddress = server.address();
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
let server;

// load Elasticsearch type mappings before starting web server
type_mapping.load(() => {
server = app.listen( port, host, () => {
// ask server for the actual address and port its listening on
const listenAddress = server.address();
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
});
});

function exitHandler() {
Expand Down

0 comments on commit ade4de6

Please sign in to comment.