1998Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: Event handling (was: request for comments) -->
<!--X-From-R13: f001tzhNabin.jevtug.rqh -->
<!--X-Date: Fri, 09 Jan 1998 16:11:42 +0000 -->
<!--X-Message-Id: Pine.PMDF.3.95.980109094922.543180133A&#45;100000#nova,wright.edu -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: 34B532D0.CB8CB4B2#4cs,com -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, Event handling (was: request for comments)</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:s001gmu#nova,wright.edu">
</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="msg00140.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00142.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00122.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00144.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00141">Author</A>
&nbsp;|&nbsp;<A HREF="#00141">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00141">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>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>: Event handling (was: request for comments)</LI>
<LI><em>From</em>: <A HREF="mailto:s001gmu#nova,wright.edu">s001gmu#nova,wright.edu</A></LI>
<LI><em>Date</em>: Fri, 09 Jan 1998 11:12:10 -0500 (EST)</LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>

On Thu, 8 Jan 1998, Vadim Tkachenko wrote:

&gt; s001gmu#nova,wright.edu wrote:
&gt; &gt; 
&gt; &gt; On Mon, 5 Jan 1998, Vadim Tkachenko wrote:
&gt; &gt; &gt;
&gt; &gt; &gt;Jon A. Lambert wrote:
&gt; &gt; 
&gt; &gt; ...
&gt; &gt; 
&gt; &gt; &gt;&gt;  Variations on event-driven design
&gt; &gt; &gt;
&gt; &gt; &gt;Can you please elaborate on that?
&gt; &gt; 
&gt; &gt; I've spent the last month or so of my free time tossing around ideas
&gt; &gt; for our event driver, and I think I've finally settled on a general
&gt; &gt; model.  I have yet to implement and test all of it, so I'm sure I'll
&gt; &gt; be fine-tuning it, but here's where it stands for the moment:
&gt; &gt; 
&gt; &gt;   Definately Multi-threaded, though not as heavily as JC's model (so far).
&gt; &gt;   I'm aiming for a statically sized pool of about 10 threads to
&gt; &gt;   handle events, though that number is most certainly not set in
&gt; &gt;   stone.
&gt; 
&gt; Well, in my model there are no limit on the number of threads, usually
&gt; the number will consist of:
&gt; 
&gt; - Some small number of threads to wrap everything up (main server,
&gt; logger, console, etc.)

I was ignoring threads outside the event driver, as the original question
was a request for expansion on event drivers, not MT techniques... ;)
FWIW, I'll have at least 3-4 other threads running, total.

&gt; - One thread per connection listener (it's possible to install more than
&gt; one protocol adaptor, and therefore, connection listener)
&gt; - One thread per incoming connection

so... at least 2 threads per user?  What happens if you have a couple
hundred users?  This wouldn't work on a unix system, unless someone has a
thread lib w/o the system imposed max # of threads.

&gt; - N threads for db connections (implemented as a resource pool with
&gt; minSpare/maxSpare)
&gt; - As many threads as needed to implement some timed actions.
&gt; 
&gt; &gt;   Pending events are stored in a queue wrapped in a pretty simple structure
&gt; &gt;   containing a reference to the event object, an event ID, and the event's
&gt; &gt;   timestamp (along with all the stuff necessary to make a doubly linked list).
&gt; &gt;   Call it a Tuple if you like.
&gt; 
&gt; May I call it a Queue? This is a basic element in my model (as well as
&gt; everybody else's, I guess), but on the lower level.

Queue for the over-all structure (a bunch of &lt;things&gt; in a list).  The
&lt;things&gt; is what I was saying you could call a Tuple.  I called it:

class EWrapper {
public:
  event *E;
  EWrapper *next, *prev;

  EWrapper(event *e);
  ~Ewrapper();
}; 

the list of EWrapper instances is a Queue, the EWrapper itself is a Tuple,
techincally.

&gt; &gt;   Each 'tick', all events scheduled to be handled are pulled off the main
&gt; &gt;   queue into a secondary queue, where the thread pool attacks them (in no
&gt; &gt;   guaranteed order), each thread grabbing an event and calling it's Handle
&gt; &gt;   method.  Threads go back to sleep once all events in the secondary queue
&gt; &gt;   have been handled.
&gt; 
&gt; No ticks in my model - everything is completely asynchronous (though
&gt; synchronized). Each object takes care about itself, performing any
&gt; operations as soon as possible. The bottleneck is expected at the
&gt; database transaction (request? Sorry, I'm not as familiar with DBs as I
&gt; wish), so the abovementioned resource pool engages scheduling (some time
&gt; in the future I may think about PriorityQueue instead of simple Queue).

I believe either transaction or request is perfectly valid in the
context. :)

