-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* upgrade-to-iceberg-1.5.2 The iceberg-kafka-connect-events module has been deprecated in this repo: consider these legacy Events. Instead we rely on iceberg-kafka-connect-events which has been moved into Iceberg Core. This will make it easier to move the remainder of the repository over to there this PR updates worker/coordinator/etc. to work off the (significantly) reworked event classes. This PR ports the marchinery from Iceberg 1.4.x to have a fallback decoder if decoding fails in the event a legacy record was left behind in the control topic during the upgrade. This code should be removed in future releases.
- Loading branch information
1 parent
690e62e
commit c16ca54
Showing
25 changed files
with
1,795 additions
and
238 deletions.
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
96 changes: 96 additions & 0 deletions
96
kafka-connect-events/src/main/java/org/apache/iceberg/avro/DeprecatedAvroEncoderUtil.java
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,96 @@ | ||
/* | ||
* 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.iceberg.avro; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import org.apache.avro.LogicalTypes; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.io.BinaryDecoder; | ||
import org.apache.avro.io.BinaryEncoder; | ||
import org.apache.avro.io.DatumReader; | ||
import org.apache.avro.io.DatumWriter; | ||
import org.apache.avro.io.DecoderFactory; | ||
import org.apache.avro.io.EncoderFactory; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
||
/** | ||
* Iceberg 1.5.0 introduced a breaking change to Avro serialization that the connector uses when encoding | ||
* messages for the control topic, requiring a way to fall back to decoding 1.4.x series messages that may | ||
* be left behind on a control topic when upgrading. | ||
* | ||
* This class should be removed in later releases. | ||
*/ | ||
public class DeprecatedAvroEncoderUtil { | ||
|
||
private DeprecatedAvroEncoderUtil() {} | ||
|
||
static { | ||
LogicalTypes.register(LogicalMap.NAME, schema -> LogicalMap.get()); | ||
} | ||
|
||
private static final byte[] MAGIC_BYTES = new byte[] {(byte) 0xC2, (byte) 0x01}; | ||
|
||
public static <T> byte[] encode(T datum, Schema avroSchema) throws IOException { | ||
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { | ||
DataOutputStream dataOut = new DataOutputStream(out); | ||
|
||
// Write the magic bytes | ||
dataOut.write(MAGIC_BYTES); | ||
|
||
// Write avro schema | ||
dataOut.writeUTF(avroSchema.toString()); | ||
|
||
// Encode the datum with avro schema. | ||
BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); | ||
DatumWriter<T> writer = new GenericAvroWriter<>(avroSchema); | ||
writer.write(datum, encoder); | ||
encoder.flush(); | ||
|
||
return out.toByteArray(); | ||
} | ||
} | ||
|
||
public static <T> T decode(byte[] data) throws IOException { | ||
try (ByteArrayInputStream in = new ByteArrayInputStream(data, 0, data.length)) { | ||
DataInputStream dataInput = new DataInputStream(in); | ||
|
||
// Read the magic bytes | ||
byte header0 = dataInput.readByte(); | ||
byte header1 = dataInput.readByte(); | ||
Preconditions.checkState( | ||
header0 == MAGIC_BYTES[0] && header1 == MAGIC_BYTES[1], | ||
"Unrecognized header bytes: 0x%02X 0x%02X", | ||
header0, | ||
header1); | ||
|
||
// Read avro schema | ||
Schema avroSchema = new Schema.Parser().parse(dataInput.readUTF()); | ||
|
||
// Decode the datum with the parsed avro schema. | ||
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(in, null); | ||
DatumReader<T> reader = new DeprecatedGenericAvroReader<>(avroSchema); | ||
reader.setSchema(avroSchema); | ||
return reader.read(null, binaryDecoder); | ||
} | ||
} | ||
} |
Oops, something went wrong.