Search in sources :

Example 1 with ObjectConnection

use of org.gradle.internal.remote.ObjectConnection 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 ObjectConnection

use of org.gradle.internal.remote.ObjectConnection in project gradle by gradle.

the class WorkerAction method execute.

@Override
public void execute(WorkerProcessContext workerProcessContext) {
    completed = new CountDownLatch(1);
    try {
        workerImplementation = Class.forName(workerImplementationName);
        implementation = workerImplementation.newInstance();
    } catch (Throwable e) {
        failure = e;
    }
    ObjectConnection connection = workerProcessContext.getServerConnection();
    connection.addIncoming(RequestProtocol.class, this);
    responder = connection.addOutgoing(ResponseProtocol.class);
    connection.connect();
    try {
        completed.await();
    } catch (InterruptedException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Example 3 with ObjectConnection

use of org.gradle.internal.remote.ObjectConnection in project gradle by gradle.

the class DefaultSingleRequestWorkerProcessBuilder method build.

@Override
public PROTOCOL build() {
    return protocolType.cast(Proxy.newProxyInstance(protocolType.getClassLoader(), new Class[] { protocolType }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Receiver receiver = new Receiver(getBaseName());
            try {
                WorkerProcess workerProcess = builder.build();
                workerProcess.start();
                ObjectConnection connection = workerProcess.getConnection();
                RequestProtocol requestProtocol = connection.addOutgoing(RequestProtocol.class);
                connection.addIncoming(ResponseProtocol.class, receiver);
                connection.useJavaSerializationForParameters(workerImplementation.getClassLoader());
                connection.connect();
                requestProtocol.runThenStop(method.getName(), method.getParameterTypes(), args);
                boolean hasResult = receiver.awaitNextResult();
                workerProcess.waitForStop();
                if (!hasResult) {
                    // Reached the end of input, worker has exited without failing
                    throw new IllegalStateException(String.format("No response was received from %s but the worker process has finished.", getBaseName()));
                }
            } catch (Exception e) {
                throw WorkerProcessException.runFailed(getBaseName(), e);
            }
            return receiver.getNextResult();
        }
    }));
}
Also used : Receiver(org.gradle.process.internal.worker.request.Receiver) RequestProtocol(org.gradle.process.internal.worker.request.RequestProtocol) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Example 4 with ObjectConnection

use of org.gradle.internal.remote.ObjectConnection in project gradle by gradle.

the class DefaultWorkerProcessBuilder method build.

@Override
public WorkerProcess build() {
    final WorkerJvmMemoryStatus memoryStatus = shouldPublishJvmMemoryInfo ? new WorkerJvmMemoryStatus() : null;
    final DefaultWorkerProcess workerProcess = new DefaultWorkerProcess(connectTimeoutSeconds, TimeUnit.SECONDS, memoryStatus);
    ConnectionAcceptor acceptor = server.accept(new Action<ObjectConnection>() {

        public void execute(final ObjectConnection connection) {
            workerProcess.onConnect(connection, new Runnable() {

                @Override
                public void run() {
                    DefaultWorkerLoggingProtocol defaultWorkerLoggingProtocol = new DefaultWorkerLoggingProtocol(outputEventListener);
                    connection.useParameterSerializers(WorkerLoggingSerializer.create());
                    connection.addIncoming(WorkerLoggingProtocol.class, defaultWorkerLoggingProtocol);
                    if (shouldPublishJvmMemoryInfo) {
                        connection.useParameterSerializers(WorkerJvmMemoryInfoSerializer.create());
                        connection.addIncoming(WorkerJvmMemoryInfoProtocol.class, memoryStatus);
                    }
                }
            });
        }
    });
    workerProcess.startAccepting(acceptor);
    Address localAddress = acceptor.getAddress();
    // Build configuration for GradleWorkerMain
    Object id = idGenerator.generateId();
    String displayName = getBaseName() + " " + id;
    LOGGER.debug("Creating {}", displayName);
    LOGGER.debug("Using application classpath {}", applicationClasspath);
    LOGGER.debug("Using implementation classpath {}", implementationClassPath);
    JavaExecHandleBuilder javaCommand = getJavaCommand();
    javaCommand.setDisplayName(displayName);
    workerImplementationFactory.prepareJavaCommand(id, displayName, this, implementationClassPath, localAddress, javaCommand, shouldPublishJvmMemoryInfo);
    javaCommand.args("'" + displayName + "'");
    ExecHandle execHandle = javaCommand.build();
    workerProcess.setExecHandle(execHandle);
    return new MemoryRequestingWorkerProcess(workerProcess, memoryManager, MemoryAmount.parseNotation(javaCommand.getMinHeapSize()));
}
Also used : ConnectionAcceptor(org.gradle.internal.remote.ConnectionAcceptor) Address(org.gradle.internal.remote.Address) JavaExecHandleBuilder(org.gradle.process.internal.JavaExecHandleBuilder) ObjectConnection(org.gradle.internal.remote.ObjectConnection) ExecHandle(org.gradle.process.internal.ExecHandle)

Example 5 with ObjectConnection

use of org.gradle.internal.remote.ObjectConnection in project gradle by gradle.

the class ActionExecutionWorker method execute.

public void execute(final WorkerContext workerContext) {
    final ObjectConnection clientConnection = workerContext.getServerConnection();
    LOGGER.debug("Starting {}.", displayName);
    WorkerProcessContext context = new WorkerProcessContext() {

        public ObjectConnection getServerConnection() {
            return clientConnection;
        }

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

        public Object getWorkerId() {
            return workerId;
        }

        public String getDisplayName() {
            return displayName;
        }
    };
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(action.getClass().getClassLoader());
    NativeServices.initialize(gradleUserHomeDir, false);
    try {
        action.execute(context);
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
    LOGGER.debug("Completed {}.", displayName);
}
Also used : WorkerProcessContext(org.gradle.process.internal.worker.WorkerProcessContext) ObjectConnection(org.gradle.internal.remote.ObjectConnection)

Aggregations

ObjectConnection (org.gradle.internal.remote.ObjectConnection)7 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TestClassProcessor (org.gradle.api.internal.tasks.testing.TestClassProcessor)1 TestResultProcessor (org.gradle.api.internal.tasks.testing.TestResultProcessor)1 UncheckedException (org.gradle.internal.UncheckedException)1 ContextClassLoaderProxy (org.gradle.internal.dispatch.ContextClassLoaderProxy)1 ClassLoaderObjectInputStream (org.gradle.internal.io.ClassLoaderObjectInputStream)1 LoggingManagerInternal (org.gradle.internal.logging.LoggingManagerInternal)1 Address (org.gradle.internal.remote.Address)1 ConnectionAcceptor (org.gradle.internal.remote.ConnectionAcceptor)1 MessagingClient (org.gradle.internal.remote.MessagingClient)1 MultiChoiceAddress (org.gradle.internal.remote.internal.inet.MultiChoiceAddress)1 MultiChoiceAddressSerializer (org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)1 MessagingServices (org.gradle.internal.remote.services.MessagingServices)1 Decoder (org.gradle.internal.serialize.Decoder)1 InputStreamBackedDecoder (org.gradle.internal.serialize.InputStreamBackedDecoder)1