Skip to content

Commit 3cf3643

Browse files
committed
This fixes some tests
that were not properly checking that the MemoryRequestServer (and the sketch) were closing intermediate increases in size when off-heap.
1 parent 4cbc160 commit 3cf3643

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/test/java/org/apache/datasketches/hll/DirectAuxHashMapTest.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,24 @@
4343
public class DirectAuxHashMapTest {
4444

4545
@Test
46-
public void checkGrow() {
46+
public void checkGrow() { //It is very rare, but this forces an HLL_4 to exceed its computed memory size.
4747
int lgConfigK = 4;
4848
TgtHllType tgtHllType = TgtHllType.HLL_4;
4949
int n = 8; //put lgConfigK == 4 into HLL mode
5050
int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
5151
HllSketch hllSketch;
5252
WritableMemory wmem = WritableMemory.allocateDirect(bytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
53+
WritableMemory wmemCopy = wmem; //copy of wmem
54+
assertTrue(wmemCopy.isDirect()); //original copy of wmem is off-heap
55+
assertTrue(wmemCopy.isAlive()); //original copy of wmem is Alive
5356
hllSketch = new HllSketch(lgConfigK, tgtHllType, wmem);
5457
for (int i = 0; i < n; i++) {
5558
hllSketch.update(i);
5659
}
5760
hllSketch.couponUpdate(HllUtil.pair(7, 15)); //mock extreme values
5861
hllSketch.couponUpdate(HllUtil.pair(8, 15));
5962
hllSketch.couponUpdate(HllUtil.pair(9, 15));
60-
//println(hllSketch.toString(true, true, true, true));
63+
//println(hllSketch.toString(true, true, true, true)); //
6164
DirectHllArray dha = (DirectHllArray) hllSketch.hllSketchImpl;
6265
assertEquals(dha.getAuxHashMap().getLgAuxArrInts(), 2);
6366
assertTrue(hllSketch.isMemory());
@@ -75,19 +78,21 @@ public void checkGrow() {
7578
byteArray = hllSketch.toUpdatableByteArray();
7679
WritableMemory wmem2 = WritableMemory.writableWrap(byteArray);
7780
hllSketch2 = HllSketch.writableWrap(wmem2);
78-
//println(hllSketch2.toString(true, true, true, true));
81+
//println(hllSketch2.toString(true, true, true, true)); //
7982
DirectHllArray dha2 = (DirectHllArray) hllSketch2.hllSketchImpl;
8083
assertEquals(dha2.getAuxHashMap().getLgAuxArrInts(), 2);
8184
assertEquals(dha2.getAuxHashMap().getAuxCount(), 3);
8285

8386
//Check grow to on-heap
8487
hllSketch.couponUpdate(HllUtil.pair(10, 15)); //puts it over the edge, must grow
85-
//println(hllSketch.toString(true, true, true, true));
88+
//println(hllSketch.toString(true, true, true, true)); //
8689
dha = (DirectHllArray) hllSketch.hllSketchImpl;
8790
assertEquals(dha.getAuxHashMap().getLgAuxArrInts(), 3);
8891
assertEquals(dha.getAuxHashMap().getAuxCount(), 4);
8992
assertTrue(hllSketch.isMemory());
9093
assertFalse(hllSketch.isOffHeap());
94+
assertTrue(wmemCopy.isDirect()); //original copy of wmem was off-heap and still is
95+
assertFalse(wmemCopy.isAlive()); //original copy of wmem has been closed
9196
}
9297

9398
@Test

src/test/java/org/apache/datasketches/quantiles/DirectQuantilesMemoryRequestTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void checkLimitedMemoryScenarios() { //Requesting application
4848
//########## Owning Implementation
4949
// This part would actually be part of the Memory owning implementation so it is faked here
5050
WritableMemory wmem = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
51+
WritableMemory wmemCopy = wmem;
5152
println("Initial mem size: " + wmem.getCapacity());
5253

5354
//########## Receiving Application
@@ -70,6 +71,8 @@ public void checkLimitedMemoryScenarios() { //Requesting application
7071
// so the the wmem reference is invalid. Use the sketch to get the last memory reference.
7172
WritableMemory lastMem = usk1.getMemory();
7273
println("Final mem size: " + usk1.getMemory().getCapacity());
74+
assertTrue(wmemCopy.isDirect());
75+
assertFalse(wmemCopy.isAlive());
7376
}
7477

7578
@Test
@@ -79,6 +82,7 @@ public void checkGrowBaseBuf() {
7982
final int initBytes = (4 + (u / 2)) << 3; // not enough to hold everything
8083

8184
WritableMemory wmem = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
85+
WritableMemory wmemCopy = wmem;
8286
println("Initial mem size: " + wmem.getCapacity());
8387
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
8488
for (int i = 1; i <= u; i++) {
@@ -88,6 +92,8 @@ public void checkGrowBaseBuf() {
8892
println("curCombBufItemCap: " + currentSpace);
8993
assertEquals(currentSpace, 2 * k);
9094
println("last Mem Cap: " + usk1.getMemory().getCapacity());
95+
assertTrue(wmemCopy.isDirect());
96+
assertFalse(wmemCopy.isAlive());
9197
}
9298

9399
@Test
@@ -97,6 +103,7 @@ public void checkGrowCombBuf() {
97103
final int initBytes = ((2 * k) + 4) << 3; //just room for BB
98104

99105
WritableMemory wmem = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
106+
WritableMemory wmemCopy = wmem;
100107
println("Initial mem size: " + wmem.getCapacity());
101108
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
102109
for (int i = 1; i <= u; i++) {
@@ -108,6 +115,8 @@ public void checkGrowCombBuf() {
108115
final int newSpace = usk1.getCombinedBufferItemCapacity();
109116
println("newCombBurItemCap: " + newSpace);
110117
assertEquals(newCB.length, 3 * k);
118+
assertTrue(wmemCopy.isDirect());
119+
assertFalse(wmemCopy.isAlive());
111120
}
112121

113122
@Test
@@ -119,6 +128,7 @@ public void checkGrowFromWrappedEmptySketch() {
119128
final Memory origSketchMem = Memory.wrap(usk1.toByteArray());
120129

121130
WritableMemory wmem = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
131+
WritableMemory wmemCopy = wmem;
122132
origSketchMem.copyTo(0, wmem, 0, initBytes);
123133
UpdateDoublesSketch usk2 = DirectUpdateDoublesSketch.wrapInstance(wmem);
124134
assertTrue(wmem.isSameResource(usk2.getMemory()));
@@ -135,6 +145,8 @@ public void checkGrowFromWrappedEmptySketch() {
135145

136146
final int expectedSize = COMBINED_BUFFER + ((2 * k) << 3);
137147
assertEquals(mem2.getCapacity(), expectedSize);
148+
assertTrue(wmemCopy.isDirect());
149+
assertFalse(wmemCopy.isAlive());
138150
}
139151

140152
@Test

src/test/java/org/apache/datasketches/quantiles/DoublesSketchTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,22 @@ public void checkEmptyExceptions() {
140140
@Test
141141
public void directSketchShouldMoveOntoHeapEventually() {
142142
WritableMemory wmem = WritableMemory.allocateDirect(1000, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer());
143+
WritableMemory wmemCopy = wmem;
143144
UpdateDoublesSketch sketch = DoublesSketch.builder().build(wmem);
144145
Assert.assertTrue(sketch.isSameResource(wmem));
145146
for (int i = 0; i < 1000; i++) {
146147
sketch.update(i);
147148
}
148149
println(sketch.toString());
150+
assertTrue(wmemCopy.isDirect());
151+
assertFalse(wmemCopy.isAlive());
149152
}
150153

151154
@Test
152155
public void directSketchShouldMoveOntoHeapEventually2() {
153156
int i = 0;
154157
WritableMemory wmem = WritableMemory.allocateDirect(50, ByteOrder.LITTLE_ENDIAN, new DefaultMemoryRequestServer());
158+
WritableMemory wmemCopy = wmem;
155159
UpdateDoublesSketch sketch = DoublesSketch.builder().build(wmem);
156160
Assert.assertTrue(sketch.isSameResource(wmem));
157161
for (; i < 1000; i++) {
@@ -163,6 +167,8 @@ public void directSketchShouldMoveOntoHeapEventually2() {
163167
}
164168
}
165169
assertFalse(wmem.isAlive());
170+
assertTrue(wmemCopy.isDirect());
171+
assertFalse(wmemCopy.isAlive());
166172
}
167173

168174
@Test

src/test/java/org/apache/datasketches/theta/DirectQuickSelectSketchTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,16 @@ public void checkMoveAndResize() {
781781
int u = 2 * k;
782782
int bytes = Sketches.getMaxUpdateSketchBytes(k);
783783
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request more memory
784+
WritableMemory wmemCopy = wmem;
784785
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
785786
assertTrue(sketch.isSameResource(wmem));
786787
for (int i = 0; i < u; i++) { sketch.update(i); }
787788
Memory mem = sketch.getMemory();
788789
assertTrue(mem.isAlive());
789790
assertFalse(mem.isDirect()); //now on heap.
791+
assertTrue(wmemCopy.isDirect()); //original copy
790792
assertFalse(wmem.isAlive()); //wmem closed by MemoryRequestServer
793+
assertFalse(wmemCopy.isAlive()); //original copy closed
791794
}
792795

793796
@Test
@@ -796,6 +799,7 @@ public void checkReadOnlyRebuildResize() {
796799
int u = 2 * k;
797800
int bytes = Sketches.getMaxUpdateSketchBytes(k);
798801
WritableMemory wmem = WritableMemory.allocateDirect(bytes/2); //will request more memory
802+
WritableMemory wmemCopy = wmem;
799803
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
800804
for (int i = 0; i < u; i++) { sketch.update(i); }
801805
double est1 = sketch.getEstimate();
@@ -808,6 +812,8 @@ public void checkReadOnlyRebuildResize() {
808812
assertTrue(mem2.isAlive());
809813
assertFalse(mem2.isDirect()); //now on heap
810814
assertFalse(wmem.isAlive()); //wmem closed by MemoryRequestServer
815+
assertTrue(wmemCopy.isDirect());
816+
assertFalse(wmemCopy.isAlive());
811817
try {
812818
roSketch.rebuild();
813819
fail();

src/test/java/org/apache/datasketches/theta/UnionImplTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ public void checkMoveAndResizeOffHeap() {
193193
final int bytes = Sketches.getMaxUpdateSketchBytes(k);
194194
WritableMemory wmem = WritableMemory.allocateDirect(bytes / 2); //not really used, except as a reference.
195195
WritableMemory wmem2 = WritableMemory.allocateDirect(bytes / 2); //too small, forces new allocation on heap
196+
WritableMemory wmem2Copy = wmem2;
196197
final UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
197198
assertTrue(sketch.isSameResource(wmem)); //also testing the isSameResource function
198199

@@ -206,6 +207,8 @@ public void checkMoveAndResizeOffHeap() {
206207
assertFalse(union2.isSameResource(wmem2)); //obviously not
207208
wmem.close(); //empty, but we must close it anyway.
208209
assertFalse(wmem2.isAlive());//previously closed via the DefaultMemoryRequestServer.
210+
assertTrue(wmem2Copy.isDirect());
211+
assertFalse(wmem2Copy.isAlive());
209212
}
210213

211214
@Test

0 commit comments

Comments
 (0)