Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: query logging memory hog #4006

Open
1 task
davidsword opened this issue Nov 14, 2024 · 0 comments
Open
1 task

BUG: query logging memory hog #4006

davidsword opened this issue Nov 14, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@davidsword
Copy link

davidsword commented Nov 14, 2024

Describe the bug

Big bulk operations in the WP-CLI context on an environment where WP_DEBUG is true can, in some use cases, result in an unsustainable growth of this query logging:

if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) {
$this->queries[] = $query;
}

A filter should be added to the conditional for this query logging so the logging can be explicitly disabled for these bulk operations without having to change the WP_DEBUG constant, ex:

public function add_elasticpress_version_to_user_agent( $user_agent ) {
	 * @param array $query Query to log.
	 */
	protected function add_query_log( $query ) {
-		if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) {
+		$disable_logging = apply_filters( 'ep_disable_query_logging', false );
+		if ( ! $disable_logging && ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) ) {
			$this->queries[] = $query;
		}

Steps to Reproduce

This doesn't happen on out of the box EP installs, but can happen in certain use cases. For us at VIP it happens out of the box for how we're using EP (more on this later), to replicate:

  • setup local wp site using vip dev-env, ensure "Enable Elasticsearch"
  • set vip-config.php to
define( 'WP_DEBUG', true );
define( 'VIP_ENABLE_VIP_SEARCH', true ); // Enable Enterprise Search
define( 'VIP_ENABLE_VIP_SEARCH_QUERY_INTEGRATION', true ); // Integrate search queries with Enterprise Search
  • have some posts on the site
  • create following wpcli cmd
if ( defined('WP_CLI') && WP_CLI ) {
    class Test_CLI_Command {

        public function ing( $args, $assoc_args ) {
            global $wpdb;

            wp_defer_term_counting( true );
            $counter = 0;
            // loop forever to replicate a huge sites bulk operation
            while ( true ) {
                $posts = get_posts( [
                    'post_type' => 'post',
                    'posts_per_page' => -1,
                    'fields' => 'ids',
                ] );
                foreach ( $posts as $post_id ) {
                    update_post_meta( $post_id, 'test', rand() );

                    \WP_CLI::line( "MEMORY: " . round(memory_get_peak_usage() / 1024 / 1024, 2) . "MB" );
                    
                    $counter++;
                    if ($counter % 100 === 0 ) {
                        \WP_CLI::line("batch of 100 done");

                        // memory cleanup 
	                $wpdb->queries = array();
                    }
                }
            }
        }
    }
    WP_CLI::add_command( 'test', 'Test_CLI_Command' );
}

This is happening specifically on VIP's use case because:

We reviewed how we've hooked into this for the index check, but we believe it would be best to have a filter for this query logging so this logging can be disabled entirely in the WP-CLI context without using the wider-use WP_DEBUG constant.

Screenshots, screen recording, code snippet

see above

Environment information

No response

WordPress and ElasticPress information

WP 6.6
EP 4.2.2

Code of Conduct

  • I agree to follow this project's Code of Conduct
@davidsword davidsword added the bug Something isn't working label Nov 14, 2024
davidsword added a commit to davidsword/ElasticPress that referenced this issue Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant