你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

IParitionLoadBalancingStrategy 接口

定义

策略定义当前主机在特定时刻应采用哪些租约。

public interface IParitionLoadBalancingStrategy
type IParitionLoadBalancingStrategy = interface
Public Interface IParitionLoadBalancingStrategy

示例

public class CustomStrategy : IParitionLoadBalancingStrategy
{
    private string hostName;
    private string hostVersion;
    private TimeSpan leaseExpirationInterval;

    private const string VersionPropertyKey = "version";

    public IEnumerable<ILease> SelectLeasesToTake(IEnumerable<ILease> allLeases)
    {
        var takenLeases = this.FindLeasesToTake(allLeases);
        foreach (var lease in takenLeases)
        {
            lease.Properties[VersionPropertyKey] = this.hostVersion;
        }

        return takenLeases;
    }

    private IEnumerable<ILease> FindLeasesToTake(IEnumerable<ILease> allLeases)
    {
        List<ILease> takenLeases = new List<ILease>();
        foreach (var lease in allLeases)
        {
            if (string.IsNullOrWhiteSpace(lease.Owner) || this.IsExpired(lease))
            {
                takenLeases.Add(lease);
            }

            if (lease.Owner != this.hostName)
            {
                var ownerVersion = lease.Properties[VersionPropertyKey];
                if (ownerVersion < this.hostVersion)
                {
                    takenLeases.Add(lease);
                }

                // more logic for leases owned by other hosts
            }
        }

        return takenLeases;
    }

    private bool IsExpired(ILease lease)
    {
        return lease.Timestamp.ToUniversalTime() + this.leaseExpirationInterval < DateTime.UtcNow;
    }
}

注解

如果需要,它可以为所有返回的租约(包括当前拥有的租约)设置新的 Properties

方法

SelectLeasesToTake(IEnumerable<ILease>)

选择应用于处理的租约。 此方法将定期调用 LeaseAcquireInterval

适用于