forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-7] Add range partitioning with automatic sampling of key distr…
…ibution This closes apache#1255
- Loading branch information
Showing
25 changed files
with
1,358 additions
and
56 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
77 changes: 77 additions & 0 deletions
77
...k-core/src/main/java/org/apache/flink/api/common/distributions/CommonRangeBoundaries.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,77 @@ | ||
/* | ||
* 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.flink.api.common.distributions; | ||
|
||
import org.apache.flink.api.common.typeutils.TypeComparator; | ||
|
||
public class CommonRangeBoundaries<T> implements RangeBoundaries<T> { | ||
private final TypeComparator<T> typeComparator; | ||
private final Object[][] boundaries; | ||
private final TypeComparator[] flatComparators; | ||
private final Object[] keys; | ||
|
||
public CommonRangeBoundaries(TypeComparator<T> typeComparators, Object[][] boundaries) { | ||
this.typeComparator = typeComparators; | ||
this.flatComparators = typeComparators.getFlatComparators(); | ||
this.keys = new Object[flatComparators.length]; | ||
this.boundaries = boundaries; | ||
} | ||
|
||
@Override | ||
public int getRangeIndex(T record) { | ||
return binarySearch(record); | ||
} | ||
|
||
// Search the range index of input record. | ||
private int binarySearch(T record) { | ||
int low = 0; | ||
int high = this.boundaries.length - 1; | ||
typeComparator.extractKeys(record, keys, 0); | ||
|
||
while (low <= high) { | ||
final int mid = (low + high) >>> 1; | ||
final int result = compareKeys(flatComparators, keys, this.boundaries[mid]); | ||
|
||
if (result > 0) { | ||
low = mid + 1; | ||
} else if (result < 0) { | ||
high = mid - 1; | ||
} else { | ||
return mid; | ||
} | ||
} | ||
// key not found, but the low index is the target | ||
// bucket, since the boundaries are the upper bound | ||
return low; | ||
} | ||
|
||
private int compareKeys(TypeComparator[] flatComparators, Object[] keys, Object[] boundary) { | ||
if (flatComparators.length != keys.length || flatComparators.length != boundary.length) { | ||
throw new RuntimeException("Can not compare keys with boundary due to mismatched length."); | ||
} | ||
|
||
for (int i=0; i<flatComparators.length; i++) { | ||
int result = flatComparators[i].compare(keys[i], boundary[i]); | ||
if (result != 0) { | ||
return result; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
flink-core/src/main/java/org/apache/flink/api/common/distributions/RangeBoundaries.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,36 @@ | ||
/* | ||
* 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.flink.api.common.distributions; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* RangeBoundaries is used to split the records into multiple ranges. | ||
* | ||
* @param <T> The boundary type. | ||
*/ | ||
public interface RangeBoundaries<T> extends Serializable { | ||
|
||
/** | ||
* Get the range index of record. | ||
* | ||
* @param record The input record. | ||
* @return The range index. | ||
*/ | ||
int getRangeIndex(T record); | ||
} |
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
Oops, something went wrong.