Example #1
0
def _perform_migration(search_app):
    app_name = search_app.name
    es_model = search_app.es_model
    logger.info(f'Migrating the {app_name} search app')

    read_alias_name = es_model.get_read_alias()
    write_alias_name = es_model.get_write_alias()
    new_index_name = es_model.get_target_index_name()

    current_read_indices, current_write_index = es_model.get_read_and_write_indices(
    )

    if current_write_index not in current_read_indices:
        raise DataHubException(
            'Cannot migrate Elasticsearch index with a read alias referencing '
            'a different index to the write alias', )

    logger.info(f'Updating aliases for the {app_name} search app')

    create_index(new_index_name, es_model._doc_type.mapping)

    with start_alias_transaction() as alias_transaction:
        alias_transaction.associate_indices_with_alias(read_alias_name,
                                                       [new_index_name])
        alias_transaction.associate_indices_with_alias(write_alias_name,
                                                       [new_index_name])
        alias_transaction.dissociate_indices_from_alias(
            write_alias_name, [current_write_index])

    _schedule_resync(search_app)
def test_update_alias(mock_es_client, add_actions, remove_actions,
                      expected_body):
    """Test get_aliases_for_index()."""
    client = mock_es_client.return_value
    with elasticsearch.start_alias_transaction() as alias_transaction:
        for action in add_actions:
            alias_transaction.associate_indices_with_alias(
                action[0], action[1])
        for action in remove_actions:
            alias_transaction.dissociate_indices_from_alias(
                action[0], action[1])
    client.indices.update_aliases.assert_called_with(body=expected_body)
def _clean_up_aliases_and_indices(search_app):
    es_model = search_app.es_model
    read_alias = es_model.get_read_alias()
    read_indices, write_index = es_model.get_read_and_write_indices()

    if write_index not in read_indices:
        raise DataHubException(
            'Write index not in read alias, aborting mapping migration...')

    indices_to_remove = read_indices - {write_index}

    if indices_to_remove:
        with start_alias_transaction() as alias_transaction:
            alias_transaction.dissociate_indices_from_alias(
                read_alias, indices_to_remove)
    else:
        logger.warning(f'No indices to remove for the {read_alias} alias')

    for index in indices_to_remove:
        if not get_aliases_for_index(index):
            delete_index(index)