Skip to content

Commit

Permalink
Merge pull request elastic#8014 from tsullivan/uuid-rewrite-datafile
Browse files Browse the repository at this point in the history
uuid: rewrite using path.data file instead of .kibana index
  • Loading branch information
tsullivan authored Aug 30, 2016
2 parents edf1ee8 + 56e4527 commit fdcd9a3
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 108 deletions.
1 change: 1 addition & 0 deletions docs/releasenotes.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The {version} release of Kibana requires Elasticsearch {esversion} or later.
* {k4issue}6531[Issue 6531]: Improved warning for URL lengths that approach browser limits.
* {k4issue}6602[Issue 6602]: Improves dark theme support.
* {k4issue}6791[Issue 6791]: Enables composition of custom user toast notifications in Advanced Settings.
* {k4pull}8014[Pull Request 8014]: Changes the UUID config setting from `uuid` to `server.uuid`, and puts UUID storage into data file instead of Elasticsearch.

[float]
[[bugfixes]]
Expand Down
3 changes: 0 additions & 3 deletions src/core_plugins/elasticsearch/lib/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import exposeClient from './expose_client';
import migrateConfig from './migrate_config';
import createKibanaIndex from './create_kibana_index';
import checkEsVersion from './check_es_version';
import manageUuid from './manage_uuid';
const NoConnections = elasticsearch.errors.NoConnections;
import util from 'util';
const format = util.format;
Expand All @@ -19,7 +18,6 @@ const REQUEST_DELAY = 2500;
module.exports = function (plugin, server) {
const config = server.config();
const client = server.plugins.elasticsearch.client;
const uuidManagement = manageUuid(server);

plugin.status.yellow('Waiting for Elasticsearch');

Expand Down Expand Up @@ -89,7 +87,6 @@ module.exports = function (plugin, server) {
return waitForPong()
.then(_.partial(checkEsVersion, server))
.then(waitForShards)
.then(uuidManagement)
.then(setGreenStatus)
.then(_.partial(migrateConfig, server))
.catch(err => plugin.status.red(err));
Expand Down
81 changes: 0 additions & 81 deletions src/core_plugins/elasticsearch/lib/manage_uuid.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import manageUuid from './server/lib/manage_uuid';
import ingest from './server/routes/api/ingest';
import search from './server/routes/api/search';
import settings from './server/routes/api/settings';
Expand Down Expand Up @@ -84,6 +85,9 @@ module.exports = function (kibana) {
},

init: function (server, options) {
// uuid
manageUuid(server);
// routes
ingest(server);
search(server);
settings(server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import expect from 'expect.js';
import sinon from 'sinon';
import Joi from 'joi';
import * as kbnTestServer from '../../../../../test/utils/kbn_server.js';
import fromRoot from '../../../../utils/from_root';
import * as kbnTestServer from '../../../../../../test/utils/kbn_server.js';
import fromRoot from '../../../../../utils/from_root';
import manageUuid from '../manage_uuid';

describe('plugins/elasticsearch', function () {
describe('core_plugins/kibana/server/lib', function () {
describe('manage_uuid', function () {
const testUuid = 'c4add484-0cba-4e05-86fe-4baa112d9e53';
let kbnServer;
Expand All @@ -23,7 +23,6 @@ describe('plugins/elasticsearch', function () {
});

await kbnServer.ready();
await kbnServer.server.plugins.elasticsearch.waitUntilReady();
});

// clear uuid stuff from previous test runs
Expand All @@ -38,53 +37,48 @@ describe('plugins/elasticsearch', function () {
});

it('ensure config uuid is validated as a guid', async function () {
config.set('uuid', testUuid);
expect(config.get('uuid')).to.be(testUuid);
config.set('server.uuid', testUuid);
expect(config.get('server.uuid')).to.be(testUuid);

expect(() => {
config.set('uuid', 'foouid');
config.set('server.uuid', 'foouid');
}).to.throwException((e) => {
expect(e.name).to.be('ValidationError');
});
});

it('finds the previously set uuid with config match', async function () {
const uuidManagement = manageUuid(kbnServer.server);
const msg = `Kibana instance UUID: ${testUuid}`;
config.set('uuid', testUuid);
config.set('server.uuid', testUuid);

await uuidManagement();
await uuidManagement();
await manageUuid(kbnServer.server);
await manageUuid(kbnServer.server);

expect(kbnServer.server.log.lastCall.args[1]).to.be.eql(msg);
});

it('updates the previously set uuid with config value', async function () {
const uuidManagement = manageUuid(kbnServer.server);
config.set('uuid', testUuid);
config.set('server.uuid', testUuid);

await uuidManagement();
await manageUuid(kbnServer.server);

const newUuid = '5b2de169-2785-441b-ae8c-186a1936b17d';
const msg = `Updating Kibana instance UUID to: ${newUuid} (was: ${testUuid})`;

config.set('uuid', newUuid);
await uuidManagement();
config.set('server.uuid', newUuid);
await manageUuid(kbnServer.server);

expect(kbnServer.server.log.lastCall.args[1]).to.be(msg);
});

it('resumes the uuid stored in data and sets it to the config', async function () {
const uuidManagement = manageUuid(kbnServer.server);
const partialMsg = 'Resuming persistent Kibana instance UUID';
config.set('uuid'); // set to undefined
config.set('server.uuid'); // set to undefined

await uuidManagement();
await manageUuid(kbnServer.server);

expect(config.get('uuid')).to.be.ok(); // not undefined any more
expect(config.get('server.uuid')).to.be.ok(); // not undefined any more
expect(kbnServer.server.log.lastCall.args[1]).to.match(new RegExp(`^${partialMsg}`));
});


});
});
72 changes: 72 additions & 0 deletions src/core_plugins/kibana/server/lib/manage_uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import uuid from 'node-uuid';
import Promise from 'bluebird';
import { join as pathJoin } from 'path';
import { readFile as readFileCallback, writeFile as writeFileCallback } from 'fs';

const FILE_ENCODING = 'utf8';

export default async function manageUuid(server) {
const config = server.config();
const serverPort = server.info.port;
const serverHostname = config.get('server.host');
const fileName = `${serverHostname}:${serverPort}`;
const uuidFile = pathJoin(config.get('path.data'), fileName);

async function detectUuid() {
const readFile = Promise.promisify(readFileCallback);
try {
const result = await readFile(uuidFile);
return result.toString(FILE_ENCODING);
} catch (e) {
return false;
}
}

async function writeUuid(uuid) {
const writeFile = Promise.promisify(writeFileCallback);
try {
return await writeFile(uuidFile, uuid, { encoding: FILE_ENCODING });
} catch (e) {
return false;
}
}

// detect if uuid exists already from before a restart
const logToServer = (msg) => server.log(['server', 'uuid', fileName], msg);
const dataFileUuid = await detectUuid();
let serverConfigUuid = config.get('server.uuid'); // check if already set in config

if (dataFileUuid) {
// data uuid found
if (serverConfigUuid === dataFileUuid) {
// config uuid exists, data uuid exists and matches
logToServer(`Kibana instance UUID: ${dataFileUuid}`);
return;
}

if (!serverConfigUuid) {
// config uuid missing, data uuid exists
serverConfigUuid = dataFileUuid;
logToServer(`Resuming persistent Kibana instance UUID: ${serverConfigUuid}`);
config.set('server.uuid', serverConfigUuid);
return;
}

if (serverConfigUuid !== dataFileUuid) {
// config uuid exists, data uuid exists but mismatches
logToServer(`Updating Kibana instance UUID to: ${serverConfigUuid} (was: ${dataFileUuid})`);
return writeUuid(serverConfigUuid);
}
}

// data uuid missing

if (!serverConfigUuid) {
// config uuid missing
serverConfigUuid = uuid.v4();
config.set('server.uuid', serverConfigUuid);
}

logToServer(`Setting new Kibana instance UUID: ${serverConfigUuid}`);
return writeUuid(serverConfigUuid);
}
2 changes: 1 addition & 1 deletion src/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module.exports = () => Joi.object({
exclusive: Joi.boolean().default(false)
}).default(),

uuid: Joi.string().guid().default(),

server: Joi.object({
uuid: Joi.string().guid().default(),
name: Joi.string().default(os.hostname()),
host: Joi.string().hostname().default('localhost'),
port: Joi.number().default(5601),
Expand Down
2 changes: 1 addition & 1 deletion src/server/status/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function (kbnServer, server, config) {
handler: function (request, reply) {
return reply({
name: config.get('server.name'),
uuid: config.get('uuid'),
uuid: config.get('server.uuid'),
status: kbnServer.status.toJSON(),
metrics: kbnServer.metrics
});
Expand Down

0 comments on commit fdcd9a3

Please sign in to comment.