Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client modifies configuration object such that it cannot be used again #33

Closed
jvonniedapn opened this issue Jan 24, 2014 · 11 comments
Closed

Comments

@jvonniedapn
Copy link
Contributor

When passing a config object to new elasticsearch.Client(), the Client modifies the passed in config object in such a way that it cannot be used to create a second client.

Example:

var elasticsearch = require('elasticsearch');

var config = {
    log : "warning"
};

console.dir(config);
var es1 = new elasticsearch.Client(config);
console.dir(config);
var es2 = new elasticsearch.Client(config);
> node es-config-bug.js 
{ log: 'warning' }
{ log: 
   { _events: 
      { closing: [Object],
        error: [Function: bound],
        warning: [Function: bound] } },
  host: 'http://localhost:9200',
  hosts: 'http://localhost:9200',
  maxSockets: 10,
  maxKeepAliveRequests: 0,
  maxKeepAliveTime: 300000 }

/Users/jvonnieda/Desktop/test/node_modules/elasticsearch/src/lib/log.js:44
      throw new TypeError('Invalid logging output config. Expected either a lo
            ^
TypeError: Invalid logging output config. Expected either a log level, array of log levels, a logger config object, or an array of logger config objects.
    at new Log (/Users/jvonnieda/Desktop/test/node_modules/elasticsearch/src/lib/log.js:44:13)
    at new Transport (/Users/jvonnieda/Desktop/test/node_modules/elasticsearch/src/lib/transport.js:17:27)
    at Object.EsApiClient (/Users/jvonnieda/Desktop/test/node_modules/elasticsearch/src/lib/client.js:49:22)
    at new Client (/Users/jvonnieda/Desktop/test/node_modules/elasticsearch/src/lib/client.js:60:10)
    at Object.<anonymous> (/Users/jvonnieda/Desktop/test/es-config-bug.js:10:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
@spalger
Copy link
Contributor

spalger commented Jan 24, 2014

I agree that this is bad. Working on a fix

@jvonniedapn
Copy link
Contributor Author

Sweet, thanks!

@spalger
Copy link
Contributor

spalger commented Jan 26, 2014

Ok, started implementing this but I've decided against it. It sounds like a good idea on the surface but the configuration is very flexible and can regularly contain objects that shouldn't be cloned (loggers, transports, etc.). The specific objects that shouldn't be cloned couldn't be defined either, since it is completely legal for users to override certain classes and therefore the config they require. Because of this, it makes more sense to say that the config object belongs to the Client once it is sent in. I'd suggest making your config in a function and then passing the result into the client constructor:

function config() {
  return {
    log: "warning"
  };
}

var es1 = new elasticsearch.Client(config());
var es2 = new elasticsearch.Client(config());

@gabegorelick
Copy link

The docs should probably highlight this.

@spalger
Copy link
Contributor

spalger commented Mar 29, 2014

@gabegorelick you got it. I added a warning to the top of the configuration section. http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/configuration.html

@gabegorelick
Copy link

Awesome. Hopefully you won't get any more bug reports about it now.

On Saturday, March 29, 2014, spenceralger notifications@github.com wrote:

@gabegorelick https://github.com/gabegorelick you got it. I added a
warning to the top of the configuration section.
http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/configuration.html

Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-39013086
.

@julien51
Copy link

IMHO, the library itself should duplicate the object passed when initializing to avoid the hassle of doing so to the implementer.

@spalger
Copy link
Contributor

spalger commented Jan 15, 2015

@julien51 I had that opinion originally, but as stated earlier it's not really an option.

cdituri added a commit to cdituri/elasticsearch-js that referenced this issue Jun 14, 2017
…nt creation

see elastic#33 (comment)

Signed-off-by: Chris Dituri <csdituri@gmail.com>
bot-linagora pushed a commit to linagora/openpaas-esn that referenced this issue Sep 6, 2017
cclient added a commit to cclient/ts-crontab that referenced this issue Dec 6, 2017
pzhine pushed a commit to pzhine/graphql-optics that referenced this issue Apr 24, 2018
kantord pushed a commit to kiwicom/graphql-optics that referenced this issue Apr 25, 2018
* tracer package

* configure package and webpack for distribution

* separate logEntry and formatEntry; send entry to elastic

* shallow copy elastic.Client options because  elastic/elasticsearch-js#33

* add API docs to readme

* adjust readme

* Update README.md

* configure frontend to work with trace data in elasticsearch

* add circleci config

* Update README.md

* move elastic call to componentDidMount

* move jest.json to root and only call test once in circle

* remove fast-async

* run packages/tracer/yarn install in circle

* try different syntax for circle deps
kantord pushed a commit to kiwicom/graphql-optics that referenced this issue Apr 26, 2018
* tracer package

* configure package and webpack for distribution

* separate logEntry and formatEntry; send entry to elastic

* shallow copy elastic.Client options because  elastic/elasticsearch-js#33

* add API docs to readme

* adjust readme

* Update README.md

* configure frontend to work with trace data in elasticsearch

* add circleci config

* Update README.md

* move elastic call to componentDidMount

* move jest.json to root and only call test once in circle

* remove fast-async

* run packages/tracer/yarn install in circle

* try different syntax for circle deps

* Add basic functional analytics frontend

* Remove superfluous curly braces from App.js
kantord pushed a commit to kiwicom/graphql-optics that referenced this issue Apr 26, 2018
* tracer package

* configure package and webpack for distribution

* separate logEntry and formatEntry; send entry to elastic

* shallow copy elastic.Client options because  elastic/elasticsearch-js#33

* add API docs to readme

* adjust readme

* Update README.md

* configure frontend to work with trace data in elasticsearch

* add circleci config

* Update README.md

* move elastic call to componentDidMount

* move jest.json to root and only call test once in circle

* remove fast-async

* run packages/tracer/yarn install in circle

* try different syntax for circle deps

* Add basic functional analytics frontend

* Remove superfluous curly braces from App.js

* Swap metrics and query columns
kantord pushed a commit to kiwicom/graphql-optics that referenced this issue Apr 26, 2018
* tracer package

* configure package and webpack for distribution

* separate logEntry and formatEntry; send entry to elastic

* shallow copy elastic.Client options because  elastic/elasticsearch-js#33

* add API docs to readme

* adjust readme

* Update README.md

* configure frontend to work with trace data in elasticsearch

* add circleci config

* Update README.md

* move elastic call to componentDidMount

* move jest.json to root and only call test once in circle

* remove fast-async

* run packages/tracer/yarn install in circle

* try different syntax for circle deps

* Add basic functional analytics frontend

* Remove superfluous curly braces from App.js

* Swap metrics and query columns

* Organize and add documentation

* Fix README anchoring/linking

* Fix README linking

* Use  instead of  as default index name
maxts added a commit to maxts/express-cassandra that referenced this issue Sep 19, 2019
Create shallow copy of elasticsearch config to prevent:
Unhandled rejection Error: Do not reuse objects to configure the elasticsearch Client class: elastic/elasticsearch-js#33
    at new Client (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\elasticsearch\src\lib\client.js:38:11)
    at Object.create_es_client (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:111:22)
    at Object._assert_es_index (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:116:25)
    at Object.tryCatcher (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\bluebird\js\release\util.js:16:23)
    at Object.ret [as assertESIndexAsync] (eval at makeNodePromisifiedEval (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:13:39)
    at Object.onUserDefinedAggregates (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:407:37)
    at Object._assert_user_defined_aggregates (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:318:7)
    at Object.f (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:426:14)
    at Object._assert_user_defined_functions (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:253:7)
    at Object.f (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:438:14)
    at Object._assert_user_defined_types (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:201:7)
    at Object.f (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:451:14)
    at C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\express-cassandra\lib\orm\apollo.js:190:9
    at HostConnectionPool.next (C:\Users\mtsaren\Documents\GitHub\facenet\server\node\node_modules\cassandra-driver\lib\utils.js:503:5)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
@j-norwood-young
Copy link

Note that this pattern works:

const config = {
   log: "warning"
}
const Elasticsearch = require("elasticsearch");
const esclient = new Elasticsearch.Client({ ...config });

brunodccarvalho added a commit to NIAEFEUP/nijobs-be that referenced this issue Mar 21, 2020
Some observations: There are two elasticsearch packages in npm worth
looking at, 'elasticsearch' and '@elastic/elasticsearch'. The second
is meant to replace the first, but mongoose-elasticsearch-xp which will
handle the replication of models apparently uses clients from the first.
Further down the line we can test if '@elastic/elasticsearch' and be
used instead.

The elasticsearch client is uncaptured.

Use makeConfig() to prevent future issues:
See elastic/elasticsearch-js#33
@jfspencerJN
Copy link

For anyone doing unit testing in the NestJS context, freezing the config object in the module definition, prevents this error from firing repeatedly. Then the service can be mocked in unit tests.

imports: [ElasticsearchModule.register(Object.freeze({ host: '' }))],

@mstar-kk
Copy link

mstar-kk commented Jan 19, 2021

My solution is:

const elasticsearch = require('elasticsearch');

const clientA = new elasticsearch.Client(R.merge(config.elasticsearch, {}));
const clientB = new elasticsearch.Client(R.merge(config.elasticsearch, {}));

In javascript, object is passed by reference.For the first time construct instance, constructor function will add a variable named __reused to config. This variable will prevent you from using same config twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants