1998Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: Re: [MUD&#45;Dev]  Event handling (was: request for comments) -->
<!--X-From-R13: Hnqvz Fxnpuraxb <inqvzgN4pf.pbz> -->
<!--X-Date: Thu, 08 Jan 1998 20:13:01 +0000 -->
<!--X-Message-Id: 34B532D0.CB8CB4B2#4cs,com -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: Pine.PMDF.3.95.980108113739.543180857A&#45;100000#nova,wright.edu -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, Re: [MUD-Dev]  Event handling (was: request for comments)</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:vadimt#4cs,com">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00121.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00123.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00117.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00141.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00122">Author</A>
&nbsp;|&nbsp;<A HREF="#00122">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00122">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>Re: [MUD-Dev]  Event handling (was: request for comments)</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#null,net">mud-dev#null,net</A></LI>
<LI><em>Subject</em>: Re: [MUD-Dev]  Event handling (was: request for comments)</LI>
<LI><em>From</em>: Vadim Tkachenko &lt;<A HREF="mailto:vadimt#4cs,com">vadimt#4cs,com</A>&gt;</LI>
<LI><em>Date</em>: Thu, 08 Jan 1998 14:10:56 -0600</LI>
<LI><em>Sender</em>: <A HREF="mailto:vt#vadimt,4cs.com">vt#vadimt,4cs.com</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
s001gmu#nova,wright.edu wrote:
&gt; 
&gt; On Mon, 5 Jan 1998, Vadim Tkachenko wrote:
&gt; &gt;
&gt; &gt;Jon A. Lambert wrote:
&gt; 
&gt; ...
&gt; 
&gt; &gt;&gt;  Variations on event-driven design
&gt; &gt;
&gt; &gt;Can you please elaborate on that?
&gt; 
&gt; I've spent the last month or so of my free time tossing around ideas for our
&gt; event driver, and I think I've finally settled on a general model.  I have yet
&gt; to implement and test all of it, so I'm sure I'll be fine-tuning it, but here's
&gt; where it stands for the moment:
&gt; 
&gt;   Definately Multi-threaded, though not as heavily as JC's model (so far).
&gt;   I'm aiming for a statically sized pool of about 10 threads to handle events,
&gt;   though that number is most certainly not set in stone.

Well, in my model there are no limit on the number of threads, usually
the number will consist of:

- Some small number of threads to wrap everything up (main server,
logger, console, etc.)
- One thread per connection listener (it's possible to install more than
one protocol adaptor, and therefore, connection listener)
- One thread per incoming connection
- N threads for db connections (implemented as a resource pool with
minSpare/maxSpare)
- As many threads as needed to implement some timed actions.

&gt;   Pending events are stored in a queue wrapped in a pretty simple structure
&gt;   containing a reference to the event object, an event ID, and the event's
&gt;   timestamp (along with all the stuff necessary to make a doubly linked list).
&gt;   Call it a Tuple if you like.

May I call it a Queue? This is a basic element in my model (as well as
everybody else's, I guess), but on the lower level.

&gt;   Each 'tick', all events scheduled to be handled are pulled off the main
&gt;   queue into a secondary queue, where the thread pool attacks them (in no
&gt;   guaranteed order), each thread grabbing an event and calling it's Handle
&gt;   method.  Threads go back to sleep once all events in the secondary queue
&gt;   have been handled.

No ticks in my model - everything is completely asynchronous (though
synchronized). Each object takes care about itself, performing any
operations as soon as possible. The bottleneck is expected at the
database transaction (request? Sorry, I'm not as familiar with DBs as I
wish), so the abovementioned resource pool engages scheduling (some time
in the future I may think about PriorityQueue instead of simple Queue).

&gt;   How many threads, executing potentially I/O intensive DB Queries, can run
&gt;   simultaineously w/o bogging down the server?

Well, a while ago I've been told to implement a Web crawler for a site,
and the first thought was - just to go to the root URL, read and parse
it and as links appear, spawn other threads using the same algorithm.

Obviously, you can parse the HTTP stream much faster than server
produces it, and, well, www.best.com (a guinea pig for that task)
started to fail when, say, something like 50th request thread was
started. Funny, but on my side (Linux, JDK 1.02) nothing bad happened.

&gt;   but for my sanity I decided on a static pool, with a boot-time configurable
&gt;   number of threads. heck, I could prolly pretty easily add a command to
&gt;   allow admin to add some threads to the pool on the fly, but I prefer to
&gt;   minimize the cost of thread creation, localizing it to a boot-time expense.

I'd suggest to shift to minSpare/maxSpare thread management strategy.
Maybe, with some limit.
Once again, the source code is available :-)

&gt;   For the lockless v. locked model, my main concern was/is how many threads,
&gt;   executing at the same tick, are going to be handling events targeting the
&gt;   same data?  I figgure there won't be too many events executed simultaineously
&gt;   that target the same data.

Why bother? Change the event handling strategy to asynchronous.

BTW, question to all: Why the timeslice-, or tick-, -based event
handling model is still out there? See, I started to work seriously with
parallel tasks on OS/2, which is multithreaded by default, unlike UNIX
until lately, and I see no problems doing asynchronous event handling -
for 4 years now.

Why?

&gt;  It will happen, there's no doubt about that, but
&gt;   I feel the majority of events (executing at the same tick) will be aimed at
&gt;   different targets.  This assumption may well be proven wrong, but for now I
&gt;   can only go with it and see what happens.  :) 

Let me point out one possible common target: if you use the concept of
context, or environment, and it's not a fixed value, then ...

Like, time, weather in the area, gravity, etc.

&gt;  If it proves to be the case
&gt;   that a large enough % of the events target the same data, I may adopt some
&gt;   schemes to try and shuffle events targeting the same data into the same
&gt;   threads, so they are executed sequentially by that thread, removing the
&gt;   issue.

Then caching data makes a good sense.

&gt;   I haven't spent much time on gracefull shut-down of the queue, or storing of
&gt;   the event list to rebuild a queue, but they are next on the hit parade.

Two options:

- save the event list and process it on the startup
- process all the list on shutdown. But, this situation may be more
difficult because events being processed may produce some more effects
and so on - for example, if you shout (RL) and then try to 'process the
event while shutting down', you're likely to:

- Hear the echoes,
- Which may, in turn, cause the avalanche,
- Which may, in turn, kill a lot of people around,
- Which, in turn, creates a LOT of other events to process,
- and so on.

&gt; -Greg

-- 
Still alive and smile stays on,
Vadim Tkachenko &lt;VadimT#4CS,Com&gt;
--
UNIX _is_ user friendly, he's just very picky about who his friends are

</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<ul compact><li><strong>Follow-Ups</strong>:
<ul>
<li><strong><A NAME="00169" HREF="msg00169.html">Re: [MUD-Dev] Event handling (was: request for comments)</A></strong>
<ul compact><li><em>From:</em> JC Lawrence &lt;claw#under,Eng.Sun.COM&gt;</li></ul>
<li><strong><A NAME="00141" HREF="msg00141.html">Event handling (was: request for comments)</A></strong>
<ul compact><li><em>From:</em> s001gmu#nova,wright.edu</li></ul>
</UL></LI></UL>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00117" HREF="msg00117.html">request for comments (was: Mud-Dev FAQ)</A></STRONG>
<UL><LI><EM>From:</EM> s001gmu#nova,wright.edu</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00121.html">Re: [MUD-Dev]  MUD Economy</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00123.html">Re: [MUD-Dev]  Totally OT... (Or is it?) (yes it is ;)</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00117.html">request for comments (was: Mud-Dev FAQ)</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00141.html">Event handling (was: request for comments)</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00122"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00122"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>Re: [MUD-Dev]  request for comments (was: Mud-Dev FAQ)</STRONG>, <EM>(continued)</EM>
<ul compact>
<ul compact>
<ul compact>
<LI><strong><A NAME="00050" HREF="msg00050.html">Re: [MUD-Dev]  request for comments (was: Mud-Dev FAQ)</A></strong>, 
coder <a href="mailto:coder#ibm,net">coder#ibm,net</a>, Tue 06 Jan 1998, 05:32 GMT
</LI>
<LI><strong><A NAME="00078" HREF="msg00078.html">Re: [MUD-Dev] request for comments (was: Mud-Dev FAQ)</A></strong>, 
JC Lawrence <a href="mailto:claw#under,Eng.Sun.COM">claw#under,Eng.Sun.COM</a>, Wed 07 Jan 1998, 00:52 GMT
<UL>
<LI><strong><A NAME="00223" HREF="msg00223.html">[MUD-Dev] Event Handling</A></strong>, 
Jon A. Lambert <a href="mailto:jlsysinc#ix,netcom.com">jlsysinc#ix,netcom.com</a>, Tue 13 Jan 1998, 03:35 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00117" HREF="msg00117.html">request for comments (was: Mud-Dev FAQ)</A></strong>, 
s001gmu <a href="mailto:s001gmu#nova,wright.edu">s001gmu#nova,wright.edu</a>, Thu 08 Jan 1998, 16:43 GMT
<UL>
<LI><strong><A NAME="00122" HREF="msg00122.html">Re: [MUD-Dev]  Event handling (was: request for comments)</A></strong>, 
Vadim Tkachenko <a href="mailto:vadimt#4cs,com">vadimt#4cs,com</a>, Thu 08 Jan 1998, 20:13 GMT
<UL>
<LI><strong><A NAME="00141" HREF="msg00141.html">Event handling (was: request for comments)</A></strong>, 
s001gmu <a href="mailto:s001gmu#nova,wright.edu">s001gmu#nova,wright.edu</a>, Fri 09 Jan 1998, 16:11 GMT
<UL>
<LI><strong><A NAME="00144" HREF="msg00144.html">Re: [MUD-Dev]  Event handling</A></strong>, 
Shawn Halpenny <a href="mailto:malachai#iname,com">malachai#iname,com</a>, Fri 09 Jan 1998, 17:50 GMT
<UL>
<LI><strong><A NAME="00147" HREF="msg00147.html">Re: [MUD-Dev]  Event handling</A></strong>, 
Vadim Tkachenko <a href="mailto:vadimt#4cs,com">vadimt#4cs,com</a>, Fri 09 Jan 1998, 19:02 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00143" HREF="msg00143.html">Re: [MUD-Dev]  Event handling (was: request for comments)</A></strong>, 
Vadim Tkachenko <a href="mailto:vadimt#4cs,com">vadimt#4cs,com</a>, Fri 09 Jan 1998, 17:50 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</UL>
</LI>
</ul>
</ul>
</ul>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>