我使用Nesper作为CEP引擎来处理我的应用程序中的事件。
我正在尝试对以下EPL语句进行建模:
Value
字段在有限的窗口时间内取平均值。 1
,则会生成一个事件。 我把它建模为:
SELECT (
(AVG(ParameterEvent1.Value) = 1) OR
(AVG(ParameterEvent2.Value) = 1)
...
(AVG(ParameterEvent50.Value) = 1)
) AS BooleanValue
FROM
ParameterEvent(Id = 1).win:time(3 sec) AS ParameterEvent1,
ParameterEvent(Id = 2).win:time(3 sec) AS ParameterEvent2
...
ParameterEvent(Id = 50).win:time(3 sec) AS ParameterEvent50
在一个单独的主题中,我正在以稳定的 50事件/秒的速度为引擎提供参数值。
此设置会导致巨大的CPU和RAM使用,直到发生com.espertech.esper.client.EPException: ReaderWriterLock timeout expired
异常。
我想知道导致这个问题的原因。
有趣的是,当我将窗口从win:time(3 sec)
更改为std:lastevent()
时,此问题就会解决。但是,在生成事件之前,我需要窗口以确保参数值为1
到3秒。
完整的演示代码如下。您只需安装Nesper
包即可运行它:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.espertech.esper.client;
namespace NesperTester
{
class Program
{
public class ParameterEvent
{
public int Id { get; set; }
public int Value { get; set; }
}
static EPServiceProvider engine;
const int NParameters = 50;
static void Main(string[] args)
{
engine = EPServiceProviderManager.GetDefaultProvider();
engine.EPAdministrator.Configuration.AddEventType("ParameterEvent", typeof(ParameterEvent));
CreateEPLStatement();
Task.Factory.StartNew(() => SimulateParameterChange(), TaskCreationOptions.LongRunning);
System.Threading.Thread.Sleep(int.MaxValue);
}
static void CreateEPLStatement()
{
const int windowLength = 3;
var sb = new StringBuilder();
for (int i = 1; i <= NParameters; i++)
{
sb.AppendFormat("(AVG(ParameterEvent{0}.Value) = 1) OR ", i);
}
var rule = sb.ToString();
rule = rule.Remove(rule.Length - 4, 4);
sb.Clear();
for (int paramId = 1; paramId <= NParameters; paramId++)
{
sb.AppendFormat("ParameterEvent(Id = {0}).win:time({1} sec) AS ParameterEvent{0}, ", paramId, windowLength);
//sb.AppendFormat("ParameterEvent(Id = {0}).std:lastevent() AS ParameterEvent{0}, ", paramId, windowLength);
}
var selectSources = sb.ToString();
selectSources = selectSources.Remove(selectSources.Length - 2, 2);
var eplStr = string.Format("SELECT ({0}) AS BooleanValue FROM {1}", rule, selectSources);
var statementName = string.Format("statement1");
var statement = engine.EPAdministrator.CreateEPL(eplStr, statementName, null);
statement.Start();
statement.Events += Statement_Events;
}
static bool PrevState = false;
static void Statement_Events(object sender, UpdateEventArgs e)
{
var underlying = e.NewEvents[0].Underlying as Dictionary<string, object>;
if (underlying.Last().Value == null)
{
Console.WriteLine("Statement value is unknown");
}
else
{
var statementValue = (bool)underlying.First().Value;
if (statementValue != PrevState)
{
PrevState = statementValue;
Console.WriteLine("Statement is {0}", statementValue);
}
}
}
static void SimulateParameterChange()
{
int counter = 0;
while (true)
{
for (int i = 1; i <= NParameters; i++)
{
var evt = new ParameterEvent();
evt.Id = i;
if (i == 1 && (counter/4) % 2 == 0)
{
evt.Value = 1;
}
else
{
evt.Value = 0;
}
engine.EPRuntime.SendEvent(evt);
System.Threading.Thread.Sleep(1000 / NParameters);
}
counter++;
}
}
}
}