18

I have a PHP script that retrieves rows from a database and then performs work based on the contents. The work can be time consuming (but not necessarily computationally expensive) and so I need to allow multiple scripts to run in parallel.

The rows in the database looks something like this:

+---------------------+---------------+------+-----+---------------------+----------------+
| Field               | Type          | Null | Key | Default             | Extra          |
+---------------------+---------------+------+-----+---------------------+----------------+
| id                  | bigint(11)    | NO   | PRI | NULL                | auto_increment |
.....
| date_update_started | datetime      | NO   |     | 0000-00-00 00:00:00 |                |
| date_last_updated   | datetime      | NO   |     | 0000-00-00 00:00:00 |                |
+---------------------+---------------+------+-----+---------------------+----------------+

My script currently selects rows with the oldest dates in date_last_updated (which is updated once the work is done) and does not make use of date_update_started.

If I were to run multiple instances of the script in parallel right now, they would select the same rows (at least some of the time) and duplicate work would be done.

What I'm thinking of doing is using a transaction to select the rows, update the date_update_started column, and then add a WHERE condition to the SQL statement selecting the rows to only select rows with date_update_started greater than some value (to ensure another script isn't working on it). E.g.

$sth = $dbh->prepare('
    START TRANSACTION;
    SELECT * FROM table WHERE date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;
    UPDATE table DAY SET date_update_started = UTC_TIMESTAMP() WHERE id IN (SELECT id FROM table WHERE date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;);
    COMMIT;
');
$sth->execute(); // in real code some values will be bound
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);

From what I've read, this is essentially a queue implementation and seems to be frowned upon in MySQL. All the same, I need to find a way to allow multiple scripts to run in parallel, and after the research I've done this is what I've come up with.

Will this type of approach work? Is there a better way?

Nate
  • 26,164
  • 34
  • 130
  • 214
  • How do you run the parallel scripts? – Lupin Dec 15 '14 at 21:30
  • @Lupin Currently the script is being executed every 15 minutes via a cron job. The script checks if another instance is running, and if so terminates. I'm not sure how I'll manage multiple scripts running yet -- I might have a counter in a database to see how many are running and limit the number of instances that way, but one problem at a time :-) – Nate Dec 15 '14 at 21:40
  • OK, some additional questions for me to fully understand: 1. You have a script that selects rows and work on them and then update it back to the DB, correct? 2. You want the ability to have parallel scripts running and doing the same but on different rows, right? 3. Each time the script runs , are the rows selected are continous, meaning are they 1-100, 101-200 etc or are they random in terms of the id and selected just by those that date_update_started is greater than 1? – Lupin Dec 16 '14 at 11:20
  • @Lupin 1. Yes, 2. Yes, 3. The rows are selected based on the date field and another field not shown in the example. So they aren't strictly "continuous," but they are ordered by two fields. – Nate Dec 16 '14 at 16:46
  • A different approach would be to have some kind of master script fetch some rows (like `SELECT ... LIMIT 5`, for example) and then start an individual instance of the processing script for each of these rows. You could even use a second table to keep track of how many processing instances are currently running, so whenever cron starts your master script it would know how many rows to fetch. But as this is not even close to what you asked for, I decided to add it as a comment instead of an answer. – Patrick Echterbruch Dec 18 '14 at 17:15

6 Answers6

8

I think your approach could work, as long as you also add some kind of identifier to the rows you selected that they are currently been worked on, it could be as @JuniusRendel suggested and i would even think about using another string key (random or instance id) for cases where the script resulted in errors and did not complete gracefully, as you will have to clean these fields once you updated the rows back after your work.

The problem with this approach as i see it is the option that there will be 2 scripts that run at the same point and will select the same rows before they were signed as locked. here as i can see it, it really depends on what kind of work you do on the rows, if the end result in these both scripts will be the same, i think the only problem you have is for wasted time and server memory (which are not small issues but i will put them aside for now...). if your work will result in different updates on both scripts your problem will be that you could have the wrong update at the end in the TB.

@Jean has mentioned the second approach you can take that involves using the MySql locks. i am not an expert of the subject but it seems like a good approach and using the 'Select .... FOR UPDATE' statement could give you what you are looking for as you could do on the same call the select & the update - which will be faster than 2 separate queries and could reduce the risk for other instances to select these rows as they will be locked.

The 'SELECT .... FOR UPDATE' allows you to run a select statement and lock those specific rows for updating them, so your statement could look like:

START TRANSACTION;
   SELECT * FROM tb where field='value' LIMIT 1000 FOR UPDATE;
   UPDATE tb SET lock_field='1' WHERE field='value' LIMIT 1000;
COMMIT;

Locks are powerful but be careful that it wont affect your application in different sections. Check if those selected rows that are currently locked for the update, are they requested somewhere else in your application (maybe for the end user) and what will happen in that case.

