-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resharding: Data elements split/join utils for list, set, and map types
- Loading branch information
Showing
46 changed files
with
2,134 additions
and
341 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
26 changes: 26 additions & 0 deletions
26
hollow/src/main/java/com/netflix/hollow/core/read/engine/AbstractHollowTypeDataElements.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,26 @@ | ||
package com.netflix.hollow.core.read.engine; | ||
|
||
import com.netflix.hollow.core.memory.MemoryMode; | ||
import com.netflix.hollow.core.memory.encoding.GapEncodedVariableLengthIntegerReader; | ||
import com.netflix.hollow.core.memory.pool.ArraySegmentRecycler; | ||
import com.netflix.hollow.core.read.HollowBlobInput; | ||
import com.netflix.hollow.core.read.engine.map.HollowMapTypeDataElements; | ||
import java.io.IOException; | ||
|
||
public abstract class AbstractHollowTypeDataElements { | ||
|
||
public int maxOrdinal; | ||
|
||
public GapEncodedVariableLengthIntegerReader encodedAdditions; | ||
public GapEncodedVariableLengthIntegerReader encodedRemovals; | ||
|
||
public final ArraySegmentRecycler memoryRecycler; | ||
public final MemoryMode memoryMode; | ||
|
||
public AbstractHollowTypeDataElements(MemoryMode memoryMode, ArraySegmentRecycler memoryRecycler) { | ||
this.memoryMode = memoryMode; | ||
this.memoryRecycler = memoryRecycler; | ||
} | ||
|
||
public abstract void destroy(); | ||
} |
75 changes: 75 additions & 0 deletions
75
...c/main/java/com/netflix/hollow/core/read/engine/AbstractHollowTypeDataElementsJoiner.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,75 @@ | ||
package com.netflix.hollow.core.read.engine; | ||
|
||
import com.netflix.hollow.core.memory.encoding.GapEncodedVariableLengthIntegerReader; | ||
|
||
public abstract class AbstractHollowTypeDataElementsJoiner <T extends AbstractHollowTypeDataElements> { | ||
public final int fromMask; | ||
public final int fromOrdinalShift; | ||
public final T[] from; | ||
|
||
public T to; | ||
|
||
public AbstractHollowTypeDataElementsJoiner(T[] from) { | ||
this.from = from; | ||
this.fromMask = from.length - 1; | ||
this.fromOrdinalShift = 31 - Integer.numberOfLeadingZeros(from.length); | ||
|
||
if (from.length<=0 || !((from.length&(from.length-1))==0)) { | ||
throw new IllegalStateException("No. of DataElements to be joined must be a power of 2"); | ||
} | ||
|
||
for (int i=0;i<from.length;i++) { | ||
if (from[i].maxOrdinal == -1) { | ||
continue; | ||
} | ||
if (from[i].maxOrdinal > (1<<29) | ||
|| from[i].maxOrdinal != 0 && (from.length > (1<<29)/from[i].maxOrdinal) | ||
|| from[i].maxOrdinal * from.length + i > (1<<29)) { | ||
throw new IllegalArgumentException("Too large to join, maxOrdinal would exceed 2<<29"); | ||
} | ||
} | ||
|
||
for (AbstractHollowTypeDataElements elements : from) { | ||
if (elements.encodedAdditions != null) { | ||
throw new IllegalStateException("Encountered encodedAdditions in data elements joiner- this is not expected " + | ||
"since encodedAdditions only exist on delta data elements and they dont carry over to target data elements, " + | ||
"delta data elements are never split/joined"); | ||
} | ||
} | ||
} | ||
|
||
public T join() { | ||
|
||
initToElements(); | ||
to.maxOrdinal = -1; | ||
|
||
populateStats(); | ||
|
||
copyRecords(); | ||
|
||
GapEncodedVariableLengthIntegerReader[] fromRemovals = new GapEncodedVariableLengthIntegerReader[from.length]; | ||
for (int i=0;i<from.length;i++) { | ||
fromRemovals[i] = from[i].encodedRemovals; | ||
} | ||
to.encodedRemovals = GapEncodedVariableLengthIntegerReader.join(fromRemovals); | ||
|
||
return to; | ||
} | ||
|
||
/** | ||
* Initialize the target data elements. | ||
*/ | ||
public abstract void initToElements(); | ||
|
||
/** | ||
* Populate the stats of the target data elements. | ||
*/ | ||
public abstract void populateStats(); | ||
|
||
/** | ||
* Copy records from the source data elements to the target data elements. | ||
*/ | ||
public abstract void copyRecords(); | ||
|
||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...main/java/com/netflix/hollow/core/read/engine/AbstractHollowTypeDataElementsSplitter.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,73 @@ | ||
package com.netflix.hollow.core.read.engine; | ||
|
||
import com.netflix.hollow.core.memory.encoding.GapEncodedVariableLengthIntegerReader; | ||
|
||
/** | ||
* Join multiple {@code HollowListTypeDataElements}s into 1 {@code HollowListTypeDataElements}. | ||
* Ordinals are remapped and corresponding data is copied over. | ||
* The original data elements are not destroyed. | ||
* The no. of passed data elements must be a power of 2. | ||
*/ | ||
public abstract class AbstractHollowTypeDataElementsSplitter<T extends AbstractHollowTypeDataElements> { | ||
public final int numSplits; | ||
public final int toMask; | ||
public final int toOrdinalShift; | ||
public final T from; | ||
|
||
public T[] to; | ||
|
||
public AbstractHollowTypeDataElementsSplitter(T from, int numSplits) { | ||
this.from = from; | ||
this.numSplits = numSplits; | ||
this.toMask = numSplits - 1; | ||
this.toOrdinalShift = 31 - Integer.numberOfLeadingZeros(numSplits); | ||
|
||
if (numSplits<=0 || !((numSplits&(numSplits-1))==0)) { | ||
throw new IllegalStateException("Must split by power of 2"); | ||
} | ||
|
||
if (from.encodedAdditions != null) { | ||
throw new IllegalStateException("Encountered encodedAdditions in data elements splitter- this is not expected " + | ||
"since encodedAdditions only exist on delta data elements and they dont carry over to target data elements, " + | ||
"delta data elements are never split/joined"); | ||
} | ||
} | ||
|
||
public T[] split() { | ||
|
||
initToElements(); | ||
for(int i=0;i<to.length;i++) { | ||
to[i].maxOrdinal = -1; | ||
} | ||
|
||
populateStats(); | ||
|
||
copyRecords(); | ||
|
||
if (from.encodedRemovals != null) { | ||
GapEncodedVariableLengthIntegerReader[] splitRemovals = from.encodedRemovals.split(numSplits); | ||
for(int i=0;i<to.length;i++) { | ||
to[i].encodedRemovals = splitRemovals[i]; | ||
} | ||
} | ||
|
||
return to; | ||
} | ||
|
||
/** | ||
* Initialize the target data elements. | ||
*/ | ||
public abstract void initToElements(); | ||
|
||
/** | ||
* Populate the stats of the target data elements. | ||
*/ | ||
public abstract void populateStats(); | ||
|
||
/** | ||
* Copy records from the source data elements to the target data elements. | ||
*/ | ||
public abstract void copyRecords(); | ||
|
||
|
||
} |
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
73 changes: 73 additions & 0 deletions
73
.../main/java/com/netflix/hollow/core/read/engine/list/HollowListTypeDataElementsJoiner.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,73 @@ | ||
package com.netflix.hollow.core.read.engine.list; | ||
|
||
import com.netflix.hollow.core.memory.FixedLengthDataFactory; | ||
import com.netflix.hollow.core.read.engine.AbstractHollowTypeDataElementsJoiner; | ||
|
||
|
||
/** | ||
* Join multiple {@code HollowListTypeDataElements}s into 1 {@code HollowListTypeDataElements}. | ||
* Ordinals are remapped and corresponding data is copied over. | ||
* The original data elements are not destroyed. | ||
* The no. of passed data elements must be a power of 2. | ||
*/ | ||
class HollowListTypeDataElementsJoiner extends AbstractHollowTypeDataElementsJoiner<HollowListTypeDataElements> { | ||
|
||
public HollowListTypeDataElementsJoiner(HollowListTypeDataElements[] from) { | ||
super(from); | ||
} | ||
|
||
@Override | ||
public void initToElements() { | ||
this.to = new HollowListTypeDataElements(from[0].memoryMode, from[0].memoryRecycler); | ||
} | ||
|
||
@Override | ||
public void populateStats() { | ||
for(int fromIndex=0;fromIndex<from.length;fromIndex++) { | ||
int mappedMaxOrdinal = from[fromIndex].maxOrdinal == -1 ? -1 : (from[fromIndex].maxOrdinal * from.length) + fromIndex; | ||
to.maxOrdinal = Math.max(to.maxOrdinal, mappedMaxOrdinal); | ||
if (from[fromIndex].bitsPerElement > to.bitsPerElement) { | ||
// uneven bitsPerElement could be the case for consumers that skip type shards with no additions, so pick max across all shards | ||
to.bitsPerElement = from[fromIndex].bitsPerElement; | ||
} | ||
} | ||
|
||
long totalOfListSizes = 0; | ||
for(int ordinal=0;ordinal<=to.maxOrdinal;ordinal++) { | ||
int fromIndex = ordinal & fromMask; | ||
int fromOrdinal = ordinal >> fromOrdinalShift; | ||
|
||
long startElement = from[fromIndex].getStartElement(fromOrdinal); | ||
long endElement = from[fromIndex].getEndElement(fromOrdinal); | ||
long numElements = endElement - startElement; | ||
totalOfListSizes += numElements; | ||
|
||
} | ||
to.bitsPerListPointer = totalOfListSizes == 0 ? 1 : 64 - Long.numberOfLeadingZeros(totalOfListSizes); | ||
to.totalNumberOfElements = totalOfListSizes; | ||
} | ||
|
||
@Override | ||
public void copyRecords() { | ||
long elementCounter = 0; | ||
|
||
to.listPointerData = FixedLengthDataFactory.get((long)to.bitsPerListPointer * (to.maxOrdinal + 1), to.memoryMode, to.memoryRecycler); | ||
to.elementData = FixedLengthDataFactory.get(to.bitsPerElement * to.totalNumberOfElements, to.memoryMode, to.memoryRecycler); | ||
|
||
for(int ordinal=0;ordinal<=to.maxOrdinal;ordinal++) { | ||
int fromIndex = ordinal & fromMask; | ||
int fromOrdinal = ordinal >> fromOrdinalShift; | ||
|
||
if (fromOrdinal <= from[fromIndex].maxOrdinal) { // else lopsided shard for e.g. when consumers skip type shards with no additions | ||
HollowListTypeDataElements source = from[fromIndex]; | ||
long startElement = source.getStartElement(fromOrdinal); | ||
long endElement = source.getEndElement(fromOrdinal); | ||
|
||
long numElements = endElement - startElement; | ||
to.copyElementsFrom(elementCounter, source, startElement, endElement); | ||
elementCounter += numElements; | ||
} | ||
to.listPointerData.setElementValue((long)to.bitsPerListPointer * ordinal, to.bitsPerListPointer, elementCounter); | ||
} | ||
} | ||
} |
Oops, something went wrong.