views:

135

answers:

2

What I need is a system I can define simple objects on (say, a "Server" than can have an "Operating System" and "Version" fields, alongside other metadata (IP, MAC address, etc)).
I'd like to be able to request objects from the system in a safe way, such that if I define a "Server", for example, can be used by 3 clients concurrently, then if 4 clients ask for a Server at the same time, one will have to wait until the server is freed.
Furthermore, I need to be able to perform requests in some sort of query-style, for example allocate(type=System, os='Linux', version=2.6).

Language doesn't matter too much, but Python is an advantage.

I've been googling for something like this for the past few days and came up with nothing, maybe there's a better name for this kind of system that I'm not aware of.

Any recommendations?

Thanks!

A: 

Resource limitation in concurrent applications - like your "up to 3 clients" example - is typically implemented by using semaphores (or more precisely, counting semaphores).

You usually initialize a semaphore with some "count" - that's the maximum number of concurrent accesses to that resource - and you decrement this counter every time a client starts using that resource and increment it when a client finishes using it. The implementation of semaphores guarantees the "increment" and "decrement" operations will be atomic.

You can read more about semaphores on Wikipedia. I'm not too familiar with Python but I think these two links can help:

Oak
Thank you. I know of semaphores. I'm searching for a system that already does the work for me.
abyx
A: 

For Java there is a very good standard library that has this functionality:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html

Just create a class with Semaphore field:

 class Server {
   private static final MAX_AVAILABLE = 100;
   private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);
   // ... put all other fields (OS, version) here...
   private Server () {}

   // add a factory method
   public static Server getServer() throws InterruptedException {
     available.acquire();
     //... do the rest here
   }

 }

Edit:

If you want things to be more "configurable" look into using AOP techniques i.e. create semaphore-based synchronization aspect.

Edit:

If you want completely standalone system, I guess you can try to use any modern DB (e.g. PostgreSQL) system that support row-level locking as semaphore. For example, create 3 rows for each representing a server and select them with locking if they are free (e.g. "select * from server where is_used = 'N' for update"), mark selected server as used, unmark it in the end, commit transaction.

Alexey Kalmykov
Yes, what you're describing in the DB solution is pretty much writing my own solution... Thanks anyway
abyx