Skip to content

Commit

Permalink
Merge branch 'master' into telemetry/always-opted-in
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Nov 6, 2019
2 parents f7fa39d + c485893 commit 3a5d874
Show file tree
Hide file tree
Showing 27 changed files with 914 additions and 482 deletions.
30 changes: 25 additions & 5 deletions packages/kbn-babel-preset/node_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@
*/

module.exports = (_, options = {}) => {
const overrides = [];
if (!process.env.ALLOW_PERFORMANCE_HOOKS_IN_TASK_MANAGER) {
overrides.push(
{
test: [/x-pack[\/\\]legacy[\/\\]plugins[\/\\]task_manager/],
plugins: [
[
'filter-imports',
{
imports: {
perf_hooks: ['performance'],
},
},
],
],
}
);
}

return {
presets: [
[
Expand All @@ -39,7 +58,7 @@ module.exports = (_, options = {}) => {
modules: 'cjs',
corejs: 3,

...(options['@babel/preset-env'] || {})
...(options['@babel/preset-env'] || {}),
},
],
require('./common_preset'),
Expand All @@ -48,9 +67,10 @@ module.exports = (_, options = {}) => {
[
require.resolve('babel-plugin-transform-define'),
{
'global.__BUILT_WITH_BABEL__': 'true'
}
]
]
'global.__BUILT_WITH_BABEL__': 'true',
},
],
],
overrides,
};
};
3 changes: 2 additions & 1 deletion packages/kbn-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react":"^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@kbn/elastic-idx": "1.0.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-filter-imports": "^3.0.0",
"babel-plugin-transform-define": "^1.3.1",
"babel-plugin-typescript-strip-namespaces": "^1.1.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
EuiPagination,
EuiText,
EuiSpacer,
EuiIcon,
} from '@elastic/eui';

