Asynchronous Semaphore

Semaphore

Semaphore is a super simple notion, in short it is a “method of controlling access by several processes to a common resource”

Think about a Database that can provide no more than 5 parallel connections OR a bit more complex, a buffer that has X slots with few parallel readers and writers..  ( readers-writers problem)

When process A “need” a unit of a specific resource, process A calls the P() method which checks: if the semaphore value is larger than 0 (which mean that the resource has some free units) then it decreases the semaphore in order to state that one of the units is now taken. otherwise P() makes process A to wait until some other process increases the semaphore value.

When process A doesn’t need the unit anymore, process A increases the semaphore value ( by calling the V() method which also signal the other processes that have been blocked).

As for naming conventions, in case the resource has only one unit we call it a binary semaphore otherwise it’s a counting semaphore.

** it’s important to mention that P() and V() are atomic methods.

Asynchronous Semaphore

This was a short description of a Semaphore, but what is Asynchronous Semaphore (hereinafter: ASemaphore)?

In the asynchronous programming world, sometimes we would like to call N async methods, imagine that we would like to invoke a different F method (async or sync) only after ALL the N methods got invoked.

Asynchronous Semaphore is a construct that support this requirement.

(In short asynchronous method is a method that will get invoked later on in time, yet the thread doesn’t block and wait() for its results, the thread runtime continues to the next line of code. the async method usually get a “callback() method” as a parameter and invoke it when the results arrive.  ( E.g. setTimeout,  DHTML events, most WebServices invocations))

The ASemaphore works a bit different than the Semaphore, it gets the F function as a parameter , and it fires it only when the counting variable equals to zero, before calling a async method we increases the counter and when the async method get invoked we decreases the counter.

Here are the details: One should initialize the ASemaphore with the F method and increases it (calling V()) to one.

Before calling a async method, One should increase the ASemaphore (V())

Internally to the callback() function one should decrease the semaphore (P()).

After calling all the N async methods, One should decrease (p()) the ASemaphore.

The Asemaphore fires() only when the counting variable equals to ZERO.

* notice that the ASemaphore is a solution for the asynchronous world for a specific process and therefor parallelism is not an issue ( the methods P() and V() aren’t atomic)
** notice that it’s irrelevant to use a binary ASemaphore, in case of only 1 async call you could easily invoke the F() method internal to the async method callback()

Examples

1) In my Alligator project, when rendering a page on the server-side, one might develop a JSSP scirpt that invokes few asynce calls (e.g. putting a variable on the memcahced, dealing with the file system, connecting a database), yet Alligator needs the send the HTTP response back to the client only when all those methods have been invoked…

2) One had like to call 10 timers with random time and notify only when the last one is all set.

JavaScript Impl’

I have implemented an ASeamphore in JavaScript, you can find it on my github repository.

The actual code can be found here

var ASemaphore = function(semaphore,fireFunc){
	this.semaphore = (semaphore==undefined?0:semaphore);
	this.fire = fireFunc;
	log.debug("Starting Asemaphore with semaphore:" +this.semaphore);
};

ASemaphore.prototype.v = function(){
	++this.semaphore;
	log.debug("Asemaphore semaphore after v(): " +this.semaphore);
	return this.semaphore;
};

ASemaphore.prototype.p = function(){
	if((--this.semaphore)<1){
		log.debug("Asemaphore fire() after p()");
		this.fire.apply(this,arguments);
	}
	//log.debug("Asemaphore counter after p(): " +this.counter);
	return this.counter;
};
Advertisements

JSSP – JavaScript Server Page engine on top of NodeJS (Alligator)

I would like to introduce Alligator, a JSP/PHP/ASP-style template engine for JavaScript on top of NodeJS.

Most web-apps have been written in Java, PHP, ASP.net, Ruby or Python, on the server side, and JavaScript on the client side.

This approach of using different technologies and different programming languages for the different “sides” has few drawbacks:

1) Serialization of objects from one language to another has to be taken care of , e.g. take a look at GSON, a library that translates Java-to-JSON and vice versa.

2) Client-side code can’t be reused on the server-side and vice versa.

3) Web-apps “factories” has to hire different people with different expertises.

4) I bet you can think of at least 2 more drawbacks

Other than that, the multi-threaded approach that most of the current web-server/application-server utilize is problematic for load since it is limited by the maximum number of OS thread per machine.

