Search in sources :

Example 1 with MultiChoiceAddressSerializer

use of org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer in project gradle by gradle.

the class SystemApplicationClassLoaderWorker method call.

public Void call() throws Exception {
    if (System.getProperty("org.gradle.worker.test.stuck") != null) {
        // Simulate a stuck worker. There's probably a way to inject this failure...
        Thread.sleep(30000);
        return null;
    }
    Decoder decoder = new InputStreamBackedDecoder(configInputStream);
    // Read logging config and setup logging
    int logLevel = decoder.readSmallInt();
    LoggingManagerInternal loggingManager = createLoggingManager();
    loggingManager.setLevelInternal(LogLevel.values()[logLevel]).start();
    // Read whether process info should be published
    boolean shouldPublishJvmMemoryInfo = decoder.readBoolean();
    // Read server address and start connecting
    MultiChoiceAddress serverAddress = new MultiChoiceAddressSerializer().read(decoder);
    MessagingServices messagingServices = new MessagingServices();
    WorkerServices workerServices = new WorkerServices(messagingServices);
    WorkerLogEventListener workerLogEventListener = null;
    try {
        // Read serialized worker
        byte[] serializedWorker = decoder.readBinary();
        // Deserialize the worker action
        Action<WorkerContext> action;
        try {
            ObjectInputStream instr = new ClassLoaderObjectInputStream(new ByteArrayInputStream(serializedWorker), getClass().getClassLoader());
            action = (Action<WorkerContext>) instr.readObject();
        } catch (Exception e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
        final ObjectConnection connection = messagingServices.get(MessagingClient.class).getConnection(serverAddress);
        workerLogEventListener = configureLogging(loggingManager, connection);
        if (shouldPublishJvmMemoryInfo) {
            configureWorkerJvmMemoryInfoEvents(workerServices, connection);
        }
        try {
            action.execute(new WorkerContext() {

                public ClassLoader getApplicationClassLoader() {
                    return ClassLoader.getSystemClassLoader();
                }

                @Override
                public ObjectConnection getServerConnection() {
                    return connection;
                }
            });
        } finally {
            connection.stop();
        }
    } finally {
        if (workerLogEventListener != null) {
            loggingManager.removeOutputEventListener(workerLogEventListener);
        }
        messagingServices.close();
        loggingManager.stop();
    }
    return null;
}
Also used : InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) LoggingManagerInternal(org.gradle.internal.logging.LoggingManagerInternal) MessagingClient(org.gradle.internal.remote.MessagingClient) MessagingServices(org.gradle.internal.remote.services.MessagingServices) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream) Decoder(org.gradle.internal.serialize.Decoder) InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) UncheckedException(org.gradle.internal.UncheckedException) ObjectConnection(org.gradle.internal.remote.ObjectConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderObjectInputStream(org.gradle.internal.io.ClassLoaderObjectInputStream)

Example 2 with MultiChoiceAddressSerializer

use of org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer in project gradle by gradle.

the class ApplicationClassesInSystemClassLoaderWorkerImplementationFactory method prepareJavaCommand.

@Override
public void prepareJavaCommand(Object workerId, String displayName, DefaultWorkerProcessBuilder processBuilder, List<URL> implementationClassPath, Address serverAddress, JavaExecHandleBuilder execSpec, boolean publishProcessInfo) {
    Collection<File> applicationClasspath = processBuilder.getApplicationClasspath();
    LogLevel logLevel = processBuilder.getLogLevel();
    Set<String> sharedPackages = processBuilder.getSharedPackages();
    Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
    ClassPath workerMainClassPath = classPathRegistry.getClassPath("WORKER_MAIN");
    execSpec.setMain("worker." + GradleWorkerMain.class.getName());
    boolean useOptionsFile = shouldUseOptionsFile(execSpec);
    if (useOptionsFile) {
        // Use an options file to pass across application classpath
        File optionsFile = temporaryFileProvider.createTemporaryFile("gradle-worker-classpath", "txt");
        List<String> jvmArgs = writeOptionsFile(workerMainClassPath.getAsFiles(), applicationClasspath, optionsFile);
        execSpec.jvmArgs(jvmArgs);
    } else {
        // Use a dummy security manager, which hacks the application classpath into the system ClassLoader
        execSpec.classpath(workerMainClassPath.getAsFiles());
        execSpec.systemProperty("java.security.manager", "worker." + BootstrapSecurityManager.class.getName());
    }
    // Serialize configuration for the worker process to it stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    try {
        DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
        if (!useOptionsFile) {
            // Serialize the application classpath, this is consumed by BootstrapSecurityManager
            outstr.writeInt(applicationClasspath.size());
            for (File file : applicationClasspath) {
                outstr.writeUTF(file.getAbsolutePath());
            }
            // Serialize the actual security manager type, this is consumed by BootstrapSecurityManager
            outstr.writeUTF(requestedSecurityManager == null ? "" : requestedSecurityManager.toString());
        }
        // Serialize the shared packages, this is consumed by GradleWorkerMain
        outstr.writeInt(sharedPackages.size());
        for (String str : sharedPackages) {
            outstr.writeUTF(str);
        }
        // Serialize the worker implementation classpath, this is consumed by GradleWorkerMain
        outstr.writeInt(implementationClassPath.size());
        for (URL entry : implementationClassPath) {
            outstr.writeUTF(entry.toString());
        }
        // Serialize the worker config, this is consumed by SystemApplicationClassLoaderWorker
        OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(outstr);
        encoder.writeSmallInt(logLevel.ordinal());
        encoder.writeBoolean(publishProcessInfo);
        new MultiChoiceAddressSerializer().write(encoder, (MultiChoiceAddress) serverAddress);
        // Serialize the worker, this is consumed by SystemApplicationClassLoaderWorker
        ActionExecutionWorker worker = new ActionExecutionWorker(processBuilder.getWorker(), workerId, displayName, processBuilder.getGradleUserHomeDir());
        byte[] serializedWorker = GUtil.serialize(worker);
        encoder.writeBinary(serializedWorker);
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    execSpec.setStandardInput(buffer.getInputStream());
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) EncodedStream(org.gradle.process.internal.streams.EncodedStream) DataOutputStream(java.io.DataOutputStream) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) LogLevel(org.gradle.api.logging.LogLevel) URL(java.net.URL) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) File(java.io.File) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Example 3 with MultiChoiceAddressSerializer

