use of org.smartdata.common.rule.RuleInfo in project SSM by Intel-bigdata.
the class RuleQueryExecutor method run.
@Override
public void run() {
if (exited) {
exitSchedule();
}
long rid = ctx.getRuleId();
try {
long startCheckTime = System.currentTimeMillis();
if (ruleManager.isClosed()) {
exitSchedule();
}
RuleInfo info = ruleManager.getRuleInfo(rid);
RuleState state = info.getState();
if (exited || state == RuleState.DELETED || state == RuleState.FINISHED || state == RuleState.DISABLED) {
exitSchedule();
}
TimeBasedScheduleInfo scheduleInfo = tr.getTbScheduleInfo();
if (scheduleInfo.getEndTime() != TimeBasedScheduleInfo.FOR_EVER && // TODO: tricky here, time passed
startCheckTime - scheduleInfo.getEndTime() > 0) {
// TODO: special for scheduleInfo.isOneShot()
LOG.info("Rule " + ctx.getRuleId() + " exit rule executor due to time passed or finished");
ruleManager.updateRuleInfo(rid, RuleState.FINISHED, timeNow(), 0, 0);
exitSchedule();
}
List<String> files = executeFileRuleQuery();
long endCheckTime = System.currentTimeMillis();
int numCmdSubmitted = submitCommands(files, rid);
ruleManager.updateRuleInfo(rid, null, timeNow(), 1, numCmdSubmitted);
if (exited) {
exitSchedule();
}
//System.out.println(this + " -> " + System.currentTimeMillis());
long endProcessTime = System.currentTimeMillis();
if (endProcessTime - startCheckTime > 3000 || LOG.isDebugEnabled()) {
LOG.warn("Rule " + ctx.getRuleId() + " execution took " + (endProcessTime - startCheckTime) + "ms. QueryTime = " + (endCheckTime - startCheckTime) + "ms, SubmitTime = " + (endProcessTime - endCheckTime) + "ms.");
}
} catch (IOException e) {
LOG.error("Rule " + ctx.getRuleId() + " exception", e);
}
}
use of org.smartdata.common.rule.RuleInfo in project SSM by Intel-bigdata.
the class TestGetRuleInfo method testMultiRules.
@Test
public void testMultiRules() throws Exception {
waitTillSSMExitSafeMode();
String rule = "file: every 1s \n | length > 10 | cachefile";
SmartAdmin client = new SmartAdmin(conf);
int nRules = 100;
for (int i = 0; i < nRules; i++) {
client.submitRule(rule, RuleState.ACTIVE);
}
List<RuleInfo> ruleInfos = client.listRulesInfo();
for (RuleInfo info : ruleInfos) {
System.out.println(info);
}
Assert.assertTrue(ruleInfos.size() == nRules);
}
use of org.smartdata.common.rule.RuleInfo in project SSM by Intel-bigdata.
the class TestGetRuleInfo method testGetSingleRuleInfo.
@Test
public void testGetSingleRuleInfo() throws Exception {
waitTillSSMExitSafeMode();
String rule = "file: every 1s \n | length > 10 | cachefile";
SmartAdmin client = new SmartAdmin(conf);
long ruleId = client.submitRule(rule, RuleState.ACTIVE);
RuleInfo info1 = client.getRuleInfo(ruleId);
System.out.println(info1);
Assert.assertTrue(info1.getRuleText().equals(rule));
RuleInfo infoTemp = info1;
for (int i = 0; i < 3; i++) {
Thread.sleep(1000);
infoTemp = client.getRuleInfo(ruleId);
System.out.println(infoTemp);
}
Assert.assertTrue(infoTemp.getNumChecked() >= info1.getNumChecked() + 2);
long fakeRuleId = 10999999999L;
try {
client.getRuleInfo(fakeRuleId);
Assert.fail("Should raise an exception when using a invalid rule id");
} catch (IOException e) {
}
}
use of org.smartdata.common.rule.RuleInfo in project SSM by Intel-bigdata.
the class TestRuleManager method testMultiThreadChangeState.
@Test
public void testMultiThreadChangeState() throws Exception {
String rule = "file: every 1s \n | length > 10 | cachefile";
long now = System.currentTimeMillis();
long length = 100;
long fid = 10000;
FileStatusInternal[] files = { new FileStatusInternal(length, false, 3, 1024, now, now, null, null, null, null, "testfile".getBytes(), "/tmp", fid, 0, null, (byte) 3) };
dbAdapter.insertFiles(files);
long rid = ruleManager.submitRule(rule, RuleState.ACTIVE);
long start = System.currentTimeMillis();
int nThreads = 10;
Thread[] threads = new Thread[nThreads];
for (int i = 0; i < nThreads; i++) {
threads[i] = new Thread(new StateChangeWorker(rid));
}
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
long end = System.currentTimeMillis();
System.out.println("Time used = " + (end - start) + " ms");
// This is needed due to async threads
Thread.sleep(1000);
RuleInfo res = ruleManager.getRuleInfo(rid);
System.out.println(res);
Thread.sleep(5000);
RuleInfo after = ruleManager.getRuleInfo(rid);
System.out.println(after);
if (res.getState() == RuleState.ACTIVE) {
Assert.assertTrue(after.getNumCmdsGen() - res.getNumCmdsGen() <= 6);
} else {
Assert.assertTrue(after.getNumCmdsGen() == res.getNumCmdsGen());
}
}
use of org.smartdata.common.rule.RuleInfo in project SSM by Intel-bigdata.
the class TestRuleManager method testResumeRule.
@Test
public void testResumeRule() throws Exception {
String rule = "file: every 1s from now to now + 100s \n | " + "length > 300 | cachefile";
long id = ruleManager.submitRule(rule, RuleState.ACTIVE);
RuleInfo ruleInfo = ruleManager.getRuleInfo(id);
Assert.assertTrue(ruleInfo.getRuleText().equals(rule));
RuleInfo info = ruleInfo;
for (int i = 0; i < 2; i++) {
Thread.sleep(1000);
info = ruleManager.getRuleInfo(id);
System.out.println(info);
}
Assert.assertTrue(info.getNumChecked() > ruleInfo.getNumChecked());
ruleManager.disableRule(ruleInfo.getId(), true);
Thread.sleep(1000);
RuleInfo info2 = ruleManager.getRuleInfo(id);
for (int i = 0; i < 3; i++) {
Thread.sleep(1000);
info = ruleManager.getRuleInfo(id);
System.out.println(info);
}
Assert.assertTrue(info.getNumChecked() == info2.getNumChecked());
RuleInfo info3 = info;
ruleManager.activateRule(ruleInfo.getId());
for (int i = 0; i < 3; i++) {
Thread.sleep(1000);
info = ruleManager.getRuleInfo(id);
System.out.println(info);
}
Assert.assertTrue(info.getNumChecked() > info3.getNumChecked());
}
Aggregations