PHOENIX-4772 phoenix.sequence.saltBuckets is not honoured

Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/abcf0d1a
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/abcf0d1a
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/abcf0d1a

Branch: refs/heads/master
Commit: abcf0d1ab3a85f8aa9bc5f5f5d54f6b229cfa247
Parents: 1b18d34
Author: Ankit Singhal <ankitsingha...@gmail.com>
Authored: Thu Jun 7 11:02:55 2018 -0700
Committer: Ankit Singhal <ankitsingha...@gmail.com>
Committed: Thu Jun 7 11:02:55 2018 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/SequenceIT.java  |  57 +---------
 .../phoenix/end2end/SequencePointInTimeIT.java  | 112 +++++++++++++++++++
 .../query/ConnectionQueryServicesImpl.java      |  13 ++-
 .../query/ConnectionlessQueryServicesImpl.java  |   8 +-
 .../apache/phoenix/query/QueryConstants.java    |   2 +
 .../org/apache/phoenix/schema/Sequence.java     |   6 +-
 6 files changed, 139 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/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 4cc9628..b76cc4e 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
@@ -18,6 +18,8 @@
 
 package org.apache.phoenix.end2end;
 
+import static 
org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA;
+import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.TYPE_SEQUENCE;
 import static 
org.apache.phoenix.query.QueryServicesTestImpl.DEFAULT_SEQUENCE_CACHE_SIZE;
 import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
 import static org.junit.Assert.assertEquals;
@@ -38,6 +40,7 @@ import org.apache.phoenix.exception.SQLExceptionCode;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
 import org.apache.phoenix.jdbc.PhoenixStatement;
 import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesTestImpl;
 import org.apache.phoenix.schema.SchemaNotFoundException;
 import org.apache.phoenix.schema.SequenceAlreadyExistsException;
 import org.apache.phoenix.schema.SequenceNotFoundException;