use of org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer in project gradle by gradle.

the class DaemonStartupCommunication method readDiagnostics.

public DaemonStartupInfo readDiagnostics(String message) {
    //Assuming the message has correct format. Not bullet proof, but seems to work ok for now.
    if (!message.startsWith(daemonGreeting())) {
        throw new IllegalArgumentException(String.format("Unexpected daemon startup message: %s", message));
    }
    try {
        String encoded = message.substring(daemonGreeting().length()).trim();
        InputStream inputStream = new EncodedStream.EncodedInput(new ByteArrayInputStream(encoded.getBytes()));
        Decoder decoder = new InputStreamBackedDecoder(inputStream);
        String pidString = decoder.readNullableString();
        String uid = decoder.readString();
        Long pid = pidString == null ? null : Long.valueOf(pidString);
        Address address = new MultiChoiceAddressSerializer().read(decoder);
        File daemonLog = new File(decoder.readString());
        return new DaemonStartupInfo(uid, address, new DaemonDiagnostics(daemonLog, pid));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) Address(org.gradle.internal.remote.Address) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) Decoder(org.gradle.internal.serialize.Decoder) InputStreamBackedDecoder(org.gradle.internal.serialize.InputStreamBackedDecoder) DaemonStartupInfo(org.gradle.launcher.daemon.diagnostics.DaemonStartupInfo) DaemonDiagnostics(org.gradle.launcher.daemon.diagnostics.DaemonDiagnostics) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Example 4 with MultiChoiceAddressSerializer

use of org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer in project gradle by gradle.

the class DaemonStartupCommunication method printDaemonStarted.

public void printDaemonStarted(PrintStream target, Long pid, String uid, Address address, File daemonLog) {
    target.print(daemonGreeting());
    // Encode as ascii
    try {
        OutputStream outputStream = new EncodedStream.EncodedOutput(target);
        FlushableEncoder encoder = new OutputStreamBackedEncoder(outputStream);
        encoder.writeNullableString(pid == null ? null : pid.toString());
        encoder.writeString(uid);
        MultiChoiceAddress multiChoiceAddress = (MultiChoiceAddress) address;
        new MultiChoiceAddressSerializer().write(encoder, multiChoiceAddress);
        encoder.writeString(daemonLog.getPath());
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    target.println();
    //ibm vm 1.6 + windows XP gotchas:
    //we need to print something else to the stream after we print the daemon greeting.
    //without it, the parent hangs without receiving the message above (flushing does not help).
    LOGGER.debug("Completed writing the daemon greeting. Closing streams...");
//btw. the ibm vm+winXP also has some issues detecting closed streams by the child but we handle this problem differently.
}
Also used : FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) MultiChoiceAddress(org.gradle.internal.remote.internal.inet.MultiChoiceAddress) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Aggregations

MultiChoiceAddressSerializer (org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)4 UncheckedIOException (org.gradle.api.UncheckedIOException)3 MultiChoiceAddress (org.gradle.internal.remote.internal.inet.MultiChoiceAddress)3 Decoder (org.gradle.internal.serialize.Decoder)2 InputStreamBackedDecoder (org.gradle.internal.serialize.InputStreamBackedDecoder)2 OutputStreamBackedEncoder (org.gradle.internal.serialize.OutputStreamBackedEncoder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 URL (java.net.URL)1 LogLevel (org.gradle.api.logging.LogLevel)1 UncheckedException (org.gradle.internal.UncheckedException)1 ClassPath (org.gradle.internal.classpath.ClassPath)1 ClassLoaderObjectInputStream (org.gradle.internal.io.ClassLoaderObjectInputStream)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1 LoggingManagerInternal (org.gradle.internal.logging.LoggingManagerInternal)1 Address (org.gradle.internal.remote.Address)1 MessagingClient (org.gradle.internal.remote.MessagingClient)1