Пример #1
0
        /// <summary>
        /// Responsible for updating/inserting an object to the data source. The Key and the
        /// object are passed as parameter.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns>-1 if writer is null, 0 if user operation returned false, 1 if successful</returns>
        private OperationResult WriteThru(WriteOperation writeOp, OperationContext operationContext)
        {
            OperationResult result = null;

            if (_dsWriter == null)
            {
                return(result);
            }
            Stopwatch writeThruWatch = new Stopwatch();

            writeThruWatch.Start();
            if (operationContext.Contains(OperationContextFieldName.MethodOverload))
            {
                writeOp.MethodOverlaod = (int)operationContext.GetValueByField(OperationContextFieldName.MethodOverload);
            }
            result = _dsWriter.WriteToDataSource(writeOp);
            writeThruWatch.Stop();
            double elapsedByWriteThru = writeThruWatch.Elapsed.TotalSeconds;

            if (elapsedByWriteThru > ServiceConfiguration.CommandExecutionThreshold &&
                ServiceConfiguration.EnableCommandThresholdLogging)
            {
                if (_context.NCacheLog != null)
                {
                    _context.NCacheLog.Warn("WriteThruProviderMgr.WriteThru",
                                            "WriteThru took " + elapsedByWriteThru + " seconds to complete. Which is longer than expected.");
                }
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="exh"></param>
        /// <param name="evh"></param>
        /// <returns></returns>
        public Dictionary <string, ProviderCacheItem> ReadThru(string[] keys, OperationContext operationContext)
        {
            Dictionary <string, ProviderCacheItem> cacheItems = null;

            try
            {
                if (_dsReader is ICustomReadThru)
                {
                    if (operationContext.Contains(OperationContextFieldName.ReadThru))
                    {
                        ((ICustomReadThru)_dsReader).DoReadThru =
                            Convert.ToBoolean(operationContext.GetValueByField(OperationContextFieldName.ReadThru));
                    }
                    if (operationContext.Contains(OperationContextFieldName.ReadThru))
                    {
                        ((ICustomReadThru)_dsReader).ProviderName =
                            operationContext.GetValueByField(OperationContextFieldName.ReadThruProviderName) as string;
                    }
                }

                System.Diagnostics.Stopwatch readThruWatch = new System.Diagnostics.Stopwatch();
                readThruWatch.Start();
                cacheItems = _dsReader.LoadFromSource(keys);
                readThruWatch.Stop();
                double elapsedByReadThru = readThruWatch.Elapsed.TotalSeconds;

                if (elapsedByReadThru > ServiceConfiguration.CommandExecutionThreshold &&
                    ServiceConfiguration.EnableCommandThresholdLogging)
                {
                    if (_context.NCacheLog != null)
                    {
                        _context.NCacheLog.Warn("ReadthruProviderMgr.ReadThru",
                                                "ReadThru took " + elapsedByReadThru +
                                                " seconds to complete. Which is longer than expected.");
                    }
                }
                this._context.PerfStatsColl.IncrementReadThruPerSecBy(keys.Length);
            }
            catch (Exception e)
            {
            }
            return(cacheItems);
        }
Пример #3
0
        private void AddNamedTag(object key, Hashtable value, CacheEntry entry, OperationContext operationContext)
        {
            string    type     = value["type"] as string;
            Hashtable tagsList = ((Hashtable)value["named-tags-list"]).Clone() as Hashtable;

            if (tagsList != null && type != null)
            {
                Hashtable   metaInfoAttribs = (Hashtable)tagsList.Clone();
                IQueryIndex index;

                lock (_indexMap.SyncRoot)
                {
                    if (!_indexMap.Contains(type))
                    {
                        if (_sharedAttributeIndex != null && _sharedAttributeIndex.ContainsKey(type))
                        {
                            AttributeIndex tempAttrib = _sharedAttributeIndex[type];
                            tempAttrib.Initialize(new ArrayList());
                            _indexMap.Add(type, tempAttrib);
                        }
                        else
                        {
                            _indexMap[type] = new AttributeIndex(null, _cacheName, type, true);
                        }
                    }

                    index = _indexMap[type] as IQueryIndex;

                    if (index != null)
                    {
                        long prevSize = index.IndexInMemorySize;

                        index.AddToIndex(key, new QueryItemContainer(entry, tagsList));

                        _queryIndexMemorySize += index.IndexInMemorySize - prevSize;
                    }
                }

                if (operationContext.Contains(OperationContextFieldName.IndexMetaInfo))
                {
                    MetaInformation metaInformation =
                        (MetaInformation)operationContext.GetValueByField(OperationContextFieldName.IndexMetaInfo);
                    metaInformation.Add(metaInfoAttribs);
                }
                else
                {
                    MetaInformation metaInformation = new MetaInformation(metaInfoAttribs);
                    metaInformation.CacheKey = key as string;
                    metaInformation.Type     = value["type"] as string;
                    operationContext.Add(OperationContextFieldName.IndexMetaInfo, metaInformation);
                    entry.ObjectType = value["type"] as string;
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Retrieve the object from the cluster.
        /// </summary>
        /// <param name="key">key of the entry.</param>
        /// <param name="excludeSelf">Set false to do a complete cluster lookup.</param>
        /// <returns>cache entry.</returns>
        protected CacheEntry Clustered_Get(Address address, object key, ref object lockId, ref DateTime lockDate, LockAccessType accessType, OperationContext operationContext)
        {
            CacheEntry retVal = null;

            try
            {
                if (operationContext.Contains(OperationContextFieldName.IsClusteredOperation))
                {
                    throw new InvalidReaderException("Reader state has been lost due to state transfer.");
                }
                if (ServerMonitor.MonitorActivity)
                {
                    ServerMonitor.LogClientActivity("RepCacheBase.Get", "enter");
                }
                Function func   = new Function((int)OpCodes.Get, new object[] { key, operationContext });
                object   result = Cluster.SendMessage(address, func, GroupRequest.GET_FIRST, false);
                if (result == null)
                {
                    return(retVal);
                }
                retVal = (CacheEntry)((OperationResponse)result).SerializablePayload;
                if (retVal != null)
                {
                    retVal.Value = ((OperationResponse)result).UserPayload;
                }
            }
            catch (CacheException e)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new GeneralFailureException(e.Message, e);
            }
            finally
            {
                if (ServerMonitor.MonitorActivity)
                {
                    ServerMonitor.LogClientActivity("RepCacheBase.Get", "exit");
                }
            }
            return(retVal);
        }
Пример #5
0
        internal override CacheEntry GetInternal(object key, bool isUserOperation, OperationContext operationContext)
        {
            CacheEntry entry = base.GetInternal(key, isUserOperation, operationContext);

            if (entry != null)
            {
                if (operationContext != null)
                {
                    if (operationContext.Contains(OperationContextFieldName.GenerateQueryInfo))
                    {
                        if (entry.ObjectType != null)
                        {
                            CacheEntry clone = (CacheEntry)entry.Clone();
                            clone.QueryInfo = _queryIndexManager.GetQueryInfo(key, entry);
                            return(clone);
                        }
                    }
                }
            }

            return(entry);
        }
Пример #6
0
        /// <summary>
        /// Removes the object and key pair from the cache. The key is specified as parameter.
        /// Moreover it take a removal reason and a boolean specifying if a notification should
        /// be raised.
        /// </summary>
        /// <param name="key">key of the entry.</param>
        /// <param name="removalReason">reason for the removal.</param>
        /// <param name="notify">boolean specifying to raise the event.</param>
        /// <param name="isUserOperation"></param>
        /// <param name="lockId"></param>
        /// <param name="accessType"></param>
        /// <param name="operationContext"></param>
        /// <returns>item value</returns>
        public override CacheEntry Remove(object key, ItemRemoveReason removalReason, bool notify, bool isUserOperation, object lockId, LockAccessType accessType, OperationContext operationContext)
        {
            CacheEntry e = null;
            CacheEntry pe = null;
            {
                object actualKey = key;
                if (key is object[])
                {
                    actualKey = ((object[])key)[0];
                }

                if (accessType != LockAccessType.IGNORE_LOCK)
                {
                    pe = GetInternal(actualKey, false, operationContext);
                    if (pe != null)
                    {
                        if (pe.IsItemLocked() && !pe.CompareLock(lockId))
                        {
                            throw new LockingException("Item is locked.");
                        }
                    }
                }



                e = RemoveInternal(actualKey, removalReason, isUserOperation, operationContext);
                EventId eventId = null;
                EventContext eventContext = null;
                OperationID opId = operationContext.OperatoinID;
                if (e != null)
                {
                    if (_stateTransferKeyList != null && _stateTransferKeyList.ContainsKey(key))
                        _stateTransferKeyList.Remove(key);
                    // commented by muds
                    try
                    {
                        if (e.ExpirationHint != null)
                        {
                            _context.ExpiryMgr.RemoveFromIndex(key);
                            ((IDisposable)e.ExpirationHint).Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        NCacheLog.Error("LocalCacheBase.Remove(object, ItemRemovedReason, bool):", ex.ToString());
                    }

                    if (IsSelfInternal)
                    {
                        // Disposed the one and only cache entry.
                        ((IDisposable)e).Dispose();

                        if (removalReason == ItemRemoveReason.Expired)
                        {
                            _context.PerfStatsColl.IncrementExpiryPerSecStats();
                        }
                        else if (!_context.CacheImpl.IsEvictionAllowed && removalReason == ItemRemoveReason.Underused)
                        {
                            _context.PerfStatsColl.IncrementEvictPerSecStats();
                        }
                        _context.PerfStatsColl.IncrementCountStats((long)Count);
                    }
                    if (notify)
                    {
                        CallbackEntry cbEtnry = e.Value as CallbackEntry;// e.DeflattedValue(_context.SerializationContext);
                        
                        if (cbEtnry != null && cbEtnry.ItemRemoveCallbackListener != null && cbEtnry.ItemRemoveCallbackListener.Count > 0)
                        {
                            //generate event id
                            if (!operationContext.Contains(OperationContextFieldName.EventContext)) //for atomic operations
                            {
                                eventId = EventId.CreateEventId(opId);
                            }
                            else //for bulk
                            {
                                eventId = ((EventContext)operationContext.GetValueByField(OperationContextFieldName.EventContext)).EventID;
                            }

                            eventId.EventType = EventType.ITEM_REMOVED_CALLBACK;
                            eventContext = new EventContext();
                            eventContext.Add(EventContextFieldName.EventID, eventId);
                            EventCacheEntry eventCacheEntry = CacheHelper.CreateCacheEventEntry(cbEtnry.ItemRemoveCallbackListener, e);
                            eventContext.Item = eventCacheEntry;
                            eventContext.Add(EventContextFieldName.ItemRemoveCallbackList, cbEtnry.ItemRemoveCallbackListener.Clone());
                            
                            //Will always reaise the whole entry for old clients
                            NotifyCustomRemoveCallback(actualKey, e, removalReason, false, (OperationContext)operationContext.Clone(), eventContext);
                        }
                    }

                   

                }
                else if (_stateTransferKeyList != null && _stateTransferKeyList.ContainsKey(key))
                {
                    _stateTransferKeyList.Remove(key);        
                }

            }
            _stats.UpdateCount(this.Count);

            if (_context.PerfStatsColl != null)
            {
                _context.PerfStatsColl.SetCacheSize(Size);
            }

            return e;
        }
Пример #7
0
        /// <summary>
        /// Adds a pair of key and value to the cache. If the specified key already exists 
        /// in the cache; it is updated, otherwise a new item is added to the cache.
        /// </summary>
        /// <param name="key">key of the entry.</param>
        /// <param name="cacheEntry">the cache entry.</param>
        /// <returns>returns the result of operation.</returns>
        public sealed override CacheInsResultWithEntry Insert(object key, CacheEntry cacheEntry, bool notify, bool isUserOperation, object lockId, LockAccessType access, OperationContext operationContext)
        {
            CacheInsResultWithEntry result = new CacheInsResultWithEntry();
            try
            {
                CacheEntry pe = null;
                CallbackEntry cbEtnry = null;
                OperationID opId = operationContext.OperatoinID;
                EventId eventId = null;
                EventContext eventContext = null;
                
                pe = GetInternal(key, false, operationContext);
                result.Entry = pe;

                if (pe != null && access != LockAccessType.IGNORE_LOCK)
                {
                    {
                        if (access == LockAccessType.RELEASE || access == LockAccessType.DONT_RELEASE)
                        {
                            if (pe.IsItemLocked() && !pe.CompareLock(lockId))
                            {
                                result.Result = CacheInsResult.ItemLocked;
                                result.Entry = null;
                                return result;
                            }
                        }
                        if (access == LockAccessType.DONT_RELEASE)
                        {
                            cacheEntry.CopyLock(pe.LockId, pe.LockDate, pe.LockExpiration);
                        }
                        else
                        {
                            cacheEntry.ReleaseLock();
                        }
                    }
                }
                ExpirationHint peExh = pe == null ? null : pe.ExpirationHint;

                if (pe != null && pe.Value is CallbackEntry)
                {
                    cbEtnry = pe.Value as CallbackEntry;
                    cacheEntry = CacheHelper.MergeEntries(pe, cacheEntry);
                }
                result.Result = InsertInternal(key, cacheEntry, isUserOperation, pe, operationContext);

                if ((result.Result == CacheInsResult.Success || result.Result == CacheInsResult.SuccessNearEvicition) && _stateTransferKeyList != null &&
                    _stateTransferKeyList.ContainsKey(key))
                {
                    result.Result = result.Result == CacheInsResult.Success ? CacheInsResult.SuccessOverwrite : CacheInsResult.SuccessOverwriteNearEviction;
                }
                // Not enough space, evict and try again.
                if (result.Result == CacheInsResult.NeedsEviction || result.Result == CacheInsResult.SuccessNearEvicition
                    || result.Result == CacheInsResult.SuccessOverwriteNearEviction)
                {
                    Evict();
                    if (result.Result == CacheInsResult.SuccessNearEvicition) result.Result = CacheInsResult.Success;
                    if (result.Result == CacheInsResult.SuccessOverwriteNearEviction) result.Result = CacheInsResult.SuccessOverwrite;
                }

                // Operation completed!
                if (result.Result == CacheInsResult.Success || result.Result == CacheInsResult.SuccessOverwrite)
                {
                    // commented by muds
                    //remove the old hint from expiry index.
                    if (peExh != null)
                        _context.ExpiryMgr.RemoveFromIndex(key);

                    if (cacheEntry.ExpirationHint != null)
                    {
                        cacheEntry.ExpirationHint.CacheKey = (string)key;
                        if (isUserOperation)
                        {
                            try
                            {
                                _context.ExpiryMgr.ResetHint(peExh, cacheEntry.ExpirationHint);
                            }
                            catch (Exception e)
                            {
                                RemoveInternal(key, ItemRemoveReason.Removed, false, operationContext);
                                throw e;
                            }
                        }
                        else
                        {
                            cacheEntry.ExpirationHint.ReInitializeHint(Context);
                        }

                        _context.ExpiryMgr.UpdateIndex(key, cacheEntry);
                    }
                    if (IsSelfInternal)
                    {
                        _context.PerfStatsColl.IncrementCountStats((long)Count);
                    }
                }

                _stats.UpdateCount(this.Count);
                switch (result.Result)
                {
                    case CacheInsResult.Success:
                        break;
                    case CacheInsResult.SuccessOverwrite:
                        if (notify)
                        {
                            EventCacheEntry eventCacheEntry = CacheHelper.CreateCacheEventEntry(Runtime.Events.EventDataFilter.DataWithMetadata, cacheEntry); ;
                            EventCacheEntry oldEventCacheEntry = CacheHelper.CreateCacheEventEntry(Runtime.Events.EventDataFilter.DataWithMetadata, pe);

                            if (cbEtnry != null)
                            {
                                if (cbEtnry.ItemUpdateCallbackListener != null && cbEtnry.ItemUpdateCallbackListener.Count > 0)
                                {
                                    if (!operationContext.Contains(OperationContextFieldName.EventContext)) //for atomic operations
                                    {
                                        eventId = EventId.CreateEventId(opId);
                                        eventContext = new EventContext();
                                    }
                                    else //for bulk
                                    {
                                        eventId = ((EventContext)operationContext.GetValueByField(OperationContextFieldName.EventContext)).EventID;
                                    }

                                    eventContext = new EventContext();
                                    eventId.EventType = EventType.ITEM_UPDATED_CALLBACK;
                                    eventContext.Add(EventContextFieldName.EventID, eventId);
                                    eventContext.Item = eventCacheEntry;
                                    eventContext.OldItem = oldEventCacheEntry;

                                    NotifyCustomUpdateCallback(key, cbEtnry.ItemUpdateCallbackListener, false, (OperationContext)operationContext.Clone(), eventContext);
                                }
                            }
                        }
                        break;
                }
            }
            finally
            {
            }

           if (_context.PerfStatsColl != null)
            {
                _context.PerfStatsColl.SetCacheSize(Size);
            }


            return result;
        }
Пример #8
0
        internal override CacheEntry GetInternal(object key, bool isUserOperation, OperationContext operationContext)
        {
            
                CacheEntry entry = base.GetInternal(key, isUserOperation, operationContext);

                if (entry != null)
                {
                    if (operationContext != null)
                    {
                        if (operationContext.Contains(OperationContextFieldName.GenerateQueryInfo))
                        {
                            if (entry.ObjectType != null)
                            {
                                CacheEntry clone = (CacheEntry)entry.Clone();

                                clone.QueryInfo = _queryIndexManager.GetQueryInfo(key, entry);
                                return clone;
                            }
                        }
                    }

                }

                return entry;
        }
Пример #9
0
        protected EventContext CreateEventContext(OperationContext operationContext, Alachisoft.NCache.Persistence.EventType eventType)
        {
            EventContext eventContext = new EventContext();
            OperationID opId = operationContext != null ? operationContext.OperatoinID : null;
            //generate event id
            if (operationContext == null || !operationContext.Contains(OperationContextFieldName.EventContext)) //for atomic operations
            {
                eventContext.EventID = EventId.CreateEventId(opId);
            }
            else //for bulk
            {
                eventContext.EventID = ((EventContext)operationContext.GetValueByField(OperationContextFieldName.EventContext)).EventID;
            }

            eventContext.EventID.EventType = eventType;
            return eventContext;

        }
Пример #10
0
        //PROTOBUF
        public override void ExecuteCommand(ClientManager clientManager, Alachisoft.NCache.Common.Protobuf.Command command)
        {
            NCache    nCache = clientManager.CmdExecuter as NCache;
            int       overload;
            long      dataLength  = 0;
            string    exception   = null;
            bool      itemUpdated = false;
            bool      itemRemove  = false;
            Stopwatch stopWatch   = new Stopwatch();

            stopWatch.Start();
            try
            {
                try
                {
                    overload             = command.MethodOverload;
                    serializationContext = nCache.CacheId;
                    cmdInfo = base.ParseCommand(command, clientManager, serializationContext);
                }
                catch (System.Exception exc)
                {
                    _addResult = OperationResult.Failure;
                    {
                        //PROTOBUF:RESPONSE
                        _serializedResponsePackets.Add(Alachisoft.NCache.Common.Util.ResponseHelper.SerializeExceptionResponseWithType(exc, command.requestID, command.commandID, clientManager.ClientVersion));
                    }

                    return;
                }

                Notifications callbackEntry = null;

                if (cmdInfo.UpdateCallbackId != -1 || cmdInfo.RemoveCallbackId != -1 || (!cmdInfo.RequestId.Equals("-1") && cmdInfo.DoAsync) || cmdInfo.DsItemAddedCallbackId != -1)
                {
                    if (cmdInfo.RemoveCallbackId != -1)
                    {
                        itemRemove = true;
                    }
                    if (cmdInfo.UpdateCallbackId != -1)
                    {
                        itemUpdated = true;
                    }

                    callbackEntry = new Notifications(!string.IsNullOrEmpty(cmdInfo.ClientID) ? cmdInfo.ClientID : clientManager.ClientID,
                                                      Convert.ToInt32(cmdInfo.RequestId),
                                                      cmdInfo.RemoveCallbackId,
                                                      cmdInfo.UpdateCallbackId,
                                                      (short)(cmdInfo.RequestId == -1 ? -1 : 0),
                                                      cmdInfo.DsItemAddedCallbackId,
                                                      (EventDataFilter)cmdInfo.UpdateDataFilter,
                                                      (EventDataFilter)cmdInfo.RemoveDataFilter);
                }

                UserBinaryObject data = cmdInfo.value as UserBinaryObject;
                if (data != null)
                {
                    dataLength = data.Length;
                }

                if (!cmdInfo.DoAsync)
                {
                    OperationContext operationContext = null;

                    try
                    {
                        operationContext = _operationContext;
                        operationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);

                        CommandsUtil.PopulateClientIdInContext(ref operationContext, clientManager.ClientAddress);
                        operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);

                        UInt64 itemVersion = 0;
                        if (cmdInfo.ItemVersion == 0)
                        {
                            itemVersion = (ulong)(DateTime.UtcNow - Common.Util.Time.ReferenceTime).TotalMilliseconds;
                        }
                        else
                        {
                            itemVersion = cmdInfo.ItemVersion;
                        }

                        operationContext.Add(OperationContextFieldName.ItemVersion, itemVersion);
                        operationContext.Add(OperationContextFieldName.MethodOverload, overload);

                        operationContext?.MarkInUse(NCModulesConstants.SocketServer);

                        if (cmdInfo.Group != null)
                        {
                            if (string.IsNullOrEmpty(cmdInfo.Type))
                            {
                                cmdInfo.Type = oldClientsGroupType;
                            }
                        }

                        nCache.Cache.Add(cmdInfo.Key,
                                         cmdInfo.value,
                                         cmdInfo.ExpirationHint,
                                         cmdInfo.EvictionHint,
                                         cmdInfo.Group,
                                         cmdInfo.SubGroup,
                                         cmdInfo.queryInfo,
                                         cmdInfo.Flag, cmdInfo.ProviderName, cmdInfo.ResyncProviderName, operationContext, null, callbackEntry, cmdInfo.Type);

                        stopWatch.Stop();

                        if (operationContext.Contains(OperationContextFieldName.ItemVersion))
                        {
                            itemVersion = (ulong)operationContext.GetValueByField(OperationContextFieldName.ItemVersion);
                        }

                        _addResponse.itemversion = itemVersion;

                        if (clientManager.ClientVersion >= 5000)
                        {
                            ResponseHelper.SetResponse(_addResponse, command.requestID, command.commandID);
                            _serializedResponsePackets.Add(ResponseHelper.SerializeResponse(_addResponse, Response.Type.ADD));
                        }
                        else
                        {
                            //PROTOBUF:RESPONSE
                            var response = Stash.ProtobufResponse;
                            response.addResponse = _addResponse;
                            ResponseHelper.SetResponse(response, command.requestID, command.commandID, Response.Type.ADD);

                            _serializedResponsePackets.Add(Alachisoft.NCache.Common.Util.ResponseHelper.SerializeResponse(response));
                        }
                    }
                    catch (System.Exception exc)
                    {
                        _addResult = OperationResult.Failure;
                        exception  = exc.ToString();
                        _serializedResponsePackets.Add(Alachisoft.NCache.Common.Util.ResponseHelper.SerializeExceptionResponseWithType(exc, command.requestID, command.commandID, clientManager.ClientVersion));
                    }
                    finally
                    {
                        operationContext?.MarkFree(NCModulesConstants.SocketServer);

                        TimeSpan executionTime = stopWatch.Elapsed;

                        try
                        {
                            if (Alachisoft.NCache.Management.APILogging.APILogManager.APILogManger != null && Alachisoft.NCache.Management.APILogging.APILogManager.EnableLogging)
                            {
                                APILogItemBuilder log = new APILogItemBuilder(MethodsName.ADD.ToLower());
                                object            toInsert;
                                if (cmdInfo.value is UserBinaryObject)
                                {
                                    toInsert = dataLength;
                                }
                                else
                                {
                                    toInsert = cmdInfo.DataFormatValue;
                                }
                                Hashtable expirationHint = log.GetDependencyExpirationAndQueryInfo(cmdInfo.ExpirationHint, cmdInfo.queryInfo);
                                log.GenerateADDInsertAPILogItem(cmdInfo.Key, toInsert, expirationHint["dependency"] != null ? expirationHint["dependency"] as ArrayList : null, expirationHint["absolute-expiration"] != null ? (long)expirationHint["absolute-expiration"] : -1, expirationHint["sliding-expiration"] != null ? (long)expirationHint["sliding-expiration"] : -1, cmdInfo.EvictionHint.Priority, expirationHint["tag-info"] != null ? expirationHint["tag-info"] as Hashtable : null, cmdInfo.Group, cmdInfo.SubGroup, cmdInfo.Flag, cmdInfo.ProviderName, cmdInfo.ResyncProviderName, false, expirationHint["named-tags"] != null ? expirationHint["named-tags"] as Hashtable : null, cmdInfo.UpdateCallbackId, cmdInfo.DsItemAddedCallbackId, false, itemUpdated, itemRemove, overload, exception, executionTime, clientManager.ClientID, clientManager.ClientSocketId.ToString());
                            }
                        }
                        catch { }
                    }
                }

                else
                {
                    OperationContext operationContext = null;

                    try
                    {
                        operationContext = new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation);

                        operationContext.Add(OperationContextFieldName.WriteThru, cmdInfo.Flag.IsBitSet(BitSetConstants.WriteThru));
                        operationContext.Add(OperationContextFieldName.WriteBehind, cmdInfo.Flag.IsBitSet(BitSetConstants.WriteBehind));
                        if (cmdInfo.ProviderName != null)
                        {
                            operationContext.Add(OperationContextFieldName.WriteThruProviderName, cmdInfo.ProviderName);
                        }

                        operationContext.Add(OperationContextFieldName.RaiseCQNotification, true);

                        UInt64 itemVersion = 0;
                        if (cmdInfo.ItemVersion == 0)
                        {
                            itemVersion = (ulong)(DateTime.UtcNow - Common.Util.Time.ReferenceTime).TotalMilliseconds;
                        }
                        else
                        {
                            itemVersion = cmdInfo.ItemVersion;
                        }


                        operationContext.Add(OperationContextFieldName.ItemVersion, itemVersion);
                        operationContext.Add(OperationContextFieldName.MethodOverload, overload);
                        operationContext.MarkInUse(NCModulesConstants.SocketServer);

                        bool onAsyncCall = false;
                        if (callbackEntry != null)
                        {
                            onAsyncCall = true;
                        }

                        if (cmdInfo.Group != null)
                        {
                            if (string.IsNullOrEmpty(cmdInfo.Type))
                            {
                                cmdInfo.Type = oldClientsGroupType;
                            }
                        }

                        // Fetching this from pool to avoid value corruption for eviction hint for old
                        cmdInfo.EvictionHint = Caching.EvictionPolicies.PriorityEvictionHint.Create(
                            clientManager.CacheTransactionalPool, cmdInfo.EvictionHint.Priority
                            );

                        nCache.Cache.AddAsync(cmdInfo.Key,
                                              cmdInfo.value,
                                              cmdInfo.ExpirationHint,
                                              cmdInfo.EvictionHint,
                                              cmdInfo.Group,
                                              cmdInfo.SubGroup,
                                              cmdInfo.Flag,
                                              cmdInfo.queryInfo, cmdInfo.ProviderName, operationContext, callbackEntry, cmdInfo.Type);

                        stopWatch.Stop();
                        TimeSpan executionTime = stopWatch.Elapsed;
                        try
                        {
                            if (Alachisoft.NCache.Management.APILogging.APILogManager.APILogManger != null && Alachisoft.NCache.Management.APILogging.APILogManager.EnableLogging)
                            {
                                APILogItemBuilder log            = new APILogItemBuilder(MethodsName.ADDASYNC.ToLower());
                                Hashtable         expirationHint = log.GetDependencyExpirationAndQueryInfo(cmdInfo.ExpirationHint, cmdInfo.queryInfo);
                                object            toInsert;
                                if (cmdInfo.value is UserBinaryObject)
                                {
                                    toInsert = dataLength;
                                }
                                else
                                {
                                    toInsert = cmdInfo.DataFormatValue;
                                }
                                log.GenerateADDInsertAPILogItem(cmdInfo.Key, toInsert, expirationHint["dependency"] != null ? expirationHint["dependency"] as ArrayList : null, expirationHint["absolute-expiration"] != null ? (long)expirationHint["absolute-expiration"] : -1, expirationHint["sliding-expiration"] != null ? (long)expirationHint["sliding-expiration"] : -1, cmdInfo.EvictionHint.Priority, expirationHint["tag-info"] != null ? expirationHint["tag-info"] as Hashtable : null, cmdInfo.Group, cmdInfo.SubGroup, cmdInfo.Flag, cmdInfo.ProviderName, cmdInfo.ResyncProviderName, false, expirationHint["named-tags"] != null ? expirationHint["named-tags"] as Hashtable : null, cmdInfo.UpdateCallbackId, cmdInfo.DsItemAddedCallbackId, onAsyncCall, itemUpdated, itemRemove, overload, exception, executionTime, clientManager.ClientID, clientManager.ClientSocketId.ToString());
                            }
                        }
                        catch { }
                    }
                    finally
                    {
                        operationContext?.MarkFree(NCModulesConstants.SocketServer);
                    }
                }
            }
            finally
            {
                cmdInfo.Flag?.MarkFree(NCModulesConstants.SocketServer);
            }
        }
Пример #11
0
        public override object RemoveSync(object[] keys, ItemRemoveReason reason, bool notify, OperationContext operationContext)
        {
            ArrayList depenedentItemList = new ArrayList();

            try
            {
                Hashtable totalRemovedItems = new Hashtable();

                CacheEntry            entry = null;
                IDictionaryEnumerator ide   = null;


                for (int i = 0; i < keys.Length; i++)
                {
                    try
                    {
                        if (keys[i] != null)
                        {
                            entry = Internal.Remove(keys[i], reason, false, null, LockAccessType.IGNORE_LOCK, operationContext);
                        }


                        if (entry != null)
                        {
                            totalRemovedItems.Add(keys[i], entry);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                ide = totalRemovedItems.GetEnumerator();
                while (ide.MoveNext())
                {
                    try
                    {
                        entry = ide.Value as CacheEntry;
                        if (entry != null)
                        {
                            if (entry.Value is CallbackEntry)
                            {
                                EventId       eventId      = null;
                                OperationID   opId         = operationContext.OperatoinID;
                                CallbackEntry cbEtnry      = (CallbackEntry)entry.Value;
                                EventContext  eventContext = null;

                                if (cbEtnry != null && cbEtnry.ItemRemoveCallbackListener != null && cbEtnry.ItemRemoveCallbackListener.Count > 0)
                                {
                                    //generate event id
                                    if (!operationContext.Contains(OperationContextFieldName.EventContext)) //for atomic operations
                                    {
                                        eventId = EventId.CreateEventId(opId);
                                    }
                                    else //for bulk
                                    {
                                        eventId = ((EventContext)operationContext.GetValueByField(OperationContextFieldName.EventContext)).EventID;
                                    }

                                    eventId.EventType = EventType.ITEM_REMOVED_CALLBACK;
                                    eventContext      = new EventContext();
                                    eventContext.Add(EventContextFieldName.EventID, eventId);
                                    EventCacheEntry eventCacheEntry = CacheHelper.CreateCacheEventEntry(cbEtnry.ItemRemoveCallbackListener, entry);
                                    eventContext.Item = eventCacheEntry;
                                    eventContext.Add(EventContextFieldName.ItemRemoveCallbackList, cbEtnry.ItemRemoveCallbackListener.Clone());

                                    //Will always reaise the whole entry for old clients
                                    NotifyCustomRemoveCallback(ide.Key, entry, reason, true, operationContext, eventContext);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(depenedentItemList);
        }
Пример #12
0
        public override object RemoveSync(object[] keys, ItemRemoveReason reason, bool notify,
            OperationContext operationContext)
        {
            try
            {
                Hashtable totalRemovedItems = new Hashtable();
                CacheEntry entry = null;
                IDictionaryEnumerator ide = null;

                if (NCacheLog.IsInfoEnabled)
                    NCacheLog.Info("PartitionedCache.RemoveSync", "Keys = " + keys.Length.ToString());

                for (int i = 0; i < keys.Length; i++)
                {
                    try
                    {
                        if (keys[i] != null)
                            entry = Local_Remove(keys[i], reason, null, null, false, null, LockAccessType.IGNORE_LOCK, operationContext);

                        if (entry != null)
                        {
                            totalRemovedItems.Add(keys[i], entry);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }

                ArrayList keysOfRemoveNotification = new ArrayList();
                ArrayList entriesOfRemoveNotification = new ArrayList();
                List<EventContext> eventContexts = new List<EventContext>();
                int sizeThreshhold = 30*1024;
                int countThreshhold = 50;
                int size = 0;

                ide = totalRemovedItems.GetEnumerator();

                while (ide.MoveNext())
                {
                    try
                    {
                        entry = ide.Value as CacheEntry;
                        if (entry != null)
                        {
                            if (entry.Value is CallbackEntry)
                            {
                                EventId eventId = null;
                                OperationID opId = operationContext.OperatoinID;
                                CallbackEntry cbEtnry = (CallbackEntry) entry.Value;
                                EventContext eventContext = null;

                                if (cbEtnry != null && cbEtnry.ItemRemoveCallbackListener != null &&
                                    cbEtnry.ItemRemoveCallbackListener.Count > 0)
                                {
                                    //generate event id
                                    if (!operationContext.Contains(OperationContextFieldName.EventContext))
                                        //for atomic operations
                                    {
                                        eventId = EventId.CreateEventId(opId);
                                    }
                                    else //for bulk
                                    {
                                        eventId =
                                            ((EventContext)
                                                operationContext.GetValueByField(OperationContextFieldName.EventContext))
                                                .EventID;
                                    }

                                    eventId.EventType = Alachisoft.NCache.Persistence.EventType.ITEM_REMOVED_CALLBACK;
                                    eventContext = new EventContext();
                                    eventContext.Add(EventContextFieldName.EventID, eventId);
                                    EventCacheEntry eventCacheEntry =
                                        CacheHelper.CreateCacheEventEntry(cbEtnry.ItemRemoveCallbackListener, entry);
                                    eventContext.Item = eventCacheEntry;
                                    eventContext.Add(EventContextFieldName.ItemRemoveCallbackList,
                                        cbEtnry.ItemRemoveCallbackListener.Clone());

                                    RaiseAsyncCustomRemoveCalbackNotifier(ide.Key, entry, reason, operationContext,
                                        eventContext);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }
            return null;
        }
Пример #13
0
        /// <summary>
        /// Responsible for loading the object from the external data source.
        /// Key is passed as parameter.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        ///
        public void ReadThru(string key, out ProviderCacheItem item, OperationContext operationContext)
        {
            item = null;
            try
            {
                if (_dsReader is ICustomReadThru)
                {
                    if (operationContext.Contains(OperationContextFieldName.ReadThru))
                    {
                        ((ICustomReadThru)_dsReader).DoReadThru =
                            Convert.ToBoolean(operationContext.GetValueByField(OperationContextFieldName.ReadThru));
                    }
                    if (operationContext.Contains(OperationContextFieldName.ReadThru))
                    {
                        ((ICustomReadThru)_dsReader).ProviderName =
                            operationContext.GetValueByField(OperationContextFieldName.ReadThruProviderName) as string;
                    }

                    if (operationContext.Contains(OperationContextFieldName.GroupInfo))
                    {
                        GroupInfo gi = operationContext.GetValueByField(OperationContextFieldName.GroupInfo) as GroupInfo;
                        if (gi != null)
                        {
                            ((ICustomReadThru)_dsReader).Group    = gi.Group;
                            ((ICustomReadThru)_dsReader).SubGroup = gi.SubGroup;
                        }
                    }
                }

                Stopwatch readThruWatch = new Stopwatch();
                readThruWatch.Start();

                _dsReader.LoadFromSource(key, out item);
                readThruWatch.Stop();
                double elapsedByReadThru = readThruWatch.Elapsed.TotalSeconds;

                if (elapsedByReadThru > ServiceConfiguration.CommandExecutionThreshold &&
                    ServiceConfiguration.EnableCommandThresholdLogging)
                {
                    if (_context.NCacheLog != null)
                    {
                        _context.NCacheLog.Warn("ReadThruProviderMgr.ReadThru",
                                                "ReadThru took " + elapsedByReadThru +
                                                " seconds to complete. Which is longer than expected.");
                    }
                }

                this._context.PerfStatsColl.IncrementReadThruPerSec();
            }
            catch (Exception e)
            {
                //Client doesnt throw the inner exception
                //Client casts the thrown exception message into Operation failed Exception therefore the current inner exception will be casted
                //in Operation failed exception > Inner Exception > Inner Exception
                throw new OperationFailedException("IReadThruProvider.LoadFromSource failed. Error: " + e.ToString(), e);
            }
            finally
            {
                try
                {
                    // reset all here
                    ((ICustomReadThru)_dsReader).DoReadThru   = false;
                    ((ICustomReadThru)_dsReader).Group        = null;
                    ((ICustomReadThru)_dsReader).SubGroup     = null;
                    ((ICustomReadThru)_dsReader).ProviderName = null;
                }
                catch {}
            }
        }