/// <summary>
 /// Adds the maximum values per platform of the specified <see cref="DomainAccountQuantityDictionary"/> to the dictionary.
 /// </summary>
 /// <param name="quantity">The <see cref="DomainAccountQuantityDictionary"/> to add.</param>
 public void Add(DomainAccountQuantityDictionary quantity)
 {
     foreach (string key in quantity.Keys)
     {
         if (!Keys.Contains(key))
         {
             this[key] = 0;
         }
         this[key] = Math.Max(this[key], quantity[key]);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Reserves a block of users.  This MUST be called before being able to get user credentials.
        /// </summary>
        /// <param name="sessionId">The session id.</param>
        /// <param name="accountQuantity">The account quantity which is separated by account pool.</param>
        /// <returns>A <see cref="DomainAccountReservationSet"/> containing information on the reserved accounts.</returns>
        /// <exception cref="System.ArgumentNullException">accountQuantity</exception>
        /// <exception cref="System.InvalidOperationException">Domain accounts have already been reserved for this Session Id</exception>
        public static DomainAccountReservationSet Reserve(string sessionId, DomainAccountQuantityDictionary accountQuantity)
        {
            if (accountQuantity == null)
            {
                throw new ArgumentNullException("accountQuantity");
            }

            DomainAccountReservationSet reservedBlock = new DomainAccountReservationSet(sessionId);

            Action action = new Action(() =>
            {
                try
                {
                    using (AssetInventoryContext context = DbConnect.AssetInventoryContext())
                    {
                        if (context.DomainAccountReservations.Any(e => e.SessionId.Equals(sessionId, StringComparison.OrdinalIgnoreCase)))
                        {
                            //This could be a subsequent call to reserve.  Clear all reservations before proceeding.
                            Release(sessionId);
                        }

                        foreach (var poolType in accountQuantity.Keys)
                        {
                            int userCount          = accountQuantity[poolType];
                            DomainAccountPool pool = SelectPool(context, poolType);
                            int startIndex         = ReserveBlock(context, sessionId, pool, userCount);

                            TraceFactory.Logger.Debug($"New pool reserved: StartIndex: {startIndex}, UserCount: {userCount}, PoolName: {pool.DomainAccountKey}");

                            reservedBlock.Add(poolType, pool, startIndex, userCount);
                        }
                    }
                }
                catch (InsufficientDomainAccountsException)
                {
                    ReleaseSessionReservations(sessionId);
                    throw;
                }
            });

            var token = new GlobalLockToken("DomainAccountReservation", TimeSpan.FromMinutes(11), TimeSpan.FromMinutes(2));

            ExecutionServices.CriticalSection.Run(token, action);

            return(reservedBlock);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SessionResourceQuantity" /> class.
 /// </summary>
 public SessionResourceQuantity()
 {
     _vmQuantity            = new VMQuantityDictionary();
     _domainAccountQuantity = new DomainAccountQuantityDictionary();
 }