private short RegisterMRListenerDescriptor(TaskListener listener) { if (listener == null) { return(0); } short returnValue = 0; lock (SyncMapReduce) { ResourcePool pool = _mapReduceListenerPool; ResourcePool poolID = _mapReduceListenerIDPool; if (pool.GetResource(listener) == null) { returnValue = ++MAPREDUCELISTENER; pool.AddResource(listener, returnValue); poolID.AddResource(returnValue, listener); } else { short val = (short)pool.GetResource(listener); if (val >= 0) { pool.AddResource(listener, returnValue); poolID.AddResource(returnValue, listener); returnValue = val; } } } return(returnValue); }
internal void FireMapReduceCallback(string taskId, int taskstatus, string taskFailureReason, short callbackId) { TaskCompletionStatus status = TaskCompletionStatus.Success; switch (taskstatus) { case 0: status = TaskCompletionStatus.Success; break; case 1: status = TaskCompletionStatus.Failure; break; case 2: status = TaskCompletionStatus.Cancelled; break; } TaskResult resp = new TaskResult(status, taskId, callbackId, taskFailureReason); if (_mapReduceListenerIDPool != null) { ResourcePool poole = _mapReduceListenerIDPool; TaskListener callback = (TaskListener)poole.GetResource(callbackId); if (callback != null) { callback.Invoke(resp); } } }
private DBPooledObject TryGetResourceFromContext(out bool isInTransaction) { isInTransaction = false; DBPooledObject obj = null; try { if (PoolControl.TransactionAffinity && ContextUtil.IsInTransaction) { isInTransaction = true; if (null != _txPool) { obj = (DBPooledObject)_txPool.GetResource(); } } else { isInTransaction = false; } } catch (Exception e) { ADP.TraceException(e); } #if ALLOWTRACING if (null != obj) { ADP.TraceObjectPoolActivity("GetFromTransactedPool", obj); } #endif //ALLOWTRACING return(obj); }
private SqlInternalConnection TryGetResourceFromContext(out bool isInTransaction) { isInTransaction = false; SqlInternalConnection con = null; try { if (_ctrl.TransactionAffinity && ContextUtil.IsInTransaction) { isInTransaction = true; if (null != _txPool) { con = (SqlInternalConnection)_txPool.GetResource(); } } else { isInTransaction = false; } } catch (Exception e) { ADP.TraceException(e); } return(con); }
/// <summary> /// Unregisters CacheDataNotificationCallback /// <para>Flag based unregistration</para> /// </summary> /// <param name="callback"></param> /// <param name="key"></param> /// <param name="eventType"></param> internal short[] UnregisterSelectiveNotification(CacheDataNotificationCallback callback, EventType eventType) { if (callback == null) { return(null); } short[] returnValue = new short[] { -1, -1 }; //First value update callback ref & sencond is remove callbackref foreach (EventType type in Enum.GetValues(typeof(EventType))) { if (type == EventType.ItemAdded) //ItemAdded not supported Yet { continue; } object id = -1; lock (SyncLockSelective) { ResourcePool pool = null; ResourcePool poolID = null; #region pool selection if (type == EventType.ItemRemoved && (eventType & EventType.ItemRemoved) != 0) { pool = _selectiveRemoveEventPool; poolID = _selectiveRemoveEventIDPool; } else if (type == EventType.ItemUpdated && (eventType & EventType.ItemUpdated) != 0) { pool = _selectiveUpdateEventPool; poolID = _selectiveUpdateEventIDPool; } if (pool == null) { continue; } #endregion // For selective callback, we dont remove the callback as it can create chaos if user try to unregister //a callback more then one time or against wrong items. int i = type == EventType.ItemUpdated ? 0 : 1; id = pool.GetResource(callback); if (id is short) { returnValue[i] = (short)id; } } } return(returnValue); }
public void Get_resource_from_resource_pool_when_has_no_avaliable() { var respurcePool = new ResourcePool(); Assert.Equal(0, respurcePool.GetAvaliableResourceCount()); Assert.Equal(0, respurcePool.GetAllocatedResourceCount()); respurcePool.GetResource("Resource1"); Assert.Equal(0, respurcePool.GetAvaliableResourceCount()); Assert.Equal(1, respurcePool.GetAllocatedResourceCount()); }
public void Get_resource_from_resource_pool() { var resource1 = new Resource("Resource1"); var avaliable = new List <Resource>() { resource1 }; var respurcePool = new ResourcePool(null, avaliable); respurcePool.GetResource("Resource1"); Assert.Equal(1, respurcePool.GetAllocatedResourceCount()); }
internal IInnerConnection GetConnection(ConnectionOptions options, VirtuosoConnection connection) { Debug.WriteLineIf(CLI.FnTrace.Enabled, "ConnectionPool.GetConnection ()"); IInnerConnection innerConnection = null; if (options.Enlist && ContextUtil.IsInTransaction) { innerConnection = (IInnerConnection)dtcPool.GetResource(); if (innerConnection != null) { innerConnection.OuterConnectionWeakRef = new WeakReference(connection); return(innerConnection); } } lock (this) { if (pool == null) { pool = new IInnerConnection[maxSize]; for (int i = 0; i < minSize; i++) { innerConnection = connection.CreateInnerConnection(options, false); innerConnection.TimeStamp = DateTime.Now; PutConnection(innerConnection); } } if (size > 0) { innerConnection = pool[--size]; } } if (innerConnection == null) { innerConnection = connection.CreateInnerConnection(options, true); innerConnection.TimeStamp = DateTime.Now; } else { innerConnection.OuterConnectionWeakRef = new WeakReference(connection); #if MTS if (options.Enlist && ContextUtil.IsInTransaction) { connection.EnlistInnerConnection(innerConnection); } #endif } return(innerConnection); }
internal short RegisterSelectiveCallback(CacheItemRemovedCallback removedCallback) { if (removedCallback == null) { return(-1); } lock (SyncLockSelective) { SelectiveRemoveCallbackWrapper callbackWrapper = null; if (_oldSelectiveCallbackPool.GetResource(removedCallback) == null) { callbackWrapper = new SelectiveRemoveCallbackWrapper(removedCallback); _oldSelectiveCallbackPool.AddResource(removedCallback, callbackWrapper); _oldSelectiveMappingCallbackPool.AddResource(callbackWrapper, removedCallback); } else { callbackWrapper = (SelectiveRemoveCallbackWrapper)_oldSelectiveCallbackPool.GetResource(removedCallback); _oldSelectiveCallbackPool.AddResource(removedCallback, callbackWrapper); } short[] callbackIds = RegisterSelectiveEvent(callbackWrapper.MappingCallback, EventType.ItemRemoved, EventDataFilter.DataWithMetadata); return(callbackIds[1]); } }
/// <summary> /// TheadSafe and no locks internally /// </summary> /// <param name="key"></param> /// <param name="eventType">Should contain one type i.e. should not be used as a flag. /// Every EventType should be executed from another thread</param> /// <param name="item"></param> /// <param name="oldItem"></param> /// <param name="reason"></param> /// <param name="_notifyAsync"></param> /// <param name="eventhandle"></param> internal void RaiseSelectiveCacheNotification(string key, EventType eventType, EventCacheItem item, EventCacheItem oldItem, CacheItemRemovedReason reason, bool _notifyAsync, EventHandle eventhandle, EventDataFilter dataFilter) { try { ResourcePool poolID = null; CacheEventArg arg = null; if ((eventType & EventType.ItemUpdated) != 0) { poolID = _selectiveUpdateEventIDPool; } else if ((eventType & EventType.ItemRemoved) != 0) { poolID = _selectiveRemoveEventIDPool; } arg = CreateCacheEventArgument(dataFilter, key, _cacheName, eventType, item, oldItem, reason); if (poolID == null) { return; } CacheDataNotificationCallback callback = poolID.GetResource((short)eventhandle.Handle) as CacheDataNotificationCallback; if (callback == null) { return; } if (_notifyAsync) { System.Threading.ThreadPool.QueueUserWorkItem(waitC, new object[] { callback, key, arg }); } else { callback.Invoke(key, arg); } } catch (Exception ex) { if (_logger != null && _logger.IsErrorEnabled) { _logger.CriticalInfo(ex.ToString()); } } }
/// <summary> /// This method Unregisters a custom callback that is fired on change in dataset of a continous query /// </summary> /// <param name="callback">A delegate to register your custom method with</param> /// <param name="eventType">Describes whether the event is to be raised on Item Added, Updated or Removed</param> /// <example> /// Lets consider we created an ItemCallback /// <code> /// ItemCallback(string key, CacheEventArg e) /// { /// ... /// } /// </code> /// Uregister your notification callback /// <code> /// cQ.RegisterNotification(new QueryDataNotificationCallback(ItemCallback),EventType.ItemAdded); /// </code> /// </example> public void UnRegisterNotification(QueryDataNotificationCallback callback, EventType eventType) { //BY LEGACY DESIGN THERE IS NO UNREGISTRATION PROCESS if (callback == null) { throw new ArgumentNullException("callback"); } object id = -1; foreach (EventType type in Enum.GetValues(typeof(EventType))) { lock (syncLock) { ResourcePool pool = null; ResourcePool poolDF = null; #region pool selection if (type == EventType.ItemAdded && (eventType & EventType.ItemAdded) != 0) { pool = _cqAddEventPool; poolDF = _cqAddEventDataFilter; } else if (type == EventType.ItemRemoved && (eventType & EventType.ItemRemoved) != 0) { pool = _cqRemoveEventPool; poolDF = _cqRemoveEventDataFilter; } else if (type == EventType.ItemUpdated && (eventType & EventType.ItemUpdated) != 0) { pool = _cqUpdateEventPool; poolDF = _cqUpdateEventDataFilter; } if (pool == null) { continue; } #endregion object temp = pool.GetResource(callback); short index = -1; index = Convert.ToInt16(temp); if (index > -1) { EventDataFilter datafilter = (EventDataFilter)poolDF.GetResource(index); object retVal = poolDF.RemoveResource(index); pool.RemoveResource(callback); if (retVal == null) { continue; } bool unregisterNotification = poolDF.Count == 0; EventDataFilter maxDataFilter = EventDataFilter.None; if (!unregisterNotification) { object[] callbackRefs = poolDF.GetAllResourceKeys(); if (callbackRefs != null) { for (int i = 0; i < callbackRefs.Length; i++) { EventDataFilter df = (EventDataFilter)callbackRefs[i]; if (df > maxDataFilter) { maxDataFilter = df; } if (maxDataFilter == EventDataFilter.DataWithMetadata) { break; } } } } if (type == EventType.ItemAdded) { _cqAddDF = maxDataFilter; } else if (type == EventType.ItemRemoved) { _cqRemoveDF = maxDataFilter; } else { _cqUpdateDF = maxDataFilter; } } } } }
/// <summary> /// Unregisters CacheDataNotificationCallback /// <para>Flag based unregistration</para> /// </summary> /// <param name="callback"></param> /// <param name="key"></param> /// <param name="eventType"></param> internal short[] UnregisterSelectiveNotification(CacheDataNotificationCallback callback, EventTypeInternal eventType) { if (callback == null) { return(null); } short[] returnValue = new short[] { -1, -1 }; //First value update callback ref & sencond is remove callbackref foreach (EventTypeInternal type in Enum.GetValues(typeof(EventTypeInternal))) { if (type == EventTypeInternal.ItemAdded) //ItemAdded not supported Yet { continue; } object id = -1; lock (SyncLockSelective) { ResourcePool pool = null; ResourcePool poolID = null; #region pool selection if (type == EventTypeInternal.ItemRemoved && (eventType & EventTypeInternal.ItemRemoved) != 0) { pool = _selectiveRemoveEventPool; poolID = _selectiveRemoveEventIDPool; if (pool == null) { removeCallbacks = 0; } } else if (type == EventTypeInternal.ItemUpdated && (eventType & EventTypeInternal.ItemUpdated) != 0) { pool = _selectiveUpdateEventPool; poolID = _selectiveUpdateEventIDPool; if (pool == null) { updateCallbacks = 0; } } if (removeCallbacks == 0 && updateCallbacks == 0) { _selectiveEventsSubscription.UnSubscribeEventTopic(); _selectiveEventsSubscription = null; } if (pool == null) { continue; } #endregion int i = type == EventTypeInternal.ItemUpdated ? 0 : 1; id = pool.GetResource(callback); if (id is short) { returnValue[i] = (short)id; } } } return(returnValue); }
/// <summary> /// Returning Negative value means operation not successfull /// </summary> /// <param name="discriptor"></param> /// <param name="eventType"></param> /// <returns>short array <para>1st value is Update callbackRef</para> <para>nd value is removeRef</para></returns> private short[] RegisterSelectiveDiscriptor(CacheDataNotificationCallback callback, EventTypeInternal eventType, CallbackType callbackType) { if (callback == null) { return(null); //FAIL CONDITION } short[] returnValue = new short[] { -1, -1 }; //First value update callback ref & sencond is remove callbackref foreach (EventTypeInternal type in Enum.GetValues(typeof(EventTypeInternal))) { if (type == EventTypeInternal.ItemAdded) //ItemAdded not supported Yet { continue; } lock (SyncLockSelective) { ResourcePool pool = null; ResourcePool poolID = null; #region pool selection if (type == EventTypeInternal.ItemRemoved && (eventType & EventTypeInternal.ItemRemoved) != 0) { pool = _selectiveRemoveEventPool; poolID = _selectiveRemoveEventIDPool; } else if (type == EventTypeInternal.ItemUpdated && (eventType & EventTypeInternal.ItemUpdated) != 0) { pool = _selectiveUpdateEventPool; poolID = _selectiveUpdateEventIDPool; } if (pool == null) { continue; } #endregion while (true) { int i = type == EventTypeInternal.ItemUpdated ? 0 : 1; if (pool.GetResource(callback) == null) { returnValue[i] = type == EventTypeInternal.ItemUpdated ? ++_selectiveUpdateCallbackRef : ++_selectveRemoveCallbackRef; pool.AddResource(callback, returnValue[i]); poolID.AddResource(returnValue[i], callback); break; } else { try { short cref = (short)pool.GetResource(callback); if (cref < 0) { break; //FAIL CONDITION } //add it again into the table for updating ref count. pool.AddResource(callback, cref); poolID.AddResource(cref, callback); returnValue[i] = cref; break; } catch (NullReferenceException) { //Legacy code: can create an infinite loop //Recomendation of returning a negative number instead of continue continue; } } } } } if (_selectiveEventsSubscription == null && callbackType != CallbackType.PullBasedCallback) { Topic topic = (Topic)_cache._messagingService.GetTopic(TopicConstant.ItemLevelEventsTopic, true); _selectiveEventsSubscription = (TopicSubscription)topic.CreateEventSubscription(OnSelectiveEventMessageReceived); } return(returnValue); }
/// <summary> /// TheadSafe and no locks internally /// </summary> /// <param name="key"></param> /// <param name="eventType">Should contain one type i.e. should not be used as a flag. /// Every EventType should be executed from another thread</param> /// <param name="item"></param> /// <param name="oldItem"></param> /// <param name="reason"></param> /// <param name="_notifyAsync"></param> /// <param name="eventhandle"></param> internal void RaiseSelectiveCacheNotification(string key, EventType eventType, EventCacheItem item, EventCacheItem oldItem, CacheItemRemovedReason reason, bool _notifyAsync, EventHandle eventhandle, EventDataFilter dataFilter) { try { ResourcePool poolID = null; CacheEventArg arg = null; var bitSet = new BitSet(); if (_cache.SerializationFormat == Common.Enum.SerializationFormat.Json) { bitSet.SetBit(BitSetConstants.JsonData); } if (item != null) { item.SetValue(_cache.SafeDeserialize <object>(item.GetValue <object>(), _cache.SerializationContext, bitSet, UserObjectType.CacheItem)); } if (oldItem != null) { oldItem.SetValue(_cache.SafeDeserialize <object>(oldItem.GetValue <object>(), _cache.SerializationContext, bitSet, UserObjectType.CacheItem)); } if ((eventType & EventType.ItemUpdated) != 0) { poolID = _selectiveUpdateEventIDPool; } else if ((eventType & EventType.ItemRemoved) != 0) { poolID = _selectiveRemoveEventIDPool; } arg = CreateCacheEventArgument(dataFilter, key, _cacheName, eventType, item, oldItem, reason); if (poolID == null) { return; } CacheDataNotificationCallback callback = poolID.GetResource((short)eventhandle.Handle) as CacheDataNotificationCallback; if (callback == null) //Can occur if Unregistered concurrently { return; } if (_notifyAsync) { System.Threading.ThreadPool.QueueUserWorkItem(waitC, new object[] { callback, key, arg }); //Faster and better } else { callback.Invoke(key, arg); } } catch (Exception ex) { if (_logger != null && _logger.IsErrorEnabled) { _logger.CriticalInfo(ex.ToString()); } } }
internal void FireCQEvents(string key, EventType eventType, EventCacheItem item, EventCacheItem oldItem, bool notifyAsync, string cacheName, BitSet flag, EventDataFilter datafilter) { try { CQEventArg arg = null; ICollection collection = null; ResourcePool pool = null; ResourcePool filterPool = null; if ((eventType & EventType.ItemAdded) != 0 && _cqAddEventPool != null) { pool = _cqAddEventPool; collection = _cqAddEventPool.Keys; filterPool = _cqAddEventDataFilter; } else if ((eventType & EventType.ItemUpdated) != 0 && _cqUpdateEventPool != null) { pool = _cqUpdateEventPool; collection = _cqUpdateEventPool.Keys; filterPool = _cqUpdateEventDataFilter; } else if ((eventType & EventType.ItemRemoved) != 0 && _cqRemoveEventPool != null) { pool = _cqRemoveEventPool; collection = _cqRemoveEventPool.Keys; filterPool = _cqRemoveEventDataFilter; } else { return; } if (collection != null && collection.Count > 0) { QueryDataNotificationCallback[] disc = null; lock (syncLock) { disc = new QueryDataNotificationCallback[collection.Count]; collection.CopyTo(disc, 0); //to avoid locking } for (int i = 0; i < disc.Length; i++) { short index = -1; object obj = pool.GetResource(disc[i]); index = Convert.ToInt16(obj); if (index > -1) { //Not to fire event if datafilter recieved is less than requried OR noDF present EventDataFilter queryDataFilter = (EventDataFilter)filterPool.GetResource(index); if ((eventType & EventType.ItemAdded) != 0) { arg = CreateCQEventArgument(queryDataFilter, key, cacheName, EventType.ItemAdded, item, oldItem); } else if ((eventType & EventType.ItemUpdated) != 0) { arg = CreateCQEventArgument(queryDataFilter, key, cacheName, EventType.ItemUpdated, item, oldItem); } else if ((eventType & EventType.ItemRemoved) != 0) { arg = CreateCQEventArgument(queryDataFilter, key, cacheName, EventType.ItemRemoved, item, oldItem); } else { return; } arg.ContinuousQuery = this; if (notifyAsync) { #if !NETCORE disc[i].BeginInvoke(key, arg, asyn, disc[i]); #elif NETCORE TaskFactory factory = new TaskFactory(); int temp = i; Task task = factory.StartNew(() => disc[temp](key, arg)); #endif } else { disc[i].Invoke(key, arg); } } } } } catch (Exception ex) { } }
private void RegisterCQ(QueryDataNotificationCallback callback, EventType eventType, EventDataFilter datafilter) { if (callback == null) { throw new ArgumentNullException("callback"); } foreach (EventType type in Enum.GetValues(typeof(EventType))) { lock (syncLock) { ResourcePool pool = null; ResourcePool poolDF = null; #region pool selection if (type == EventType.ItemAdded && (eventType & EventType.ItemAdded) != 0) { pool = _cqAddEventPool; poolDF = _cqAddEventDataFilter; } else if (type == EventType.ItemRemoved && (eventType & EventType.ItemRemoved) != 0) { pool = _cqRemoveEventPool; poolDF = _cqRemoveEventDataFilter; } else if (type == EventType.ItemUpdated && (eventType & EventType.ItemUpdated) != 0) { pool = _cqUpdateEventPool; poolDF = _cqUpdateEventDataFilter; } if (pool == null) { continue; } #endregion while (true) { if (pool.GetResource(callback) == null) { short refcallback = -1; if (type == EventType.ItemAdded) { refcallback = ++_cqAddCallbackRef; _cqAddDF = _cqAddDF < datafilter ? datafilter : _cqAddDF; } else if (type == EventType.ItemRemoved) { refcallback = ++_cqRemoveCallbackRef; _cqRemoveDF = _cqRemoveDF < datafilter ? datafilter : _cqRemoveDF; } else { refcallback = ++_cqUpdateCallbackRef; _cqUpdateDF = _cqUpdateDF < datafilter ? datafilter : _cqUpdateDF; } pool.AddResource(callback, refcallback); poolDF.AddResource(refcallback, datafilter); break; } else { try { short cref = (short)pool.GetResource(callback); if (cref < 0) { break; //FAIL CONDITION } poolDF.RemoveResource(cref); pool.RemoveResource(callback); //add it again into the table for updating ref count. pool.AddResource(callback, cref); poolDF.AddResource(cref, datafilter); break; } catch (NullReferenceException) { continue; } } } } } }
/// <summary> /// Returning Negative value means operation not successfull /// </summary> /// <param name="discriptor"></param> /// <param name="eventType"></param> /// <returns>short array <para>1st value is Update callbackRef</para> <para>nd value is removeRef</para></returns> private short[] RegisterSelectiveDiscriptor(CacheDataNotificationCallback callback, EventType eventType) { if (callback == null) { return(null); //FAIL CONDITION } short[] returnValue = new short[] { -1, -1 }; //First value update callback ref & sencond is remove callbackref foreach (EventType type in Enum.GetValues(typeof(EventType))) { if (type == EventType.ItemAdded) //ItemAdded not supported Yet { continue; } lock (SyncLockSelective) { ResourcePool pool = null; ResourcePool poolID = null; #region pool selection if (type == EventType.ItemRemoved && (eventType & EventType.ItemRemoved) != 0) { pool = _selectiveRemoveEventPool; poolID = _selectiveRemoveEventIDPool; } else if (type == EventType.ItemUpdated && (eventType & EventType.ItemUpdated) != 0) { pool = _selectiveUpdateEventPool; poolID = _selectiveUpdateEventIDPool; } if (pool == null) { continue; } #endregion while (true) { int i = type == EventType.ItemUpdated ? 0 : 1; if (pool.GetResource(callback) == null) { returnValue[i] = type == EventType.ItemUpdated ? ++_selectiveUpdateCallbackRef : ++_selectveRemoveCallbackRef; pool.AddResource(callback, returnValue[i]); poolID.AddResource(returnValue[i], callback); break; } else { try { short cref = (short)pool.GetResource(callback); if (cref < 0) { break; } //add it again into the table for updating ref count. pool.AddResource(callback, cref); poolID.AddResource(cref, callback); returnValue[i] = cref; break; } catch (NullReferenceException) { continue; } } } } } return(returnValue); }