简体   繁体   English

如何通过 Azure 服务总线会话避免饥饿

[英]How to avoid starvation with Azure Service Bus Sessions

I have an Azure Function as a ServiceBusTrigger listening on a queue that has session enabled.我有一个 Azure Function 作为 ServiceBusTrigger 侦听启用了 session 的队列。

The producer may send messages with a very wide variety of session IDs (let's say 1,000 different values for the session ID).生产者可能会发送具有各种 session ID 的消息(假设 session ID 有 1,000 个不同的值)。

By default, the Azure Function host will allow 8 concurrent sessions to be processed.默认情况下,Azure Function 主机将允许处理 8 个并发会话。

What this means is that out of the 1,000 session IDs, only 8 will be processed at any given time.这意味着在 1,000 个 session ID 中,在任何给定时间只会处理 8 个。 So when the Azure Function host starts, the first 8 session IDs will have their messages processed.因此,当 Azure Function 主机启动时,前 8 个 session ID 将处理其消息。 If one of these session IDs is idle for one minute (ie if one of these session IDs does not have a message for more than one minute), then its lock will be released and a new session (the ninth one) will have its messages processed.如果这些 session ID 中的一个空闲了一分钟(即,如果这些 session ID 中的一个 ID 超过一分钟没有消息),那么它的锁将被释放并且新的 session(第九个)将有它的消息处理。

So what this means is that if the first 8 session IDs each receive at least one message per minute, their lock will not be released and their consumer will not be allowed to process another session ID, thus leaving all remaining 992 session IDs to starve (ie they will never be given a chance to get their messages processed).所以这意味着如果前 8 个 session ID 每分钟至少收到一条消息,他们的锁将不会被释放,他们的消费者将不允许处理另一个 session ID,从而使所有剩余的 992 个 session ID 挨饿(即他们永远不会有机会处理他们的消息)。

Obviously, I could update my host.json so that maxConcurrentSessions is set to 1,000 .显然,我可以更新我的host.json以便将maxConcurrentSessions设置为1,000 But I don't like this solution because it means that my configuration is hardcoded to my system's current requirements, but these requirements may vary over time ie I would have to find a way to monitor that session IDs are not starving because 6 months from now, maybe I would need to increase maxConcurrentSessions to 2,000 .但我不喜欢这个解决方案,因为它意味着我的配置是硬编码到我的系统的当前要求,但这些要求可能会随着时间的推移而变化,即我必须找到一种方法来监控 session ID 不会因为从现在起 6 个月后挨饿,也许我需要将maxConcurrentSessions增加到2,000

What I am looking for is a mechanism that would auto-adjust itself.我正在寻找的是一种可以自动调整自身的机制。 For instance, it seems to me that the Azure Service Bus extension is missing a setting that would represent a maximum time-to-live for the lock.例如,在我看来,Azure 服务总线扩展缺少一个代表锁最长生存时间的设置。 For instance, I should be allowed to specify something like:例如,我应该被允许指定如下内容:

{
  "extensions": {
    "serviceBus": {
      "sessionIdleTimeout": "00:00:15",
      "sessionTimeToLive":  "00:00:30"
    }
  }
}

With a configuration like this, what I would be basically saying is that if a session ID does not receive messages for 15 seconds, then its lock should released so that another session ID can be given a chance to process.对于这样的配置,我基本上要说的是,如果一个 session ID 在 15 秒内没有收到消息,那么它的锁应该被释放,以便另一个 session ID 有机会处理。 Additionally, the TTL would kick in because if that same session ID is constantly receiving a new message every second, then its lock would be forcibly released after 30 seconds despite that session ID having more messages needing to be processed;此外,TTL 会启动,因为如果相同的 session ID 每秒不断接收新消息,那么尽管 session ID 有更多消息需要处理,但它的锁定将在 30 秒后被强制释放; this way, another session ID is given a chance at processing.这样,另一个 session ID 就有机会进行处理。

Now given that there is nothing functionally equivalent to sessionTimeToLive in Azure Service Bus to my knowledge, would anyone have an idea on how I am supposed to handle this?现在,据我所知,在 Azure 服务总线中没有任何功能等同于sessionTimeToLive ,有人知道我应该如何处理这个问题吗?

The entity lock duration combined with the "maxAutoLockRenewalDuration" setting already behaves like the proposed "sessionTimeToLive".实体锁定持续时间结合“maxAutoLockRenewalDuration”设置已经表现得像建议的“sessionTimeToLive”。 By default the "maxAutoLockRenewalDuration" is set to 5 minutes, but you can set this to a lower value, (or 0 if you don't want the lock to be renewed at all).默认情况下,“maxAutoLockRenewalDuration”设置为 5 分钟,但您可以将其设置为较低的值(如果您根本不想更新锁,则为 0)。

Essentially, the max processing time for a session would be Max(LockDuration, MaxAutoLockRenewalDuration).本质上,session 的最大处理时间为 Max(LockDuration, MaxAutoLockRenewalDuration)。

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

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