private void RemoveInactiveClients() { var inactiveClients = _store.GetInActiveClientSubscriptions(ServiceConfiguration.PubSubClientInactivityTimeout); int runCount = 0; if (inactiveClients != null) { OperationContext context = null; try { context = OperationContext.CreateAndMarkInUse(_context.TransactionalPoolManager, NCModulesConstants.PubSub, OperationContextFieldName.InternalOperation, true); foreach (KeyValuePair <string, IList <string> > pair in inactiveClients) { foreach (string inactiveClient in pair.Value) { if (runCount++ > 200) { _hadPendingWork = true; break; //to prevent starvation and let other operations happen } try { if (_context.NCacheLog.IsInfoEnabled) { _context.NCacheLog.Info("MessageProcessor.RemoveInactiveClients", inactiveClient + " subscription removed due to inactivity against topic " + pair.Key); } _store.TopicOperation(new SubscriptionOperation(pair.Key, TopicOperationType.RemoveInavtiveSubscripber, new SubscriptionInfo() { ClientId = inactiveClient }), context); } catch (Exception e) { _context.NCacheLog.Error("MessageProcessor.RemoveInactiveClients", e.ToString()); } } } } finally { MiscUtil.ReturnOperationContextToPool(context, _context.TransactionalPoolManager); context?.MarkFree(NCModulesConstants.PubSub); } } }
public void Run() { OperationContext context = null; try { var activeClients = _store.GetActiveClientSubscriptions(TimeSpan.FromMilliseconds(_interval)); if (activeClients != null) { context = OperationContext.CreateAndMarkInUse(_context.TransactionalPoolManager, NCModulesConstants.PubSub, OperationContextFieldName.InternalOperation, true); foreach (KeyValuePair <string, IList <string> > pair in activeClients) { foreach (string client in pair.Value) { if (_context.NCacheLog.IsInfoEnabled) { _context.NCacheLog.Info("SubscriptionRefresher", client + " being refreshed for topic " + pair.Key); } _store.TopicOperation(new SubscriptionOperation(pair.Key, Common.Enum.TopicOperationType.RefreshSubscription, new SubscriptionInfo() { ClientId = client }), context); } } } } catch (Exception e) { _context.NCacheLog.Error("SubscriptionReresherTask", e.ToString()); } finally { MiscUtil.ReturnOperationContextToPool(context, _context.TransactionalPoolManager); context?.MarkFree(NCModulesConstants.PubSub); } }
public override void ExecuteCommand(ClientManager clientManager, Alachisoft.NCache.Common.Protobuf.Command command) { int overload; string exception = null; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); try { overload = command.MethodOverload; cmdInfo = ParseCommand(command, clientManager); } catch (System.Exception exc) { _removeResult = OperationResult.Failure; if (!base.immatureId.Equals("-2")) { //PROTOBUF:RESPONSE _serializedResponsePackets.Add(Alachisoft.NCache.Common.Util.ResponseHelper.SerializeExceptionResponseWithType(exc, command.requestID, command.commandID, clientManager.ClientVersion)); } return; } CompressedValueEntry flagValueEntry = null; NCache nCache = clientManager.CmdExecuter as NCache; if (!cmdInfo.DoAsync) { OperationContext operationContext = null; try { Notifications notification = null; if (cmdInfo.DsItemRemovedId != -1) { notification = new Notifications(clientManager.ClientID, -1, -1, -1, -1, cmdInfo.DsItemRemovedId , Runtime.Events.EventDataFilter.None, Runtime.Events.EventDataFilter.None); //DataFilter not required } operationContext = _operationContext; operationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); operationContext.Add(OperationContextFieldName.ClientId, clientManager.ClientID); operationContext.Add(OperationContextFieldName.MethodOverload, overload); operationContext.Add(OperationContextFieldName.RaiseCQNotification, true); CommandsUtil.PopulateClientIdInContext(ref operationContext, clientManager.ClientAddress); flagValueEntry = nCache.Cache.Remove(cmdInfo.Key, cmdInfo.FlagMap, notification, cmdInfo.LockId, cmdInfo.Version, cmdInfo.LockAccessType, cmdInfo.ProviderName, operationContext); stopWatch.Stop(); UserBinaryObject ubObject = null; if (flagValueEntry != null) { if (flagValueEntry.Value is UserBinaryObject) { ubObject = (UserBinaryObject)flagValueEntry.Value; } else { var flag = flagValueEntry.Flag; ubObject = (flagValueEntry == null) ? null : (UserBinaryObject)nCache.Cache.SocketServerDataService.GetClientData(flagValueEntry.Value, ref flag, LanguageContext.DOTNET); } } //PROTOBUF:RESPONSE RemoveResponse removeResponse = _removeResponse; if (ubObject != null) { removeResponse.value.AddRange(ubObject.DataList); removeResponse.flag = flagValueEntry.Flag.Data; removeResponse.itemType = MiscUtil.EntryTypeToProtoItemType(flagValueEntry.Type); } if (clientManager.ClientVersion >= 5000) { ResponseHelper.SetResponse(removeResponse, command.requestID, command.commandID); _serializedResponsePackets.Add(ResponseHelper.SerializeResponse(removeResponse, Response.Type.REMOVE)); } else { Response response = Stash.ProtobufResponse; { response.remove = removeResponse; }; ResponseHelper.SetResponse(response, command.requestID, command.commandID, Response.Type.REMOVE); _serializedResponsePackets.Add(Alachisoft.NCache.Common.Util.ResponseHelper.SerializeResponse(response)); } } catch (System.Exception exc) { _removeResult = 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); cmdInfo.FlagMap?.MarkFree(NCModulesConstants.SocketServer); TimeSpan executionTime = stopWatch.Elapsed; if (flagValueEntry != null) { MiscUtil.ReturnEntryToPool(flagValueEntry.Entry, clientManager.CacheTransactionalPool); MiscUtil.ReturnCompressedEntryToPool(flagValueEntry, clientManager.CacheTransactionalPool); } try { if (Alachisoft.NCache.Management.APILogging.APILogManager.APILogManger != null && Alachisoft.NCache.Management.APILogging.APILogManager.EnableLogging) { APILogItemBuilder log = new APILogItemBuilder(MethodsName.REMOVE.ToLower()); log.GenerateRemoveAPILogItem(cmdInfo.Key, cmdInfo.FlagMap, cmdInfo.LockId, (long)cmdInfo.Version, cmdInfo.LockAccessType, cmdInfo.ProviderName, cmdInfo.DsItemRemovedId, overload, exception, executionTime, clientManager.ClientID.ToLower(), clientManager.ClientSocketId.ToString()); } } catch { } } } else { OperationContext operationContext = null; try { cmdInfo.FlagMap = new BitSet { Data = cmdInfo.FlagMap.Data }; object[] package = null; if (cmdInfo.RequestId != -1 || cmdInfo.DsItemRemovedId != -1) { package = new object[] { cmdInfo.Key, cmdInfo.FlagMap, new Notifications(clientManager.ClientID, Convert.ToInt32(cmdInfo.RequestId), -1, -1, (short)(cmdInfo.RequestId == -1 ? -1 : 0), cmdInfo.DsItemRemovedId , Runtime.Events.EventDataFilter.None, Runtime.Events.EventDataFilter.None), cmdInfo.ProviderName }; //DataFilter not required } else { package = new object[] { cmdInfo.Key, cmdInfo.FlagMap, null, cmdInfo.ProviderName }; } operationContext = OperationContext.CreateAndMarkInUse(clientManager.CacheFakePool, NCModulesConstants.SocketServer); operationContext.Add(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation); operationContext.Add(OperationContextFieldName.RaiseCQNotification, true); operationContext.Add(OperationContextFieldName.MethodOverload, overload); nCache.Cache.RemoveAsync(package, operationContext); stopWatch.Stop(); } 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.RemoveAsync.ToLower()); log.GenerateRemoveAPILogItem(cmdInfo.Key, cmdInfo.FlagMap, cmdInfo.LockId, (long)cmdInfo.Version, cmdInfo.LockAccessType, cmdInfo.ProviderName, cmdInfo.DsItemRemovedId, overload, exception, executionTime, clientManager.ClientID.ToLower(), clientManager.ClientSocketId.ToString()); } } catch { } } } }