-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VL] IndicatorVectorPool to avoid sharing native columnar batches' ow…
…nerships among runtime instances (#6293)
- Loading branch information
1 parent
b0d836b
commit 457898b
Showing
18 changed files
with
300 additions
and
144 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
104 changes: 104 additions & 0 deletions
104
backends-velox/src/test/java/org/apache/gluten/columnarbatch/ColumnarBatchTest.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,104 @@ | ||
/* | ||
* 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.gluten.columnarbatch; | ||
|
||
import org.apache.gluten.memory.arrow.alloc.ArrowBufferAllocators; | ||
import org.apache.gluten.test.VeloxBackendTestBase; | ||
import org.apache.gluten.vectorized.ArrowWritableColumnVector; | ||
|
||
import org.apache.spark.sql.types.StructType; | ||
import org.apache.spark.sql.vectorized.ColumnarBatch; | ||
import org.apache.spark.util.TaskResources$; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class ColumnarBatchTest extends VeloxBackendTestBase { | ||
|
||
@Test | ||
public void testOffloadAndLoad() { | ||
TaskResources$.MODULE$.runUnsafe( | ||
() -> { | ||
final int numRows = 100; | ||
final ColumnarBatch batch = newArrowBatch("a boolean, b int", numRows); | ||
Assert.assertTrue(ColumnarBatches.isHeavyBatch(batch)); | ||
final ColumnarBatch offloaded = | ||
ColumnarBatches.ensureOffloaded(ArrowBufferAllocators.contextInstance(), batch); | ||
Assert.assertTrue(ColumnarBatches.isLightBatch(offloaded)); | ||
final ColumnarBatch loaded = | ||
ColumnarBatches.ensureLoaded(ArrowBufferAllocators.contextInstance(), offloaded); | ||
Assert.assertTrue(ColumnarBatches.isHeavyBatch(loaded)); | ||
long cnt = | ||
StreamSupport.stream( | ||
Spliterators.spliteratorUnknownSize( | ||
loaded.rowIterator(), Spliterator.ORDERED), | ||
false) | ||
.count(); | ||
Assert.assertEquals(numRows, cnt); | ||
loaded.close(); | ||
return null; | ||
}); | ||
} | ||
|
||
@Test | ||
public void testCreateByHandle() { | ||
TaskResources$.MODULE$.runUnsafe( | ||
() -> { | ||
final int numRows = 100; | ||
final ColumnarBatch batch = newArrowBatch("a boolean, b int", numRows); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(batch)); | ||
final ColumnarBatch offloaded = | ||
ColumnarBatches.ensureOffloaded(ArrowBufferAllocators.contextInstance(), batch); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(offloaded)); | ||
final long handle = ColumnarBatches.getNativeHandle(offloaded); | ||
final ColumnarBatch created = ColumnarBatches.create(handle); | ||
Assert.assertEquals(handle, ColumnarBatches.getNativeHandle(created)); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(created)); | ||
ColumnarBatches.retain(created); | ||
Assert.assertEquals(2, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(2, ColumnarBatches.getRefCnt(created)); | ||
ColumnarBatches.retain(offloaded); | ||
Assert.assertEquals(3, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(3, ColumnarBatches.getRefCnt(created)); | ||
created.close(); | ||
Assert.assertEquals(2, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(2, ColumnarBatches.getRefCnt(created)); | ||
offloaded.close(); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(1, ColumnarBatches.getRefCnt(created)); | ||
created.close(); | ||
Assert.assertEquals(0, ColumnarBatches.getRefCnt(offloaded)); | ||
Assert.assertEquals(0, ColumnarBatches.getRefCnt(created)); | ||
return null; | ||
}); | ||
} | ||
|
||
private static ColumnarBatch newArrowBatch(String schema, int numRows) { | ||
final ArrowWritableColumnVector[] columns = | ||
ArrowWritableColumnVector.allocateColumns(numRows, StructType.fromDDL(schema)); | ||
for (ArrowWritableColumnVector col : columns) { | ||
col.setValueCount(numRows); | ||
} | ||
final ColumnarBatch batch = new ColumnarBatch(columns); | ||
batch.setNumRows(numRows); | ||
return batch; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
backends-velox/src/test/java/org/apache/gluten/test/VeloxBackendTestBase.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,86 @@ | ||
/* | ||
* 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.gluten.test; | ||
|
||
import org.apache.gluten.GlutenConfig; | ||
import org.apache.gluten.backendsapi.ListenerApi; | ||
import org.apache.gluten.backendsapi.velox.VeloxListenerApi; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import org.apache.spark.SparkConf; | ||
import org.apache.spark.SparkContext; | ||
import org.apache.spark.api.plugin.PluginContext; | ||
import org.apache.spark.resource.ResourceInformation; | ||
import org.junit.BeforeClass; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** For testing Velox backend without starting a Spark context. */ | ||
public abstract class VeloxBackendTestBase { | ||
@BeforeClass | ||
public static void setup() { | ||
final ListenerApi api = new VeloxListenerApi(); | ||
api.onDriverStart(mockSparkContext(), mockPluginContext()); | ||
} | ||
|
||
private static SparkContext mockSparkContext() { | ||
// Not yet implemented. | ||
return null; | ||
} | ||
|
||
private static PluginContext mockPluginContext() { | ||
return new PluginContext() { | ||
@Override | ||
public MetricRegistry metricRegistry() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SparkConf conf() { | ||
final SparkConf conf = new SparkConf(); | ||
conf.set(GlutenConfig.GLUTEN_NUM_TASK_SLOTS_PER_EXECUTOR_KEY(), "0"); | ||
return conf; | ||
} | ||
|
||
@Override | ||
public String executorID() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public String hostname() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Map<String, ResourceInformation> resources() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void send(Object message) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object ask(Object message) throws Exception { | ||
throw new UnsupportedOperationException(); | ||
} | ||
}; | ||
} | ||
} |
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
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
Oops, something went wrong.