简体   繁体   English

寻找资源管理/分配系统

[英]Looking for resource management/allocation system

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)).我需要的是一个系统,我可以在上面定义简单的对象(例如,一个“服务器”,可以有一个“操作系统”和“版本”字段,以及其他元数据(IP、MAC 地址等))。
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.我希望能够以安全的方式从系统请求对象,例如,如果我定义了一个“服务器”,可以由 3 个客户端同时使用,那么如果 4 个客户端同时请求一个服务器时间,人们将不得不等到服务器被释放。
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) .此外,我需要能够以某种查询样式执行请求,例如allocate(type=System, os='Linux', version=2.6)

Language doesn't matter too much, but Python is an advantage.语言并不重要,但 Python 是一个优势。

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!谢谢!

Resource limitation in concurrent applications - like your "up to 3 clients" example - is typically implemented by using semaphores (or more precisely, counting semaphores).并发应用程序中的资源限制——比如“最多 3 个客户端”的例子——通常是通过使用信号量(或更准确地说,计数信号量)来实现的。

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 .您可以在 Wikipedia 上阅读有关信号量的更多信息。 I'm not too familiar with Python but I think these two links can help:我对 Python 不太熟悉,但我认为这两个链接可以提供帮助:

For Java there is a very good standard library that has this functionality:对于 Java,有一个非常好的标准库具有此功能:

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

Just create a class with Semaphore field:只需创建一个带有 Semaphore 字段的类:

 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 ie create semaphore-based synchronization aspect.如果您希望事情更“可配置”,请考虑使用 AOP 技术,即创建基于信号量的同步方面。

Edit:编辑:

If you want completely standalone system, I guess you can try to use any modern DB (eg PostgreSQL) system that support row-level locking as semaphore.如果您想要完全独立的系统,我想您可以尝试使用任何支持行级锁定作为信号量的现代数据库(例如 PostgreSQL)系统。 For example, create 3 rows for each representing a server and select them with locking if they are free (eg " select * from server where is_used = 'N' for update "), mark selected server as used, unmark it in the end, commit transaction.例如,为每个代表一个服务器创建 3 行,如果它们空闲,则使用锁定选择它们(例如“ select * from server where is_used = 'N' for update ”),将所选服务器标记为已使用,最后取消标记,提交事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM