Search in sources :

Example 71 with PrintStream

use of java.io.PrintStream in project Gaffer by gchq.

the class SampleDataAndCreateSplitsFileTool method run.

@Override
public int run(final String[] strings) throws OperationException {
    try {
        LOGGER.info("Creating job using SampleDataForSplitPointsJobFactory");
        job = new SampleDataForSplitPointsJobFactory().createJob(operation, store);
    } catch (final IOException e) {
        LOGGER.error("Failed to create Hadoop job: {}", e.getMessage());
        throw new OperationException("Failed to create the Hadoop job: " + e.getMessage(), e);
    }
    try {
        LOGGER.info("Running SampleDataForSplitPoints job (job name is {})", job.getJobName());
        job.waitForCompletion(true);
    } catch (final IOException | InterruptedException | ClassNotFoundException e) {
        LOGGER.error("Exception running job: {}", e.getMessage());
        throw new OperationException("Error while waiting for job to complete: " + e.getMessage(), e);
    }
    try {
        if (!job.isSuccessful()) {
            LOGGER.error("Job was not successful (job name is {})", job.getJobName());
            throw new OperationException("Error running job");
        }
    } catch (final IOException e) {
        LOGGER.error("Exception running job: {}", e.getMessage());
        throw new OperationException("Error running job" + e.getMessage(), e);
    }
    // Find the number of records output
    // NB In the following line use mapred.Task.Counter.REDUCE_OUTPUT_RECORDS rather than
    // mapreduce.TaskCounter.REDUCE_OUTPUT_RECORDS as this is more compatible with earlier
    // versions of Hadoop.
    Counter counter;
    try {
        counter = job.getCounters().findCounter(Task.Counter.REDUCE_OUTPUT_RECORDS);
        LOGGER.info("Number of records output = {}", counter);
    } catch (final IOException e) {
        LOGGER.error("Failed to get counter org.apache.hadoop.mapred.Task.Counter.REDUCE_OUTPUT_RECORDS from job: {}", e.getMessage());
        throw new OperationException("Failed to get counter: " + Task.Counter.REDUCE_OUTPUT_RECORDS, e);
    }
    int numberTabletServers;
    try {
        numberTabletServers = store.getConnection().instanceOperations().getTabletServers().size();
        LOGGER.info("Number of tablet servers is {}", numberTabletServers);
    } catch (final StoreException e) {
        LOGGER.error("Exception thrown getting number of tablet servers: {}", e.getMessage());
        throw new OperationException(e.getMessage(), e);
    }
    long outputEveryNthRecord = counter.getValue() / (numberTabletServers - 1);
    final Path resultsFile = new Path(operation.getOutputPath(), "part-r-00000");
    LOGGER.info("Will output every {}-th record from {}", outputEveryNthRecord, resultsFile);
    // Read through resulting file, pick out the split points and write to file.
    final Configuration conf = getConf();
    final FileSystem fs;
    try {
        fs = FileSystem.get(conf);
    } catch (final IOException e) {
        LOGGER.error("Exception getting filesystem: {}", e.getMessage());
        throw new OperationException("Failed to get filesystem from configuration: " + e.getMessage(), e);
    }
    LOGGER.info("Writing splits to {}", operation.getResultingSplitsFilePath());
    final Key key = new Key();
    final Value value = new Value();
    long count = 0;
    int numberSplitPointsOutput = 0;
    try (final SequenceFile.Reader reader = new SequenceFile.Reader(fs, resultsFile, conf);
        final PrintStream splitsWriter = new PrintStream(new BufferedOutputStream(fs.create(new Path(operation.getResultingSplitsFilePath()), true)), false, CommonConstants.UTF_8)) {
        while (reader.next(key, value) && numberSplitPointsOutput < numberTabletServers - 1) {
            count++;
            if (count % outputEveryNthRecord == 0) {
                LOGGER.debug("Outputting split point number {} ({})", numberSplitPointsOutput, Base64.encodeBase64(key.getRow().getBytes()));
                numberSplitPointsOutput++;
                splitsWriter.println(new String(Base64.encodeBase64(key.getRow().getBytes()), CommonConstants.UTF_8));
            }
        }
        LOGGER.info("Total number of records read was {}", count);
    } catch (final IOException e) {
        LOGGER.error("Exception reading results file and outputting split points: {}", e.getMessage());
        throw new OperationException(e.getMessage(), e);
    }
    try {
        fs.delete(resultsFile, true);
        LOGGER.info("Deleted the results file {}", resultsFile);
    } catch (final IOException e) {
        LOGGER.error("Failed to delete the results file {}", resultsFile);
        throw new OperationException("Failed to delete the results file: " + e.getMessage(), e);
    }
    return SUCCESS_RESPONSE;
}
Also used : SampleDataForSplitPointsJobFactory(uk.gov.gchq.gaffer.accumulostore.operation.hdfs.handler.job.factory.SampleDataForSplitPointsJobFactory) Path(org.apache.hadoop.fs.Path) PrintStream(java.io.PrintStream) Configuration(org.apache.hadoop.conf.Configuration) IOException(java.io.IOException) StoreException(uk.gov.gchq.gaffer.store.StoreException) Counter(org.apache.hadoop.mapreduce.Counter) SequenceFile(org.apache.hadoop.io.SequenceFile) FileSystem(org.apache.hadoop.fs.FileSystem) Value(org.apache.accumulo.core.data.Value) BufferedOutputStream(java.io.BufferedOutputStream) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Key(org.apache.accumulo.core.data.Key)

