Skip to content

Commit

Permalink
[HUDI-4841] Fix sort idempotency issue (#6669)
Browse files Browse the repository at this point in the history
  • Loading branch information
voonhous authored Sep 17, 2022
1 parent 21cbfce commit f1caa3a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.hudi.table.format.cow;

import java.util.Comparator;
import org.apache.hudi.common.fs.FSUtils;
import org.apache.hudi.table.format.cow.vector.reader.ParquetColumnarRowSplitReader;
import org.apache.hudi.util.DataTypeUtils;
Expand All @@ -42,7 +43,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -220,13 +220,7 @@ public FileInputSplit[] createInputSplits(int minNumSplits) throws IOException {

// get the block locations and make sure they are in order with respect to their offset
final BlockLocation[] blocks = fs.getFileBlockLocations(file, 0, len);
Arrays.sort(blocks, new Comparator<BlockLocation>() {
@Override
public int compare(BlockLocation o1, BlockLocation o2) {
long diff = o1.getLength() - o2.getOffset();
return Long.compare(diff, 0L);
}
});
Arrays.sort(blocks, Comparator.comparingLong(BlockLocation::getOffset));

long bytesUnassigned = len;
long position = 0;
Expand Down Expand Up @@ -399,4 +393,5 @@ private InflaterInputStreamFactory<?> getInflaterInputStreamFactory(org.apache.h
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.table.format.cow;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

import java.util.Arrays;
import java.util.Comparator;
import org.apache.hadoop.fs.BlockLocation;
import org.junit.jupiter.api.Test;

public class TestBlockLocationSort {

private static BlockLocation createBlockLocation(int offset, int length) {
return new BlockLocation(new String[0], new String[0], offset, length);
}

@Test
void testBlockLocationSort() {
BlockLocation o1 = createBlockLocation(0, 5);
BlockLocation o2 = createBlockLocation(6, 4);
BlockLocation o3 = createBlockLocation(5, 5);

BlockLocation[] blocks = {o1, o2, o3};
BlockLocation[] sortedBlocks = {o1, o3, o2};

Arrays.sort(blocks, Comparator.comparingLong(BlockLocation::getOffset));
assertThat(blocks, equalTo(sortedBlocks));

// Sort again to ensure idempotency
Arrays.sort(blocks, Comparator.comparingLong(BlockLocation::getOffset));
assertThat(blocks, equalTo(sortedBlocks));
}

}

0 comments on commit f1caa3a

Please sign in to comment.