Skip to content

Commit

Permalink
[savedObjects] Use index template (#14271)
Browse files Browse the repository at this point in the history
* [es][savedObjects/index] put template on each savedObject write

The elasticsearch plugin currently checks for the Kibana index on each iteration of the healthCheck, and creates it if it does not exist. This removes that step from the healthCheck and instead, before each savedObject is written to elasticsearch, ensures that Elasticsearch has the necessary index template should the write result in index creation.

The healthCheck still has the `patchKibanaIndex()` logic, which checks the type in the Kibana index and adds any missing types. This step now does nothing when the Kibana index does not exist, and does what it has always done when it does.

* [ftr] remove unused kibanaIndex service

(cherry picked from commit b1ef897dafeb6d43fe279776e44a9d793a389dc3)

* [savedObjects/integration] create now creates kibana index

* [es/healthCheck] remove use of format()

* [es/healthCheck/tests] use sinon assertions

* [es/patchKibanaIndex] test for kibana index missing behavior

* [savedObjects/errors] add tests for EsAutoCreateIndexError

* [savedObjects/config] deprecate and remove savedObjects.indexCheckTimeout config

* use dangling commas consistently

* [ui/error_auto_create_index] fix class names

* [ui/savedObjectsClient] no need to specify basePath

* [eslint] fix linting issue
  • Loading branch information
spalger authored Nov 22, 2017
1 parent cd1a49c commit 90e2aa0
Show file tree
Hide file tree
Showing 37 changed files with 422 additions and 439 deletions.
120 changes: 0 additions & 120 deletions src/core_plugins/elasticsearch/lib/__tests__/create_kibana_index.js

This file was deleted.

74 changes: 7 additions & 67 deletions src/core_plugins/elasticsearch/lib/__tests__/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,17 @@ describe('plugins/elasticsearch', () => {

it('should set the cluster green if everything is ready', function () {
cluster.callWithInternalUser.withArgs('ping').returns(Promise.resolve());
cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any).returns(
Promise.resolve({ timed_out: false, status: 'green' })
);

return health.run()
.then(function () {
sinon.assert.calledOnce(plugin.status.yellow);
expect(plugin.status.yellow.args[0][0]).to.be('Waiting for Elasticsearch');
sinon.assert.calledWithExactly(plugin.status.yellow, 'Waiting for Elasticsearch');

sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('ping'));
sinon.assert.calledTwice(cluster.callWithInternalUser.withArgs('nodes.info', sinon.match.any));
sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any));
sinon.assert.notCalled(plugin.status.red);
sinon.assert.calledOnce(plugin.status.green);

expect(plugin.status.green.args[0][0]).to.be('Kibana index ready');
sinon.assert.calledWithExactly(plugin.status.green, 'Ready');
});
});

Expand All @@ -137,88 +132,33 @@ describe('plugins/elasticsearch', () => {
ping.onCall(0).returns(Promise.reject(new NoConnections()));
ping.onCall(1).returns(Promise.resolve());

cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any).returns(
Promise.resolve({ timed_out: false, status: 'green' })
);

return health.run()
.then(function () {
sinon.assert.calledOnce(plugin.status.yellow);
expect(plugin.status.yellow.args[0][0]).to.be('Waiting for Elasticsearch');
sinon.assert.calledWithExactly(plugin.status.yellow, 'Waiting for Elasticsearch');

sinon.assert.calledOnce(plugin.status.red);
expect(plugin.status.red.args[0][0]).to.be(
sinon.assert.calledWithExactly(
plugin.status.red,
`Unable to connect to Elasticsearch at ${esUrl}.`
);

sinon.assert.calledTwice(ping);
sinon.assert.calledTwice(cluster.callWithInternalUser.withArgs('nodes.info', sinon.match.any));
sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any));
sinon.assert.calledOnce(plugin.status.green);
expect(plugin.status.green.args[0][0]).to.be('Kibana index ready');
});
});