Example 72 with PrintStream

use of java.io.PrintStream in project bazel by bazelbuild.

the class ExperimentalTestRunner method main.

/**
   * Takes as arguments the classes or packages to test.
   *
   * <p>To help just run one test or method in a suite, the test suite
   * may be passed in via system properties (-Dbazel.test_suite).
   * An empty args parameter means to run all tests in the suite.
   * A non-empty args parameter means to run only the specified tests/methods.
   *
   * <p>Return codes:
   * <ul>
   * <li>Test runner failure, bad arguments, etc.: exit code of 2</li>
   * <li>Normal test failure: exit code of 1</li>
   * <li>All tests pass: exit code of 0</li>
   * </ul>
   */
public static void main(String[] args) {
    PrintStream stderr = System.err;
    String suiteClassName = System.getProperty(TEST_SUITE_PROPERTY_NAME);
    if ("true".equals(System.getenv("PERSISTENT_TEST_RUNNER"))) {
        System.exit(runPersistentTestRunner(suiteClassName));
    }
    System.out.println("WARNING: RUNNING EXPERIMENTAL TEST RUNNER");
    if (!checkTestSuiteProperty(suiteClassName)) {
        System.exit(2);
    }
    int exitCode = runTestsInSuite(suiteClassName, args);
    System.err.printf("%nExperimentalTestRunner exiting with a return value of %d%n", exitCode);
    System.err.println("JVM shutdown hooks (if any) will run now.");
    System.err.println("The JVM will exit once they complete.");
    System.err.println();
    printStackTracesIfJvmExitHangs(stderr);
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date shutdownTime = new Date();
    String formattedShutdownTime = format.format(shutdownTime);
    System.err.printf("-- JVM shutdown starting at %s --%n%n", formattedShutdownTime);
    System.exit(exitCode);
}
Also used : PrintStream(java.io.PrintStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 73 with PrintStream

use of java.io.PrintStream in project bazel by bazelbuild.

the class DumpCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    DumpOptions dumpOptions = options.getOptions(DumpOptions.class);
    boolean anyOutput = dumpOptions.dumpPackages || dumpOptions.dumpVfs || dumpOptions.dumpActionCache || dumpOptions.dumpRuleClasses || (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF);
    if (!anyOutput) {
        Map<String, String> categories = new HashMap<>();
        categories.put("verbosity", "Options that control what internal state is dumped");
        Collection<Class<? extends OptionsBase>> optionList = new ArrayList<>();
        optionList.add(DumpOptions.class);
        env.getReporter().getOutErr().printErrLn(BlazeCommandUtils.expandHelpTopic(getClass().getAnnotation(Command.class).name(), getClass().getAnnotation(Command.class).help(), getClass(), optionList, categories, OptionsParser.HelpVerbosity.LONG, runtime.getProductName()));
        return ExitCode.ANALYSIS_FAILURE;
    }
    PrintStream out = new PrintStream(env.getReporter().getOutErr().getOutputStream());
    try {
        out.println("Warning: this information is intended for consumption by developers");
        out.println("only, and may change at any time.  Script against it at your own risk!");
        out.println();
        boolean success = true;
        if (dumpOptions.dumpPackages) {
            env.getPackageManager().dump(out);
            out.println();
        }
        if (dumpOptions.dumpVfs) {
            out.println("Filesystem cache");
            FileSystemUtils.dump(env.getOutputBase().getFileSystem(), out);
            out.println();
        }
        if (dumpOptions.dumpActionCache) {
            success &= dumpActionCache(env, out);
            out.println();
        }
        if (dumpOptions.dumpRuleClasses) {
            dumpRuleClasses(runtime, out);
            out.println();
        }
        if (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF) {
            success &= dumpSkyframe(env.getSkyframeExecutor(), dumpOptions.dumpSkyframe == SkyframeDumpOption.SUMMARY, out);
            out.println();
        }
        return success ? ExitCode.SUCCESS : ExitCode.ANALYSIS_FAILURE;
    } finally {
        out.flush();
    }
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) Command(com.google.devtools.build.lib.runtime.Command) ArrayList(java.util.ArrayList) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 74 with PrintStream

use of java.io.PrintStream in project bazel by bazelbuild.

the class AnsiStrippingOutputStreamTest method createStreams.

@Before
public final void createStreams() throws Exception {
    output = new ByteArrayOutputStream();
    OutputStream inputStream = new AnsiStrippingOutputStream(output);
    input = new PrintStream(inputStream);
}
Also used : PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Example 75 with PrintStream

use of java.io.PrintStream in project bazel by bazelbuild.

the class AndroidManifestProcessor method mergeManifest.

