Search in sources :

Example 16 with Signal

use of sun.misc.Signal in project hive by apache.

the class CliDriver method processLine.

/**
 * Processes a line of semicolon separated commands
 *
 * @param line
 *          The commands to process
 * @param allowInterrupting
 *          When true the function will handle SIG_INT (Ctrl+C) by interrupting the processing and
 *          returning -1
 * @return 0 if ok
 */
public CommandProcessorResponse processLine(String line, boolean allowInterrupting) throws CommandProcessorException {
    SignalHandler oldSignal = null;
    Signal interruptSignal = null;
    if (allowInterrupting) {
        // Remember all threads that were running at the time we started line processing.
        // Hook up the custom Ctrl+C handler while processing this line
        interruptSignal = new Signal("INT");
        oldSignal = Signal.handle(interruptSignal, new SignalHandler() {

            private boolean interruptRequested;

            @Override
            public void handle(Signal signal) {
                boolean initialRequest = !interruptRequested;
                interruptRequested = true;
                // Kill the VM on second ctrl+c
                if (!initialRequest) {
                    console.printInfo("Exiting the JVM");
                    System.exit(127);
                }
                // Interrupt the CLI thread to stop the current statement and return
                // to prompt
                console.printInfo("Interrupting... Be patient, this might take some time.");
                console.printInfo("Press Ctrl+C again to kill JVM");
                // First, kill any running MR jobs
                HadoopJobExecHelper.killRunningJobs();
                TezJobExecHelper.killRunningJobs();
                HiveInterruptUtils.interrupt();
            }
        });
    }
    try {
        CommandProcessorResponse lastRet = new CommandProcessorResponse();
        CommandProcessorResponse ret;
        // we can not use "split" function directly as ";" may be quoted
        List<String> commands = splitSemiColon(line);
        StringBuilder command = new StringBuilder();
        for (String oneCmd : commands) {
            if (StringUtils.endsWith(oneCmd, "\\")) {
                command.append(StringUtils.chop(oneCmd) + ";");
                continue;
            } else {
                command.append(oneCmd);
            }
            if (StringUtils.isBlank(command.toString())) {
                continue;
            }
            try {
                ret = processCmd(command.toString());
                lastRet = ret;
            } catch (CommandProcessorException e) {
                boolean ignoreErrors = HiveConf.getBoolVar(conf, HiveConf.ConfVars.CLIIGNOREERRORS);
                if (!ignoreErrors) {
                    throw e;
                }
            } finally {
                command.setLength(0);
            }
        }
        return lastRet;
    } finally {
        // Once we are done processing the line, restore the old handler
        if (oldSignal != null && interruptSignal != null) {
            Signal.handle(interruptSignal, oldSignal);
        }
    }
}
Also used : Signal(sun.misc.Signal) CommandProcessorException(org.apache.hadoop.hive.ql.processors.CommandProcessorException) CommandProcessorResponse(org.apache.hadoop.hive.ql.processors.CommandProcessorResponse) SignalHandler(sun.misc.SignalHandler)

Example 17 with Signal

use of sun.misc.Signal in project jstorm by alibaba.

the class JStormSignalHandler method registerSignal.

/**
 * Register signal to system
 * if callback is null, then the current process will ignore this signal
 */
public synchronized void registerSignal(int signalNumber, Runnable callback, boolean replace) {
    String signalName = signalMap.get(signalNumber);
    if (signalName == null) {
        LOG.warn("Invalid signalNumber " + signalNumber);
        return;
    }
    LOG.info("Begin to register signal of {}", signalName);
    try {
        SignalHandler oldHandler = Signal.handle(new Signal(signalName), this);
        LOG.info("Successfully register {} handler", signalName);
        Runnable old = signalHandlers.put(signalNumber, callback);
        if (old != null) {
            if (!replace) {
                oldSignalHandlers.put(signalNumber, oldHandler);
            } else {
                LOG.info("Successfully old {} handler will be replaced", signalName);
            }
        }
        LOG.info("Successfully register signal of {}", signalName);
    } catch (Exception e) {
        LOG.error("Failed to register " + signalName + ":" + signalNumber + ", Signal already used by VM or OS: SIGILL");
    }
}
Also used : Signal(sun.misc.Signal) SignalHandler(sun.misc.SignalHandler)

Example 18 with Signal

use of sun.misc.Signal in project Saturn by vipshop.

the class ShutdownGracefullyIT method test_A_TERM_Signal.

