Search in sources :

Example 26 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class JobVertexBackPressureHandler method handleRequest.

@Override
public String handleRequest(AccessExecutionJobVertex accessJobVertex, Map<String, String> params) throws Exception {
    if (accessJobVertex instanceof ArchivedExecutionJobVertex) {
        return "";
    }
    ExecutionJobVertex jobVertex = (ExecutionJobVertex) accessJobVertex;
    try (StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer)) {
        gen.writeStartObject();
        Option<OperatorBackPressureStats> statsOption = backPressureStatsTracker.getOperatorBackPressureStats(jobVertex);
        if (statsOption.isDefined()) {
            OperatorBackPressureStats stats = statsOption.get();
            // Check whether we need to refresh
            if (refreshInterval <= System.currentTimeMillis() - stats.getEndTimestamp()) {
                backPressureStatsTracker.triggerStackTraceSample(jobVertex);
                gen.writeStringField("status", "deprecated");
            } else {
                gen.writeStringField("status", "ok");
            }
            gen.writeStringField("backpressure-level", getBackPressureLevel(stats.getMaxBackPressureRatio()));
            gen.writeNumberField("end-timestamp", stats.getEndTimestamp());
            // Sub tasks
            gen.writeArrayFieldStart("subtasks");
            int numSubTasks = stats.getNumberOfSubTasks();
            for (int i = 0; i < numSubTasks; i++) {
                double ratio = stats.getBackPressureRatio(i);
                gen.writeStartObject();
                gen.writeNumberField("subtask", i);
                gen.writeStringField("backpressure-level", getBackPressureLevel(ratio));
                gen.writeNumberField("ratio", ratio);
                gen.writeEndObject();
            }
            gen.writeEndArray();
        } else {
            backPressureStatsTracker.triggerStackTraceSample(jobVertex);
            gen.writeStringField("status", "deprecated");
        }
        gen.writeEndObject();
        gen.close();
        return writer.toString();
    }
}
Also used : ArchivedExecutionJobVertex(org.apache.flink.runtime.executiongraph.ArchivedExecutionJobVertex) StringWriter(java.io.StringWriter) ExecutionJobVertex(org.apache.flink.runtime.executiongraph.ExecutionJobVertex) ArchivedExecutionJobVertex(org.apache.flink.runtime.executiongraph.ArchivedExecutionJobVertex) AccessExecutionJobVertex(org.apache.flink.runtime.executiongraph.AccessExecutionJobVertex) OperatorBackPressureStats(org.apache.flink.runtime.webmonitor.OperatorBackPressureStats) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 27 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class SubtaskExecutionAttemptDetailsHandler method createAttemptDetailsJson.

public static String createAttemptDetailsJson(AccessExecution execAttempt, String jobID, String vertexID, @Nullable MetricFetcher fetcher) throws IOException {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);
    final ExecutionState status = execAttempt.getState();
    final long now = System.currentTimeMillis();
    TaskManagerLocation location = execAttempt.getAssignedResourceLocation();
    String locationString = location == null ? "(unassigned)" : location.getHostname();
    long startTime = execAttempt.getStateTimestamp(ExecutionState.DEPLOYING);
    if (startTime == 0) {
        startTime = -1;
    }
    long endTime = status.isTerminal() ? execAttempt.getStateTimestamp(status) : -1;
    long duration = startTime > 0 ? ((endTime > 0 ? endTime : now) - startTime) : -1;
    gen.writeStartObject();
    gen.writeNumberField("subtask", execAttempt.getParallelSubtaskIndex());
    gen.writeStringField("status", status.name());
    gen.writeNumberField("attempt", execAttempt.getAttemptNumber());
    gen.writeStringField("host", locationString);
    gen.writeNumberField("start-time", startTime);
    gen.writeNumberField("end-time", endTime);
    gen.writeNumberField("duration", duration);
    MutableIOMetrics counts = new MutableIOMetrics();
    counts.addIOMetrics(execAttempt, fetcher, jobID, vertexID);
    counts.writeIOMetricsAsJson(gen);
    gen.writeEndObject();
    gen.close();
    return writer.toString();
}
Also used : ExecutionState(org.apache.flink.runtime.execution.ExecutionState) MutableIOMetrics(org.apache.flink.runtime.webmonitor.utils.MutableIOMetrics) StringWriter(java.io.StringWriter) TaskManagerLocation(org.apache.flink.runtime.taskmanager.TaskManagerLocation) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 28 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class XmlTemplateEngine method createTemplate.

