Page MenuHomePhabricator

Throttling Cyberbot tool user as it is consuming most of the CPU
Closed, ResolvedPublic

Description

Hi, the cyberbot tool is using most of the CPU and contributing to overload the tool labs DB (see also T131897).

Because the tool is also using an high number of connections, I've been forced to throttle them to 10 concurrent connections to alleviate the database overload.

GRANT USAGE ON *.* TO 's51059' WITH MAX_USER_CONNECTIONS 10;

The server load went from 28 to 13 after applying the throttle.
Please remember that this is a shared environment and review the DB usage of your tool to not overload it affecting all other tools and users.

Event Timeline

The queries shouldn't be long running though. As a matter of fact the queries take less than a second to execute. But this is now causing problems for Cyberbot. Cyberbot runs multiple unique workers, and they each have their own DB connection.

This change has completely crashed cyberbot, as it is heavily DB dependent.

So what are my options?

I'm adding Ryan to this task, since it is IABot that's contributing to the load.

The problem are not single queries but the high number of them and the concurrency, let me gather some additional statistic to show it better.
If you will throttle on the application side the amount of queries performed by the tool I can increase or remove the limit.

Most metrics started to increase around 1am~2am UTC on March 31st, did you change anything around that time in the tool?
Given the shared environment of course the metric increase could also be related to changes in other tools as well.

The problem are not single queries but the high number of them and the concurrency, let me gather some additional statistic to show it better.
If you will throttle on the application side the amount of queries performed by the tool I can increase or remove the limit.

The problem is worker is a seperate bot. They don't know what the other is doing.

Most metrics started to increase around 1am~2am UTC on March 31st, did you change anything around that time in the tool?
Given the shared environment of course the metric increase could also be related to changes in other tools as well.

I don't know exactly when I migrated away from toollabs but when I did I increased the bot workers to 28 on the cyberbot project. Cyberbot no longer runs on tool labs, but it's DB is massive. It consists of over 20 million entries in 1 table?

Perhaps it's time to move the DB off of toollabs as well?

Here some 5 minutes stats related to the whole DB that changed when applying the throttle:

Before After
~360k  ~230k user queries
 ~37k   ~17k connections
 ~70k   ~25k updates

Given those numbers it probably make sense to consider moving the DB too, you can try to ask for some dedicated resource for it. I cannot guarantee such a request would be approved.

In the meanwhile can the tool work with less workers, maybe using the old configuration value before the last increase?

Here some 5 minutes stats related to the whole DB that changed when applying the throttle:

Before After
~360k  ~230k user queries
 ~37k   ~17k connections
 ~70k   ~25k updates

Given those numbers it probably make sense to consider moving the DB too, you can try to ask for some dedicated resource for it. I cannot guarantee such a request would be approved.

In the meanwhile can the tool work with less workers, maybe using the old configuration value before the last increase?

Where do I ask for that? In terms of workers, because enwiki is large 26 workers, each handling a set of articles that start with a letter of the alphabet, were started. One more specifically handles pages that are tagged dead, and the last handles all of Wikipedia to handle articles that don't start with a letter. I can disable the alphabet workers for the time being.

The problem is worker is a seperate bot. They don't know what the other is doing.

Erm, you really shouldn't be running API processes concurrently in general.

The problem is worker is a seperate bot. They don't know what the other is doing.

Erm, you really shouldn't be running API processes concurrently in general.

With Cyberbot having cached almost every link on every article, it may not need to. When Cyberbot analyzes a page with no new links, all it does is fetch the page text with the index.php script using action=raw. One of many goals in developing Cyberbot was to reduce overall API calls needed to get the job done. If it edits a page, that's 2 edit calls. If there is a new link without an access date, then the more expensive operation of determining the access dates kicks in. It does a binary sweep of the history over the API, and then saves that information for future use. Doing bench marking the API usage with workers is minimal, practically non-existent, while index is called for each page it analyzes. Also http://tools.wmflabs.org/deadlinks/ suggests that Cyberbot isn't doing much any more. So I highly believe that Cyberbot isn't stressing the API. The DB seems to be the new problem, as it is heavily dependent on the DB.

I have reduced the workers down to 2 for now.

For reference, after the above change the same stats of few posts above went to:

~180k user queries
  ~5k connections
  ~4k updates

Caveat: they could also be related to other tools activity, but the time aligns perfectly with the previous post.

I guess your tool is doing mostly updates given that the insert pattern is completely unrelated to those changes.

For reference, after the above change the same stats of few posts above went to:

~180k user queries
  ~5k connections
  ~4k updates

Caveat: they could also be related to other tools activity, but the time aligns perfectly with the previous post.