@@ -202,6 +205,8 @@ public class SequenceIT extends ParallelStatsDisabledIT {
         String schemaName = getSchemaName(sequenceName);
         
         conn.createStatement().execute("CREATE SEQUENCE " + sequenceName + " 
START WITH 2 INCREMENT BY 4");
+        int bucketNum = PhoenixRuntime.getTableNoCache(conn, 
SYSTEM_CATALOG_SCHEMA + "." + TYPE_SEQUENCE).getBucketNum();
+        assertEquals("Salt bucket for SYSTEM.SEQUENCE should be test 
default",bucketNum , QueryServicesTestImpl.DEFAULT_SEQUENCE_TABLE_SALT_BUCKETS);
         String query = "SELECT sequence_schema, sequence_name, current_value, 
increment_by FROM \"SYSTEM\".\"SEQUENCE\" WHERE sequence_name='" + 
sequenceNameWithoutSchema + "'";
         ResultSet rs = conn.prepareStatement(query).executeQuery();
         assertTrue(rs.next());
@@ -1406,56 +1411,4 @@ public class SequenceIT extends ParallelStatsDisabledIT {
        return tableName.substring(tableName.indexOf(".") + 1, 
tableName.length());
     }    
 
-    @Test
-    public void testPointInTimeSequence() throws Exception {
-        String seqName = generateSequenceNameWithSchema();     
-        Properties scnProps = PropertiesUtil.deepCopy(TEST_PROPERTIES);
-        scnProps.put(PhoenixRuntime.CURRENT_SCN_ATTRIB, 
Long.toString(EnvironmentEdgeManager.currentTimeMillis()));
-        Connection beforeSeqConn = DriverManager.getConnection(getUrl(), 
scnProps);
-
-        ResultSet rs;
-        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
-        Connection conn = DriverManager.getConnection(getUrl(), props);
-        conn.createStatement().execute("CREATE SEQUENCE " + seqName + "");
-
-        try {
-            beforeSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
-            fail();
-        } catch (SequenceNotFoundException e) {
-            beforeSeqConn.close();
-        }
-
-        scnProps.put(PhoenixRuntime.CURRENT_SCN_ATTRIB, 
Long.toString(EnvironmentEdgeManager.currentTimeMillis()));
-        Connection afterSeqConn = DriverManager.getConnection(getUrl(), 
scnProps);
-
-        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
-        assertTrue(rs.next());
-        assertEquals(1, rs.getInt(1));
-        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
-        assertTrue(rs.next());
-        assertEquals(2, rs.getInt(1));
-
-        conn.createStatement().execute("DROP SEQUENCE " + seqName + "");
-        
-        rs = afterSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
-        assertTrue(rs.next());
-        assertEquals(3, rs.getInt(1));
-
-        try {
-            rs = conn.createStatement().executeQuery("SELECT next value for " 
+ seqName);
-            fail();
-        } catch (SequenceNotFoundException e) { // expected
-        }
-
-        conn.createStatement().execute("CREATE SEQUENCE " + seqName);
-        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
-        assertTrue(rs.next());
-        assertEquals(1, rs.getInt(1));
-
-        rs = afterSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
-        assertTrue(rs.next());
-        assertEquals(4, rs.getInt(1));
-        afterSeqConn.close();
-    }
-
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequencePointInTimeIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequencePointInTimeIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequencePointInTimeIT.java
new file mode 100644
index 0000000..3608901
--- /dev/null
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequencePointInTimeIT.java
@@ -0,0 +1,112 @@
+/*
+ * 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.phoenix.end2end;
+
+import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.schema.SequenceNotFoundException;
+import org.apache.phoenix.util.EnvironmentEdgeManager;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.apache.phoenix.util.PropertiesUtil;
+import org.apache.phoenix.util.ReadOnlyProps;
+import org.apache.phoenix.util.SchemaUtil;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.google.common.collect.Maps;
+
+
+public class SequencePointInTimeIT extends BaseUniqueNamesOwnClusterIT {
+    private static final String SCHEMA_NAME = "S";
+
+    private static String generateSequenceNameWithSchema() {
+        return SchemaUtil.getTableName(SCHEMA_NAME, 
generateUniqueSequenceName());
+    }
+    @BeforeClass
+    public static void doSetup() throws Exception {
+        Map<String,String> props = Maps.newHashMapWithExpectedSize(5);
+        // Must update config before starting server
+        props.put(QueryServices.DEFAULT_SYSTEM_KEEP_DELETED_CELLS_ATTRIB, 
Boolean.TRUE.toString());
+        props.put(QueryServices.DEFAULT_SYSTEM_MAX_VERSIONS_ATTRIB, "5");
+        setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
+    }
+
+    @Test
+    public void testPointInTimeSequence() throws Exception {
+        String seqName = generateSequenceNameWithSchema();
+        Properties scnProps = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        scnProps.put(PhoenixRuntime.CURRENT_SCN_ATTRIB, 
Long.toString(EnvironmentEdgeManager.currentTimeMillis()));
+        Connection beforeSeqConn = DriverManager.getConnection(getUrl(), 
scnProps);
+
+        ResultSet rs;
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        conn.createStatement().execute("CREATE SEQUENCE " + seqName + "");
+
+        try {
+            beforeSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
+            fail();
+        } catch (SequenceNotFoundException e) {
+            beforeSeqConn.close();
+        }
+
+        scnProps.put(PhoenixRuntime.CURRENT_SCN_ATTRIB, 
Long.toString(EnvironmentEdgeManager.currentTimeMillis()));
+        Connection afterSeqConn = DriverManager.getConnection(getUrl(), 
scnProps);
+
+        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
+        assertTrue(rs.next());
+        assertEquals(1, rs.getInt(1));
+        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
+        assertTrue(rs.next());
+        assertEquals(2, rs.getInt(1));
+
+        conn.createStatement().execute("DROP SEQUENCE " + seqName + "");
+
+        rs = afterSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
+        assertTrue(rs.next());
+        assertEquals(3, rs.getInt(1));
+
+        try {
+            rs = conn.createStatement().executeQuery("SELECT next value for " 
+ seqName);
+            fail();
+        } catch (SequenceNotFoundException e) { // expected
+        }
+
+        conn.createStatement().execute("CREATE SEQUENCE " + seqName);
+        rs = conn.createStatement().executeQuery("SELECT next value for " + 
seqName);
+        assertTrue(rs.next());
+        assertEquals(1, rs.getInt(1));
+
+        rs = afterSeqConn.createStatement().executeQuery("SELECT next value 
for " + seqName);
+        assertTrue(rs.next());
+        assertEquals(4, rs.getInt(1));
+        afterSeqConn.close();
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
index 32333eb..f1ab653 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java
@@ -2497,6 +2497,11 @@ public class ConnectionQueryServicesImpl extends 
DelegateQueryServices implement
         return setSystemDDLProperties(QueryConstants.CREATE_TABLE_METADATA);
     }
 
+    protected String getSystemSequenceTableDDL(int nSaltBuckets) {
+        String schema = 
String.format(setSystemDDLProperties(QueryConstants.CREATE_SEQUENCE_METADATA));
+        return Sequence.getCreateTableStatement(schema, nSaltBuckets);
+    }
+
     // Available for testing
     protected String getFunctionTableDDL() {
         return setSystemDDLProperties(QueryConstants.CREATE_FUNCTION_METADATA);
@@ -2707,7 +2712,11 @@ public class ConnectionQueryServicesImpl extends 
DelegateQueryServices implement
 
     private void createOtherSystemTables(PhoenixConnection metaConnection, 
Admin hbaseAdmin) throws SQLException, IOException {
         try {
-            
metaConnection.createStatement().execute(QueryConstants.CREATE_SEQUENCE_METADATA);
+
+            nSequenceSaltBuckets = 
ConnectionQueryServicesImpl.this.props.getInt(
+                    QueryServices.SEQUENCE_SALT_BUCKETS_ATTRIB,
+                    QueryServicesOptions.DEFAULT_SEQUENCE_TABLE_SALT_BUCKETS);
+            
metaConnection.createStatement().execute(getSystemSequenceTableDDL(nSequenceSaltBuckets));
         } catch (TableAlreadyExistsException e) {
             nSequenceSaltBuckets = getSaltBuckets(e);
         }
@@ -3084,7 +3093,7 @@ public class ConnectionQueryServicesImpl extends 
DelegateQueryServices implement
                     QueryServices.SEQUENCE_SALT_BUCKETS_ATTRIB,
                     QueryServicesOptions.DEFAULT_SEQUENCE_TABLE_SALT_BUCKETS);
             try {
-                String createSequenceTable = 
Sequence.getCreateTableStatement(nSaltBuckets);
+                String createSequenceTable = 
getSystemSequenceTableDDL(nSaltBuckets);
                 
metaConnection.createStatement().executeUpdate(createSequenceTable);
                 nSequenceSaltBuckets = nSaltBuckets;
             } catch (NewerTableAlreadyExistsException e) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
index 5b17565..9b30616 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionlessQueryServicesImpl.java
@@ -40,7 +40,6 @@ import org.apache.hadoop.hbase.client.Mutation;
 import org.apache.hadoop.hbase.client.RegionInfo;
 import org.apache.hadoop.hbase.client.RegionInfoBuilder;
 import org.apache.hadoop.hbase.client.Table;
-import org.apache.hadoop.hbase.client.TableDescriptor;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.security.User;
 import org.apache.hadoop.hbase.util.Addressing;
@@ -161,6 +160,11 @@ public class ConnectionlessQueryServicesImpl extends 
DelegateQueryServices imple
         return setSystemDDLProperties(QueryConstants.CREATE_TABLE_METADATA);
     }
 
+    protected String getSystemSequenceTableDDL(int nSaltBuckets) {
+        String schema = 
String.format(setSystemDDLProperties(QueryConstants.CREATE_SEQUENCE_METADATA));
+        return Sequence.getCreateTableStatement(schema, nSaltBuckets);
+    }
+
     protected String getFunctionTableDDL() {
         return setSystemDDLProperties(QueryConstants.CREATE_FUNCTION_METADATA);
     }
@@ -347,7 +351,7 @@ public class ConnectionlessQueryServicesImpl extends 
DelegateQueryServices imple
                 }
                 try {
                     int nSaltBuckets = getSequenceSaltBuckets();
-                    String createTableStatement = 
Sequence.getCreateTableStatement(nSaltBuckets);
+                    String createTableStatement = 
getSystemSequenceTableDDL(nSaltBuckets);
                    
metaConnection.createStatement().executeUpdate(createTableStatement);
                 } catch (NewerTableAlreadyExistsException ignore) {
                     // Ignore, as this will happen if the SYSTEM.SEQUENCE 
already exists at this fixed timestamp.

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java 
b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
index 13a2a44..ad5afd8 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java
@@ -280,6 +280,8 @@ public interface QueryConstants {
             CYCLE_FLAG + " BOOLEAN, \n" +
             LIMIT_REACHED_FLAG + " BOOLEAN \n" +
             " CONSTRAINT " + SYSTEM_TABLE_PK_NAME + " PRIMARY KEY (" + 
TENANT_ID + "," + SEQUENCE_SCHEMA + "," + SEQUENCE_NAME + "))\n" +
+            HConstants.VERSIONS + "=%s,\n" +
+            ColumnFamilyDescriptorBuilder.KEEP_DELETED_CELLS + "=%s,\n"+
             PhoenixDatabaseMetaData.TRANSACTIONAL + "=" + Boolean.FALSE;
     public static final String CREATE_SYSTEM_SCHEMA = "CREATE SCHEMA " + 
SYSTEM_CATALOG_SCHEMA;
     public static final String UPGRADE_TABLE_SNAPSHOT_PREFIX = 
"_UPGRADING_TABLE_";

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abcf0d1a/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 665ac39..e313b92 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
@@ -626,10 +626,10 @@ public class Sequence {
             .build().buildException();
     }
 
-    public static String getCreateTableStatement(int nSaltBuckets) {
+    public static String getCreateTableStatement(String schema, int 
nSaltBuckets) {
         if (nSaltBuckets <= 0) {
-            return QueryConstants.CREATE_SEQUENCE_METADATA;
+            return schema;
         }
-        return QueryConstants.CREATE_SEQUENCE_METADATA + "," + 
PhoenixDatabaseMetaData.SALT_BUCKETS + "=" + nSaltBuckets;
+        return schema + "," + PhoenixDatabaseMetaData.SALT_BUCKETS + "=" + 
nSaltBuckets;
     }
 }

Reply via email to