Skip to content

Commit eb06338

Browse files
committed
map type num shards
1 parent 03344fc commit eb06338

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

hollow/src/main/java/com/netflix/hollow/core/write/HollowMapTypeWriteState.java

+29-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.netflix.hollow.core.write;
1818

19+
import static com.netflix.hollow.core.index.FieldPaths.FieldPathException.ErrorKind.NOT_BINDABLE;
20+
1921
import com.netflix.hollow.core.index.FieldPaths;
2022
import com.netflix.hollow.core.memory.ByteData;
2123
import com.netflix.hollow.core.memory.ByteDataArray;
@@ -30,8 +32,6 @@
3032
import java.util.logging.Level;
3133
import java.util.logging.Logger;
3234

33-
import static com.netflix.hollow.core.index.FieldPaths.FieldPathException.ErrorKind.NOT_BINDABLE;
34-
3535
public class HollowMapTypeWriteState extends HollowTypeWriteState {
3636
private static final Logger LOG = Logger.getLogger(HollowMapTypeWriteState.class.getName());
3737

@@ -77,20 +77,32 @@ public void prepareForWrite() {
7777
}
7878

7979
private void gatherStatistics() {
80-
if(numShards == -1)
81-
calculateNumShards();
82-
revNumShards = numShards;
80+
int maxOrdinal = ordinalMap.maxOrdinal();
81+
if(numShards == -1) {
82+
numShards = calculateNumShards(maxOrdinal);
83+
revNumShards = numShards;
84+
} else {
85+
revNumShards = numShards;
86+
if (allowTypeResharding()) {
87+
numShards = calculateNumShards(maxOrdinal);
88+
if (numShards != revNumShards) { // re-sharding
89+
// limit numShards to 2x or .5x of prevShards per producer cycle
90+
numShards = numShards > revNumShards ? revNumShards * 2 : revNumShards / 2;
91+
92+
LOG.info(String.format("Num shards for type %s changing from %s to %s", schema.getName(), revNumShards, numShards));
93+
addReshardingHeader(revNumShards, numShards); // SNAP: TODO: Here,
94+
}
95+
}
96+
}
97+
98+
maxShardOrdinal = calcMaxShardOrdinal(maxOrdinal, numShards);
99+
if (revNumShards > 0) {
100+
revMaxShardOrdinal = calcMaxShardOrdinal(maxOrdinal, revNumShards);
101+
}
83102

84103
int maxKeyOrdinal = 0;
85104
int maxValueOrdinal = 0;
86105

87-
int maxOrdinal = ordinalMap.maxOrdinal();
88-
89-
maxShardOrdinal = new int[numShards];
90-
int minRecordLocationsPerShard = (maxOrdinal + 1) / numShards;
91-
for(int i=0;i<numShards;i++)
92-
maxShardOrdinal[i] = (i < ((maxOrdinal + 1) & (numShards - 1))) ? minRecordLocationsPerShard : minRecordLocationsPerShard - 1;
93-
94106
int maxMapSize = 0;
95107
ByteData data = ordinalMap.getByteData().getUnderlyingArray();
96108

@@ -143,10 +155,9 @@ private void gatherStatistics() {
143155
bitsPerMapPointer = 64 - Long.numberOfLeadingZeros(maxShardTotalOfMapBuckets);
144156
}
145157

146-
private void calculateNumShards() {
158+
int calculateNumShards(int maxOrdinal) {
147159
int maxKeyOrdinal = 0;
148160
int maxValueOrdinal = 0;
149-
int maxOrdinal = ordinalMap.maxOrdinal();
150161

151162
int maxMapSize = 0;
152163
ByteData data = ordinalMap.getByteData().getUnderlyingArray();
@@ -194,9 +205,10 @@ private void calculateNumShards() {
194205
long projectedSizeOfType = (bitsPerMapSizeValue + bitsPerMapPointer) * (maxOrdinal + 1) / 8;
195206
projectedSizeOfType += ((bitsPerKeyElement + bitsPerValueElement) * totalOfMapBuckets) / 8;
196207

197-
numShards = 1;
198-
while(stateEngine.getTargetMaxTypeShardSize() * numShards < projectedSizeOfType)
199-
numShards *= 2;
208+
int targetNumShards = 1;
209+
while(stateEngine.getTargetMaxTypeShardSize() * targetNumShards < projectedSizeOfType)
210+
targetNumShards *= 2;
211+
return targetNumShards;
200212
}
201213

202214
@Override

0 commit comments

Comments
 (0)