/// <summary> /// Performs a binary search on a sorted list, from a specified position /// </summary> /// <param name="arr"></param> /// <param name="p"></param> /// <param name="v"></param> /// <param name="counter"></param> /// <returns></returns> public static int BinarySearch(IList <int> arr, int p, int v, ICounter counter) { var start = p; var end = arr.Count - 1; if (start > end) { return(start); } while (true) { var mid = (start + end) / 2; var val = arr[mid]; if (mid == start) { counter.Add(1); return(val < v ? mid + 1 : mid); } counter.Add(1); switch (val.CompareTo(v)) { case -1: start = mid + 1; break; case 0: return(mid); case 1: end = mid - 1; break; } } }
public async Task Invoke(HttpContext context) { try { totalCounter.Add(); log.Debug($"Request {context.Request.Method} {context.Request.Path}"); if (HttpMethods.IsGet(context.Request.Method)) { await context.Response.WriteAsync("Frontier is running"); } else if (HttpMethods.IsPost(context.Request.Method)) { var requestPath = context.Request.Path.Value; if (string.IsNullOrEmpty(requestPath)) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } var streamReader = new StreamReader(context.Request.Body); var body = await streamReader.ReadToEndAsync(); try { var reportHandler = reportHandlers.FirstOrDefault(x => x.CanHandle(requestPath)); if (reportHandler == null) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } var report = await reportHandler.Handle(context, body); var logEventData = report.ToLogEventData(); var routingKey = RoutingKey.Create(report.GetProject(), environment, "frontier_" + reportHandler.Name, RoutingKey.LogsSuffix); log.Debug("Send data via airlock to " + routingKey); airlockClient.Push(routingKey, logEventData, logEventData.Timestamp); context.Response.StatusCode = (int)HttpStatusCode.NoContent; } finally { if (resendTo != null) { await ResendRequest(context, body); } } } else if (HttpMethods.IsOptions(context.Request.Method)) { context.Response.StatusCode = (int)HttpStatusCode.NoContent; } } catch (Exception e) { errorCounter.Add(); log.Error(e); } }
private IEnumerable <AirlockEvent <LogEventData> > ThrottleEvents(IEnumerable <AirlockEvent <LogEventData> > events, ICounter messageIgnoredCounter) { var lastTimestampIndex = 0L; var periodCounter = 0; foreach (var airlockEvent in events) { var normalizedTimestampIndex = airlockEvent.Timestamp.Ticks / processorSettings.ThrottlingPeriod.Ticks; if (normalizedTimestampIndex == lastTimestampIndex) { periodCounter++; } else { periodCounter = 0; lastTimestampIndex = normalizedTimestampIndex; } if (periodCounter < processorSettings.ThrottlingThreshold) { yield return(airlockEvent); } else { messageIgnoredCounter.Add(); } } }
public static async Task <int> CounterOrchestrator( [OrchestrationTrigger] IDurableOrchestrationContext context) { var input = context.GetInput <CounterOrchestratorInput>(); ICounter counter = context.CreateEntityProxy <ICounter>(input.EntityKey); // EntityId also works as parameter. // Two-way call to the entity which awaits the response value. return(await counter.Add(input.Amount)); }
public void SendPacket(JsonPacket packet, ICounter sendingErrorCounter) { retriableCallStrategy.Call(() => { var requester = new Requester(packet, ravenClient); return(requester.Request()); }, e => { sendingErrorCounter.Add(); return(IsRetriableException(e)); }, log); }
public void Enqueue(Message <Null, byte[]> message) { if (pausedPartitions != null) { throw new InvalidOperationException($"ProcessorHost is paused for routingKey: {routingKey}"); } messageEnqueuedCounter.Add(); eventsQueue.Add(message, CancellationToken.None); if (eventsQueue.Count >= processorHostSettings.MaxProcessorQueueSize) { var partitionsToPause = AssignedPartitions.ToArray(); consumer.Pause(partitionsToPause.Select(p => new TopicPartition(message.Topic, p))); log.Warn($"PausedConsumption: consumerName: {consumer.Name}, memberId: {consumer.MemberId}, routingKey: {message.Topic}, processorId: {processor.ProcessorId}, pausedPartitions: [{string.Join(", ", partitionsToPause)}]"); pausedPartitions = partitionsToPause; } }
private void BulkIndex(PostData <object> bulkIndexPostData, ICounter sendingErrorCounter) { retriableCallStrategy.Call( () => { var response = elasticClient.Bulk <byte[]>(bulkIndexPostData); if (!response.Success) { throw new ElasticOperationFailedException(response); } }, ex => { sendingErrorCounter.Add(); return((ex as ElasticOperationFailedException)?.IsRetriable == true); }, log); }
public void Parse(ICounter counter) { for (int idx = 0; idx < this._args.Length; ++idx) { bool isSentenceEnd = this.IsEndOfSentence(this._args[idx]); if (idx + 1 < this._args.Length) { if (!this._crossSentenceBoundaries && isSentenceEnd) { continue; } string word1 = this._args[idx]; string word2 = this._args[idx + 1]; counter.Add(this._args[idx], this._args[idx + 1]); } } }
public async Task <Report> Handle(HttpContext context, string body) { totalCounter.Add(); T report; try { report = body.FromJson <T>(); await HandleReport(report); } catch (Exception e) { errorCounter.Add(); context.Response.StatusCode = (int)HttpStatusCode.BadRequest; log.Error(e); return(null); } //report.Timestamp = DateTime.UtcNow.ToString("O"); report.Host = context.Request.Host.Value; log.Debug("report:\n" + report.ToPrettyJson()); return(report); }
/// <summary> /// Splits the line into words using the passed seps and adds the bigrams to the passed counter /// </summary> /// <param name="counter"></param> /// <param name="seps"></param> /// <param name="line"></param> /// <param name="lastWord">carry over last word for the next line</param> private void ProcessLine(ICounter counter, char[] seps, string line, ref string lastWord) { if (!String.IsNullOrWhiteSpace(lastWord)) { line = String.Format("{0} {1}", lastWord, line); } lastWord = ""; StringSplitOptions opts = this._crossSentenceBoundaries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None; string[] words = line.Split(seps, opts); for (int idx = 0; idx < words.Length; ++idx) { if (!this._crossSentenceBoundaries && this.IsEndOfSentence(words[idx])) { continue; } if (idx + 1 < words.Length) { string word1 = words[idx]; string word2 = words[idx + 1]; if (!string.IsNullOrWhiteSpace(word1) && !string.IsNullOrWhiteSpace(word2)) { counter.Add(words[idx], words[idx + 1]); } } else { lastWord = words[idx].Trim(); } } }
public void Add(long value = 1) { counter.Add(value); }
public void AddCreated(int count) { _created.Add(count); }