Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { retryWhen, concatMap } from 'rxjs/operators';
import { defer, throwError, iif, timer } from 'rxjs';
import * as legacyElasticsearch from 'elasticsearch';
import { CallAPIOptions } from '.';
import { Logger } from '../logging';
const esErrors = legacyElasticsearch.errors;
/**
* Retries the provided Elasticsearch API call when an error such as
* `AuthenticationException` `NoConnections`, `ConnectionFault`,
* `ServiceUnavailable` or `RequestTimeout` are encountered. The API call will
* be retried once a second, indefinitely, until a successful response or a
* different error is received.
*
* @param apiCaller
*/
// TODO: Replace with APICaller from './scoped_cluster_client' once #46668 is merged
export function migrationsRetryCallCluster(
apiCaller: (
endpoint: string,
clientParams: Record,
test('generic elasticsearch error', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.Generic('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 500, 'Internal Server Error' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
test('request timeout', function(t) {
var res = { body: { geocoding: {
errors: [ new es.errors.RequestTimeout('an error') ]
}}};
res.status = function( code ){
return { json: function( body ){
t.equal( code, 502, 'Bad Gateway' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};
middleware(null, res);
});
};
Client.prototype.get = function (params = {}, source = {}) {
if (params === elasticsearch.errors.NotFound) return elasticsearch.errors.NotFound;
const _source = Object.assign({
jobtype: 'jobtype',
created_by: false,
payload: {
id: 'sample-job-1',
now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)'
},
priority: 10,
timeout: 10000,
created_at: '2016-04-25T21:13:04.738Z',
attempts: 0,
max_attempts: 3,
status: 'pending'
}, source);
import elasticsearch from 'elasticsearch';
import expect from 'expect.js';
import sinon from 'sinon';
import { SavedObjectsClient } from '../saved_objects_client';
const { BadRequest } = elasticsearch.errors;
describe('SavedObjectsClient', () => {
let callAdminCluster;
let savedObjectsClient;
const illegalArgumentException = { type: 'type_missing_exception' };
describe('mapping', () => {
beforeEach(() => {
callAdminCluster = sinon.stub();
savedObjectsClient = new SavedObjectsClient('.kibana-test', {}, callAdminCluster);
});
afterEach(() => {
callAdminCluster.reset();
});
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Promise from 'bluebird';
import sinon from 'sinon';
import expect from '@kbn/expect';
const NoConnections = require('elasticsearch').errors.NoConnections;
import healthCheck from '../health_check';
import kibanaVersion from '../kibana_version';
const esPort = 9220;
describe('plugins/elasticsearch', () => {
describe('lib/health_check', function () {
let health;
let plugin;
let cluster;
let server;
const sandbox = sinon.createSandbox();
function getTimerCount() {
return Object.keys(sandbox.clock.timers || {}).length;
}
if (endpoint === 'index') {
const shardCount = 2;
return Promise.resolve({
_index: params.index || 'index',
_id: params.id || uniqueId('testDoc'),
_seq_no: 1,
_primary_term: 1,
_shards: { total: shardCount, successful: shardCount, failed: 0 },
created: true
});
}
if (endpoint === 'get') {
if (params === legacyElasticsearch.errors.NotFound) return legacyElasticsearch.errors.NotFound;
const _source = {
jobtype: 'jobtype',
created_by: false,
payload: {
id: 'sample-job-1',
now: 'Mon Apr 25 2016 14:13:04 GMT-0700 (MST)'
},
priority: 10,
timeout: 10000,
created_at: '2016-04-25T21:13:04.738Z',
attempts: 0,
max_attempts: 3,
status: 'pending',
function isTimeoutError(error) {
return error instanceof es.errors.RequestTimeout;
}
formatESError(error) {
let
kuzzleError,
messageReplaced,
message = error.message || '';
if (error instanceof KuzzleError) {
return error;
}
if (error instanceof es.errors.NoConnections) {
return new ServiceUnavailableError('Elasticsearch service is not connected');
}
messageReplaced = errorMessagesMapping.some(mapping => {
message = message.replace(mapping.regex, mapping.replacement);
return message !== error.message;
});
switch (error.displayName) {
case 'BadRequest':
if (!messageReplaced) {
if (error.body && error.body.error) {
message = error.body.error.root_cause ? error.body.error.root_cause[0].reason : error.body.error.reason;
}
debug('unhandled "BadRequest" elasticsearch error: %a', error);
var Promise = require('bluebird');
var elasticsearch = require('elasticsearch');
var exposeClient = require('./expose_client');
var migrateConfig = require('./migrate_config');
var createKibanaIndex = require('./create_kibana_index');
var checkEsVersion = require('./check_es_version');
var NoConnections = elasticsearch.errors.NoConnections;
var util = require('util');
var format = util.format;
module.exports = function (plugin, server) {
var config = server.config();
var client = server.plugins.elasticsearch.client;
plugin.status.yellow('Waiting for Elasticsearch');
function waitForPong() {
return client.ping({ requestTimeout: 1500 }).catch(function (err) {
if (!(err instanceof NoConnections)) throw err;
plugin.status.red(format('Unable to connect to Elasticsearch at %s. Retrying in 2.5 seconds.', config.get('elasticsearch.url')));
return Promise.delay(2500).then(waitForPong);