Example #1
0
    def test_handle(self, elasticsearch_dsl_default_connection):
        """ Verify the command removes all but the newest indexes. """

        # Use now as initial time, so indexes are created AFTER the current index so expected values are accurate
        initial_time = datetime.datetime.now()
        # Create 2 more indexes than we expect to exist after removal
        for number in range(
                1, settings.ELASTICSEARCH_DSL_INDEX_RETENTION_LIMIT + 2):
            current_time = initial_time + datetime.timedelta(seconds=number)
            freezer = freeze_time(current_time)
            freezer.start()
            for index in registry.get_indices():
                ElasticsearchUtils.create_index(index)
            freezer.stop()

        # Prune indexes and confirm the right indexes are removed
        call_command('remove_unused_indexes')
        indices_client = elasticsearch_dsl_default_connection.indices
        for index in registry.get_indices():
            # pylint: disable=protected-access
            index_alias = ElasticsearchUtils.get_alias_by_index_name(
                index._name)
            current_alias = indices_client.get_alias(name=index_alias)
            indexes_to_keep = current_alias.keys()

            # check that we keep the current indexes, which we don't want removed
            all_indexes = self.get_current_index_names(
                indices_client=indices_client, index_prefix=index_alias)
            assert set(all_indexes).issuperset(set(indexes_to_keep))

            # check that other indexes are removed, excepting those that don't hit the retention limit
            expected_count = settings.ELASTICSEARCH_DSL_INDEX_RETENTION_LIMIT + len(
                indexes_to_keep)
            assert len(all_indexes) == expected_count

        # Attempt to prune indexes again and confirm that no indexes are removed
        call_command('remove_unused_indexes')

        for index in registry.get_indices():
            # check that we keep the current indexes, which we don't want removed
            # pylint: disable=protected-access
            index_alias = ElasticsearchUtils.get_alias_by_index_name(
                index._name)
            all_indexes = self.get_current_index_names(
                indices_client=indices_client, index_prefix=index_alias)
            current_alias = indices_client.get_alias(name=index_alias)
            indexes_to_keep = current_alias.keys()
            assert set(all_indexes).issuperset(set(indexes_to_keep))

            # check that index count remains the same as before
            expected_count = settings.ELASTICSEARCH_DSL_INDEX_RETENTION_LIMIT + len(
                indexes_to_keep)
            assert len(all_indexes) == expected_count

        # Cleanup indexes created by this test
        for index in registry.get_indices():
            # pylint: disable=protected-access
            indices_client.delete(index=index._name + '_*')
Example #2
0
    def test_handle(self):
        """ Verify the command removes all but the newest indexes. """

        # Create initial index with alias
        ElasticsearchUtils.create_alias_and_index(
            es_connection=self.backend.conn, alias=self.backend.index_name)
        # Use now as initial time, so indexes are created AFTER the current index so expected values are accurate
        initial_time = datetime.datetime.now()
        # Create 2 more indexes than we expect to exist after removal
        for number in range(1, settings.HAYSTACK_INDEX_RETENTION_LIMIT + 2):
            current_time = initial_time + datetime.timedelta(seconds=number)
            freezer = freeze_time(current_time)
            freezer.start()
            ElasticsearchUtils.create_index(es_connection=self.backend.conn,
                                            prefix=self.backend.index_name)
            freezer.stop()

        # Prune indexes and confirm the right indexes are removed
        call_command('remove_unused_indexes')
        current_alias_name = self.backend.index_name
        indices_client = self.backend.conn.indices
        current_alias = indices_client.get_alias(name=current_alias_name)
        indexes_to_keep = current_alias.keys()

        # check that we keep the current indexes, which we don't want removed
        all_indexes = self.get_current_index_names(
            indices_client=indices_client,
            index_prefix=self.backend.index_name)
        assert set(all_indexes).issuperset(set(indexes_to_keep))

        # check that other indexes are removed, excepting those that don't hit the retention limit
        expected_count = settings.HAYSTACK_INDEX_RETENTION_LIMIT + len(
            indexes_to_keep)
        assert len(all_indexes) == expected_count

        # Attempt to prune indexes again and confirm that no indexes are removed
        call_command('remove_unused_indexes')

        # check that we keep the current indexes, which we don't want removed
        all_indexes = self.get_current_index_names(
            indices_client=indices_client,
            index_prefix=self.backend.index_name)
        assert set(all_indexes).issuperset(set(indexes_to_keep))

        # check that index count remains the same as before
        assert len(all_indexes) == expected_count
Example #3
0
 def _create(self, models, options):
     for backend in self.backends:
         for index in registry.get_indices(models):
             created_index_info = ElasticsearchUtils.create_index(
                 index, backend)
             es_connection = get_connection(backend)
             self.stdout.write(
                 'Creating index "{1}".\nSet alias "{0}" for index "{1}".'.
                 format(created_index_info.alias, created_index_info.name))
             ElasticsearchUtils.set_alias(es_connection,
                                          created_index_info.alias,
                                          created_index_info.name)
    def prepare_backend_index(self, backend):
        """
        Prepares an index that will be used to store data by the backend.

        Args:
            backend (ElasticsearchSearchBackend): Backend to update.

        Returns:
            (tuple): tuple containing:

                alias(str): Recommended alias for the new index.
                index_name(str): Name of the newly-created index.
        """
        alias = backend.index_name
        index_name = ElasticsearchUtils.create_index(backend.conn, alias)
        backend.index_name = index_name
        return alias, index_name