The decision to go with ticks and a limited number of threads to handle
the events for a tick stems directly from concerns of system load.  I
don't want to design a game that _requires_ it be the only significant
process running on a server.  Having large numbers of threads, especially
if the end implimentation uses a kernel threads library, only adds to the
cpu usage for that process. 

&gt; &gt;   How many threads, executing potentially I/O intensive DB Queries, can run
&gt; &gt;   simultaineously w/o bogging down the server?
&gt; 
&gt; Well, a while ago I've been told to implement a Web crawler for a site,
&gt; and the first thought was - just to go to the root URL, read and parse
&gt; it and as links appear, spawn other threads using the same algorithm.
&gt; 
&gt; Obviously, you can parse the HTTP stream much faster than server
&gt; produces it, and, well, www.best.com (a guinea pig for that task)
&gt; started to fail when, say, something like 50th request thread was
&gt; started. Funny, but on my side (Linux, JDK 1.02) nothing bad happened.

As I said, it's very much a result of a lot of factors, including, but not
limited to cpu speed, threads implementation, compiler, OS, the DB in
use... etc.

&gt; &gt;   but for my sanity I decided on a static pool, with a boot-time
&gt; &gt;   configurable number of threads. heck, I could prolly pretty easily
&gt; &gt;   add a command to allow admin to add some threads to the pool on the
&gt; &gt;   fly, but I prefer to minimize the cost of thread creation,
&gt; &gt;   localizing it to a boot-time expense.
&gt; 
&gt; I'd suggest to shift to minSpare/maxSpare thread management strategy.
&gt; Maybe, with some limit.
&gt; Once again, the source code is available :-)

I am unfamiliar with the minSpare/maxSpare management strategy... the
O'reily PThreads book didn't discuss it, and as that's the only book I
really have avail... :)  Care to give a brief tutorial, or point me to a
web page?  I am a tinkerer at heart, and would prefer an algorithm/design
strategy to source code, as I prefer to code my own stuff.  :)
 
&gt; &gt;   For the lockless v. locked model, my main concern was/is how many threads,
&gt; &gt;   executing at the same tick, are going to be handling events targeting the
&gt; &gt;   same data?  I figgure there won't be too many events executed 
&gt; &gt;   simultaineously that target the same data.
&gt; 
&gt; Why bother? Change the event handling strategy to asynchronous.

mmm.. even in an asynchronous event handler, you can get two events
targetting the same data executing at 'the same time'.  This requires
either a lockless/rollback method or a data locking method. 

I should probably restate my main argument, because the more I think about
it, the more the above argues for a lockless model... ;)  I think my real
motivation is that a locked model is easier to implement... IMHO.
 
&gt; BTW, question to all: Why the timeslice-, or tick-, -based event
&gt; handling model is still out there? See, I started to work seriously with
&gt; parallel tasks on OS/2, which is multithreaded by default, unlike UNIX
&gt; until lately, and I see no problems doing asynchronous event handling -
&gt; for 4 years now.
&gt; 
&gt; Why?