Also, Tables must be InnoDB and it is recommended that the fields you are checking the where clause with have a Mysql index as if not you may lock the whole table or encounter the 'Gap Lock'.

There is also a possibility that the locking process and especially when running parallel scripts will be heavy on your CPU & memory.

here is another read on the subject: http://www.percona.com/blog/2006/08/06/select-lock-in-share-mode-and-for-update/

Hope this helps, and would like to hear how you progressed.

Lupin
  • 1,225
  • 11
  • 17
5

We have something like this implemented in production.

To avoid duplicates, we do a MySQL UPDATE like this (I modified the query to resemble your table):

UPDATE queue SET id = LAST_INSERT_ID(id), date_update_started = ... 
WHERE date_update_started IS NULL AND ...
LIMIT 1;

We do this UPDATE in a single transaction, and we leverage the LAST_INSERT_ID function. When used like that, with a parameter, it writes in the transaction session the parameter that, in this case, it's the ID of the single (LIMIT 1) queue that has been updated (if there is one).

Just after that, we do:

SELECT LAST_INSERT_ID();

When used without parameter, it retrieves the previously stored value, obtaining the queue item's ID that has to be performed.

Alessandro Lai
  • 2,254
  • 2
  • 24
  • 32
1

Edit: Sorry, I totally misunderstood your question

You should just put a "locked" column on your table put the value to true on the entries your script is working with, and when it's done put it to false.

In my case i have put 3 other timestamp (integer) columns: target_ts , start_ts , done_ts. You

UPDATE table SET locked = TRUE WHERE target_ts<=UNIX_TIMESTAMP() AND ISNULL(done_ts) AND ISNULL(start_ts);

and then

SELECT * FROM table WHERE target_ts<=UNIX_TIMESTAMP() AND ISNULL(start_ts) AND locked=TRUE;

Do your jobs and update each entry one by one (to avoid data inconcistencies) setting the done_ts property to current timestamp (you can also unlock them now). You can update target_ts to the next update you wish or you can ignore this column and just use done_ts for your select

n00dl3
  • 21,213
  • 7
  • 66
  • 76
  • I don't think PHP actually supports multithreading, but in any case getting multiple instances of the script to run isn't the issue. The question is primarily how to handle retrieving rows from the DB. – Nate Dec 16 '14 at 16:47
  • I updated, sorry maybe I was drunk :). For the threads, I don't know, that is what the PECL extension claims, but i didn't test it, so... – n00dl3 Dec 16 '14 at 17:26
1

Each time the script runs I would have the script generate a uniqid.

$sctiptInstance = uniqid();

I would add a script instance column to hold this value as a varchar and put an index on it. When the script runs I would use select for update inside of a transaction to select your rows based on whatever logic, excluding rows with a script instance, and then update those rows with the script instance. Something like:

START TRANSACTION;
SELECT * FROM table WHERE script_instance = '' AND date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000 FOR UPDATE;
UPDATE table SET date_update_started = UTC_TIMESTAMP(), script_instance = '{$scriptInstance}' WHERE script_instance = '' AND date_update_started > 1 DAY ORDER BY date_last_updated LIMIT 1000;
COMMIT;

Now those rows will be excluded from other instances of the script. Do you work, and then update the rows to set the script instance back to null or blank, and also update your date last updated column.

You could also use the script instance to write to another table called "current instances" or something like that, and have the script check that table to get a count of running scripts to control the number of concurrent scripts. I would add the PID of the script to the table as well. You could then use that information to create a housekeeping script to run from cron periodically to check for long running or rogue processes and kill them, etc.

Craig Jacobs
  • 940
  • 2
  • 16
  • 28
1

I have a system working exactly like this in production. We run a script every minute to do some processing, and sometimes that run can take more than a minute.

We have a table column for status, which is 0 for NOT RUN YET, 1 for FINISHED, and other value for under way.

The first thing the script does is to update the table, setting a line or multiple lines with a value meaning that we are working on that line. We use getmypid() to update the lines that we want to work on, and that are still unprocessed.

When we finish the processing, the script updates the lines that have the same process ID, marking them as finished (status 1).

This way we avoid each of the scripts to try and process a line that is already under processing, and it works like a charm. This doesn't mean that there isn't a better way, but this does get the work done.

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
1

I have used a stored procedure for very similar reasons in the past. We used the FOR UPDATE read lock to lock the table while a selected flag was updated to remove that entry from any future selects. It looked something like this:

CREATE PROCEDURE `select_and_lock`()
 BEGIN
  START TRANSACTION;
  SELECT your_fields FROM a_table WHERE some_stuff=something 
   AND selected = 0 FOR UPDATE;
  UPDATE a_table SET selected = 1;
  COMMIT;
 END$$

No reason it has to be done in a stored procedure though now I think about it.

Mike Miller
  • 3,071
  • 3
  • 25
  • 32