Exemplo n.º 1
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void StatsDSender()
        {
            DateTime lastFlushTime = DateTime.MinValue;

            using (var syncHandle = new ManualResetEventSlim())
            {
                // Execute the query
                while (!Stop)
                {
                    if (!CancelToken.IsCancellationRequested)
                    {
                        try
                        {
                            int            messageCount = 0;
                            List <JObject> messages     = new List <JObject>();

                            // Lets get whats in the queue
                            lock (_locker)
                            {
                                messageCount = _jsonQueue.Count;

                                // Time to flush?
                                if (messageCount >= _flushSize || (DateTime.UtcNow - lastFlushTime).Seconds >= _idleFlushTimeSeconds)
                                {
                                    messages = _jsonQueue.Take(messageCount).ToList();
                                    _jsonQueue.RemoveRange(0, messageCount);
                                    if (messages.Count > 0)
                                    {
                                        _manager.IncrementMessageCount(messages.Count);
                                    }
                                }
                            }

                            TransmitStats(messages);

                            if (!Stop)
                            {
                                syncHandle.Wait(TimeSpan.FromMilliseconds(_interval), CancelToken);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                        catch (ThreadAbortException)
                        {
                            break;
                        }
                        catch (Exception ex)
                        {
                            Interlocked.Increment(ref _errorCount);
                            LogManager.GetCurrentClassLogger().Error(ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void ElasticsearchSender()
        {
            while (!CancelToken.IsCancellationRequested)
            {
                JObject[] messages;
                lock (_locker)
                {
                    var count = _jsonQueue.Count;
                    messages = _jsonQueue.Take(count).ToArray();
                    _jsonQueue.RemoveRange(0, count);
                    if (messages.Length > 0)
                    {
                        _manager.IncrementMessageCount(messages.Length);
                    }
                }

                if (messages.Length > 0)
                {
                    int numHosts = _host.Length;
                    while (numHosts-- > 0)
                    {
                        try
                        {
                            // Get the next client
                            RestClient client = getClient();
                            if (client != null)
                            {
                                LogManager.GetCurrentClassLogger()
                                .Debug("Sending {0} Messages to {1}", messages.Length, client.BaseUrl);

                                foreach (JObject json in messages)
                                {
                                    string typeName = "Win32-Elasticsearch";
                                    if (json["type"] != null)
                                    {
                                        typeName = json["type"].ToString();
                                    }

                                    ////check if the submitted JSON object provides a custom index. If yes, use this one
                                    var    token     = json["_index"];
                                    string indexName = token == null ? _index : token.Value <string>();

                                    if (string.IsNullOrEmpty(indexName))
                                    {
                                        indexName = string.Format("logstash-{0}", DateTime.UtcNow.ToString("yyyy.MM.dd"));
                                    }
                                    var req = new RestRequest(string.Format("/{0}/{1}/", indexName, typeName), Method.POST);

                                    req.AddParameter("text/json", json.ToString(), ParameterType.RequestBody);

                                    req.RequestFormat = DataFormat.Json;

                                    try
                                    {
                                        client.ExecuteAsync(req, response =>
                                        {
                                            if (response.StatusCode != HttpStatusCode.Created)
                                            {
                                                LogManager.GetCurrentClassLogger()
                                                .Error("Failed to send: {0}", response.ErrorMessage);
                                                Interlocked.Increment(ref _errorCount);
                                            }
                                            else
                                            {
                                                _sentMessages++;
                                                GC.Collect();
                                            }
                                        });
                                    }
                                    catch (Exception error)
                                    {
                                        LogManager.GetCurrentClassLogger().Error(error);
                                        Interlocked.Increment(ref _errorCount);
                                    }
                                }
                                GC.Collect();
                            }
                            else
                            {
                                LogManager.GetCurrentClassLogger()
                                .Fatal("Unable to connect with any Elasticsearch hosts, {0}",
                                       String.Join(",", _host));
                                Interlocked.Increment(ref _errorCount);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetCurrentClassLogger().Error(ex);
                            Interlocked.Increment(ref _errorCount);
                        }
                    }
                }
                GC.Collect();
                System.Threading.Thread.Sleep(_interval);
            }
        }
Exemplo n.º 3
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void ElasticsearchSender()
        {
            // Force an inital flush
            DateTime lastFlushTime = DateTime.MinValue;

            LogManager.GetCurrentClassLogger()
            .Info("{0}: Elasticsarch Output To {1} Ready", Thread.CurrentThread.ManagedThreadId, string.Join(",", _hosts));


            using (var syncHandle = new ManualResetEventSlim())
            {
                // Execute the query
                while (!Stop)
                {
                    if (!CancelToken.IsCancellationRequested)
                    {
                        try
                        {
                            int            messageCount = 0;
                            List <JObject> messages     = new List <JObject>();

                            // Lets get whats in the queue
                            lock (_locker)
                            {
                                messageCount = _jsonQueue.Count;

                                // Time to flush?
                                if (messageCount >= _flushSize || (DateTime.UtcNow - lastFlushTime).Seconds >= _idleFlushTimeSeconds)
                                {
                                    messages = _jsonQueue.Take(messageCount).ToList();
                                    _jsonQueue.RemoveRange(0, messageCount);
                                    if (messages.Count > 0)
                                    {
                                        _manager.IncrementMessageCount(messages.Count);
                                    }
                                }
                            }

                            // We have some messages to work with
                            if (messages.Count > 0)
                            {
                                var client = getClient();

                                LogManager.GetCurrentClassLogger()
                                .Debug("Sending {0} Messages to {1}", messages.Count, string.Join(",", _hosts));
                                // This loop will process all messages we've taken from the queue
                                // that have the same index and type (an elasticsearch requirement)
                                do
                                {
                                    try
                                    {
                                        // Grab all messages with same index and type (this is the whole point, group the same ones)
                                        var bulkTypeName  = this._parameters.GetTypeName(messages[0]);
                                        var bulkIndexName = this._parameters.GetIndexName(messages[0]);

                                        IEnumerable <JObject> bulkItems =
                                            messages.TakeWhile(
                                                message =>
                                                String.Compare(bulkTypeName, _parameters.GetTypeName(message), false) == 0 &&
                                                String.Compare(bulkIndexName, _parameters.GetIndexName(message), false) == 0);

                                        // Send the message(s), if the are successfully sent, they
                                        // are removed from the queue
                                        lastFlushTime = transmitBulkData(bulkItems, bulkIndexName, bulkTypeName, client, lastFlushTime, messages);

                                        GC.Collect();
                                    }
                                    catch (Exception ex)
                                    {
                                        LogManager.GetCurrentClassLogger().Error(ex);
                                        break;
                                    }
                                } while (messages.Count > 0);
                            }
                            GC.Collect();
                            if (!Stop)
                            {
                                syncHandle.Wait(TimeSpan.FromMilliseconds(_interval), CancelToken);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetCurrentClassLogger().Error(ex);
                        }
                    }
                }
            }

            LogManager.GetCurrentClassLogger()
            .Info("{0}: Elasticsarch Output To {1} Terminated", Thread.CurrentThread.ManagedThreadId, string.Join(",", _hosts));
        }
Exemplo n.º 4
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void RedisSender()
        {
            while (!CancelToken.IsCancellationRequested)
            {
                string[] messages;
                lock (_locker)
                {
                    messages = _jsonQueue.Take(_batchCount).ToArray();
                    _jsonQueue.RemoveRange(0, messages.Length);
                    if (messages.Length > 0)
                    {
                        _manager.IncrementMessageCount(messages.Length);
                    }
                }

                if (messages.Length > 0)
                {
                    int numHosts = _redisHosts.Length;
                    while (numHosts-- > 0)
                    {
                        try
                        {
                            // Get the next client
                            using (RedisClient client = getClient())
                            {
                                if (client != null)
                                {
                                    client.StartPipe();
                                    LogManager.GetCurrentClassLogger()
                                    .Debug("Sending {0} Messages to {1}", messages.Length, client.Host);

                                    foreach (string jsonMessage in messages)
                                    {
                                        try
                                        {
                                            _redisDepth = client.RPush(_logstashIndexName, jsonMessage);
                                            _sentMessages++;
                                        }
                                        catch (SocketException ex)
                                        {
                                            LogManager.GetCurrentClassLogger().Warn(ex);
                                            Interlocked.Increment(ref _errorCount);
                                        }
                                    }
                                    client.EndPipe();
                                    GC.Collect();
                                    break;
                                }
                                else
                                {
                                    Interlocked.Increment(ref _errorCount);
                                    LogManager.GetCurrentClassLogger()
                                    .Fatal("Unable to connect with any Redis hosts, {0}",
                                           String.Join(",", _redisHosts));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetCurrentClassLogger().Error(ex);
                            Interlocked.Increment(ref _errorCount);
                        }
                    }
                }
                GC.Collect();
                System.Threading.Thread.Sleep(_interval);
            }
        }
Exemplo n.º 5
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void ElasticsearchSender()
        {
            while (!CancelToken.IsCancellationRequested)
            {
                JObject[] messages;
                lock (_locker)
                {
                    var count = _jsonQueue.Count;
                    messages = _jsonQueue.Take(count).ToArray();
                    _jsonQueue.RemoveRange(0, count);
                    if (messages.Length > 0)
                    {
                        _manager.IncrementMessageCount(messages.Length);
                    }
                }

                if (messages.Length > 0)
                {
                    int numHosts = _host.Length;
                    while (numHosts-- > 0)
                    {
                        try
                        {
                            // Get the next client
                            RestClient client = getClient();
                            if (client != null)
                            {
                                LogManager.GetCurrentClassLogger()
                                .Debug("Sending {0} Messages to {1}", messages.Length, client.BaseUrl);

                                foreach (JObject json in messages)
                                {
                                    var typeName  = this.eo.GetTypeName(json);
                                    var indexName = this.eo.GetIndexName(json);
                                    var req       = new RestRequest(string.Format("/{0}/{1}/", indexName, typeName), Method.POST);

                                    req.AddParameter("text/json", json.ToString(), ParameterType.RequestBody);

                                    req.RequestFormat = DataFormat.Json;

                                    try
                                    {
                                        client.ExecuteAsync(req, response =>
                                        {
                                            if (response.StatusCode != HttpStatusCode.Created)
                                            {
                                                LogManager.GetCurrentClassLogger()
                                                .Error("Failed to send: {0}", response.ErrorMessage);
                                                Interlocked.Increment(ref _errorCount);
                                            }
                                            else
                                            {
                                                _sentMessages++;
                                                GC.Collect();
                                            }
                                        });
                                    }
                                    catch (Exception error)
                                    {
                                        LogManager.GetCurrentClassLogger().Error(error);
                                        Interlocked.Increment(ref _errorCount);
                                    }
                                }
                                GC.Collect();
                            }
                            else
                            {
                                LogManager.GetCurrentClassLogger()
                                .Fatal("Unable to connect with any Elasticsearch hosts, {0}",
                                       String.Join(",", _host));
                                Interlocked.Increment(ref _errorCount);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetCurrentClassLogger().Error(ex);
                            Interlocked.Increment(ref _errorCount);
                        }
                    }
                }
                GC.Collect();
                System.Threading.Thread.Sleep(_interval);
            }
        }
Exemplo n.º 6
0
        //
        // Pull off messages from the Queue, batch them up and send them all across
        //
        private void RedisSender()
        {
            using (var syncHandle = new ManualResetEventSlim())
            {
                // Execute the query
                while (!Stop)
                {
                    if (!CancelToken.IsCancellationRequested)
                    {
                        try
                        {
                            string[] messages;
                            // Exclusively
                            lock (_locker)
                            {
                                _batchCounter.SampleQueueDepth(_jsonQueue.Count);
                                // Re-compute current batch size

                                LogManager.GetCurrentClassLogger().Trace("{0}: Average Queue Depth: {1}, Current Length: {2}", Thread.CurrentThread.ManagedThreadId, _batchCounter.AverageQueueDepth(), _jsonQueue.Count);

                                _currentBatchCount = _batchCounter.UpdateCurrentBatchCount(_jsonQueue.Count, _currentBatchCount);

                                messages = _jsonQueue.Take(_currentBatchCount).ToArray();
                                _jsonQueue.RemoveRange(0, messages.Length);
                            }

                            if (messages.Length > 0)
                            {
                                int  numHosts         = _redisHosts.Length;
                                bool sentSuccessfully = false;
                                while (numHosts-- > 0)
                                {
                                    try
                                    {
                                        // Get the next client
                                        using (RedisClient client = getClient())
                                        {
                                            if (client != null)
                                            {
                                                client.StartPipe();
                                                LogManager.GetCurrentClassLogger()
                                                .Debug("{0}: Sending {1} Messages to {2}", Thread.CurrentThread.ManagedThreadId, messages.Length, client.Host);

                                                try
                                                {
                                                    _redisDepth = client.RPush(_logstashIndexName, messages);
                                                    Interlocked.Add(ref _sentMessages, messages.Length);
                                                    client.EndPipe();
                                                    sentSuccessfully = true;
                                                    if (messages.Length > 0)
                                                    {
                                                        _manager.IncrementMessageCount(messages.Length);
                                                    }
                                                }
                                                catch (SocketException ex)
                                                {
                                                    LogManager.GetCurrentClassLogger().Warn(ex);
                                                    Interlocked.Increment(ref _errorCount);
                                                    _lastErrorTimeUTC = DateTime.UtcNow;
                                                }
                                                catch (Exception ex)
                                                {
                                                    LogManager.GetCurrentClassLogger().Error(ex);
                                                    Interlocked.Increment(ref _errorCount);
                                                    _lastErrorTimeUTC = DateTime.UtcNow;
                                                }
                                                break;
                                            }
                                            else
                                            {
                                                Interlocked.Increment(ref _errorCount);
                                                LogManager.GetCurrentClassLogger()
                                                .Fatal("Unable to connect with any Redis hosts, {0}",
                                                       String.Join(",", _redisHosts));
                                                _lastErrorTimeUTC = DateTime.UtcNow;
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        LogManager.GetCurrentClassLogger().Error(ex);
                                        Interlocked.Increment(ref _errorCount);
                                        _lastErrorTimeUTC = DateTime.UtcNow;
                                    }
                                } // No more hosts to try.

                                // Couldn't send, put it back into the queue.
                                if (!sentSuccessfully)
                                {
                                    lock (_locker)
                                    {
                                        _jsonQueue.InsertRange(0, messages);
                                    }
                                }
                            }
                            if (!Stop)
                            {
                                syncHandle.Wait(TimeSpan.FromMilliseconds(_interval), CancelToken);
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                        catch (ThreadAbortException)
                        {
                            break;
                        }
                        catch (Exception ex)
                        {
                            _lastErrorTimeUTC = DateTime.UtcNow;
                            Interlocked.Increment(ref _errorCount);
                            LogManager.GetCurrentClassLogger().Error(ex);
                        }
                    }
                }
            }
        }