Well, when you come down to it, even a so-called asynchronous model is
still time-slice or tick-based.  the system's clock just has a very fine
grain.  I suppose it's a throw-back to paper RPGs.  I played a lot of them
for many years (really haven't played much the past couple... *sigh*), and
it's hard to break out of the mold they establish.  Most (if not all?) use
some form of a turn-based system, to allow for a changing time scale w/i
the game.  At times, whole weeks of game time pass in mere minutes of real
time, and at times it takes hours to work through a 20 second combat.
We are building our game as a turn-based game, so it makes sense that all
events w/i the game are handled on a turn-based event driver.  I see no
problem with asynchronous event handling for some admin commands, and will
prolly flag them as "to be handled immediately", but when I tell my
character to "cast fireball at bubba", it becomes trivial to implement the
delay inherent to the action by scheduling the event to go off 3 ticks
down the road.

yes, you could just as easily let bubba's character wait for however long
and then generate a 'fireball is cast at bubba from boffo' event, to be
handled immediately.  Either way works.  It's just a matter of where you
choose to do your time and potential interrupt handeling.

&gt; &gt;  It will happen, there's no doubt about that, but
&gt; &gt;   I feel the majority of events (executing at the same tick) will be aimed
&gt; &gt;   at different targets.  This assumption may well be proven wrong,
&gt; &gt;   but for now I can only go with it and see what happens.  :) 
&gt; 
&gt; Let me point out one possible common target: if you use the concept of
&gt; context, or environment, and it's not a fixed value, then ...
&gt; 
&gt; Like, time, weather in the area, gravity, etc.

mmm.. definately something to consider.  Thanks.  Anything else I missed,
anyone?  ;)
 
&gt; &gt;  If it proves to be the case
&gt; &gt;   that a large enough % of the events target the same data, I may adopt some
&gt; &gt;   schemes to try and shuffle events targeting the same data into the same
&gt; &gt;   threads, so they are executed sequentially by that thread, removing the
&gt; &gt;   issue.
&gt; 
&gt; Then caching data makes a good sense.

I plan on having active data in memory... The point of my original comment
was that rather than having two threads contending for a lock on a piece
of commonly used data, I can simplify by having all events for that tick,
targetting that data, handled sequentially by one thread, freeing one
thread for use by other events, targeting other data.
 
&gt; &gt;   I haven't spent much time on gracefull shut-down of the queue, or
&gt; &gt;   storing of the event list to rebuild a queue, but they are next on the
&gt; &gt;   hit parade.
&gt; 
&gt; Two options:
&gt; 
&gt; - save the event list and process it on the startup
&gt; - process all the list on shutdown. But, this situation may be more
&gt; difficult because events being processed may produce some more effects
&gt; and so on - for example, if you shout (RL) and then try to 'process the
&gt; event while shutting down', you're likely to:
&gt; 
&gt; - Hear the echoes,
&gt; - Which may, in turn, cause the avalanche,
&gt; - Which may, in turn, kill a lot of people around,
&gt; - Which, in turn, creates a LOT of other events to process,
&gt; - and so on.

*nod*  good point.  I just realized that one of the other things we
planned to do would work nicely with saving events and processing them at
re-start...  We planned on setting it up so that if you logout outside of
an inn or other "long-term storage" type safe-room, your character just
sits there...  so, in the event of a... less than expected shutdown, all
events are saved, all characters out and about are saved as such.  reboot,
all characters are put back, and all events are reloaded.  Rather nicely
takes care of problems like "cast fireball boffo" being reloaded with
neither the caster or the target being around. I can see players being
unhappy with the way things would work out, though.  It might be better
to just let the events die.

-Greg


</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="00143" HREF="msg00143.html">Re: [MUD-Dev]  Event handling (was: request for comments)</A></strong>
<ul compact><li><em>From:</em> Vadim Tkachenko &lt;vadimt#4cs,com&gt;</li></ul>
<li><strong><A NAME="00144" HREF="msg00144.html">Re: [MUD-Dev]  Event handling</A></strong>
<ul compact><li><em>From:</em> Shawn Halpenny &lt;malachai#iname,com&gt;</li></ul>
</UL></LI></UL>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00122" HREF="msg00122.html">Re: [MUD-Dev]  Event handling (was: request for comments)</A></STRONG>
<UL><LI><EM>From:</EM> Vadim Tkachenko &lt;vadimt#4cs,com&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00140.html">Re: [MUD-Dev] Commercial value of RP</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00142.html">Re: [MUD-Dev]  OT: DCOM and RMI</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00122.html">Re: [MUD-Dev]  Event handling (was: request for comments)</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00144.html">Re: [MUD-Dev]  Event handling</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00141"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00141"><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="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
<UL>
<LI><strong><A NAME="00153" HREF="msg00153.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, 21:51 GMT
</LI>
</UL>
</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>