Skip to content

Commit

Permalink
[UA] Upgrade assistant migration meta data can become stale (#60789)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Mar 23, 2020
1 parent 3c66662 commit 85481a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export interface ReindexService {
*/
findReindexOperation(indexName: string): Promise<ReindexSavedObject | null>;

/**
* Delete reindex operations for completed indices with deprecations.
* @param indexNames
*/
cleanupReindexOperations(indexNames: string[]): Promise<void> | null;

/**
* Process the reindex operation through one step of the state machine and resolves
* to the updated reindex operation.
Expand Down Expand Up @@ -603,6 +609,23 @@ export const reindexServiceFactory = (
return findResponse.saved_objects[0];
},

async cleanupReindexOperations(indexNames: string[]) {
const performCleanup = async (indexName: string) => {
const existingReindexOps = await actions.findReindexOperations(indexName);

if (existingReindexOps && existingReindexOps.total !== 0) {
const existingOp = existingReindexOps.saved_objects[0];
if (existingOp.attributes.status === ReindexStatus.completed) {
// Delete the existing one if its status is completed, but still contains deprecation warnings
// example scenario: index was upgraded, but then deleted and restored with an old snapshot
await actions.deleteReindexOp(existingOp);
}
}
};

await Promise.all(indexNames.map(performCleanup));
},

findAllByStatus: actions.findAllByStatus,

async processNextStep(reindexOp: ReindexSavedObject) {
Expand Down
23 changes: 21 additions & 2 deletions x-pack/plugins/upgrade_assistant/server/routes/cluster_checkup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import { getUpgradeAssistantStatus } from '../lib/es_migration_apis';
import { versionCheckHandlerWrapper } from '../lib/es_version_precheck';
import { RouteDependencies } from '../types';
import { reindexActionsFactory } from '../lib/reindexing/reindex_actions';
import { reindexServiceFactory } from '../lib/reindexing';

export function registerClusterCheckupRoutes({ cloud, router }: RouteDependencies) {
export function registerClusterCheckupRoutes({ cloud, router, licensing, log }: RouteDependencies) {
const isCloudEnabled = Boolean(cloud?.isCloudEnabled);

router.get(
Expand All @@ -20,15 +22,32 @@ export function registerClusterCheckupRoutes({ cloud, router }: RouteDependencie
async (
{
core: {
savedObjects: { client: savedObjectsClient },
elasticsearch: { dataClient },
},
},
request,
response
) => {
try {
const status = await getUpgradeAssistantStatus(dataClient, isCloudEnabled);

const callAsCurrentUser = dataClient.callAsCurrentUser.bind(dataClient);
const reindexActions = reindexActionsFactory(savedObjectsClient, callAsCurrentUser);
const reindexService = reindexServiceFactory(
callAsCurrentUser,
reindexActions,
log,
licensing
);
const indexNames = status.indices
.filter(({ index }) => typeof index !== 'undefined')
.map(({ index }) => index as string);

await reindexService.cleanupReindexOperations(indexNames);

return response.ok({
body: await getUpgradeAssistantStatus(dataClient, isCloudEnabled),
body: status,
});
} catch (e) {
if (e.status === 403) {
Expand Down

0 comments on commit 85481a7

Please sign in to comment.