<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.distribution.internal.job.step;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.eventstream.EventStore;
import org.xwiki.eventstream.EventStream;
import org.xwiki.eventstream.internal.AbstractAsynchronousEventStore;
import org.xwiki.eventstream.query.SimpleEventQuery;

/**
 * Migrate event from legacy to new store.
 * 
 * @version $Id$
 * @since 12.6.1
 * @since 12.7RC1
 */
@Component
@Named(EventMigrationStep.ID)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class EventMigrationStep extends AbstractDistributionStep
{
    /**
     * The identifier of the step.
     */
    public static final String ID = "eventmigration";

    @Inject
    private transient Logger logger;

    @Inject
    private EventStore eventStore;

    @Inject
    private ComponentManager componentManager;

    /**
     * Default constructor.
     */
    public EventMigrationStep()
    {
        super(ID);
    }

    @Override
    public void prepare()
    {
        if (getState() == null) {
            setState(State.COMPLETED);

            if (this.componentManager.hasComponent(EventStream.class)) {
                try {
                    EventStream eventStream = this.componentManager.getInstance(EventStream.class);

                    long legacyCount = eventStream.countEvents();

                    // The total might not be fully accurate but counting 1 or 2 more events than the reality should not
                    // have much impact on the logic of that step which is generally used to migrate events that used to
                    // be only stored in the legacy store
                    long eventCount = (this.eventStore instanceof AbstractAsynchronousEventStore
                        ? ((AbstractAsynchronousEventStore) this.eventStore).getQueueSize() : 0)
                        + this.eventStore.search(new SimpleEventQuery(0, 0)).getTotalHits();
<commit_msg>XWIKI-17747: Possible race condition when comparing legacy and new store size in the migration step of the DW * trying to account for events sent by XWiki but not fully stored by Solr yet<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.distribution.internal.job.step;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.eventstream.EventStore;
import org.xwiki.eventstream.EventStream;
import org.xwiki.eventstream.internal.AbstractAsynchronousEventStore;
import org.xwiki.eventstream.query.SimpleEventQuery;

/**
 * Migrate event from legacy to new store.
 * 
 * @version $Id$
 * @since 12.6.1
 * @since 12.7RC1
 */
@Component
@Named(EventMigrationStep.ID)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class EventMigrationStep extends AbstractDistributionStep
{
    /**
     * The identifier of the step.
     */
    public static final String ID = "eventmigration";

    @Inject
    private transient Logger logger;

    @Inject
    private EventStore eventStore;

    @Inject
    private ComponentManager componentManager;

    /**
     * Default constructor.
     */
    public EventMigrationStep()
    {
        super(ID);
    }

    @Override
    public void prepare()
    {
        if (getState() == null) {
            setState(State.COMPLETED);

            if (this.componentManager.hasComponent(EventStream.class)) {
                try {
                    EventStream eventStream = this.componentManager.getInstance(EventStream.class);

                    long legacyCount = eventStream.countEvents();

                    long eventCount = (this.eventStore instanceof AbstractAsynchronousEventStore
                        ? ((AbstractAsynchronousEventStore) this.eventStore).getQueueSize() : 0)
                        + this.eventStore.search(new SimpleEventQuery(0, 0)).getTotalHits();

                    // Bulletproofing in case some events have been sent to solr but not yet fully taken into account.
                    // The main target of this step is use cases where there is a lot more legacy events than new
                    // events.
                    eventCount += 10;

{'fraction_non_alphanumeric': 0.07466168922071861, 'fraction_numerical': 0.00917716596671333, 'mean_word_length': 3.072197593413553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5534213', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1607, 'n_words': 592}