Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make workflow-api compatible with Guava 21.0 and newer #135

Merged
merged 12 commits into from
Mar 15, 2021
25 changes: 3 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<properties>
<revision>2.42</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.273-rc30689.09147672b85b</jenkins.version>
<jenkins.version>2.222.4</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
Expand All @@ -74,8 +74,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.176.x</artifactId>
<version>16</version>
<artifactId>bom-2.222.x</artifactId>
<version>25</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -105,11 +105,6 @@
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>trilead-api</artifactId>
<scope>test</scope>
</dependency>
<!-- For Semaphore step -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -139,18 +134,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- TODO the tests depend on workflow-support and cps which both need api published first... -->
<!-- need to run at least one flag or the pipeline fails -->
<test>InjectedTest</test>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Expand Down Expand Up @@ -181,11 +184,9 @@ public void onLoaded() {
Futures.addCallback(e.getCurrentExecutions(false), new FutureCallback<List<StepExecution>>() {
@Override
public void onSuccess(List<StepExecution> result) {
if (result != null) {
LOGGER.log(FINE, "Will resume {0}", result);
for (StepExecution se : result) {
se.onResume();
}
LOGGER.log(FINE, "Will resume {0}", result);
for (StepExecution se : result) {
se.onResume();
}
}

Expand All @@ -197,7 +198,7 @@ public void onFailure(Throwable t) {
LOGGER.log(WARNING, "Failed to load " + e, t);
}
}
}, MoreExecutors.newDirectExecutorService());
}, newExecutorService());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I get it. The original code was not specifying an executor service, so why do we need to add this code now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
Expand All @@ -220,13 +221,11 @@ public ListenableFuture<?> apply(final Function<StepExecution, Void> f) {
Futures.addCallback(execs,new FutureCallback<List<StepExecution>>() {
@Override
public void onSuccess(List<StepExecution> result) {
if (result != null) {
for (StepExecution e : result) {
try {
f.apply(e);
} catch (RuntimeException x) {
LOGGER.log(Level.WARNING, null, x);
}
for (StepExecution e : result) {
try {
f.apply(e);
} catch (RuntimeException x) {
LOGGER.log(Level.WARNING, null, x);
}
}
}
Expand All @@ -235,7 +234,7 @@ public void onSuccess(List<StepExecution> result) {
public void onFailure(Throwable t) {
LOGGER.log(Level.WARNING, null, t);
}
}, MoreExecutors.newDirectExecutorService());
}, newExecutorService());
}

return Futures.allAsList(all);
Expand All @@ -259,4 +258,25 @@ public void onFailure(Throwable t) {
executor.awaitTermination(1, TimeUnit.MINUTES);
}

/**
* Returns an {@link ExecutorService} to be used as a parameter in other methods.
* It calls {@code MoreExecutors#sameThreadExecutor} or falls back to {@code MoreExecutors#newDirectExecutorService}
* for compatibility with newer (> 18.0) versions of guava.
*/
private static ExecutorService newExecutorService() {
try {
try {
// guava older than 18
Method method = MoreExecutors.class.getMethod("sameThreadExecutor");
return (ExecutorService) method.invoke(null);
} catch (NoSuchMethodException e) {
// TODO invert this to prefer the newer guava method once guava is upgraded in Jenkins core
timja marked this conversation as resolved.
Show resolved Hide resolved
timja marked this conversation as resolved.
Show resolved Hide resolved
Method method = MoreExecutors.class.getMethod("newDirectExecutorService");
return (ExecutorService) method.invoke(null);
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.common.base.Predicate;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction;
import org.jenkinsci.plugins.workflow.actions.TimingAction;
import org.jenkinsci.plugins.workflow.graph.BlockEndNode;
Expand All @@ -37,6 +36,7 @@

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
package org.jenkinsci.plugins.workflow.graphanalysis;

import com.google.common.base.Predicate;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jenkinsci.plugins.workflow.graph.FlowEndNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graph.FlowStartNode;
import org.jenkinsci.plugins.workflow.graph.StepNode;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
package org.jenkinsci.plugins.workflow.graphanalysis;

import com.google.common.base.Predicate;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jenkinsci.plugins.workflow.graph.FlowEndNode;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graph.FlowStartNode;
import org.jenkinsci.plugins.workflow.graph.StepNode;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand Down