-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#11628] YSQL: Implementing Async Flush for COPY command.
Summary: Currently, as part of any statement, YSQL does some processing and buffers writes. The write buffer is flushed once either of the below conditions is hit - (1) the write buffer is full (i.e., hits ysql_session_max_batch_size limit) (2) a read op is required On a flush, YSQL directs the writes to required tablet servers in different rpcs (all issued in parallel). Only once responses to all RPCs are received, the YSQL backend makes further progress. This waiting behaviour affects performance of bulk loading using COPY FROM because YSQL spends a lot of time waiting for responses. It would be ideal to use that wait time for reading further tuples from the input source and perform necessary processing. In this diff, we are adding some asynchrony to the flush to allow the YSQL's COPY FROM to read more tuples after sending a set of rpcs to tablet servers (without waiting for the responses). This is done by storing the flush future and not waiting for its result immediately. Only when YSQL refills its write buffer, it will wait for the earlier flush's result just before performing the next flush call. Note that the right choice of ysql_session_max_batch_size is required to help us mask almost all of the wait time. The optimal batch size is one in which both of the following tasks (which will run simultaneously after this diff) take almost the same time - (1) YSQL fetching and buffering ysql_session_max_batch_size rows (2) Sending rpcs for the previous ysql_session_max_batch_size rows and arrival of responses from the tserver Note also that there might not be any value of ysql_session_max_batch_size for which both tasks complete at roughly the same time. This could be due to the inherently different speeds of disk reading and tablet servers' performance. Test Plan: Tested manually locally and on portal clusters. Experiments show that there is generally a 20-25% increase in speed when using async flush versus using regular flushing. Reviewers: kannan, smishra, pjain Reviewed By: pjain Subscribers: mtakahara, zyu, lnguyen, yql Differential Revision: https://phabricator.dev.yugabyte.com/D15757
- Loading branch information
1 parent
398a2b1
commit 1a3a344
Showing
25 changed files
with
297 additions
and
97 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
java/yb-pgsql/src/test/java/org/yb/pgsql/TestAsyncFlush.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,57 @@ | ||
package org.yb.pgsql; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.sql.Statement; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.yb.client.TestUtils; | ||
import org.yb.util.YBTestRunnerNonTsanOnly; | ||
|
||
@RunWith(value = YBTestRunnerNonTsanOnly.class) | ||
public class TestAsyncFlush extends BasePgSQLTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(TestAsyncFlush.class); | ||
|
||
private void createDataFile(String absFilePath, int totalLines) throws IOException { | ||
File file = new File(absFilePath); | ||
file.createNewFile(); | ||
|
||
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | ||
writer.write("a,b,c\n"); | ||
|
||
for (int i = 0; i < totalLines; i++) { | ||
writer.write(i+","+i+","+i+"\n"); | ||
} | ||
writer.close(); | ||
} | ||
|
||
@Test | ||
public void testCopyWithAsyncFlush() throws Exception { | ||
String absFilePath = TestUtils.getBaseTmpDir() + "/copy-async-flush.txt"; | ||
String tableName = "copyAsyncFlush"; | ||
int totalLines = 100000; | ||
|
||
createDataFile(absFilePath, totalLines); | ||
|
||
try (Statement statement = connection.createStatement()) { | ||
statement.execute(String.format("CREATE TABLE %s (a int PRIMARY KEY, b int, c int)", | ||
tableName)); | ||
statement.execute(String.format( | ||
"COPY %s FROM \'%s\' WITH (FORMAT CSV, HEADER)", | ||
tableName, absFilePath)); | ||
|
||
// Verify row count. | ||
assertOneRow(statement, "SELECT COUNT(*) FROM " + tableName, totalLines); | ||
|
||
// Verify specific rows are present. | ||
assertOneRow(statement, "SELECT * FROM " + tableName + " WHERE a=0", 0, 0, 0); | ||
assertOneRow(statement, "SELECT * FROM " + tableName + " WHERE a=50000", 50000, 50000, 50000); | ||
assertOneRow(statement, "SELECT * FROM " + tableName + " WHERE a=99999", 99999, 99999, 99999); | ||
} | ||
} | ||
} |
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
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.