Skip to content

Commit fb30430

Browse files
andyscottAndre Rocha
authored and
Andre Rocha
committed
clean up workers (bazel-contrib#1005)
* clean up workers * whoops! * git grep --name-only 'String args\[\]' | xargs sed -i 's|String args\[\]|String[] args|' * subclass ByteArrayOutputStream to allow shrinking of internal buffer * gc after each work request * split workerMain implementation into two private methods for readability * add comment about stdin * add a test for the buffer * improve tests * wrap gc and friends in the finally clause * comments * make the worker public * cleanup * comments * add stdin test * clean up test
1 parent 69a3f1c commit fb30430

File tree

17 files changed

+509
-288
lines changed

17 files changed

+509
-288
lines changed

scala/scalafmt/BUILD

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ filegroup(
1414

1515
scala_binary(
1616
name = "scalafmt",
17-
srcs = ["scalafmt/ScalafmtRunner.scala"],
18-
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtRunner",
17+
srcs = ["scalafmt/ScalafmtWorker.scala"],
18+
main_class = "io.bazel.rules_scala.scalafmt.ScalafmtWorker",
1919
visibility = ["//visibility:public"],
2020
deps = [
2121
"//src/java/io/bazel/rulesscala/worker",

scala/scalafmt/scalafmt/ScalafmtRunner.scala renamed to scala/scalafmt/scalafmt/ScalafmtWorker.scala

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.bazel.rules_scala.scalafmt
22

3-
import io.bazel.rulesscala.worker.{GenericWorker, Processor};
3+
import io.bazel.rulesscala.worker.Worker
44
import java.io.File
55
import java.nio.file.Files
66
import org.scalafmt.Scalafmt
@@ -10,21 +10,13 @@ import scala.annotation.tailrec
1010
import scala.collection.JavaConverters._
1111
import scala.io.Codec
1212

13-
object ScalafmtRunner extends GenericWorker(new ScalafmtProcessor) {
14-
def main(args: Array[String]) {
15-
try run(args)
16-
catch {
17-
case x: Exception =>
18-
x.printStackTrace()
19-
System.exit(1)
20-
}
21-
}
22-
}
13+
object ScalafmtWorker extends Worker.Interface {
14+
15+
def main(args: Array[String]): Unit = Worker.workerMain(args, ScalafmtWorker)
2316

24-
class ScalafmtProcessor extends Processor {
25-
def processRequest(args: java.util.List[String]) {
17+
def work(args: Array[String]) {
2618
val argName = List("config", "input", "output")
27-
val argFile = args.asScala.map{x => new File(x)}
19+
val argFile = args.map{x => new File(x)}
2820
val namespace = argName.zip(argFile).toMap
2921

3022
val source = FileOps.readFile(namespace.getOrElse("input", new File("")))(Codec.UTF8)

scala_proto/scala_proto_toolchain.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ scala_proto_toolchain = rule(
2929
"code_generator": attr.label(
3030
executable = True,
3131
cfg = "host",
32-
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_generator"),
32+
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_worker"),
3333
allow_files = True,
3434
),
3535
"named_generators": attr.string_dict(),

src/java/io/bazel/rulesscala/coverage/instrumenter/JacocoInstrumenter.java

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.bazel.rulesscala.coverage.instrumenter;
22

33
import io.bazel.rulesscala.jar.JarCreator;
4-
import io.bazel.rulesscala.worker.GenericWorker;
5-
import io.bazel.rulesscala.worker.Processor;
4+
import io.bazel.rulesscala.worker.Worker;
65

76
import java.io.BufferedInputStream;
87
import java.io.BufferedOutputStream;
@@ -26,28 +25,18 @@
2625
import org.jacoco.core.instr.Instrumenter;
2726
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;
2827

29-
public final class JacocoInstrumenter implements Processor {
28+
public final class JacocoInstrumenter implements Worker.Interface {
3029

3130
public static void main(String[] args) throws Exception {
32-
(new Worker()).run(args);
33-
}
34-
35-
private static final class Worker extends GenericWorker {
36-
public Worker() {
37-
super(new JacocoInstrumenter());
38-
}
31+
Worker.workerMain(args, new JacocoInstrumenter());
3932
}
4033

4134
@Override
42-
public void processRequest(List < String > args) {
35+
public void work(String[] args) throws Exception {
4336
Instrumenter jacoco = new Instrumenter(new OfflineInstrumentationAccessGenerator());
44-
args.forEach(arg -> {
45-
try {
46-
processArg(jacoco, arg);
47-
} catch (final Exception e) {
48-
throw new RuntimeException(e);
49-
}
50-
});
37+
for (String arg : args) {
38+
processArg(jacoco, arg);
39+
}
5140
}
5241

5342
private void processArg(Instrumenter jacoco, String arg) throws Exception {

src/java/io/bazel/rulesscala/scalac/BUILD

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ java_binary(
1717
"-source 1.8",
1818
"-target 1.8",
1919
],
20-
main_class = "io.bazel.rulesscala.scalac.ScalaCInvoker",
20+
main_class = "io.bazel.rulesscala.scalac.ScalacWorker",
2121
visibility = ["//visibility:public"],
2222
deps = [
2323
":exported_scalac_repositories_from_toolchain_to_jvm",
@@ -38,8 +38,7 @@ filegroup(
3838
"CompileOptions.java",
3939
"ProtoReporter.java",
4040
"Resource.java",
41-
"ScalaCInvoker.java",
42-
"ScalacProcessor.java",
41+
"ScalacWorker.java",
4342
],
4443
visibility = ["//visibility:public"],
4544
)

src/java/io/bazel/rulesscala/scalac/ScalaCInvoker.java

-47
This file was deleted.

src/java/io/bazel/rulesscala/scalac/ScalacProcessor.java renamed to src/java/io/bazel/rulesscala/scalac/ScalacWorker.java

+39-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import com.illicitonion.scalac.reporters.CompositeReporter;
44
import io.bazel.rulesscala.jar.JarCreator;
5-
import io.bazel.rulesscala.worker.GenericWorker;
6-
import io.bazel.rulesscala.worker.Processor;
5+
import io.bazel.rulesscala.worker.Worker;
76
import java.io.File;
87
import java.io.FileNotFoundException;
98
import java.io.FileOutputStream;
@@ -32,7 +31,7 @@
3231
import scala.tools.nsc.reporters.ConsoleReporter;
3332
import scala.tools.nsc.reporters.Reporter;
3433

35-
class ScalacProcessor implements Processor {
34+
class ScalacWorker implements Worker.Interface {
3635
private static boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
3736

3837
/** This is the reporter field for scalac, which we want to access */
@@ -47,11 +46,15 @@ class ScalacProcessor implements Processor {
4746
}
4847
}
4948

49+
public static void main(String[] args) throws Exception {
50+
Worker.workerMain(args, new ScalacWorker());
51+
}
52+
5053
@Override
51-
public void processRequest(List<String> args) throws Exception {
54+
public void work(String[] args) throws Exception {
5255
Path tmpPath = null;
5356
try {
54-
CompileOptions ops = new CompileOptions(args);
57+
CompileOptions ops = new CompileOptions(Arrays.asList(args));
5558

5659
Path outputPath = FileSystems.getDefault().getPath(ops.outputName);
5760
tmpPath = Files.createTempDirectory(outputPath.getParent(), "tmp");
@@ -67,7 +70,7 @@ public void processRequest(List<String> args) throws Exception {
6770

6871
String[] scalaSources = collectSrcJarSources(ops.files, scalaJarFiles, javaJarFiles);
6972

70-
String[] javaSources = GenericWorker.appendToString(ops.javaFiles, javaJarFiles);
73+
String[] javaSources = appendToString(ops.javaFiles, javaJarFiles);
7174
if (scalaSources.length == 0 && javaSources.length == 0) {
7275
throw new RuntimeException("Must have input files from either source jars or local files.");
7376
}
@@ -99,8 +102,8 @@ public void processRequest(List<String> args) throws Exception {
99102

100103
private static String[] collectSrcJarSources(
101104
String[] files, List<File> scalaJarFiles, List<File> javaJarFiles) {
102-
String[] scalaSources = GenericWorker.appendToString(files, scalaJarFiles);
103-
return GenericWorker.appendToString(scalaSources, javaJarFiles);
105+
String[] scalaSources = appendToString(files, scalaJarFiles);
106+
return appendToString(scalaSources, javaJarFiles);
104107
}
105108

106109
private static List<File> filterFilesByExtension(List<File> files, String extension) {
@@ -170,7 +173,7 @@ private static boolean matchesFileExtensions(String fileName, String[] extension
170173
}
171174

172175
private static String[] encodeBazelTargets(String[] targets) {
173-
return Arrays.stream(targets).map(ScalacProcessor::encodeBazelTarget).toArray(String[]::new);
176+
return Arrays.stream(targets).map(ScalacWorker::encodeBazelTarget).toArray(String[]::new);
174177
}
175178

176179
private static String encodeBazelTarget(String target) {
@@ -227,7 +230,7 @@ private static void compileScalaSources(CompileOptions ops, String[] scalaSource
227230
String[] constParams = {"-classpath", ops.classpath, "-d", tmpPath.toString()};
228231

229232
String[] compilerArgs =
230-
GenericWorker.merge(ops.scalaOpts, ops.pluginArgs, constParams, pluginParams, scalaSources);
233+
merge(ops.scalaOpts, ops.pluginArgs, constParams, pluginParams, scalaSources);
231234

232235
MainClass comp = new MainClass() {
233236
private Global compiler;
@@ -357,4 +360,30 @@ private static void copyResourceJars(String[] resourceJars, Path dest) throws IO
357360
extractJar(jarPath, dest.toString(), null);
358361
}
359362
}
363+
364+
private static <T> String[] appendToString(String[] init, List<T> rest) {
365+
String[] tmp = new String[init.length + rest.size()];
366+
System.arraycopy(init, 0, tmp, 0, init.length);
367+
int baseIdx = init.length;
368+
for (T t : rest) {
369+
tmp[baseIdx] = t.toString();
370+
baseIdx += 1;
371+
}
372+
return tmp;
373+
}
374+
375+
private static String[] merge(String[]... arrays) {
376+
int totalLength = 0;
377+
for (String[] arr : arrays) {
378+
totalLength += arr.length;
379+
}
380+
381+
String[] result = new String[totalLength];
382+
int offset = 0;
383+
for (String[] arr : arrays) {
384+
System.arraycopy(arr, 0, result, offset, arr.length);
385+
offset += arr.length;
386+
}
387+
return result;
388+
}
360389
}
+15-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
load("@rules_java//java:defs.bzl", "java_library")
2-
31
java_library(
42
name = "worker",
3+
srcs = ["Worker.java"],
4+
visibility = ["//visibility:public"],
5+
deps = [
6+
"//third_party/bazel/src/main/protobuf:worker_protocol_java_proto",
7+
],
8+
)
9+
10+
java_test(
11+
name = "worker_test",
512
srcs = [
6-
"GenericWorker.java",
7-
"Processor.java",
13+
"WorkerTest.java",
814
],
9-
visibility = ["//visibility:public"],
15+
jvm_flags = [
16+
"-Dcom.google.testing.junit.runner.shouldInstallTestSecurityManager=false",
17+
],
18+
test_class = "io.bazel.rulesscala.worker.WorkerTest",
1019
deps = [
20+
":worker",
1121
"//third_party/bazel/src/main/protobuf:worker_protocol_java_proto",
1222
],
1323
)

0 commit comments

Comments
 (0)