Skip to content

Commit

Permalink
[YSQL][#417] Adding TServer GFlag to Dictate the Cache Value for Sequ…
Browse files Browse the repository at this point in the history
…ences

Summary:
The logic for setting sequence cache size has changed to allow values to be set using the cache flag passed in from TServer Gflag.
Originally only cache option passed in from the sequence query was used to set the cache size eg. `CREATE SEQUENCE s1 CACHE 10`.

With this change, maximum value will be chosen between the cache flag and cache option only if the maximum value is less than or equal to
 the total number of elements there exists in the sequence `abs((sequence max - sequence min) / sequence increment by)`.
If the calculated cache size exceeds the total elements available, total elements will be used as cache size.

The default cache flag is currently set to 100.

In order to turn off the cache flag, the flag should be set to 0. This will fall back to the original behaviour where only cache option is used to determine the cache size.

Note when cache flag value is reset by restarting the cluster, previously created sequences will continue to use its initial cache size until the end of its life time.
New cache size will only apply to new sequences created in the restarted cluster.

ALTER SEQUENCE dynamically changes the cache size when the size of total elements changes.

This feature is to support serial pseudo-type and reduce RPC calls when generating values by allowing higher cache size to be configured at TServer level.

Eg.
```
./bin/yb-ctl create "ysql_sequence_cache_minval=1000"
CREATE SEQUENCE s1;
```
The sequence s1 above will use cache size of 1000.

Test Plan:
- Added separate sequence test with the gflag
- Tested existing regression tests related to sequences by turning off the gflag

Reviewers: mihnea, hector

Reviewed By: hector

Subscribers: yql

Differential Revision: https://phabricator.dev.yugabyte.com/D9521
  • Loading branch information
emhna committed Oct 8, 2020
1 parent 146545a commit 6d29fc6
Show file tree
Hide file tree
Showing 10 changed files with 1,353 additions and 0 deletions.
12 changes: 12 additions & 0 deletions java/yb-pgsql/src/test/java/org/yb/pgsql/TestPgRegressFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
package org.yb.pgsql;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.yb.util.YBTestRunnerNonTsanOnly;
Expand All @@ -21,11 +23,21 @@
*/
@RunWith(value=YBTestRunnerNonTsanOnly.class)
public class TestPgRegressFeature extends BasePgSQLTest {

private static final int TURN_OFF_SEQUENCE_CACHE_FLAG = 0;

@Override
public int getTestMethodTimeoutSec() {
return 1800;
}

@Override
protected Map<String, String> getTServerFlags() {
Map<String, String> flagMap = super.getTServerFlags();
flagMap.put("ysql_sequence_cache_minval", Integer.toString(TURN_OFF_SEQUENCE_CACHE_FLAG));
return flagMap;
}

@Test
public void testPgRegressFeature() throws Exception {
runPgRegressTest("yb_feature_serial_schedule");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
package org.yb.pgsql;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.yb.util.YBTestRunnerNonTsanOnly;
Expand All @@ -21,11 +23,21 @@
*/
@RunWith(value=YBTestRunnerNonTsanOnly.class)
public class TestPgRegressPgMiscIndependent extends BasePgSQLTest {

private static final int TURN_OFF_SEQUENCE_CACHE_FLAG = 0;

@Override
public int getTestMethodTimeoutSec() {
return 1800;
}

@Override
protected Map<String, String> getTServerFlags() {
Map<String, String> flagMap = super.getTServerFlags();
flagMap.put("ysql_sequence_cache_minval", Integer.toString(TURN_OFF_SEQUENCE_CACHE_FLAG));
return flagMap;
}

@Test
public void testPgRegressPgMiscIndependent() throws Exception {
runPgRegressTest("yb_pg_misc_independent_serial_schedule");
Expand Down
10 changes: 10 additions & 0 deletions java/yb-pgsql/src/test/java/org/yb/pgsql/TestPgSequences.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
public class TestPgSequences extends BasePgSQLTest {
private static final Logger LOG = LoggerFactory.getLogger(TestPgSequences.class);

private static final int TURN_OFF_SEQUENCE_CACHE_FLAG = 0;

@Override
protected Map<String, String> getTServerFlags() {
Map<String, String> flagMap = super.getTServerFlags();
flagMap.put("ysql_sequence_cache_minval", Integer.toString(TURN_OFF_SEQUENCE_CACHE_FLAG));
return flagMap;
}

@After
public void deleteSequences() throws Exception {
if (connection == null) {
Expand Down Expand Up @@ -189,6 +198,7 @@ public void testSequenceWithMaxValueAndCache() throws Exception {

try (Connection connection2 = getConnectionBuilder().connect();
Statement statement = connection2.createStatement()) {
statement.executeQuery("SELECT nextval('s1')");
// Since the previous client already got all the available sequence numbers in its cache,
// we should get an error when we request another sequence number from another client.
thrown.expect(org.postgresql.util.PSQLException.class);
Expand Down
Loading

0 comments on commit 6d29fc6

Please sign in to comment.