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

[#25582] Use a Pathing Jar instead of long command line class paths in Java Boot loader. #26087

Merged
merged 7 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ on:
pull_request:
branches: ['master', 'release-*']
tags: ['v*']
paths: ['sdks/go/pkg/**', 'sdks/go.mod', 'sdks/go.sum']
paths: ['sdks/go/pkg/**', 'sdks/go.mod', 'sdks/go.sum', 'sdks/go/container/*', 'sdks/java/container/*', 'sdks/python/container/*', 'sdks/typescript/container/*']
# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
Expand All @@ -48,7 +48,7 @@ jobs:
- name: Delete old coverage
run: "cd sdks/go/pkg && rm -rf .coverage || :"
- name: Run coverage
run: cd sdks/go/pkg && go test -coverprofile=coverage.txt -covermode=atomic ./...
run: cd sdks && go test -coverprofile=coverage.txt -covermode=atomic ./go/pkg/... ./go/container/... ./java/container/... ./python/container/... ./typescript/container/...
Copy link
Contributor

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).

Copy link
Contributor Author

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.

Copy link
Contributor Author

@lostluck lostluck Apr 4, 2023

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.

Copy link
Contributor

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

- uses: codecov/codecov-action@v3
with:
flags: go
Expand Down
6 changes: 5 additions & 1 deletion sdks/java/container/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,12 @@ func main() {

// (2) Add classpath: "-cp foo.jar:bar.jar:.."
if len(javaOptions.Classpath) > 0 {
pathingjar, err := makePathingJar(javaOptions.Classpath)
if err != nil {
logger.Fatalf(ctx, "makePathingJar failed: %v", err)
}
args = append(args, "-cp")
args = append(args, strings.Join(javaOptions.Classpath, ":"))
args = append(args, pathingjar)
}

// (3) Add (sorted) properties: "-Dbar=baz -Dfoo=bar .."
Expand Down
88 changes: 47 additions & 41 deletions sdks/java/container/boot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,63 @@
package main

import (
"reflect"
"testing"
"context"
"reflect"
"testing"

"github.com/apache/beam/sdks/v2/go/container/tools"
)

func TestBuildOptionsEmpty(t *testing.T) {
dir := "test/empty"
metaOptions, err := LoadMetaOptions(dir)
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}
if metaOptions != nil {
t.Fatalf("LoadMetaOptions(%v) = %v, want nil", dir, metaOptions)
}
ctx, logger := context.Background(), &tools.Logger{}
dir := "test/empty"
metaOptions, err := LoadMetaOptions(ctx, logger, dir)
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}
if metaOptions != nil {
t.Fatalf("LoadMetaOptions(%v) = %v, want nil", dir, metaOptions)
}

javaOptions := BuildOptions(metaOptions)
if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 {
t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions)
}
javaOptions := BuildOptions(ctx, logger, metaOptions)
if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 {
t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions)
}
}

func TestBuildOptionsDisabled(t *testing.T) {
metaOptions, err := LoadMetaOptions("test/disabled")
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}
ctx, logger := context.Background(), &tools.Logger{}
metaOptions, err := LoadMetaOptions(ctx, logger, "test/disabled")
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}

javaOptions := BuildOptions(metaOptions)
if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 {
t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions)
}
javaOptions := BuildOptions(ctx, logger, metaOptions)
if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 {
t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions)
}
}

func TestBuildOptions(t *testing.T) {
metaOptions, err := LoadMetaOptions("test/priority")
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}
ctx, logger := context.Background(), &tools.Logger{}
metaOptions, err := LoadMetaOptions(ctx, logger, "test/priority")
if err != nil {
t.Fatalf("Got error %v running LoadMetaOptions", err)
}

javaOptions := BuildOptions(metaOptions)
wantJavaArguments := []string{"java_args=low", "java_args=high"}
wantClasspath := []string{"classpath_high", "classpath_low"}
wantProperties := map[string]string{
"priority":"high",
}
if !reflect.DeepEqual(javaOptions.JavaArguments, wantJavaArguments) {
t.Errorf("BuildOptions(%v).JavaArguments = %v, want %v", metaOptions, javaOptions.JavaArguments, wantJavaArguments)
}
if !reflect.DeepEqual(javaOptions.Classpath, wantClasspath) {
t.Errorf("BuildOptions(%v).Classpath = %v, want %v", metaOptions, javaOptions.Classpath, wantClasspath)
}
if !reflect.DeepEqual(javaOptions.Properties, wantProperties) {
t.Errorf("BuildOptions(%v).JavaProperties = %v, want %v", metaOptions, javaOptions.Properties, wantProperties)
}
javaOptions := BuildOptions(ctx, logger, metaOptions)
wantJavaArguments := []string{"java_args=low", "java_args=high"}
wantClasspath := []string{"classpath_high", "classpath_low"}
wantProperties := map[string]string{
"priority": "high",
}
if !reflect.DeepEqual(javaOptions.JavaArguments, wantJavaArguments) {
t.Errorf("BuildOptions(%v).JavaArguments = %v, want %v", metaOptions, javaOptions.JavaArguments, wantJavaArguments)
}
if !reflect.DeepEqual(javaOptions.Classpath, wantClasspath) {
t.Errorf("BuildOptions(%v).Classpath = %v, want %v", metaOptions, javaOptions.Classpath, wantClasspath)
}
if !reflect.DeepEqual(javaOptions.Properties, wantProperties) {
t.Errorf("BuildOptions(%v).JavaProperties = %v, want %v", metaOptions, javaOptions.Properties, wantProperties)
}
}
55 changes: 55 additions & 0 deletions sdks/java/container/pathingjar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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"
)

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
}

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\n"))
zf.Write([]byte("Class-Path: " + strings.Join(classpaths, " ")))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: We could use Created-By: xyz to be able to trace back from a JAR to this pathing code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

zf.Write([]byte("\n"))
return nil
}
60 changes: 60 additions & 0 deletions sdks/java/container/pathingjar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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"
"bytes"
"io"
"strings"
"testing"
)

func TestWritePathingJar(t *testing.T) {
var buf bytes.Buffer
want := []string{"a.jar", "b.jar", "c.jar"}
err := writePathingJar(want, &buf)
if err != nil {
t.Errorf("writePathingJar(%v) = %v, want nil", want, err)
}

zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
t.Errorf("zip.NewReader() = %v, want nil", err)
}

var found bool
for _, f := range zr.File {
if f.Name != "META-INF/MANIFEST.MF" {
continue
}
found = true
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)
}
if !strings.Contains(string(all), "Class-Path: "+strings.Join(want, " ")) {
t.Errorf("%v = %v, want nil", f.Name, err)
}
}
if !found {
t.Error("Jar didn't contain manifest")
}
}