Search in sources :

Example 1 with DefaultChainContext

use of com.qlangtech.tis.exec.impl.DefaultChainContext in project tis by qlangtech.

the class TisServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    final HttpExecContext execContext = createHttpExecContext(req);
    final MDCParamContext mdcContext = this.getMDCParam(execContext, res);
    try {
        if (!isValidParams(execContext, req, res)) {
            return;
        }
        if (!mdcContext.validateParam()) {
            return;
        }
        final ExecuteLock lock = mdcContext.getExecLock();
        // getLog().info("start to execute index swap work flow");
        final CountDownLatch countDown = new CountDownLatch(1);
        DefaultChainContext chainContext = new DefaultChainContext(execContext);
        chainContext.setMdcParamContext(mdcContext);
        chainContext.setAppSourcePipelineController(IncrStatusUmbilicalProtocolImpl.getInstance());
        final Integer newTaskId = createNewTask(chainContext);
        lock.addTaskFuture(newTaskId, executeService.submit(() -> {
            // MDC.put("app", indexName);
            getLog().info("index swap start to work");
            try {
                while (true) {
                    try {
                        if (lock.lock()) {
                            try {
                                String msg = "trigger task" + mdcContext.getExecLockKey() + " successful";
                                getLog().info(msg);
                                mdcContext.resetParam(newTaskId);
                                writeResult(true, msg, res, new KV(IExecChainContext.KEY_TASK_ID, String.valueOf(newTaskId)));
                                // IndexBuilderTriggerFactory builderFactory = HeteroEnum.INDEX_BUILD_CONTAINER.getPlugin();
                                // Objects.requireNonNull(builderFactory, "builderFactory can not be null");
                                // chainContext.setIndexBuildFileSystem(builderFactory.getFsFactory());
                                // chainContext.setIndexBuilderTriggerFactory(builderFactory);
                                // IPluginStore<TableDumpFactory> tableDumpFactory = TIS.getPluginStore(TableDumpFactory.class);
                                // if (tableDumpFactory.getPlugin() != null) {
                                // Objects.requireNonNull(tableDumpFactory.getPlugin(), "tableDumpFactory can not be null");
                                // chainContext.setTableDumpFactory(tableDumpFactory.getPlugin());
                                // }
                                countDown.countDown();
                                /**
                                 **********************************************************
                                 * 开始执行内部任务
                                 ***********************************************************
                                 */
                                ExecResult execResult = startWork(chainContext).isSuccess() ? ExecResult.SUCCESS : ExecResult.FAILD;
                                DagTaskUtils.createTaskComplete(newTaskId, chainContext, execResult);
                            } catch (InterruptedException e) {
                                // 说明当前任务被 终止了
                                logger.info("taskid:{} has been canceled", newTaskId);
                                return;
                            } catch (Throwable e) {
                                DagTaskUtils.createTaskComplete(newTaskId, chainContext, ExecResult.FAILD);
                                getLog().error(e.getMessage(), e);
                                throw new RuntimeException(e);
                            } finally {
                                lock.clearLockFutureQueue();
                            }
                        } else {
                            if (lock.isExpire()) {
                                getLog().warn("this lock has expire,this lock will cancel");
                                // 执行已經超時
                                lock.clearLockFutureQueue();
                                // while (lock.futureQueue.size() >= 1)
                                // {
                                // lock.futureQueue.poll().cancel(true);
                                // }
                                getLog().warn("this lock[" + lock.getTaskOwnerUniqueName() + "] has expire,has unlocked");
                                continue;
                            } else {
                                String msg = "pre task[" + lock.getTaskOwnerUniqueName() + "] is executing ,so this commit will be ignore";
                                getLog().warn(msg);
                                writeResult(false, msg, res);
                            }
                            countDown.countDown();
                        }
                        // }
                        break;
                    } catch (Throwable e) {
                        getLog().error(e.getMessage(), e);
                        try {
                            if (countDown.getCount() > 0) {
                                writeResult(false, ExceptionUtils.getMessage(e), res);
                            }
                        } catch (Exception e1) {
                        } finally {
                            try {
                                countDown.countDown();
                            } catch (Throwable ee) {
                            }
                        }
                        break;
                    }
                }
            } finally {
                mdcContext.removeParam();
            }
        // end run
        }));
        try {
            countDown.await(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
        }
    } finally {
        mdcContext.removeParam();
    }
}
Also used : ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpExecContext(com.qlangtech.tis.fullbuild.servlet.impl.HttpExecContext) DefaultChainContext(com.qlangtech.tis.exec.impl.DefaultChainContext) ExecResult(com.qlangtech.tis.assemble.ExecResult)

