/
PortablePipelineJarCreator.java
261 lines (239 loc) · 10.7 KB
/
PortablePipelineJarCreator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.runners.jobsubmission;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.apache.beam.model.jobmanagement.v1.JobApi;
import org.apache.beam.model.pipeline.v1.RunnerApi;
import org.apache.beam.model.pipeline.v1.RunnerApi.Pipeline;
import org.apache.beam.runners.core.construction.PipelineOptionsTranslation;
import org.apache.beam.runners.core.construction.resources.PipelineResources;
import org.apache.beam.runners.fnexecution.artifact.ArtifactRetrievalService;
import org.apache.beam.runners.fnexecution.provisioning.JobInfo;
import org.apache.beam.sdk.io.ClassLoaderFileSystem;
import org.apache.beam.sdk.metrics.MetricResults;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PortablePipelineOptions;
import org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.MessageOrBuilder;
import org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.util.JsonFormat;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.ByteStreams;
import org.apache.commons.compress.utils.IOUtils;
import org.joda.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link PortablePipelineRunner} that bundles the input pipeline along with all dependencies,
* artifacts, etc. required to run the pipeline into a jar that can be executed later.
*
* <p>Each {@link PortablePipelineJarCreator} instance is not threadsafe; a new instance is expected
* to be constructed and {@link #run} once per job.
*/
public class PortablePipelineJarCreator implements PortablePipelineRunner {
private static final Logger LOG = LoggerFactory.getLogger(PortablePipelineJarCreator.class);
private final Class mainClass;
@VisibleForTesting JarOutputStream outputStream;
/** Wrapper over {@link #outputStream}. */
@VisibleForTesting WritableByteChannel outputChannel;
public PortablePipelineJarCreator(Class mainClass) {
this.mainClass = mainClass;
}
/**
* <em>Does not actually run the pipeline.</em> Instead bundles the input pipeline along with all
* dependencies, artifacts, etc. required to run the pipeline into a jar that can be executed
* later.
*/
@Override
public PortablePipelineResult run(Pipeline pipeline, JobInfo jobInfo) throws Exception {
PortablePipelineOptions pipelineOptions =
PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions())
.as(PortablePipelineOptions.class);
final String jobName = jobInfo.jobName();
File outputFile = new File(pipelineOptions.getOutputExecutablePath());
LOG.info("Creating jar {} for job {}", outputFile.getAbsolutePath(), jobName);
outputStream =
new JarOutputStream(new FileOutputStream(outputFile), createManifest(mainClass, jobName));
outputChannel = Channels.newChannel(outputStream);
PortablePipelineJarUtils.writeDefaultJobName(outputStream, jobName);
writeClassPathResources(mainClass.getClassLoader(), pipelineOptions);
writeAsJson(
PipelineOptionsTranslation.toProto(pipelineOptions),
PortablePipelineJarUtils.getPipelineOptionsUri(jobName));
Pipeline pipelineWithClasspathArtifacts = writeArtifacts(pipeline, jobName);
writeAsJson(pipelineWithClasspathArtifacts, PortablePipelineJarUtils.getPipelineUri(jobName));
// Closing the channel also closes the underlying stream.
outputChannel.close();
LOG.info("Jar {} created successfully.", outputFile.getAbsolutePath());
return new JarCreatorPipelineResult();
}
@VisibleForTesting
Manifest createManifest(Class mainClass, String defaultJobName) {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
boolean classHasMainMethod = false;
try {
Class returnType = mainClass.getMethod("main", String[].class).getReturnType();
if (returnType == Void.TYPE) {
classHasMainMethod = true;
} else {
LOG.warn(
"No Main-Class will be set in jar because main method in {} returns {}, expected void",
mainClass,
returnType);
}
} catch (NoSuchMethodException e) {
LOG.warn("No Main-Class will be set in jar because {} lacks a main method.", mainClass);
}
if (classHasMainMethod) {
manifest.getMainAttributes().put(Name.MAIN_CLASS, mainClass.getName());
}
return manifest;
}
/** Copy resources from {@code classLoader} to {@link #outputStream}. */
private void writeClassPathResources(ClassLoader classLoader, PipelineOptions options)
throws IOException {
List<String> classPathResources =
PipelineResources.detectClassPathResourcesToStage(classLoader, options);
Preconditions.checkArgument(
classPathResources.size() == 1, "Expected exactly one jar on " + classLoader.toString());
copyResourcesFromJar(new JarFile(classPathResources.get(0)));
}
/** Copy resources from {@code inputJar} to {@link #outputStream}. */
@VisibleForTesting
protected void copyResourcesFromJar(JarFile inputJar) throws IOException {
Enumeration<JarEntry> inputJarEntries = inputJar.entries();
// The zip spec allows multiple files with the same name; the Java zip libraries do not.
// Keep track of the files we've already written to filter out duplicates.
// Also, ignore the old manifest; we want to write our own.
Set<String> previousEntryNames = new HashSet<>(ImmutableList.of(JarFile.MANIFEST_NAME));
while (inputJarEntries.hasMoreElements()) {
JarEntry inputJarEntry = inputJarEntries.nextElement();
InputStream inputStream = inputJar.getInputStream(inputJarEntry);
String entryName = inputJarEntry.getName();
if (previousEntryNames.contains(entryName)) {
LOG.debug("Skipping duplicated file {}", entryName);
} else {
JarEntry outputJarEntry = new JarEntry(inputJarEntry);
outputStream.putNextEntry(outputJarEntry);
LOG.trace("Copying jar entry {}", inputJarEntry);
IOUtils.copy(inputStream, outputStream);
previousEntryNames.add(entryName);
}
}
}
/**
* Stages all dependencies in pipeline into the jar file at outputStream, returning a new pipeline
* that references these artifacts as classpath artifacts.
*/
@VisibleForTesting
protected Pipeline writeArtifacts(Pipeline pipeline, String jobName) throws IOException {
Pipeline.Builder result = pipeline.toBuilder();
for (Map.Entry<String, RunnerApi.Environment> env :
pipeline.getComponents().getEnvironmentsMap().entrySet()) {
result
.getComponentsBuilder()
.putEnvironments(env.getKey(), writeArtifacts(env.getValue(), jobName));
}
return result.build();
}
/**
* Stages all dependencies in environment into the jar file at outputStream, returning a new
* environment that references these artifacts as classpath artifacts.
*/
private RunnerApi.Environment writeArtifacts(RunnerApi.Environment environment, String jobName)
throws IOException {
RunnerApi.Environment.Builder result = environment.toBuilder();
result.clearDependencies();
for (RunnerApi.ArtifactInformation artifact : environment.getDependenciesList()) {
result.addDependencies(writeArtifact(artifact, jobName));
}
return result.build();
}
/**
* Stages all artifact into the jar file at outputStream, returning a new artifact pointing to its
* location in the classpath.
*/
private RunnerApi.ArtifactInformation writeArtifact(
RunnerApi.ArtifactInformation artifact, String jobName) throws IOException {
String path = PortablePipelineJarUtils.getArtifactUri(jobName, UUID.randomUUID().toString());
LOG.trace("Copying artifact {} to {}", artifact, path);
outputStream.putNextEntry(new JarEntry(path));
try (InputStream artifactStream = ArtifactRetrievalService.getArtifact(artifact)) {
ByteStreams.copy(artifactStream, outputStream);
}
return artifact
.toBuilder()
.setTypeUrn(ArtifactRetrievalService.FILE_ARTIFACT_URN)
.setTypePayload(
RunnerApi.ArtifactFilePayload.newBuilder()
.setPath(ClassLoaderFileSystem.SCHEMA + "://" + path)
.build()
.toByteString())
.build();
}
/** Helper method for writing {@code message} in UTF-8 JSON format. */
private void writeAsJson(MessageOrBuilder message, String outputPath) throws IOException {
outputStream.putNextEntry(new JarEntry(outputPath));
outputChannel.write(StandardCharsets.UTF_8.encode(JsonFormat.printer().print(message)));
}
private static class JarCreatorPipelineResult implements PortablePipelineResult {
@Override
public State getState() {
return State.DONE;
}
@Override
public State cancel() {
return State.DONE;
}
@Override
public State waitUntilFinish(Duration duration) {
return State.DONE;
}
@Override
public State waitUntilFinish() {
return State.DONE;
}
@Override
public MetricResults metrics() {
throw new UnsupportedOperationException("Jar creation does not yield metrics.");
}
@Override
public JobApi.MetricResults portableMetrics() throws UnsupportedOperationException {
return JobApi.MetricResults.getDefaultInstance();
}
}
}