it('should set the cluster red if the health check status is red, then to green', function () {
cluster.callWithInternalUser.withArgs('ping').returns(Promise.resolve());

const clusterHealth = cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any);
clusterHealth.onCall(0).returns(Promise.resolve({ timed_out: false, status: 'red' }));
clusterHealth.onCall(1).returns(Promise.resolve({ timed_out: false, status: 'green' }));

return health.run()
.then(function () {
sinon.assert.calledOnce(plugin.status.yellow);
expect(plugin.status.yellow.args[0][0]).to.be('Waiting for Elasticsearch');
sinon.assert.calledOnce(plugin.status.red);
expect(plugin.status.red.args[0][0]).to.be(
'Elasticsearch is still initializing the kibana index.'
);
sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('ping'));
sinon.assert.calledTwice(cluster.callWithInternalUser.withArgs('nodes.info', sinon.match.any));
sinon.assert.calledTwice(cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any));
sinon.assert.calledOnce(plugin.status.green);
expect(plugin.status.green.args[0][0]).to.be('Kibana index ready');
});
});

it('should set the cluster yellow if the health check timed_out and create index', function () {
cluster.callWithInternalUser.withArgs('ping').returns(Promise.resolve());

const clusterHealth = cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any);
clusterHealth.onCall(0).returns(Promise.resolve({ timed_out: true, status: 'red' }));
clusterHealth.onCall(1).returns(Promise.resolve({ timed_out: false, status: 'green' }));

cluster.callWithInternalUser.withArgs('indices.create', sinon.match.any).returns(Promise.resolve());

return health.run()
.then(function () {
sinon.assert.calledTwice(plugin.status.yellow);
expect(plugin.status.yellow.args[0][0]).to.be('Waiting for Elasticsearch');
expect(plugin.status.yellow.args[1][0]).to.be('No existing Kibana index found');

sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('ping'));
sinon.assert.calledOnce(cluster.callWithInternalUser.withArgs('indices.create', sinon.match.any));
sinon.assert.calledTwice(cluster.callWithInternalUser.withArgs('nodes.info', sinon.match.any));
sinon.assert.calledTwice(clusterHealth);
sinon.assert.calledWithExactly(plugin.status.green, 'Ready');
});
});

describe('#waitUntilReady', function () {
it('polls health until index is ready, then waits for green status', function () {
const clusterHealth = cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any);
clusterHealth.onCall(0).returns(Promise.resolve({ timed_out: true }));
clusterHealth.onCall(1).returns(Promise.resolve({ status: 'red' }));
clusterHealth.onCall(2).returns(Promise.resolve({ status: 'green' }));

it('waits for green status', function () {
plugin.status.once = sinon.spy(function (event, handler) {
expect(event).to.be('green');
setImmediate(handler);
});

return health.waitUntilReady().then(function () {
sinon.assert.calledOnce(plugin.status.once);
sinon.assert.calledThrice(clusterHealth);
});
});
});
Expand Down
26 changes: 24 additions & 2 deletions src/core_plugins/elasticsearch/lib/__tests__/patch_kibana_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ function createCallCluster(index) {
return sinon.spy(async (method, params) => {
switch (method) {
case 'indices.get':
expect(params).to.have.property('index', Object.keys(index)[0]);
return cloneDeep(index);
if (!index) {
return { status: 404 };
} else {
expect(params).to.have.property('index', Object.keys(index)[0]);
return cloneDeep(index);
}
case 'indices.putMapping':
return { ok: true };
default:
Expand Down Expand Up @@ -76,6 +80,24 @@ describe('es/healthCheck/patchKibanaIndex()', () => {
});
});

describe('missing index', () => {
it('returns without doing anything', async () => {
const indexName = chance.word();
const mappings = createRandomMappings();
const callCluster = createCallCluster(null);
const log = sinon.stub();
await patchKibanaIndex({
callCluster,
indexName,
kibanaIndexMappingsDsl: mappings,
log
});

sinon.assert.calledOnce(callCluster);
sinon.assert.notCalled(log);
});
});

describe('multi-type index', () => {
it('rejects', async () => {
try {
Expand Down
25 changes: 0 additions & 25 deletions src/core_plugins/elasticsearch/lib/create_kibana_index.js

This file was deleted.

Loading

0 comments on commit 90e2aa0

Please sign in to comment.