Example 2 with DefaultChainContext

use of com.qlangtech.tis.exec.impl.DefaultChainContext in project tis by qlangtech.

the class TestDefaultChainContext method testLoadPhaseStatusFromLatest.

public void testLoadPhaseStatusFromLatest() {
    IParamContext paramContext = this.mock("paramContext", IParamContext.class);
    DefaultChainContext chainContext = new DefaultChainContext(paramContext);
    PhaseStatusCollection statusCollection = chainContext.loadPhaseStatusFromLatest(dataXname);
    assertNull(statusCollection);
    // ./src/test/resources/com/qlangtech/tis/full/dump/cfg_repo/df-logs/66/dump
    statusCollection = chainContext.loadPhaseStatusFromLatest(dataXname);
    assertNotNull(statusCollection);
    DumpPhaseStatus dumpPhase = statusCollection.getDumpPhase();
    assertNotNull(dumpPhase);
    assertEquals(62, dumpPhase.getTaskId());
    String dataXFileName = "instancedetail_0.json";
    DumpPhaseStatus.TableDumpStatus dataXExecStatus = dumpPhase.getTable(dataXFileName);
    assertNotNull(dataXFileName + " relevant dataX instance can be null", dataXExecStatus);
    assertEquals(524525, dataXExecStatus.getReadRows());
    assertEquals(1000001, dataXExecStatus.getAllRows());
}
Also used : PhaseStatusCollection(com.qlangtech.tis.fullbuild.phasestatus.PhaseStatusCollection) IParamContext(com.qlangtech.tis.order.center.IParamContext) DefaultChainContext(com.qlangtech.tis.exec.impl.DefaultChainContext) DumpPhaseStatus(com.qlangtech.tis.fullbuild.phasestatus.impl.DumpPhaseStatus)

Example 3 with DefaultChainContext

use of com.qlangtech.tis.exec.impl.DefaultChainContext in project tis by qlangtech.

the class TestIndexSwapTaskflowLauncher method createRangeChainContext.

// 
public static DefaultChainContext createRangeChainContext(String collectionName, FullbuildPhase start, FullbuildPhase end, String... pts) throws Exception {
    TestParamContext params = new TestParamContext();
    params.set(IFullBuildContext.KEY_APP_SHARD_COUNT, String.valueOf(shardCount));
    params.set(IFullBuildContext.KEY_APP_NAME, collectionName);
    params.set(IFullBuildContext.KEY_WORKFLOW_NAME, "totalpay");
    params.set(IFullBuildContext.KEY_WORKFLOW_ID, WF_ID);
    params.set(IExecChainContext.COMPONENT_START, start.getName());
    params.set(IExecChainContext.COMPONENT_END, end.getName());
    final DefaultChainContext chainContext = new DefaultChainContext(params);
    ExecutePhaseRange range = chainContext.getExecutePhaseRange();
    Assert.assertEquals(start, range.getStart());
    Assert.assertEquals(end, range.getEnd());
    Map<IDumpTable, ITabPartition> dateParams = Maps.newHashMap();
    chainContext.setAttribute(ExecChainContextUtils.PARTITION_DATA_PARAMS, new TabPartitions(dateParams));
    chainContext.setAttribute(IExecChainContext.KEY_TASK_ID, TASK_ID);
    // final PluginStore<IndexBuilderTriggerFactory> buildTriggerFactory = TIS.getPluginStore(IndexBuilderTriggerFactory.class);
    // assertNotNull(buildTriggerFactory.getPlugin());
    // if (pts.length > 0) {
    // chainContext.setPs(pts[0]);
    // } else {
    // SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    // String pt = format.format(new Date());
    // chainContext.setPs(pt);
    // }
    chainContext.setMdcParamContext(() -> {
    });
    return chainContext;
}
Also used : ExecutePhaseRange(com.qlangtech.tis.exec.ExecutePhaseRange) DefaultChainContext(com.qlangtech.tis.exec.impl.DefaultChainContext) ITabPartition(com.qlangtech.tis.fullbuild.indexbuild.ITabPartition) IDumpTable(com.qlangtech.tis.fullbuild.indexbuild.IDumpTable) TabPartitions(com.qlangtech.tis.sql.parser.TabPartitions) TestParamContext(com.qlangtech.tis.fullbuild.taskflow.TestParamContext)