/**
   * Merge several manifests into one and perform placeholder substitutions. This operation uses
   * Gradle semantics.
   *
   * @param manifest The primary manifest of the merge.
   * @param mergeeManifests Manifests to be merged into {@code manifest}.
   * @param mergeType Whether the merger should operate in application or library mode.
   * @param values A map of strings to be used as manifest placeholders and overrides. packageName
   *     is the only disallowed value and will be ignored.
   * @param output The path to write the resultant manifest to.
   * @param logFile The path to write the merger log to.
   * @return The path of the resultant manifest, either {@code output}, or {@code manifest} if no
   *     merging was required.
   * @throws IOException if there was a problem writing the merged manifest.
   */
// TODO(corysmith): Extract manifest processing.
public Path mergeManifest(Path manifest, Map<Path, String> mergeeManifests, MergeType mergeType, Map<String, String> values, Path output, Path logFile) throws IOException {
    if (mergeeManifests.isEmpty() && values.isEmpty()) {
        return manifest;
    }
    Invoker<?> manifestMerger = ManifestMerger2.newMerger(manifest.toFile(), stdLogger, mergeType);
    MergedManifestKind mergedManifestKind = MergedManifestKind.MERGED;
    if (mergeType == MergeType.APPLICATION) {
        manifestMerger.withFeatures(Feature.REMOVE_TOOLS_DECLARATIONS);
    }
    // Add mergee manifests
    List<Pair<String, File>> libraryManifests = new ArrayList<>();
    for (Entry<Path, String> mergeeManifest : mergeeManifests.entrySet()) {
        libraryManifests.add(Pair.of(mergeeManifest.getValue(), mergeeManifest.getKey().toFile()));
    }
    manifestMerger.addLibraryManifests(libraryManifests);
    // Extract SystemProperties from the provided values.
    Map<String, Object> placeholders = new HashMap<>();
    placeholders.putAll(values);
    for (SystemProperty property : SystemProperty.values()) {
        if (values.containsKey(SYSTEM_PROPERTY_NAMES.get(property))) {
            manifestMerger.setOverride(property, values.get(SYSTEM_PROPERTY_NAMES.get(property)));
            // placeholders to have the same value as specified by SystemProperty.PACKAGE.
            if (property == SystemProperty.PACKAGE) {
                placeholders.remove(PlaceholderHandler.APPLICATION_ID);
                placeholders.remove(PlaceholderHandler.PACKAGE_NAME);
            }
        }
    }
    // Add placeholders for all values.
    // packageName is populated from either the applicationId override or from the manifest itself;
    // it cannot be manually specified.
    placeholders.remove(PlaceholderHandler.PACKAGE_NAME);
    manifestMerger.setPlaceHolderValues(placeholders);
    try {
        MergingReport mergingReport = manifestMerger.merge();
        if (logFile != null) {
            logFile.getParent().toFile().mkdirs();
            try (PrintStream stream = new PrintStream(logFile.toFile())) {
                mergingReport.log(new AndroidResourceProcessor.PrintStreamLogger(stream));
            }
        }
        switch(mergingReport.getResult()) {
            case WARNING:
                mergingReport.log(stdLogger);
                Files.createDirectories(output.getParent());
                writeMergedManifest(mergedManifestKind, mergingReport, output);
                break;
            case SUCCESS:
                Files.createDirectories(output.getParent());
                writeMergedManifest(mergedManifestKind, mergingReport, output);
                break;
            case ERROR:
                mergingReport.log(stdLogger);
                throw new RuntimeException(mergingReport.getReportString());
            default:
                throw new RuntimeException("Unhandled result type : " + mergingReport.getResult());
        }
    } catch (MergeFailureException e) {
        throw new RuntimeException(e);
    }
    return output;
}
Also used : Path(java.nio.file.Path) MergingReport(com.android.manifmerger.MergingReport) PrintStream(java.io.PrintStream) MergeFailureException(com.android.manifmerger.ManifestMerger2.MergeFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SystemProperty(com.android.manifmerger.ManifestMerger2.SystemProperty) MergedManifestKind(com.android.manifmerger.MergingReport.MergedManifestKind) Pair(com.android.utils.Pair)

Aggregations

PrintStream (java.io.PrintStream)1828 ByteArrayOutputStream (java.io.ByteArrayOutputStream)805 Test (org.junit.Test)583 File (java.io.File)331 IOException (java.io.IOException)295 FileOutputStream (java.io.FileOutputStream)207 ArrayList (java.util.ArrayList)89 FileNotFoundException (java.io.FileNotFoundException)83 OutputStream (java.io.OutputStream)81 Before (org.junit.Before)66 BufferedReader (java.io.BufferedReader)53 BufferedOutputStream (java.io.BufferedOutputStream)49 Map (java.util.Map)49 Date (java.util.Date)46 Path (org.apache.hadoop.fs.Path)42 UnsupportedEncodingException (java.io.UnsupportedEncodingException)41 InputStreamReader (java.io.InputStreamReader)38 Matchers.anyString (org.mockito.Matchers.anyString)38 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)36 List (java.util.List)35