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

[HUDI-4825] Remove redundant fields in serialized commit metadata in JSON #6646

Merged
Merged
Show file tree
Hide file tree
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 @@ -29,7 +29,13 @@ public class JsonUtils {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// We need to exclude custom getters, setters and creators which can use member fields
// to derive new fields, so that they are not included in the serialization
MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
MAPPER.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
MAPPER.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
MAPPER.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
MAPPER.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE);
}

public static ObjectMapper getObjectMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
package org.apache.hudi.common.model;

import org.apache.hudi.common.testutils.HoodieTestUtils;
import org.apache.hudi.common.util.CollectionUtils;
import org.apache.hudi.common.util.FileIOUtils;
import org.apache.hudi.common.util.JsonUtils;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand All @@ -34,6 +39,30 @@
*/
public class TestHoodieCommitMetadata {

private static final List<String> EXPECTED_FIELD_NAMES = Arrays.asList(
"partitionToWriteStats", "compacted", "extraMetadata", "operationType");

public static void verifyMetadataFieldNames(
HoodieCommitMetadata commitMetadata, List<String> expectedFieldNameList)
throws IOException {
String serializedCommitMetadata = commitMetadata.toJsonString();
List<String> actualFieldNameList = CollectionUtils.toStream(
JsonUtils.getObjectMapper().readTree(serializedCommitMetadata).fieldNames())
.collect(Collectors.toList());
assertEquals(
expectedFieldNameList.stream().sorted().collect(Collectors.toList()),
actualFieldNameList.stream().sorted().collect(Collectors.toList())
);
}

@Test
public void verifyFieldNamesInCommitMetadata() throws IOException {
List<HoodieWriteStat> fakeHoodieWriteStats = HoodieTestUtils.generateFakeHoodieWriteStat(10);
HoodieCommitMetadata commitMetadata = new HoodieCommitMetadata();
fakeHoodieWriteStats.forEach(stat -> commitMetadata.addWriteStat(stat.getPartitionPath(), stat));
verifyMetadataFieldNames(commitMetadata, EXPECTED_FIELD_NAMES);
}

@Test
public void testPerfStatPresenceInHoodieMetadata() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.hudi.common.model;

import org.apache.hudi.common.testutils.HoodieTestUtils;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static org.apache.hudi.common.model.TestHoodieCommitMetadata.verifyMetadataFieldNames;

public class TestHoodieReplaceCommitMetadata {

private static final List<String> EXPECTED_FIELD_NAMES = Arrays.asList(
"partitionToWriteStats", "partitionToReplaceFileIds", "compacted", "extraMetadata", "operationType");

@Test
public void verifyFieldNamesInReplaceCommitMetadata() throws IOException {
List<HoodieWriteStat> fakeHoodieWriteStats = HoodieTestUtils.generateFakeHoodieWriteStat(10);
HoodieReplaceCommitMetadata commitMetadata = new HoodieReplaceCommitMetadata();
fakeHoodieWriteStats.forEach(stat -> {
commitMetadata.addWriteStat(stat.getPartitionPath(), stat);
commitMetadata.addReplaceFileId(stat.getPartitionPath(), stat.getFileId());
});
verifyMetadataFieldNames(commitMetadata, EXPECTED_FIELD_NAMES);
}
}