This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-12902-a7c1ed67e7734a4291f36213d29ea61499aa6831 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 3f78e15099679252efc6dc6cbcba770a671e0acf Author: Tiefang Hu <[email protected]> AuthorDate: Sat Nov 7 06:41:00 2026 +0000 [Fix][Format] Use native protoc on macOS ARM64 (#12902) --- docs/en/connectors/formats/protobuf.md | 10 ++ docs/zh/connectors/formats/protobuf.md | 10 ++ .../format/protobuf/CompileDescriptor.java | 59 ++++++++- .../format/protobuf/ProtobufConverterTest.java | 133 +++++++++++++++++++++ 4 files changed, 207 insertions(+), 5 deletions(-) diff --git a/docs/en/connectors/formats/protobuf.md b/docs/en/connectors/formats/protobuf.md index 1dd120b3012..a167482cacc 100644 --- a/docs/en/connectors/formats/protobuf.md +++ b/docs/en/connectors/formats/protobuf.md @@ -4,6 +4,16 @@ Protobuf (Protocol Buffers) is a language-neutral, platform-independent data ser Currently, Protobuf format can be used with Kafka. +## Compiler availability + +SeaTunnel compiles `protobuf_schema` at runtime. On macOS ARM64 (Apple Silicon) with a JVM reporting `os.arch=aarch64`, it uses protoc `3.25.3` because the compiler bundled by protoc-jar has no native binary for that platform. Other platforms keep their existing compiler and arguments. + +If the compiler is missing from protoc-jar's temporary cache, compilation downloads it from Maven Central or a matching `mirror` in `~/.m2/settings.xml` (`mirrorOf=central` or a value containing `*`). The temporary cache may be cleared, requiring another download. A failed download can surface as `Unsupported platform: protoc-3.25.3-osx-aarch_64.exe`; check network access and whether the mirror serves that artifact. protoc-jar uses a simplified settings parser, not full Maven mirror resolution. + +SeaTunnel's platform selector also accepts `os.arch=arm64` defensively, but protoc-jar 3.11.4 does not normalize that alias to its native classifier. This does not provide end-to-end support for JVMs reporting `arm64`. + +For schemas shared across platforms, use features accepted by the compiler on every target platform; a schema accepted by the newer macOS ARM64 compiler may not compile with the older compiler elsewhere. + ## Kafka Usage Example - Example of simulating a randomly generated data source and writing it to Kafka in Protobuf format diff --git a/docs/zh/connectors/formats/protobuf.md b/docs/zh/connectors/formats/protobuf.md index 14c874edbe5..0375caa9f68 100644 --- a/docs/zh/connectors/formats/protobuf.md +++ b/docs/zh/connectors/formats/protobuf.md @@ -4,6 +4,16 @@ Protobuf(Protocol Buffers)是一种由Google开发的语言中立、平台 目前支持在 Kafka 中使用 protobuf 格式。 +## 编译器可用性 + +SeaTunnel 在运行时编译 `protobuf_schema`。在 JVM 报告 `os.arch=aarch64` 的 macOS ARM64(Apple Silicon)上,使用 protoc `3.25.3`,因为 protoc-jar 自带的编译器没有该平台的原生二进制文件。其他平台保持原有编译器和参数。 + +当 protoc-jar 的临时缓存中没有编译器时,会从 Maven Central 或 `~/.m2/settings.xml` 中匹配的 `mirror` 下载(`mirrorOf=central` 或包含 `*` 的值)。临时缓存可能被清理,之后需要重新下载。下载失败可能表现为 `Unsupported platform: protoc-3.25.3-osx-aarch_64.exe`;此时应检查网络访问,以及镜像是否提供该构件。protoc-jar 使用简化的配置解析器,并非完整的 Maven 镜像解析机制。 + +SeaTunnel 的平台判断也防御性地接受 `os.arch=arm64`,但 protoc-jar 3.11.4 不会将该别名归一化为原生构件分类器。因此,这并不提供对报告 `arm64` 的 JVM 的端到端支持。 + +跨平台使用的 schema 应采用所有目标平台编译器都支持的语法;较新 macOS ARM64 编译器接受的 schema,不一定能由其他平台的旧版编译器编译。 + ## Kafka 使用示例 - 模拟随机生成数据源,并以 protobuf 的格式 写入 kafka 的实例 diff --git a/seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java b/seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java index 17a14bc84f7..280f1ccf1fd 100644 --- a/seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java +++ b/seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java @@ -28,10 +28,16 @@ import com.google.protobuf.Descriptors; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Locale; public class CompileDescriptor { + // The bundled protoc has no macOS ARM64 binary. Keep other platforms on their existing + // compiler so working offline installations do not require a new download. + private static final String MACOS_ARM64_PROTOC_VERSION = "3.25.3"; + public static Descriptors.Descriptor compileDescriptorTempFile( String protoContent, String messageName) throws IOException, InterruptedException, Descriptors.DescriptorValidationException { @@ -77,11 +83,11 @@ public class CompileDescriptor { int exitCode = Protoc.runProtoc( - new String[] { - "--proto_path=" + protoFile.getParent(), - "--descriptor_set_out=" + targetDesc, - protoFile.getPath() - }); + buildProtocArgs( + protoFile, + targetDesc, + System.getProperty("os.name", ""), + System.getProperty("os.arch", ""))); if (exitCode != 0) { throw new SeaTunnelProtobufFormatException( @@ -91,6 +97,49 @@ public class CompileDescriptor { return targetDesc; } + /** + * Builds the complete protoc argument list for one schema compilation. + * + * <p>Kept pure and package-private so the platform gate is assertable on any CI host. The + * compiler version selector is prepended only on macOS ARM64; every other platform keeps the + * earlier arguments in the earlier order. + * + * @param protoFile schema file to compile + * @param targetDesc descriptor file protoc writes + * @param osName value of the {@code os.name} system property + * @param osArch value of the {@code os.arch} system property + * @return protoc arguments, with the version selector first when one is selected + */ + static String[] buildProtocArgs( + File protoFile, String targetDesc, String osName, String osArch) { + List<String> protocArgs = new ArrayList<>(); + if (isMacOsArm64(osName, osArch)) { + // protoc-jar consumes "-v<version>" itself and resolves that exact compiler release + // instead of its bundled one, which has no macOS ARM64 binary. + protocArgs.add("-v" + MACOS_ARM64_PROTOC_VERSION); + } + protocArgs.add("--proto_path=" + protoFile.getParent()); + protocArgs.add("--descriptor_set_out=" + targetDesc); + protocArgs.add(protoFile.getPath()); + return protocArgs.toArray(new String[0]); + } + + /** + * Detects macOS ARM64 for compiler selection. + * + * <p>Accepting {@code arm64} is a defensive superset only: protoc-jar 3.11.4 normalizes {@code + * aarch64} to its native classifier and has no such mapping for {@code arm64}. + */ + private static boolean isMacOsArm64(String osName, String osArch) { + String name = osName.toLowerCase(Locale.ROOT); + boolean macOs = name.startsWith("mac os x") || name.startsWith("macosx"); + if (!macOs) { + return false; + } + String arch = osArch.toLowerCase(Locale.ROOT); + return "arm64".equals(arch) || "aarch64".equals(arch); + } + private static Descriptors.FileDescriptor[] buildFileDescriptors( DescriptorProtos.FileDescriptorSet descriptorSet) throws Descriptors.DescriptorValidationException { diff --git a/seatunnel-formats/seatunnel-format-protobuf/src/test/java/org/apache/seatunnel/format/protobuf/ProtobufConverterTest.java b/seatunnel-formats/seatunnel-format-protobuf/src/test/java/org/apache/seatunnel/format/protobuf/ProtobufConverterTest.java index e3d6dd35a2d..074291d3522 100644 --- a/seatunnel-formats/seatunnel-format-protobuf/src/test/java/org/apache/seatunnel/format/protobuf/ProtobufConverterTest.java +++ b/seatunnel-formats/seatunnel-format-protobuf/src/test/java/org/apache/seatunnel/format/protobuf/ProtobufConverterTest.java @@ -23,19 +23,31 @@ import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType; import org.apache.seatunnel.api.table.type.SeaTunnelDataType; import org.apache.seatunnel.api.table.type.SeaTunnelRow; import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.format.protobuf.exception.ProtobufFormatErrorCode; +import org.apache.seatunnel.format.protobuf.exception.SeaTunnelProtobufFormatException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import com.google.protobuf.Descriptors; import com.google.protobuf.DynamicMessage; +import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; class ProtobufConverterTest { + /** Holds the schema and descriptor paths the argument matrix asserts on. */ + @TempDir Path tempDir; + private SeaTunnelRow buildSeaTunnelRow() { SeaTunnelRow seaTunnelRow = new SeaTunnelRow(10); @@ -152,4 +164,125 @@ class ProtobufConverterTest { Assertions.assertEquals(originalRow, convertedRow); } + + /** Guards that the compiled descriptor keeps the typed presence declared in a proto2 schema. */ + @Test + public void testProto2RequiredAndOptionalFieldPresence() + throws Descriptors.DescriptorValidationException, IOException, InterruptedException { + String protoContent = + "syntax = \"proto2\";\n" + + "\n" + + "message Order {\n" + + " required int32 id = 1;\n" + + " optional string note = 2;\n" + + " repeated string tags = 3;\n" + + "}"; + + Descriptors.Descriptor descriptor = + CompileDescriptor.compileDescriptorTempFile(protoContent, "Order"); + + Descriptors.FieldDescriptor idField = descriptor.findFieldByName("id"); + Assertions.assertTrue(idField.isRequired(), "proto2 required field stays required"); + Assertions.assertTrue(idField.hasPresence(), "proto2 field tracks presence"); + Assertions.assertFalse(idField.isRepeated()); + + Descriptors.FieldDescriptor noteField = descriptor.findFieldByName("note"); + Assertions.assertTrue(noteField.isOptional()); + Assertions.assertFalse(noteField.isRequired()); + Assertions.assertTrue(noteField.hasPresence(), "proto2 optional scalar has presence"); + + Descriptors.FieldDescriptor tagsField = descriptor.findFieldByName("tags"); + Assertions.assertTrue(tagsField.isRepeated()); + Assertions.assertFalse(tagsField.hasPresence(), "repeated field has no presence"); + } + + /** Guards the exact proto3 wire bytes and the row -> bytes -> row round trip. */ + @Test + public void testProto3SimpleMessageWireBytesAndRoundTrip() + throws Descriptors.DescriptorValidationException, IOException, InterruptedException { + String protoContent = + "syntax = \"proto3\";\n" + "\n" + "message Simple {\n" + " int32 id = 1;\n" + "}"; + + String messageName = "Simple"; + Descriptors.Descriptor descriptor = + CompileDescriptor.compileDescriptorTempFile(protoContent, messageName); + + SeaTunnelRowType rowType = + new SeaTunnelRowType( + new String[] {"id"}, new SeaTunnelDataType<?>[] {BasicType.INT_TYPE}); + + SeaTunnelRow originalRow = new SeaTunnelRow(1); + originalRow.setField(0, 7); + + RowToProtobufConverter rowToProtobufConverter = + new RowToProtobufConverter(rowType, descriptor); + byte[] protobufMessage = rowToProtobufConverter.convertRowToGenericRecord(originalRow); + + // field 1 as varint: tag 0x08 followed by the value 0x07 + Assertions.assertArrayEquals(new byte[] {8, 7}, protobufMessage); + + DynamicMessage dynamicMessage = DynamicMessage.parseFrom(descriptor, protobufMessage); + ProtobufToRowConverter protobufToRowConverter = + new ProtobufToRowConverter(protoContent, messageName); + SeaTunnelRow convertedRow = + protobufToRowConverter.converter(descriptor, dynamicMessage, rowType); + + Assertions.assertEquals(originalRow, convertedRow); + } + + /** Guards that a malformed schema keeps the existing DESCRIPTOR_CONVERT_FAILED contract. */ + @Test + public void testInvalidProtoSchemaKeepsDescriptorConvertFailedContract() { + String badProto = + "syntax = \"proto3\";\n" + "\n" + "message Broken {\n" + " int32 id = 1\n" + "}"; + String messageName = "Broken"; + + SeaTunnelProtobufFormatException exception = + Assertions.assertThrows( + SeaTunnelProtobufFormatException.class, + () -> CompileDescriptor.compileDescriptorTempFile(badProto, messageName)); + + Assertions.assertEquals( + ProtobufFormatErrorCode.DESCRIPTOR_CONVERT_FAILED, + exception.getSeaTunnelErrorCode()); + Assertions.assertEquals("PROTOBUF-01", exception.getSeaTunnelErrorCode().getCode()); + Assertions.assertTrue( + exception.getMessage().contains("Protoc compile error, exit code: "), + exception.getMessage()); + } + + /** + * Guards the compiler selection of the new macOS ARM64 branch on the hosts CI actually runs on. + * The matrix asserts the complete argument list, so every other platform must stay on the + * previous three arguments in the previous order. No protoc process is started. + */ + @ParameterizedTest(name = "os.name={0}, os.arch={1}, macOS ARM64={2}") + @CsvSource({ + "Mac OS X, aarch64, true", + "Mac OS X, arm64, true", + "macOSX, AARCH64, true", + "Mac OS X, x86_64, false", + "Linux, x86_64, false", + "Linux, aarch64, false", + "Windows 11, amd64, false", + "Unknown OS, '', false", + "'', '', false" + }) + void testBuildProtocArgsSelectsMacOsArm64Compiler( + String osName, String osArch, boolean macOsArm64) { + File protoFile = tempDir.resolve("platform.proto").toFile(); + String targetDesc = tempDir.resolve("platform.desc").toString(); + + List<String> expected = new ArrayList<>(); + if (macOsArm64) { + expected.add("-v3.25.3"); + } + expected.add("--proto_path=" + protoFile.getParent()); + expected.add("--descriptor_set_out=" + targetDesc); + expected.add(protoFile.getPath()); + + Assertions.assertArrayEquals( + expected.toArray(new String[0]), + CompileDescriptor.buildProtocArgs(protoFile, targetDesc, osName, osArch)); + } }
