我有一个队列,其中包含根据作业类型进入不同执行程序池的作业。该队列位于数据库表中,包含来自具有优先级的不同客户端的作业等。我省略了一些与问题无关的细节。 在某些时候,不同的客户端同时以相同的优先级将许多作业放入队列中,例如大约 15-20'000 个作业。
在当前的实现中,作业是根据此标准使用 hibernate 获取的,并且为了简单起见,我再次省略了一些限制。
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -minutes);
Criteria c = getSession().createCriteria(QueueEntry.class)
.add(Restrictions.eq("processing", false))
.add(Restrictions.or(Restrictions.ge("serverTimestamp", cal.getTime()), Restrictions.ge("sentTimestamp", cal.getTime())))
.add(Restrictions.lt("attemps", attemps))
.addOrder(Order.asc("priority"))
.addOrder(Order.asc("serverTimestamp"))
.setMaxResults(limit);
在当前情况下,如果客户端A在10:00:00插入15k个任务,客户端B在10:00:05(5秒后)插入3k个相同优先级的任务,B的任务将在这些任务之后获取并执行的 A。
我需要在客户端之间平衡获取的作业(队列表中有一个“客户端”列)——例如,如果吞吐量为 10 个任务/秒,则要获得 A 的 5 个任务和 B 的 5 个任务。当客户端B没有任务时,获取A的10个任务。
是否有一些简单的方法或技巧可以对查询执行此操作?数据库是 Postgres。
最佳答案
我认为您无法通过修改现有的 Criteria
或仅使用一个查询来实现。为了防止客户端饥饿,您必须为每个客户端创建单独的资源池,这是 Fair Scheduler for Hadoop 采取的方法。 :
The fair scheduler organizes jobs into pools, and divides resources fairly between these pools. By default, there is a separate pool for each user, so that each user gets an equal share of the cluster. It is also possible to set a job's pool based on the user's Unix group or any jobconf property. Within each pool, jobs can be scheduled using either fair sharing or first-in-first-out (FIFO) scheduling.
您可以运行查询以获取不同客户端的列表以及等待作业的总数。根据不同的客户端数量划分全局作业限制,并在单独的查询中获取每个给定客户端的等待作业。
关于java - 数据处理平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55352253/