@Test
public void test_A_TERM_Signal() throws Exception {
    SystemEnvProperties.VIP_SATURN_SHUTDOWN_TIMEOUT = 10;
    startExecutorList(1);
    final int shardCount = 3;
    final String jobName = "test_A_TERM_Signal";
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
        status.runningCount = 0;
        status.sleepSeconds = 8;
        status.finished = false;
        status.timeout = false;
        LongtimeJavaJob.statusMap.put(key, status);
    }
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(LongtimeJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("0=0,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(50);
    ShutdownHandler.exitAfterHandler(false);
    Signal.raise(new Signal("TERM"));
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                Collection<LongtimeJavaJob.JobStatus> values = LongtimeJavaJob.statusMap.values();
                for (LongtimeJavaJob.JobStatus status : values) {
                    if (!status.finished) {
                        return false;
                    }
                }
                return true;
            }
        }, 10);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    stopExecutorListGracefully();
}
Also used : Signal(sun.misc.Signal) FinishCheck(com.vip.saturn.it.base.FinishCheck) Collection(java.util.Collection) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob)

Example 19 with Signal

use of sun.misc.Signal in project Saturn by vipshop.

the class ShutdownGracefullyIT method test_B_INT_Signal.

@Test
public void test_B_INT_Signal() throws Exception {
    SystemEnvProperties.VIP_SATURN_SHUTDOWN_TIMEOUT = 5;
    startExecutorList(1);
    final int shardCount = 3;
    final String jobName = "test_B_INT_Signal";
    for (int i = 0; i < shardCount; i++) {
        String key = jobName + "_" + i;
        LongtimeJavaJob.JobStatus status = new LongtimeJavaJob.JobStatus();
        status.runningCount = 0;
        status.sleepSeconds = 3;
        status.finished = false;
        status.timeout = false;
        LongtimeJavaJob.statusMap.put(key, status);
    }
    JobConfig jobConfig = new JobConfig();
    jobConfig.setJobName(jobName);
    jobConfig.setCron("9 9 9 9 9 ? 2099");
    jobConfig.setJobType(JobType.JAVA_JOB.toString());
    jobConfig.setJobClass(LongtimeJavaJob.class.getCanonicalName());
    jobConfig.setShardingTotalCount(shardCount);
    jobConfig.setShardingItemParameters("0=0,1=1,2=2");
    addJob(jobConfig);
    Thread.sleep(1000);
    enableJob(jobName);
    Thread.sleep(1000);
    runAtOnce(jobName);
    Thread.sleep(50);
    ShutdownHandler.exitAfterHandler(false);
    Signal.raise(new Signal("INT"));
    try {
        waitForFinish(new FinishCheck() {

            @Override
            public boolean isOk() {
                Collection<LongtimeJavaJob.JobStatus> values = LongtimeJavaJob.statusMap.values();
                for (LongtimeJavaJob.JobStatus status : values) {
                    if (!status.finished) {
                        return false;
                    }
                }
                return true;
            }
        }, 5);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    stopExecutorListGracefully();
}
Also used : Signal(sun.misc.Signal) FinishCheck(com.vip.saturn.it.base.FinishCheck) Collection(java.util.Collection) JobConfig(com.vip.saturn.job.console.domain.JobConfig) LongtimeJavaJob(com.vip.saturn.it.job.LongtimeJavaJob)

Example 20 with Signal

use of sun.misc.Signal in project oap by oaplatform.

the class Boot method installSignals.

private static void installSignals() {
    SignalHandler handler = signal -> {
        log.info("cought signal: {}", signal.getName());
        System.out.println("cought signal: " + signal.getName());
        System.out.flush();
        exit(0);
    };
    Signal.handle(new Signal("INT"), handler);
    Signal.handle(new Signal("TERM"), handler);
}
Also used : SignalHandler(sun.misc.SignalHandler) Slf4j(lombok.extern.slf4j.Slf4j) Cli(oap.cli.Cli) Signal(sun.misc.Signal) Module(oap.application.module.Module) Option(oap.cli.Option) Path(java.nio.file.Path) Signal(sun.misc.Signal) SignalHandler(sun.misc.SignalHandler)

Aggregations

Signal (sun.misc.Signal)37 SignalHandler (sun.misc.SignalHandler)15 Test (org.junit.Test)8 IOException (java.io.IOException)4 BatchSearchLoop (org.icij.datashare.tasks.BatchSearchLoop)4 ReporterConfiguration (com.uber.jaeger.Configuration.ReporterConfiguration)2 SamplerConfiguration (com.uber.jaeger.Configuration.SamplerConfiguration)2 ConstSampler (com.uber.jaeger.samplers.ConstSampler)2 FinishCheck (com.vip.saturn.it.base.FinishCheck)2 LongtimeJavaJob (com.vip.saturn.it.job.LongtimeJavaJob)2 JobConfig (com.vip.saturn.job.console.domain.JobConfig)2 ActiveSpan (io.opentracing.ActiveSpan)2 GlobalTracer (io.opentracing.util.GlobalTracer)2 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2