Example 4 with DefaultChainContext

use of com.qlangtech.tis.exec.impl.DefaultChainContext in project tis by qlangtech.

the class TestIndexSwapTaskflowLauncherWithDataXTrigger method testDataXProcessTrigger.

public void testDataXProcessTrigger() throws Exception {
    IndexSwapTaskflowLauncher taskflowLauncher = new IndexSwapTaskflowLauncher();
    DefaultChainContext chainContext = createRangeChainContext(FullbuildPhase.FullDump, FullbuildPhase.FullDump);
    ExecuteResult executeResult = taskflowLauncher.startWork(chainContext);
    assertTrue(executeResult.isSuccess());
}
Also used : DefaultChainContext(com.qlangtech.tis.exec.impl.DefaultChainContext) ExecuteResult(com.qlangtech.tis.exec.ExecuteResult)

Example 5 with DefaultChainContext

use of com.qlangtech.tis.exec.impl.DefaultChainContext in project tis by qlangtech.

the class TestIndexSwapTaskflowLauncherWithDataXTrigger method createRangeChainContext.

public static DefaultChainContext createRangeChainContext(FullbuildPhase start, FullbuildPhase end) throws Exception {
    TestParamContext params = new TestParamContext();
    params.set(IFullBuildContext.KEY_APP_NAME, DATAX_NAME);
    params.set(IExecChainContext.COMPONENT_START, start.getName());
    params.set(IExecChainContext.COMPONENT_END, end.getName());
    final DefaultChainContext chainContext = new DefaultChainContext(params);
    ExecutePhaseRange range = chainContext.getExecutePhaseRange();
    Assert.assertEquals(start, range.getStart());
    Assert.assertEquals(end, range.getEnd());
    chainContext.setAttribute(IExecChainContext.KEY_TASK_ID, TASK_ID);
    chainContext.setMdcParamContext(() -> {
    });
    return chainContext;
}
Also used : ExecutePhaseRange(com.qlangtech.tis.exec.ExecutePhaseRange) DefaultChainContext(com.qlangtech.tis.exec.impl.DefaultChainContext) TestParamContext(com.qlangtech.tis.fullbuild.taskflow.TestParamContext)

Aggregations

DefaultChainContext (com.qlangtech.tis.exec.impl.DefaultChainContext)6 ExecutePhaseRange (com.qlangtech.tis.exec.ExecutePhaseRange)2 TestParamContext (com.qlangtech.tis.fullbuild.taskflow.TestParamContext)2 ExecResult (com.qlangtech.tis.assemble.ExecResult)1 ExecuteResult (com.qlangtech.tis.exec.ExecuteResult)1 IDumpTable (com.qlangtech.tis.fullbuild.indexbuild.IDumpTable)1 ITabPartition (com.qlangtech.tis.fullbuild.indexbuild.ITabPartition)1 PhaseStatusCollection (com.qlangtech.tis.fullbuild.phasestatus.PhaseStatusCollection)1 DumpPhaseStatus (com.qlangtech.tis.fullbuild.phasestatus.impl.DumpPhaseStatus)1 HttpExecContext (com.qlangtech.tis.fullbuild.servlet.impl.HttpExecContext)1 IParamContext (com.qlangtech.tis.order.center.IParamContext)1 TabPartitions (com.qlangtech.tis.sql.parser.TabPartitions)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 Reactor (org.jvnet.hudson.reactor.Reactor)1 ReactorListener (org.jvnet.hudson.reactor.ReactorListener)1