PHOENIX-2149 MAX Value of Sequences not honored when closing Connection between calls to NEXT VALUE FOR (Jan Fernando)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/58f0236f Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/58f0236f Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/58f0236f Branch: refs/heads/4.5-HBase-1.0 Commit: 58f0236fca6eed43c2aa15d020da202555ebb698 Parents: bbed18f Author: Thomas D'Silva <tdsi...@salesforce.com> Authored: Mon Jul 27 13:15:17 2015 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Tue Aug 11 18:42:11 2015 -0700 ---------------------------------------------------------------------- .../org/apache/phoenix/end2end/SequenceIT.java | 33 ++++++++++++++++++++ .../org/apache/phoenix/schema/Sequence.java | 3 +- 2 files changed, 34 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/58f0236f/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java index 4273022..78f8132 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java @@ -54,6 +54,7 @@ import com.google.common.collect.Lists; public class SequenceIT extends BaseClientManagedTimeIT { private static final String NEXT_VAL_SQL = "SELECT NEXT VALUE FOR foo.bar FROM SYSTEM.\"SEQUENCE\""; + private static final String SELECT_NEXT_VALUE_SQL = "SELECT NEXT VALUE FOR %s FROM SYSTEM.\"SEQUENCE\""; private static final long BATCH_SIZE = 3; private Connection conn; @@ -1147,6 +1148,38 @@ public class SequenceIT extends BaseClientManagedTimeIT { assertEquals(1, rs.getLong("metric_val")); assertFalse(rs.next()); } + + @Test + /** + * Test to validate that the bug discovered in PHOENIX-2149 has been fixed. There was an issue + * whereby, when closing connections and returning sequences we were not setting the limit + * reached flag correctly and this was causing the max value to be ignored as the LIMIT_REACHED_FLAG + * value was being unset from true to false. + */ + public void testNextValuesForSequenceClosingConnections() throws Exception { + + // Create Sequence + nextConnection(); + conn.createStatement().execute("CREATE SEQUENCE seqtest.closeconn START WITH 4990 MINVALUE 4990 MAXVALUE 5000 CACHE 10"); + nextConnection(); + + // Call NEXT VALUE FOR 1 time more than available values in the Sequence. We expected the final time + // to throw an error as we will have reached the max value + try { + long val = 0L; + for (int i = 0; i <= 11; i++) { + ResultSet rs = conn.createStatement().executeQuery(String.format(SELECT_NEXT_VALUE_SQL, "seqtest.closeconn")); + rs.next(); + val = rs.getLong(1); + nextConnection(); + } + fail("Expect to fail as we have arrived at the max sequence value " + val); + } catch (SQLException e) { + assertEquals(SQLExceptionCode.SEQUENCE_VAL_REACHED_MAX_VALUE.getErrorCode(), + e.getErrorCode()); + assertTrue(e.getNextException() == null); + } + } private void insertEvent(long id, String userId, long val) throws SQLException { PreparedStatement stmt = conn.prepareStatement("UPSERT INTO events VALUES(?,?,?)"); http://git-wip-us.apache.org/repos/asf/phoenix/blob/58f0236f/phoenix-core/src/main/java/org/apache/phoenix/schema/Sequence.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/Sequence.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/Sequence.java index adca5e8..a2041f7 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/Sequence.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/Sequence.java @@ -302,8 +302,7 @@ public class Sequence { Map<byte[], List<Cell>> familyMap = append.getFamilyCellMap(); familyMap.put(PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, Arrays.<Cell>asList( (Cell)KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, value.timestamp, PLong.INSTANCE.toBytes(value.currentValue)), - // set LIMIT_REACHED flag to false since we are returning unused sequence values - (Cell)KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG_BYTES, value.timestamp, PDataType.FALSE_BYTES) + (Cell)KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG_BYTES, value.timestamp, PBoolean.INSTANCE.toBytes(value.limitReached)) )); return append; }