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

[6.x] advanced setting to control search request preference (#17271) #17447

Merged
merged 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/core_plugins/kibana/ui_setting_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ export function getUiSettingDefaults() {
'When set to true, filter(s) will be ignored for a visualization ' +
'when the visualization\'s index does not contain the filtering field.'
},
'courier:setRequestPreference': {
value: 'sessionId',
options: ['sessionId', 'custom', 'none'],
type: 'select',
description: 'Allows you to set which shards handle your search requests. ' +
'<ul>' +
'<li><strong>sessionId:</strong> restricts operations to execute all search requests on the same shards. ' +
'This has the benefit of reusing shard caches across requests. ' +
'<li><strong>custom:</strong> allows you to define a your own preference. ' +
'Use <strong>courier:customRequestPreference</strong> to customize your preference value. ' +
'<li><strong>none:</strong> means do not set a preference. ' +
'This might provide better performance because requests can be spread across all shard copies. ' +
'However, results might be inconsistent because different shards might be in different refresh states.' +
'</ul>'
},
'courier:customRequestPreference': {
value: '_local',
type: 'string',
description: '<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html" target="_blank" rel="noopener noreferrer">Request Preference</a> ' +
' used when <strong>courier:setRequestPreference</strong> is set to "custom".'
},
'fields:popularLimit': {
value: 10,
description: 'The top N most popular fields to show',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function requestFetchParamsToBody(
Promise,
timeFilter,
kbnIndex,
sessionId) {
sessionId,
config) {
const indexToListMapping = {};
const timeBounds = timeFilter.getActiveBounds();
const promises = requestsFetchParams.map(function (fetchParams) {
Expand Down Expand Up @@ -68,15 +69,19 @@ export function requestFetchParamsToBody(
index = indexList;
}

return JSON.stringify({
const header = {
index,
type: fetchParams.type,
search_type: fetchParams.search_type,
ignore_unavailable: true,
preference: sessionId,
})
+ '\n'
+ toJson(body, JSON.stringify);
};
if (config.get('courier:setRequestPreference') === 'sessionId') {
header.preference = sessionId;
} else if (config.get('courier:setRequestPreference') === 'custom') {
header.preference = config.get('courier:customRequestPreference');
}

return `${JSON.stringify(header)}\n${toJson(body, JSON.stringify)}`;
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { requestFetchParamsToBody } from './request_fetch_params_to_body';
import _ from 'lodash';

const DEFAULT_SESSION_ID = '1';

function requestFetchParamsToBodyWithDefaults(paramOverrides) {
const paramDefaults = {
requestFetchParams: [],
Expand All @@ -9,7 +11,12 @@ function requestFetchParamsToBodyWithDefaults(paramOverrides) {
getActiveBounds: () => undefined,
},
kbnIndex: '.kibana',
sessionId: '1',
sessionId: DEFAULT_SESSION_ID,
config: {
get: () => {
return 'sessionId';
}
}
};
const params = { ...paramDefaults, ...paramOverrides };

Expand All @@ -19,6 +26,7 @@ function requestFetchParamsToBodyWithDefaults(paramOverrides) {
params.timeFilter,
params.kbnIndex,
params.sessionId,
params.config,
);
}

Expand Down Expand Up @@ -73,10 +81,68 @@ describe('when indexList is empty', () => {
}
];

it('queries the kibana index (.kibana) with a must_not match_all boolean', () => {
test('queries the kibana index (.kibana) with a must_not match_all boolean', () => {
return requestFetchParamsToBodyWithDefaults({ requestFetchParams }).then(value => {
expect(_.includes(value, '"index":[".kibana"]')).toBe(true);
expect(_.includes(value, emptyMustNotQuery)).toBe(true);
});
});
});

describe('headers', () => {

const requestFetchParams = [
{
index: ['logstash-123'],
type: 'blah',
search_type: 'blah2',
body: { foo: 'bar' }
}
];

const getHeader = async (paramOverrides) => {
const request = await requestFetchParamsToBodyWithDefaults(paramOverrides);
const requestParts = request.split('\n');
if (requestParts.length < 2) {
throw new Error('fetch Body does not contain expected format header newline body.');
}
return JSON.parse(requestParts[0]);
};

describe('search request preference', async () => {
test('should be set to sessionId when courier:setRequestPreference is "sessionId"', async () => {
const config = {
get: () => {
return 'sessionId';
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(DEFAULT_SESSION_ID);
});

test('should be set to custom string when courier:setRequestPreference is "custom"', async () => {
const CUSTOM_PREFERENCE = '_local';
const config = {
get: (key) => {
if (key === 'courier:setRequestPreference') {
return 'custom';
} else if (key === 'courier:customRequestPreference') {
return CUSTOM_PREFERENCE;
}
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(CUSTOM_PREFERENCE);
});

test('should not be set when courier:setRequestPreference is "none"', async () => {
const config = {
get: () => {
return 'none';
}
};
const header = await getHeader({ requestFetchParams, config });
expect(header.preference).toBe(undefined);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { requestFetchParamsToBody } from './request_fetch_params_to_body';

export function RequestFetchParamsToBodyProvider(Promise, timefilter, kbnIndex, sessionId) {
export function RequestFetchParamsToBodyProvider(Promise, timefilter, kbnIndex, sessionId, config) {
return (requestsFetchParams) => (
requestFetchParamsToBody(
requestsFetchParams,
Promise,
timefilter,
kbnIndex,
sessionId)
sessionId,
config)
);
}