public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException {
    Node root;
    try {
        root = xmlParser.parse(reader);
    } catch (SAXException e) {
        throw new RuntimeException("Parsing XML source failed.", e);
    }
    if (root == null) {
        throw new IOException("Parsing XML source failed: root node is null.");
    }
    StringWriter writer = new StringWriter(1024);
    writer.write("/* Generated by XmlTemplateEngine */\n");
    new GspPrinter(new PrintWriter(writer), indentation).print(root);
    Script script;
    try {
        script = groovyShell.parse(writer.toString(), "XmlTemplateScript" + counter++ + ".groovy");
    } catch (Exception e) {
        throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
    }
    return new XmlTemplate(script);
}
Also used : Script(groovy.lang.Script) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) StringWriter(java.io.StringWriter) Node(groovy.util.Node) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SAXException(org.xml.sax.SAXException) PrintWriter(java.io.PrintWriter)

Example 29 with StringWriter

use of java.io.StringWriter in project groovy by apache.

the class BaseTemplate method stringOf.

public String stringOf(Closure cl) throws IOException {
    Writer old = out;
    StringWriter stringWriter = new StringWriter(32);
    out = stringWriter;
    Object result = cl.call();
    if (result != null && result != this) {
        stringWriter.append(result.toString());
    }
    out = old;
    return stringWriter.toString();
}
Also used : StringWriter(java.io.StringWriter) NullWriter(org.codehaus.groovy.control.io.NullWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 30 with StringWriter

use of java.io.StringWriter in project flink by apache.

the class AbstractTaskManagerProcessFailureRecoveryTest method testTaskManagerProcessFailure.

@Test
public void testTaskManagerProcessFailure() {
    final StringWriter processOutput1 = new StringWriter();
    final StringWriter processOutput2 = new StringWriter();
    final StringWriter processOutput3 = new StringWriter();
    ActorSystem jmActorSystem = null;
    Process taskManagerProcess1 = null;
    Process taskManagerProcess2 = null;
    Process taskManagerProcess3 = null;
    File coordinateTempDir = null;
    try {
        // check that we run this test only if the java command
        // is available on this machine
        String javaCommand = getJavaCommandPath();
        if (javaCommand == null) {
            System.out.println("---- Skipping Process Failure test : Could not find java executable ----");
            return;
        }
        // create a logging file for the process
        File tempLogFile = File.createTempFile(getClass().getSimpleName() + "-", "-log4j.properties");
        tempLogFile.deleteOnExit();
        CommonTestUtils.printLog4jDebugConfig(tempLogFile);
        // coordination between the processes goes through a directory
        coordinateTempDir = CommonTestUtils.createTempDirectory();
        // find a free port to start the JobManager
        final int jobManagerPort = NetUtils.getAvailablePort();
        // start a JobManager
        Tuple2<String, Object> localAddress = new Tuple2<String, Object>("localhost", jobManagerPort);
        Configuration jmConfig = new Configuration();
        jmConfig.setString(ConfigConstants.AKKA_WATCH_HEARTBEAT_INTERVAL, "1000 ms");
        jmConfig.setString(ConfigConstants.AKKA_WATCH_HEARTBEAT_PAUSE, "6 s");
        jmConfig.setInteger(ConfigConstants.AKKA_WATCH_THRESHOLD, 9);
        jmConfig.setString(ConfigConstants.RESTART_STRATEGY_FIXED_DELAY_DELAY, "10 s");
        jmConfig.setString(ConfigConstants.AKKA_ASK_TIMEOUT, "100 s");
        jmActorSystem = AkkaUtils.createActorSystem(jmConfig, new Some<>(localAddress));
        ActorRef jmActor = JobManager.startJobManagerActors(jmConfig, jmActorSystem, TestingUtils.defaultExecutor(), TestingUtils.defaultExecutor(), JobManager.class, MemoryArchivist.class)._1();
        // the TaskManager java command
        String[] command = new String[] { javaCommand, "-Dlog.level=DEBUG", "-Dlog4j.configuration=file:" + tempLogFile.getAbsolutePath(), "-Xms80m", "-Xmx80m", "-classpath", getCurrentClasspath(), TaskManagerProcessEntryPoint.class.getName(), String.valueOf(jobManagerPort) };
        // start the first two TaskManager processes
        taskManagerProcess1 = new ProcessBuilder(command).start();
        new CommonTestUtils.PipeForwarder(taskManagerProcess1.getErrorStream(), processOutput1);
        taskManagerProcess2 = new ProcessBuilder(command).start();
        new CommonTestUtils.PipeForwarder(taskManagerProcess2.getErrorStream(), processOutput2);
        // we wait for the JobManager to have the two TaskManagers available
        // since some of the CI environments are very hostile, we need to give this a lot of time (2 minutes)
        waitUntilNumTaskManagersAreRegistered(jmActor, 2, 120000);
        // the program will set a marker file in each of its parallel tasks once they are ready, so that
        // this coordinating code is aware of this.
        // the program will very slowly consume elements until the marker file (later created by the
        // test driver code) is present
        final File coordinateDirClosure = coordinateTempDir;
        final AtomicReference<Throwable> errorRef = new AtomicReference<>();
        // we trigger program execution in a separate thread
        Thread programTrigger = new Thread("Program Trigger") {

            @Override
            public void run() {
                try {
                    testTaskManagerFailure(jobManagerPort, coordinateDirClosure);
                } catch (Throwable t) {
                    t.printStackTrace();
                    errorRef.set(t);
                }
            }
        };
        //start the test program
        programTrigger.start();
        // max 20 seconds
        if (!waitForMarkerFiles(coordinateTempDir, READY_MARKER_FILE_PREFIX, PARALLELISM, 120000)) {
            // check if the program failed for some reason
            if (errorRef.get() != null) {
                Throwable error = errorRef.get();
                error.printStackTrace();
                fail("The program encountered a " + error.getClass().getSimpleName() + " : " + error.getMessage());
            } else {
                // no error occurred, simply a timeout
                fail("The tasks were not started within time (" + 120000 + "msecs)");
            }
        }
        // start the third TaskManager
        taskManagerProcess3 = new ProcessBuilder(command).start();
        new CommonTestUtils.PipeForwarder(taskManagerProcess3.getErrorStream(), processOutput3);
        // we wait for the third TaskManager to register
        // since some of the CI environments are very hostile, we need to give this a lot of time (2 minutes)
        waitUntilNumTaskManagersAreRegistered(jmActor, 3, 120000);
        // kill one of the previous TaskManagers, triggering a failure and recovery
        taskManagerProcess1.destroy();
        taskManagerProcess1 = null;
        // we create the marker file which signals the program functions tasks that they can complete
        touchFile(new File(coordinateTempDir, PROCEED_MARKER_FILE));
        // wait for at most 5 minutes for the program to complete
        programTrigger.join(300000);
        // check that the program really finished
        assertFalse("The program did not finish in time", programTrigger.isAlive());
        // check whether the program encountered an error
        if (errorRef.get() != null) {
            Throwable error = errorRef.get();
            error.printStackTrace();
            fail("The program encountered a " + error.getClass().getSimpleName() + " : " + error.getMessage());
        }
    // all seems well :-)
    } catch (Exception e) {
        e.printStackTrace();
        printProcessLog("TaskManager 1", processOutput1.toString());
        printProcessLog("TaskManager 2", processOutput2.toString());
        printProcessLog("TaskManager 3", processOutput3.toString());
        fail(e.getMessage());
    } catch (Error e) {
        e.printStackTrace();
        printProcessLog("TaskManager 1", processOutput1.toString());
        printProcessLog("TaskManager 2", processOutput2.toString());
        printProcessLog("TaskManager 3", processOutput3.toString());
        throw e;
    } finally {
        if (taskManagerProcess1 != null) {
            taskManagerProcess1.destroy();
        }
        if (taskManagerProcess2 != null) {
            taskManagerProcess2.destroy();
        }
        if (taskManagerProcess3 != null) {
            taskManagerProcess3.destroy();
        }
        if (jmActorSystem != null) {
            jmActorSystem.shutdown();
        }
        if (coordinateTempDir != null) {
            try {
                FileUtils.deleteDirectory(coordinateTempDir);
            } catch (Throwable t) {
            // we can ignore this
            }
        }
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) MemoryArchivist(org.apache.flink.runtime.jobmanager.MemoryArchivist) Configuration(org.apache.flink.configuration.Configuration) ActorRef(akka.actor.ActorRef) AtomicReference(java.util.concurrent.atomic.AtomicReference) JobManager(org.apache.flink.runtime.jobmanager.JobManager) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Some(scala.Some) StringWriter(java.io.StringWriter) Tuple2(scala.Tuple2) File(java.io.File) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)3175 PrintWriter (java.io.PrintWriter)1057 Test (org.junit.Test)612 IOException (java.io.IOException)516 StringReader (java.io.StringReader)232 Writer (java.io.Writer)211 StreamResult (javax.xml.transform.stream.StreamResult)207 File (java.io.File)194 InputStreamReader (java.io.InputStreamReader)140 HashMap (java.util.HashMap)136 Transformer (javax.xml.transform.Transformer)125 InputStream (java.io.InputStream)119 Map (java.util.Map)116 ArrayList (java.util.ArrayList)106 DOMSource (javax.xml.transform.dom.DOMSource)99 BufferedReader (java.io.BufferedReader)96 ByteArrayInputStream (java.io.ByteArrayInputStream)84 Reader (java.io.Reader)77 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)75 HttpServletResponse (javax.servlet.http.HttpServletResponse)73