I guess your tool is doing mostly updates given that the insert pattern is completely unrelated to those changes.

Workers that can't connect to the DB automatically terminate execution as a fail safe. So you wouldn't see any stat changes. I suppose I could make updates, to make the connections persistent. Would that reduce CPU load?

IABot minimizes queries to the DB by batch fetching anticipated URLs to process, and fetches additional ones that were missed afterwards. It stores those results, for the remainder of the analysis and flags entries in the cache that are new and need to be added (INSERT), those that have been modified, (UPDATE), and those that weren't even used for analysis (DELETE). After that it compiles a set of commands which is executed by mysqli_multi_query. My knowledge of SQL is limited so I do one entry per INSERT/UPDATE/DELETE, so it amounts to a lot of SQL commands being sent to the DB. If there is a way to make that more economic, I'm all for it. I will gladly work to make IABot less stressful on the DB, provided there's no significant tradeoff to performance.

I would say it was just the amount of concurrent queries given the limited shared resources, I don't think persistent will help much in this case and might have other issues.
To improve the efficiency of the tool you could investigate if the table structure and the way it's accessed and modified are optimal (usual optimization techniques apply), check if sharding could help (both done directly by MySQL or at the application level), etc.
But all this is too dependent on how this tool works to be able to give specific advices.

I would say it was just the amount of concurrent queries given the limited shared resources, I don't think persistent will help much in this case and might have other issues.
To improve the efficiency of the tool you could investigate if the table structure and the way it's accessed and modified are optimal (usual optimization techniques apply), check if sharding could help (both done directly by MySQL or at the application level), etc.
But all this is too dependent on how this tool works to be able to give specific advices.

I'm not familiar with sharding, If your interested, the bot's entire DB class is managed in this file. https://github.com/cyberpower678/Cyberbot_II/blob/master/IABot/DB.php

@Volans since I have now reduced the workers to 2 can the throttle be removed again. You have my word that I won't restart more workers until he have a solution we can agree on. I have other bots that need access to the tool, and due to a bug in mysql for PHP connection refusals result in segfault. Not that that's a big deal, since I have my bots set to terminate when it can't connect to the DB, but I have other bots that need to access the DB, and throttle is definitely causing problems with other bot scripts.

@Cyberpower678, thanks for the collaboration, I've removed the limit for now:

GRANT USAGE ON *.* TO 's51059' WITH MAX_USER_CONNECTIONS 0;

I'll let you know in case the load raise again, but should not.

Just a heads up I'm running ALTER TABLE s51059__cyberbot.externallinks_enwiki ADD COLUMN last_deadCheck INT(10) UNSIGNED NOT NULL DEFAULT 0 AFTER live_state, DROP PRIMARY KEY, ADD PRIMARY KEY (url, pageid, live_state, archived, reviewed, archivable, notified, last_deadCheck), ADD INDEX LASTDEADCHECK (last_deadCheck ASC) /* SLOW_OK */; to add a column and index to the table cyberbot needs. The table is pretty big so the operation might run for a while, but this is a one time thing, so I hope you don't mind.

@Cyberpower678 I see that your table has a quite big composite primary key (8 fields including a varchar(767)) and various additional indexes.

Are you aware that in InnoDB primary keys are repeated for each secondary index?
Given that there is a unique key on the field url, is not clear to me the reason behind the composite primary key that contains url, given that there will always be only 1 row for a given url.
You have also an unused index on url that is not needed for 2 reasons, it's already the first part of the primary key and has also a unique index.

Consider using a single field and small primary key and to review the other indexes usage based on how the table is accessed.

@Cyberpower678 I see that your table has a quite big composite primary key (8 fields including a varchar(767)) and various additional indexes.

Are you aware that in InnoDB primary keys are repeated for each secondary index?
Given that there is a unique key on the field url, is not clear to me the reason behind the composite primary key that contains url, given that there will always be only 1 row for a given url.
You have also an unused index on url that is not needed for 2 reasons, it's already the first part of the primary key and has also a unique index.

Consider using a single field and small primary key and to review the other indexes usage based on how the table is accessed.

Admittedly, my experience with indexing tables is still limited. Could you dumb that down for me, so I can learn and understand?

So the table has finished altering.

@Volans I would appreciate your input on what I should on the table to make it better, in terms of indexing.

If you could explain those changes that would help me learn.

@Cyberpower678 apart some general advice at the end it really comes to the queries that the application will perform, how the data is inserted and searched and many other factors.

I'll do my best to find some time to come back to this but with Jaime on vacation and the switchover next week, I doubt I can make it in the next 2 weeks, I'm sorry.
Unfortunately we do not have the manpower to follow up and help tuning queries and schemas for every app/tool that runs on all the databases we manage.