-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[#25582] Use a Pathing Jar instead of long command line class paths in Java Boot loader. #26087
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4942059
Use a Pathing Jar instead of long class paths.
lostluck f6a3d72
Ensure boot.go container changes trigger go tests action.
lostluck 7ffa7c2
Add comments for pathingjar.
lostluck fb2c1c1
Only set classpath once.
lostluck 18b4af5
URIs are a prefixed scheme.
lostluck 3d0a61d
Add comment about URIs
lostluck 4b35162
Correct max line length length.
lostluck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 main | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// makePathingJar produces a context or 'pathing' jar which only contains the relative | ||
// classpaths in its META-INF/MANIFEST.MF. | ||
// | ||
// Since we build with Java 8 as a minimum, this is the only supported way of reducing | ||
// command line length, since argsfile support wasn't added until Java 9. | ||
// | ||
// https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html is the spec. | ||
// | ||
// In particular, we only need to populate the Jar with a Manifest-Version and Class-Path | ||
// attributes. | ||
// Per https://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#classpath | ||
// the classpath URLs must be relative for security reasons. | ||
func makePathingJar(classpaths []string) (string, error) { | ||
f, err := os.Create("pathing.jar") | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create pathing.jar: %w", err) | ||
} | ||
defer f.Close() | ||
if err := writePathingJar(classpaths, f); err != nil { | ||
return "", fmt.Errorf("unable to write pathing.jar: %w", err) | ||
} | ||
return f.Name(), nil | ||
} | ||
|
||
var lineBreak = []byte{'\r', '\n'} | ||
var continuation = []byte{' '} | ||
|
||
func writePathingJar(classpaths []string, w io.Writer) error { | ||
jar := zip.NewWriter(w) | ||
defer jar.Close() | ||
|
||
if _, err := jar.Create("META-INF/"); err != nil { | ||
return fmt.Errorf("unable to create META-INF/ directory: %w", err) | ||
} | ||
|
||
zf, err := jar.Create("META-INF/MANIFEST.MF") | ||
if err != nil { | ||
return fmt.Errorf("unable to create META-INF/MANIFEST.MF: %w", err) | ||
} | ||
|
||
zf.Write([]byte("Manifest-Version: 1.0")) | ||
zf.Write(lineBreak) | ||
zf.Write([]byte("Created-By: sdks/java/container/pathingjar.go")) | ||
zf.Write(lineBreak) | ||
// Class-Path: must have a sequence of relative URIs for the paths | ||
// which we assume outright in this case. | ||
|
||
// We could do this memory efficiently, but it's not worthwhile compared to the complexity | ||
// at this stage. | ||
allCPs := "Class-Path: file:" + strings.Join(classpaths, " file:") | ||
|
||
const lineLenMax = 71 // it's actually 72, but we remove 1 to account for the continuation line prefix. | ||
buf := make([]byte, lineLenMax) | ||
cur := 0 | ||
for cur+lineLenMax < len(allCPs) { | ||
next := cur + lineLenMax | ||
copy(buf, allCPs[cur:next]) | ||
zf.Write(buf) | ||
zf.Write(lineBreak) | ||
zf.Write(continuation) | ||
cur = next | ||
} | ||
zf.Write([]byte(allCPs[cur:])) | ||
zf.Write(lineBreak) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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 main | ||
|
||
import ( | ||
"archive/zip" | ||
"bufio" | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWritePathingJar(t *testing.T) { | ||
var buf bytes.Buffer | ||
input := []string{"a.jar", "b.jar", "c.jar", "thisVeryLongJarNameIsOverSeventyTwoCharactersLongAndNeedsToBeSplitCorrectly.jar"} | ||
err := writePathingJar(input, &buf) | ||
if err != nil { | ||
t.Errorf("writePathingJar(%v) = %v, want nil", input, err) | ||
} | ||
|
||
zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) | ||
if err != nil { | ||
t.Errorf("zip.NewReader() = %v, want nil", err) | ||
} | ||
|
||
f := getManifest(zr) | ||
if f == nil { | ||
t.Fatalf("Jar didn't contain manifest") | ||
} | ||
|
||
{ | ||
fr, err := f.Open() | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
all, err := io.ReadAll(fr) | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
fr.Close() | ||
want := "\nClass-Path: file:a.jar file:b.jar file:c.jar" | ||
if !strings.Contains(string(all), want) { | ||
t.Errorf("%v = %v, want nil", f.Name, err) | ||
} | ||
} | ||
|
||
{ | ||
fr, err := f.Open() | ||
if err != nil { | ||
t.Errorf("(%v).Open() = %v, want nil", f.Name, err) | ||
} | ||
defer fr.Close() | ||
fs := bufio.NewScanner(fr) | ||
fs.Split(bufio.ScanLines) | ||
|
||
for fs.Scan() { | ||
if got, want := len(fs.Bytes()), 72; got > want { | ||
t.Errorf("Manifest line exceeds limit got %v:, want %v line: %q", got, want, fs.Text()) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// getManifest extracts the java manifest from the zip file. | ||
func getManifest(zr *zip.Reader) *zip.File { | ||
for _, f := range zr.File { | ||
if f.Name != "META-INF/MANIFEST.MF" { | ||
continue | ||
} | ||
return f | ||
} | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: does codecov count uncovered non-go files (e.g. https://github.com/apache/beam/blob/master/sdks/java/container/license_scripts/pull_licenses_java.py)? I'm wondering if it makes sense to run container tests separately outside of the context of codecov (I'm leaning towards thinking that they should be included so this is probably a no-op).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will ignore those files, because it's unaware of files that don't receive any coverage numbers/changes.
I think we also don't have the covbot enabled for these directories either. But I'm less concerned about that than I am about the unit tests being run at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabling covbot here would be out of scope for this change I think. The go tests is different because it's otherwise hard to ensure an action will run, without having a change that ensures the action runs and that was fixing a clear gap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM - was meant to be nonblocking, but I realize now I didn't specify that