-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opt](rowcount) refresh external table's rowcount async
- Loading branch information
1 parent
50815b1
commit b5cded1
Showing
3 changed files
with
94 additions
and
5 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
83 changes: 83 additions & 0 deletions
83
fe/fe-core/src/test/java/org/apache/doris/datasource/ExternalRowCountCacheTest.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,83 @@ | ||
package org.apache.doris.datasource; | ||
|
||
import org.apache.doris.datasource.ExternalRowCountCache.RowCountKey; | ||
import org.apache.doris.statistics.BasicAsyncCacheLoader; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class ExternalRowCountCacheTest { | ||
private ExternalRowCountCache cache; | ||
private ExecutorService executorService; | ||
|
||
public static class TestLoader extends BasicAsyncCacheLoader<RowCountKey, Optional<Long>> { | ||
|
||
private AtomicLong incr = new AtomicLong(333); | ||
|
||
@Override | ||
protected Optional<Long> doLoad(RowCountKey rowCountKey) { | ||
if (rowCountKey.getTableId() == 1) { | ||
return Optional.of(111L); | ||
} else if (rowCountKey.getTableId() == 2) { | ||
return Optional.of(222L); | ||
} else if (rowCountKey.getTableId() == 3) { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
System.out.println("load: " + incr.get()); | ||
return Optional.of(incr.incrementAndGet()); | ||
} | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
executorService = Executors.newFixedThreadPool(2); | ||
cache = new ExternalRowCountCache(executorService, 2, new TestLoader()); | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
// table 1 | ||
long rowCount = cache.getCachedRowCount(1, 1, 1); | ||
Assertions.assertEquals(0, rowCount); | ||
Thread.sleep(1000); | ||
rowCount = cache.getCachedRowCount(1, 1, 1); | ||
Assertions.assertEquals(111, rowCount); | ||
|
||
// table 2 | ||
rowCount = cache.getCachedRowCount(1, 1, 2); | ||
Assertions.assertEquals(0, rowCount); | ||
Thread.sleep(1000); | ||
rowCount = cache.getCachedRowCount(1, 1, 2); | ||
Assertions.assertEquals(222, rowCount); | ||
|
||
// table 3 | ||
rowCount = cache.getCachedRowCount(1, 1, 3); | ||
// first get, it should be 0 because the loader is async | ||
Assertions.assertEquals(0, rowCount); | ||
// After sleep 2 sec and then get, it should be 1 | ||
Thread.sleep(2000); | ||
rowCount = cache.getCachedRowCount(1, 1, 3); | ||
Assertions.assertEquals(334, rowCount); | ||
// sleep 3 sec to trigger refresh | ||
Thread.sleep(3000); | ||
rowCount = cache.getCachedRowCount(1, 1, 3); | ||
// the refresh will be triggered only when query it, so it should still be 1 | ||
Assertions.assertEquals(334, rowCount); | ||
// sleep 2 sec to wait for the doLoad | ||
Thread.sleep(2000); | ||
rowCount = cache.getCachedRowCount(1, 1, 3); | ||
// refresh done, value should be 2 | ||
Assertions.assertEquals(335, rowCount); | ||
} | ||
} |