1

出于某种原因,我们计划在我们的项目中使用红隼队列。我们做了一些恶魔,主要问题是如何有效地从 CPU 利用率低的队列中获取数据。我们实现获取的方式是,如果我们从队列中获取数据失败超过 5 次,我们将线程休眠 100 毫秒以降低 CPU 利用率。

while (running) {
            try {
                LoginLogQueueEntry data = kestrelQueue.fetch();
                if (null != data && data.isLegal()) {
                    entryCacheList.add(data); //add the data to the local caceh
                    resetStatus();
                } else {
                    failedCount++;
                    //if there is no data in the kestrel and the local cache is not empty, insert the data into mysql database
                    if (failedCount == 1 && !entryCacheList.isEmpty()) {
                        resetStatus();
                        insertLogList(entryCacheList); // insert current data into database
                        entryCacheList.clear(); //empty local cache
                    }

                    if (failedCount >= 5 && entryCacheList.isEmpty()) {
                    //fail 5 times. Sleep current thread. 
                        failedCount = 0;
                        Thread.sleep((sleepTime + MIN_SLEEP_TIME) % MAX_SLEEP_TIME);
                    }
                }
                //Insert 1000 rows once
                if (entryCacheList.size() >= 1000) {
                    insertLogList(entryCacheList);
                    entryCacheList.clear();
                }
            } catch (Exception e) {
                logger.warn(e.getMessage());
            }

有没有其他好的方法可以做到这一点?我认为完美的方式是队列可以通知工作人员我们得到数据并获取它们。

4

3 回答 3

3

请参阅http://robey.lag.net/2008/11/27/scarling-to-kestrel.html上的“阻止获取”部分

于 2011-09-22T14:49:31.010 回答
2

此处描述了阻塞读取,在“Memcache 命令”下:https ://github.com/robey/kestrel/blob/master/docs/guide.md

您可以通过用斜杠分隔选项标志来将选项标志添加到 get 命令,以便从“作业”队列中获取项目,最多等待一秒钟:

get jobs/t=1000

如果一秒钟内队列中没有出现任何内容,您将获得相同的空响应,只是比您现在得到它晚一秒钟。:)

执行此操作时调整响应超时很重要。如果您使用超时为一秒的阻塞读取,但您的客户端库的响应超时为 500 毫秒,则库将在阻塞读取完成之前断开与服务器的连接。因此,请确保响应超时大于您在读取请求中使用的超时。

于 2011-10-31T18:01:07.673 回答
-1

您需要使用阻塞获取。我无法找到 API 文档,但我发现一篇文章暗示它可以在 kestrel 中使用。

于 2011-07-27T19:37:39.940 回答