Unix Technical Forum

Queue handling design pattern?

This is a discussion on Queue handling design pattern? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi! I'm looking for other good ideas how to implement an queue where multiple jobs. For now I have ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-06-2008, 03:04 PM
B D Jensen
 
Posts: n/a
Default Queue handling design pattern?

Hi!
I'm looking for other good ideas how to implement an queue where
multiple
jobs.

For now I have a single table with identity column and some
information about what to do. Combined with multiple jobs "eating"
from queue, e.g. they
take specific rows for that job corresponding to id % #queue
(modulus).

But each jobs take different time, so I see often that only half of
queues running
while long running jobs blocks for run of waiting jobs.

Best regards
Bjorn
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-06-2008, 03:04 PM
David Portas
 
Posts: n/a
Default Re: Queue handling design pattern?

On 4 Mar, 19:25, B D Jensen <bjorn.d.jen...@gmail.com> wrote:
> Hi!
> I'm looking for other good ideas how to implement an queue where
> multiple
> jobs.
>
> For now I have a single table with identity column and some
> information about what to do. Combined with multiple jobs "eating"
> from queue, e.g. they
> take specific rows for that job corresponding to id % #queue
> (modulus).
>
> But each jobs take different time, so I see often that only half of
> queues running
> while long running jobs blocks for run of waiting jobs.
>
> Best regards
> Bjorn


Have you considered using Service Broker?
http://msdn2.microsoft.com/en-us/library/ms166043.aspx

--
David Portas
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-06-2008, 03:04 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Queue handling design pattern?

B D Jensen (bjorn.d.jensen@gmail.com) writes:
> I'm looking for other good ideas how to implement an queue where
> multiple jobs.
>
> For now I have a single table with identity column and some information
> about what to do. Combined with multiple jobs "eating" from queue, e.g.
> they take specific rows for that job corresponding to id % #queue
> (modulus).
>
> But each jobs take different time, so I see often that only half of
> queues running while long running jobs blocks for run of waiting jobs.


THe unavoidable question is: what's wrong with Service Broker?


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-06-2008, 03:04 PM
--CELKO--
 
Posts: n/a
Default Re: Queue handling design pattern?

> For now I have a single table with an IDENTITY column [sic: table property] and some information about what to do. Combined with multiple jobs "eating" from queue <<

If you really want to mimic a queue in SQL for some strange reason,
then get out an old book on batch systems from the 1970's. You can
mimic them with a little effort:

CREATE TABLE Queue
(job_nbr INTEGER NOT NULL PRIMARY KEY,
job_submission_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
job_priority INTEGER DEFAULT 100 NOT NULL,
etc.);

You pick the job to run with something like this:

SELECT job_nbr, ...
FROM Queue AS Q1
WHERE Q1.job_nbr
= (SELECT MIN(job_nbr)
FROM Queue AS Q2
WHERE Q2.job_priority
=(SELECT MIN(Q3.job_priority)
FROM Queue AS Q3));

Every now and then, run a procedure that decrements the job_priority
of rows that have been in the queue for over (n) seconds until it is
zero and will be done next. If you want to force a job to the top of
the queue, insert it with a low (non-zero) job_priority value.
Remember to delete the jobs as they are processed. It is easy to see
that all jobs will clear the queue.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-06-2008, 03:04 PM
B D Jensen
 
Posts: n/a
Default Re: Queue handling design pattern?

Hi!
Thank to all ;-)
There's probably nothing wrong with Service Broker, actually I
expected there must be something usefull built-in :-)

What is better than "mimic a queue in SQL"?
Best regards
Bjorn
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-06-2008, 03:04 PM
--CELKO--
 
Posts: n/a
Default Re: Queue handling design pattern?

>> What is better than "mimic a queue in SQL"? <<

Service Broker or a batch operating system of some kind. Hey, they
work great on Mainframes!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-06-2008, 03:04 PM
B D Jensen
 
Posts: n/a
Default Re: Queue handling design pattern?

Hi again!
I'm still trying to find out how to use Service Broker (SB) -
I will see what I can find, but maybe you have time
to explain me:
can I make some custom ordering of queue usage, e.g. handling that
request from bosses are more important than standard
requests (for now i use simple ordering on table column storing info
about source of request)?

where to find a simple example for paralell batch processesing using
SB?

Thanks
Bjorn
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-07-2008, 03:29 PM
--CELKO--
 
Posts: n/a
Default Re: Queue handling design pattern?

>> I'm still trying to find out how to use Service Broker (SB) - I will see what I can find, but maybe you have time to explain me: <<

I would have to learn it first There are more experienced people
on this newsgroup that can help you.

>> can I make some custom ordering of queue usage, e.g. handling requests from bosses is more important than standard requests (for now I use simple ordering on table column storing info about source of request)? <<


That is the idea of the priority number I showed you in my posting. A
worker can come into the queue with (priority = 100), middle
management can come into the queue with (priority = 50) and the
company president can start at (priority = 0). But the decrement
procedure will assure that every job eventually gets to (priority =
0), so nobody is live-locked.

I did an article decades ago for INFORMATION SYSTEMS NEWS when I had a
column on software Engineering about the different queue handling
methods; I compared them to traffic signals. I don't know if I have
an electronic copy of it, but it was funny and might help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-07-2008, 03:29 PM
B D Jensen
 
Posts: n/a
Default Re: Queue handling design pattern?

Hi Joe!
I now looked a little more at SB, it seems a bit complex for this type
for this purpose (=> much more code than I had written in my
solution)...
.... but I probably should read more so I fully understand what it can
give...

I looked also at your select statement picking job to do, but my
problem is not to find out wich
job to process. I have 8 scheduled jobs. Each job/queue has its "own"
rows to process, but since some reports are faster than others, some
jobs will be delayed, while not all queues have something to do after
a while.

The reason why each row is related to a specifik job/queue is to
ensure, that only one single jobs tries to handle the request (row).
And I also like to see which job is processing which row, so I can
stop the right job,
if I need to stop processing manually because it tooks too long.

Thank you for your ideas ;-)
Best Regards
Bjorn
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-07-2008, 03:29 PM
B D Jensen
 
Posts: n/a
Default Re: Queue handling design pattern?

Hi Erland!
There is nothing wrong wil SB, and as I found out in SS2008 it also
support
"ordering" after priority.

It seems a bit complex, but thats probably because it's new to me ;-)
Thank you for your help
Best Regards
Bjorn
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:46 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com