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

BugFix: Fixed an issue where JDBC OceanBaseSink might cause data loss in non-primary key tables #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.oceanbase.connector.flink.table;

import java.io.Serializable;
import java.util.Objects;
import java.util.Optional;

public class DataChangeRecord implements Record {
Expand All @@ -36,11 +37,15 @@ static KeyExtractor simple() {
return record ->
Optional.ofNullable(record.getTable().getKey())
.map(
keys ->
new DataChangeRecordData(
keys.stream()
.map(record::getFieldValue)
.toArray()))
keys -> {
Object[] array =
keys.stream().map(record::getFieldValue).toArray();
if (array.length == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

What's it for? The process should directly go to orElse if the record is from a non-pk table,

Copy link
Member Author

Choose a reason for hiding this comment

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

Object key = keyExtractor.extract(dataChangeRecord);
if (key == null) {
synchronized (buffer) {
buffer.computeIfAbsent(record.getTableId().identifier(), k -> new ArrayList<>())
.add(dataChangeRecord);
}
} else {
synchronized (reducedBuffer) {
reducedBuffer
.computeIfAbsent(record.getTableId().identifier(), k -> new HashMap<>())
.put(key, dataChangeRecord);
}
}

  • If record.getTable().getKey() is an empty but not null primary key List, this KeyExtractor.simple() will return an empty DataChangeRecord object but not NULL
  • Then line 133 of the code block above will return false, causing the non-primary key table to be mistakenly treated as the primary key table.
        static KeyExtractor simple() {
            return record ->
                    // when record.getTable().getKey() is empty List
                    Optional.ofNullable(record.getTable().getKey())
                            .map(
                                    keys ->
                                            new DataChangeRecordData(
                                                    keys.stream()
                                                            .map(record::getFieldValue)
                                                            .toArray()))
                            .orElse(null);
        }

return null;
} else {
return new DataChangeRecordData(array);
}
})
.orElse(null);
}
}
Expand Down Expand Up @@ -76,6 +81,21 @@ public Object getFieldValue(String fieldName) {
return data.getValue(table.getFieldIndex(fieldName));
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
DataChangeRecord record = (DataChangeRecord) object;
return Objects.equals(table, record.table)
&& type == record.type
&& Objects.equals(data, record.data);
}

@Override
public int hashCode() {
return Objects.hash(table, type, data);
}

@Override
public String toString() {
return "DataChangeRecord{" + "table=" + table + ", type=" + type + ", data=" + data + '}';
Expand Down
Loading