Search in sources :

Example 6 with RuleInfo

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);
    }
}
Also used : RuleState(org.smartdata.common.rule.RuleState) IOException(java.io.IOException) RuleInfo(org.smartdata.common.rule.RuleInfo) TimeBasedScheduleInfo(org.smartdata.server.rule.parser.TimeBasedScheduleInfo)

Example 7 with RuleInfo

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);
}
Also used : SmartAdmin(org.smartdata.admin.SmartAdmin) RuleInfo(org.smartdata.common.rule.RuleInfo) Test(org.junit.Test)

Example 8 with RuleInfo

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) {
    }
}
Also used : SmartAdmin(org.smartdata.admin.SmartAdmin) IOException(java.io.IOException) RuleInfo(org.smartdata.common.rule.RuleInfo) Test(org.junit.Test)

Example 9 with RuleInfo

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());
    }
}
Also used : FileStatusInternal(org.smartdata.server.metastore.FileStatusInternal) RuleInfo(org.smartdata.common.rule.RuleInfo) Test(org.junit.Test)

Example 10 with RuleInfo

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());
}
Also used : RuleInfo(org.smartdata.common.rule.RuleInfo) Test(org.junit.Test)

Aggregations

RuleInfo (org.smartdata.common.rule.RuleInfo)22 Test (org.junit.Test)14 IOException (java.io.IOException)5 SmartAdmin (org.smartdata.admin.SmartAdmin)4 ArrayList (java.util.ArrayList)3 ServiceException (com.google.protobuf.ServiceException)2 File (java.io.File)2 SQLException (java.sql.SQLException)2 RuleInfoProto (org.smartdata.common.protocol.AdminServerProto.RuleInfoProto)2 DBAdapter (org.smartdata.server.metastore.DBAdapter)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)1 MiniDFSCluster (org.apache.hadoop.hdfs.MiniDFSCluster)1 ListRulesInfoRequestProto (org.smartdata.common.protocol.AdminServerProto.ListRulesInfoRequestProto)1