NodeJS solved the later issue, by taking advantage of asynchronous I/O and the event-loop paradigm ( I will blog about it later on.. )

Moreover, NodeJS, let you write JavaScript code that runs outside a browser (utilizing the V8 runtime) + it includes many useful built-in Async I/O libraries as HttpServer and File System I/O management.

In the last few weeks, I have developed Alligator on top of this top-notch technology.

Alligator enable you to write PHP-style code but in the coolest and most popular language, JavaScript!!! 🙂

Let’s “Hello Word”-ing using Alligator:

<HTML>
   <BODY>
   <?="Hello World"?>
   </BODY>
</HTML>

In this example the <?= code?> just print whatever code.toString() evaluates to, yet it does that on the Server-Side..

Lets take a look at something a bit more “complex”:

<HTML>
   <BODY>
      <?  var test = 1;
          if (1 == test){?>
          YOOOO!!!!
          <?}?>
   </BODY>
</HTML>

Well, it’s pretty straight forward, the <? code ?> get rendered on the server-side, which mean that if test equals 1
then the String “YOOOO!!!!” will be written to the HTTP response.

Alligatror supports 3 type of object scopes, request, session and application, both session and application support set() and get() methods that receive key, value (if needed) and a callback function.  **One must pay attention to the fact that a callback function might get invoked later on in time regardless it’s chronological line in the code block.

The request parameters can be accessed easily using request.parameters.myKey..

Lets see the counter example which count how many people have been visit this page since the application is alive.

In this example, we have separated the logic and view and used the “commands.forward() built-in command” in order to pass the logic outcome to the view..

logic.jssp:

<?
	var counter = 1;
	application.get("counter",function(value){
		log.debug("ApplicationLOGIC.JSSP, value - " +value);
		if(value == undefined){
			application.set("counter",1);
		}else{
			counter = value+1;
			application.set("counter",counter);
		}
		request.parameters.counter = counter;
		commands.forward("counter/view.jssp");
	});
?>

view.jssp:

<HTML>
	<HEAD><TITLE>Application Scope Counter Tester</TITLE></HEAD>
	<BODY>
	<?
		var counter = request.parameters.counter;
		if(counter==1)
			commands.write("First Time");
		else
			commands.write("Number of users:" + counter);

	?>
	</BODY>
</HTML>
 

The main drawback of NodeJS is that it utlize only O(1) threads per instance, which mean that only one core can be used in a multi-core CPU system, in order to solver it, Alligator is using multi-node and memcached.

Multi-node duplicates the Node process as many time as you want, the smart thing to do is to set it to the number of available cores.

Memcached served as a shared memory between those processes.

One can easily set up Alligator to “work” on a distributed network, using a load balancer.

This is pretty existing, isn’t it? How can you start using it?
1 ) Install NodeJS – http://nodejs.org/#download

2 ) Extract Alligator somewhere – http://github.com/mrohad/Alligator/downloads

3 ) Setup alligator, by modifying settings.json -> read more here – http://github.com/mrohad/Alligator
the main thing you need to set is the root folder and the port.
I must mention that Alligator is built on top of anti-node and serves static files as well!

One can setup the server-side tags notations, for instance if you are a Java-person you can change the <?= code?> notation to <%=code%> in a super easy fashion.

Also, Alligator enables you to change the extension of the server-side script files, the default is JSSP.

If you would like to utilize a multi-core architecture, you should download memcached and set up the memcached values ( server, port ..)

Example for a setting file:

{
	"web_app_name" : "Alligator Test App",
	"port"         : 80,
	"path"         : {
				"root":"/home/vadmin/ws/Alligator/WWW/",
				"lib":"/home/vadmin/ws/Alligator/WWWlib/"
			},
	"server_script": {
				"ext":"jssp",
				"begin":"<?",
				"begin_additional_write":"=",
				"end":"?>",
				"session_minutes":30,
				"memcached":{
						"enable":1,
						"server":"localhost",
						"port":11211
					}
			 },
	"debug_mode"   : 1,
	"nodes"	      : 2
}

5 ) Run memcached ( if needed)

6 ) Run Alligator – $ sudo node alligator.js

7 ) Add your first .jssp file to the root folder

8 ) Test it , go to http://yourServer:port/yourfile.jssp

We are looking for people who would like to contribute code and suggest featuressssssss 🙂

Enjoy!!