Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Check document count of old and new index before applying aliases #45

Merged
merged 2 commits into from
Oct 5, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A useful CLI for working with Next's Elasticsearch clusters. Includes tools to s

## Prerequisites

- Node 6+
- Node 8.11+
- [Vault CLI](https://github.com/Financial-Times/vault/wiki/Getting-Started#login-with-the-cli) (optional, see notes below)

## Installation and setup
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"standard": "^8.5.0"
},
"engines": {
"node": ">=8.9.0"
"node": ">=8.11.0"
}
}
48 changes: 36 additions & 12 deletions tools/alias.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,34 @@ function extractLatestIndex (indices) {
.pop()
}

function updateAliases (aliases, indices) {
async function getDifference (current, latest) {
const [ a, b ] = await Promise.all([
client.count({ index: current }),
client.count({ index: latest })
])

console.log(`Current index has ${a.count} documents`)
console.log(`Latest index has ${b.count} documents`)

return Math.abs(a.count - b.count)
}

async function updateAliases (aliases, indices) {
const current = extractCurrentIndex(aliases)
const latest = extractLatestIndex(indices)

if (current === latest) {
throw new Error(`Current index and latest index are the same (${latest})`)
}

const diff = await getDifference(current, latest)

// This is an arbitrary number, but the newly promoted index will be synced quickly with
// an index from another region if there are only a small number of differences.
if (diff > 10) {
throw new Error(`Current index and latest index are too out of sync (${diff} documents)`)
}

const actions = aliases.reduce((actions, { alias }) => {
console.log(`Removing ${current} => ${alias}`)
actions.push({ remove: { index: current, alias } })
Expand All @@ -63,19 +83,23 @@ function updateAliases (aliases, indices) {
return client.indices.updateAliases({ body: { actions } })
}

function run (cluster, command) {
async function run (cluster, command) {
client = elastic(cluster)

return Promise.all([ fetchAliases(), fetchIndices() ])
.then(([ aliases, indices ]) => updateAliases(aliases, indices))
.then(() => {
console.log('Alias complete')
process.exit()
})
.catch((err) => {
console.error(`Alias failed: ${err.toString()}`)
process.exit(1)
})
try {
const [ aliases, indices ] = await Promise.all([
fetchAliases(),
fetchIndices()
])

await updateAliases(aliases, indices)

console.log('Alias complete')
process.exit()
} catch (error) {
console.error(`Alias failed: ${error}`)
process.exit(1)
}
}

module.exports = function (program) {
Expand Down