Example #1
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)
Example #2
0
    def _update(self, models, options):
        """
        Update indices with sanity check.

        Will be created a new index and populate with data.
        The index will be masked with previous one to prevent missing data.
        """

        alias_mappings = []
        for document in registry.get_documents(models):
            # pylint: disable=protected-access
            index = document._index
            record_count = self.get_record_count(document)
            alias, new_index_name = self.prepare_backend_index(index)
            alias_mappings.append(
                AliasMapper(document, index, new_index_name, alias,
                            record_count))
        # Set the alias (from settings) to the timestamped catalog.
        run_attempts = 0
        indexes_pending = {
            key: ''
            for key in [x.new_index_name for x in alias_mappings]
        }
        conn = get_connection()
        while indexes_pending and run_attempts < 1:  # Only try once, as retries gave buggy results. See VAN-391
            run_attempts += 1
            self._populate(models, options)
            for doc, __, new_index_name, alias, record_count in alias_mappings:
                # Run a sanity check to ensure we aren't drastically changing the
                # index, which could be indicative of a bug.
                if new_index_name in indexes_pending and not options.get(
                        'disable_change_limit', False):
                    record_count_is_sane, index_info_string = self.sanity_check_new_index(
                        run_attempts, doc, new_index_name, record_count)
                    if record_count_is_sane:
                        ElasticsearchUtils.set_alias(conn, alias,
                                                     new_index_name)
                        indexes_pending.pop(new_index_name, None)
                    else:
                        indexes_pending[new_index_name] = index_info_string
                else:
                    ElasticsearchUtils.set_alias(conn, alias, new_index_name)
                    indexes_pending.pop(new_index_name, None)

        for index_alias_mapper in alias_mappings:
            index_alias_mapper.registered_index._name = index_alias_mapper.alias  # pylint: disable=protected-access

        if indexes_pending:
            raise CommandError(
                'Sanity check failed for the new index(es): {}'.format(
                    indexes_pending))

        return True