import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -116,8 +117,6 @@ export const SavedQueryManagementComponent: FunctionComponent<Props> = ({

const savedQueryPopoverButton = (
<EuiButtonEmpty
iconType="arrowDown"
iconSide="right"
onClick={() => {
setIsOpen(!isOpen);
}}
Expand All @@ -129,7 +128,8 @@ export const SavedQueryManagementComponent: FunctionComponent<Props> = ({
})}
data-test-subj="saved-query-management-popover-button"
>
#
<EuiIcon type="save" className="euiQuickSelectPopover__buttonText" />
<EuiIcon type="arrowDown" />
</EuiButtonEmpty>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import {
EuiDraggable,
EuiSpacer,
EuiPanel,
EuiFormErrorText,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { AggConfig } from '../../../../agg_types/agg_config';
import { aggGroupNamesMap, AggGroupNames } from '../agg_groups';
Expand Down Expand Up @@ -80,7 +82,15 @@ function DefaultEditorAggGroup({

const [aggsState, setAggsState] = useReducer(aggGroupReducer, group, initAggsState);

const isGroupValid = Object.values(aggsState).every(item => item.valid);
const bucketsError =
lastParentPipelineAggTitle && groupName === AggGroupNames.Buckets && !group.length
? i18n.translate('common.ui.aggTypes.buckets.mustHaveBucketErrorMessage', {
defaultMessage: 'Add a bucket with "Date Histogram" or "Histogram" aggregation.',
description: 'Date Histogram and Histogram should not be translated',
})
: undefined;

const isGroupValid = !bucketsError && Object.values(aggsState).every(item => item.valid);
const isAllAggsTouched = isInvalidAggsTouched(aggsState);
const isMetricAggregationDisabled = useMemo(
() => groupName === AggGroupNames.Metrics && getEnabledMetricAggsCount(group) === 1,
Expand Down Expand Up @@ -144,6 +154,12 @@ function DefaultEditorAggGroup({
<h3>{groupNameLabel}</h3>
</EuiTitle>
<EuiSpacer size="s" />
{bucketsError && (
<>
<EuiFormErrorText>{bucketsError}</EuiFormErrorText>
<EuiSpacer size="s" />
</>
)}
<EuiDroppable droppableId={`agg_group_dnd_${groupName}`}>
<>
{group.map((agg: AggConfig, index: number) => (
Expand Down
1 change: 1 addition & 0 deletions x-pack/.i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"xpack.server": "legacy/server",
"xpack.snapshotRestore": "legacy/plugins/snapshot_restore",
"xpack.spaces": ["legacy/plugins/spaces", "plugins/spaces"],
"xpack.taskManager": "legacy/plugins/task_manager",
"xpack.transform": "legacy/plugins/transform",
"xpack.upgradeAssistant": "legacy/plugins/upgrade_assistant",
"xpack.uptime": "legacy/plugins/uptime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
// Which is basically the Haskel equivalent of Rust/ML/Scala's Result
// I'll reach out to other's in Kibana to see if we can merge these into one type

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type Ok<T> = {
export interface Ok<T> {
tag: 'ok';
value: T;
};
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type Err<E> = {
}

export interface Err<E> {
tag: 'err';
error: E;
};
}
export type Result<T, E> = Ok<T> | Err<E>;

export function asOk<T>(value: T): Ok<T> {
Expand Down
11 changes: 6 additions & 5 deletions x-pack/legacy/plugins/task_manager/lib/fill_pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import _ from 'lodash';
import sinon from 'sinon';
import { fillPool } from './fill_pool';
import { TaskPoolRunResult } from '../task_pool';

describe('fillPool', () => {
test('stops filling when there are no more tasks in the store', async () => {
const tasks = [[1, 2, 3], [4, 5]];
let index = 0;
const fetchAvailableTasks = async () => tasks[index++] || [];
const run = sinon.spy(async () => true);
const run = sinon.spy(async () => TaskPoolRunResult.RunningAllClaimedTasks);
const converter = _.identity;

await fillPool(run, fetchAvailableTasks, converter);
Expand All @@ -25,7 +26,7 @@ describe('fillPool', () => {
const tasks = [[1, 2, 3], [4, 5]];
let index = 0;
const fetchAvailableTasks = async () => tasks[index++] || [];
const run = sinon.spy(async () => false);
const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity);
const converter = _.identity;

await fillPool(run, fetchAvailableTasks, converter);
Expand All @@ -37,7 +38,7 @@ describe('fillPool', () => {
const tasks = [[1, 2, 3], [4, 5]];
let index = 0;
const fetchAvailableTasks = async () => tasks[index++] || [];
const run = sinon.spy(async () => false);
const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity);
const converter = (x: number) => x.toString();

await fillPool(run, fetchAvailableTasks, converter);
Expand All @@ -47,7 +48,7 @@ describe('fillPool', () => {

describe('error handling', () => {
test('throws exception from fetchAvailableTasks', async () => {
const run = sinon.spy(async () => false);
const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity);
const converter = (x: number) => x.toString();

try {
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('fillPool', () => {
const tasks = [[1, 2, 3], [4, 5]];
let index = 0;
const fetchAvailableTasks = async () => tasks[index++] || [];
const run = sinon.spy(async () => false);
const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity);
const converter = (x: number) => {
throw new Error(`can not convert ${x}`);
};
Expand Down
32 changes: 27 additions & 5 deletions x-pack/legacy/plugins/task_manager/lib/fill_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

type BatchRun<T> = (tasks: T[]) => Promise<boolean>;
import { performance } from 'perf_hooks';
import { TaskPoolRunResult } from '../task_pool';

export enum FillPoolResult {
NoTasksClaimed = 'NoTasksClaimed',
RanOutOfCapacity = 'RanOutOfCapacity',
}

type BatchRun<T> = (tasks: T[]) => Promise<TaskPoolRunResult>;
type Fetcher<T> = () => Promise<T[]>;
type Converter<T1, T2> = (t: T1) => T2;

Expand All @@ -24,18 +32,32 @@ export async function fillPool<TRecord, TRunner>(
run: BatchRun<TRunner>,
fetchAvailableTasks: Fetcher<TRecord>,
converter: Converter<TRecord, TRunner>
): Promise<void> {
): Promise<FillPoolResult> {
performance.mark('fillPool.start');
while (true) {
const instances = await fetchAvailableTasks();

if (!instances.length) {
return;
performance.mark('fillPool.bailNoTasks');
performance.measure(
'fillPool.activityDurationUntilNoTasks',
'fillPool.start',
'fillPool.bailNoTasks'
);
return FillPoolResult.NoTasksClaimed;
}

const tasks = instances.map(converter);

if (!(await run(tasks))) {
return;
if ((await run(tasks)) === TaskPoolRunResult.RanOutOfCapacity) {
performance.mark('fillPool.bailExhaustedCapacity');
performance.measure(
'fillPool.activityDurationUntilExhaustedCapacity',
'fillPool.start',
'fillPool.bailExhaustedCapacity'
);
return FillPoolResult.RanOutOfCapacity;
}
performance.mark('fillPool.cycle');
}
}
6 changes: 6 additions & 0 deletions x-pack/legacy/plugins/task_manager/lib/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,23 @@ describe('addMiddlewareToChain', () => {
return opts;
},
beforeRun: defaultBeforeRun,
beforeMarkRunning: defaultBeforeRun,
};
const m2 = {
beforeSave: async (opts: BeforeSaveOpts) => {
Object.assign(opts.taskInstance.params, { m2: true });
return opts;
},
beforeRun: defaultBeforeRun,
beforeMarkRunning: defaultBeforeRun,
};
const m3 = {
beforeSave: async (opts: BeforeSaveOpts) => {
Object.assign(opts.taskInstance.params, { m3: true });
return opts;
},
beforeRun: defaultBeforeRun,
beforeMarkRunning: defaultBeforeRun,
};

let middlewareChain;
Expand Down Expand Up @@ -119,6 +122,7 @@ describe('addMiddlewareToChain', () => {
m1: true,
};
},
beforeMarkRunning: defaultBeforeRun,
};
const m2 = {
beforeSave: defaultBeforeSave,
Expand All @@ -128,6 +132,7 @@ describe('addMiddlewareToChain', () => {
m2: true,
};
},
beforeMarkRunning: defaultBeforeRun,
};
const m3 = {
beforeSave: defaultBeforeSave,
Expand All @@ -137,6 +142,7 @@ describe('addMiddlewareToChain', () => {
m3: true,
};
},
beforeMarkRunning: defaultBeforeRun,
};

let middlewareChain;
Expand Down
8 changes: 8 additions & 0 deletions x-pack/legacy/plugins/task_manager/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export type BeforeSaveFunction = (
) => Promise<BeforeSaveMiddlewareParams>;

export type BeforeRunFunction = (params: RunContext) => Promise<RunContext>;
export type BeforeMarkRunningFunction = (params: RunContext) => Promise<RunContext>;

export interface Middleware {
beforeSave: BeforeSaveFunction;
beforeRun: BeforeRunFunction;
beforeMarkRunning: BeforeMarkRunningFunction;
}

export function addMiddlewareToChain(prevMiddleware: Middleware, middleware: Middleware) {
Expand All @@ -39,8 +41,14 @@ export function addMiddlewareToChain(prevMiddleware: Middleware, middleware: Mid
? (params: RunContext) => middleware.beforeRun(params).then(prevMiddleware.beforeRun)
: prevMiddleware.beforeRun;

const beforeMarkRunning = middleware.beforeMarkRunning
? (params: RunContext) =>
middleware.beforeMarkRunning(params).then(prevMiddleware.beforeMarkRunning)
: prevMiddleware.beforeMarkRunning;

return {
beforeSave,
beforeRun,
beforeMarkRunning,
};
}
7 changes: 6 additions & 1 deletion x-pack/legacy/plugins/task_manager/task_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ describe('TaskManager', () => {
const middleware = {
beforeSave: async (saveOpts: any) => saveOpts,
beforeRun: async (runOpts: any) => runOpts,
beforeMarkRunning: async (runOpts: any) => runOpts,
};
expect(() => client.addMiddleware(middleware)).not.toThrow();
});
Expand All @@ -183,6 +184,7 @@ describe('TaskManager', () => {
const middleware = {
beforeSave: async (saveOpts: any) => saveOpts,
beforeRun: async (runOpts: any) => runOpts,
beforeMarkRunning: async (runOpts: any) => runOpts,
};

client.start();
Expand Down Expand Up @@ -241,7 +243,10 @@ describe('TaskManager', () => {

claimAvailableTasks(claim, 10, logger);

sinon.assert.calledWithMatch(logger.warn, /inline scripts/);
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn.mock.calls[0][0]).toMatchInlineSnapshot(
`"Task Manager cannot operate when inline scripts are disabled in Elasticsearch"`
);
});
});
});
Loading

0 comments on commit 3a5d874

Please sign in to comment.