phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdsi...@apache.org
Subject phoenix git commit: PHOENIX-3027 Upserting rows to a table with a mutable index using a tenant specific connection fails
Date Tue, 28 Jun 2016 18:48:34 GMT
Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-0.98 5626308be -> 05fa65b60


PHOENIX-3027 Upserting rows to a table with a mutable index using a tenant specific connection
fails


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

Branch: refs/heads/4.x-HBase-0.98
Commit: 05fa65b6022c75b4d8e23ec84c32ad2abdedf371
Parents: 5626308
Author: Thomas D'Silva <tdsilva@salesforce.com>
Authored: Fri Jun 24 16:46:52 2016 -0700
Committer: Thomas D'Silva <tdsilva@salesforce.com>
Committed: Tue Jun 28 11:48:26 2016 -0700

----------------------------------------------------------------------
 .../phoenix/end2end/index/MutableIndexIT.java   | 37 ++++++++++++++++++++
 .../apache/phoenix/execute/MutationState.java   | 18 +++++++---
 2 files changed, 50 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/05fa65b6/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java
index 997d3dc..15f6037 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java
@@ -51,6 +51,7 @@ import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.schema.PTableKey;
 import org.apache.phoenix.util.ByteUtil;
+import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.PropertiesUtil;
 import org.apache.phoenix.util.QueryUtil;
 import org.apache.phoenix.util.ReadOnlyProps;
@@ -93,6 +94,8 @@ public class MutableIndexIT extends BaseHBaseManagedTimeIT {
     public static void doSetup() throws Exception {
         Map<String,String> props = Maps.newHashMapWithExpectedSize(1);
         props.put(QueryServices.TRANSACTIONS_ENABLED, Boolean.toString(true));
+        // Forces server cache to be used
+        props.put(QueryServices.INDEX_MUTATE_BATCH_SIZE_THRESHOLD_ATTRIB, Integer.toString(1));
         setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
     }
 	
@@ -823,4 +826,38 @@ public class MutableIndexIT extends BaseHBaseManagedTimeIT {
                         + (tableDDLOptions!=null?tableDDLOptions:"") + (splits != null ?
(" split on " + splits) : "");
         conn.createStatement().execute(ddl);
     }
+    
+  @Test
+  public void testTenantSpecificConnection() throws Exception {
+      String tableName = TestUtil.DEFAULT_DATA_TABLE_NAME + "_" + System.currentTimeMillis();
+      String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
+      Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+      try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+          conn.setAutoCommit(false);
+          // create data table
+          conn.createStatement().execute(
+              "CREATE TABLE IF NOT EXISTS " + fullTableName + 
+              "(TENANT_ID CHAR(15) NOT NULL,"+
+              "TYPE VARCHAR(25) NOT NULL,"+
+              "ENTITY_ID CHAR(15) NOT NULL,"+
+              "CONSTRAINT PK_CONSTRAINT PRIMARY KEY (TENANT_ID, TYPE, ENTITY_ID)) MULTI_TENANT=TRUE
"
+              + (!tableDDLOptions.isEmpty() ? "," + tableDDLOptions : "") );
+          // create index
+          conn.createStatement().execute("CREATE INDEX IF NOT EXISTS " + indexName + " ON
" + fullTableName + " (ENTITY_ID, TYPE)");
+          
+          // upsert rows
+          String dml = "UPSERT INTO " + fullTableName + " (ENTITY_ID, TYPE) VALUES ( ?, ?)";
+          props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "tenant1");
+          // connection is tenant-specific
+          try (Connection tenantConn = DriverManager.getConnection(getUrl(), props)) {
+              for (int i=0; i<2; ++i) {
+                  PreparedStatement stmt = tenantConn.prepareStatement(dml);
+                  stmt.setString(1, "00000000000000" + String.valueOf(i));
+                  stmt.setString(2, String.valueOf(i));
+                  assertEquals(1,stmt.executeUpdate());
+              }
+              tenantConn.commit();
+          }
+      }
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/05fa65b6/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java b/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java
index 0a279b6..ca026f5 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java
@@ -85,6 +85,7 @@ import org.apache.phoenix.util.LogUtil;
 import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.SQLCloseable;
 import org.apache.phoenix.util.SQLCloseables;
+import org.apache.phoenix.util.ScanUtil;
 import org.apache.phoenix.util.ServerUtil;
 import org.apache.phoenix.util.TransactionUtil;
 import org.apache.tephra.Transaction;
@@ -98,10 +99,8 @@ import org.apache.tephra.TransactionSystemClient;
 import org.apache.tephra.hbase.TransactionAwareHTable;
 import org.apache.tephra.visibility.FenceWait;
 import org.apache.tephra.visibility.VisibilityFence;
-
 import org.cloudera.htrace.Span;
 import org.cloudera.htrace.TraceScope;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -1054,7 +1053,16 @@ public class MutationState implements SQLCloseable {
     private ServerCache setMetaDataOnMutations(TableRef tableRef, List<? extends Mutation>
mutations,
             ImmutableBytesWritable indexMetaDataPtr) throws SQLException {
         PTable table = tableRef.getTable();
-        byte[] tenantId = connection.getTenantId() == null ? null : connection.getTenantId().getBytes();
+        final byte[] tenantIdBytes;
+        if(table.isMultiTenant()) {
+            tenantIdBytes = connection.getTenantId() == null ? null :
+                    ScanUtil.getTenantIdBytes(
+                            table.getRowKeySchema(),
+                            table.getBucketNum() != null,
+                            connection.getTenantId(), table.getViewIndexId() != null);
+        } else {
+            tenantIdBytes = connection.getTenantId() == null ? null : connection.getTenantId().getBytes();
+        }
         ServerCache cache = null;
         byte[] attribValue = null;
         byte[] uuidValue = null;
@@ -1078,8 +1086,8 @@ public class MutationState implements SQLCloseable {
         // Either set the UUID to be able to access the index metadata from the cache
         // or set the index metadata directly on the Mutation
         for (Mutation mutation : mutations) {
-            if (tenantId != null) {
-                mutation.setAttribute(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
+            if (connection.getTenantId() != null) {
+                mutation.setAttribute(PhoenixRuntime.TENANT_ID_ATTRIB, tenantIdBytes);
             }
             mutation.setAttribute(PhoenixIndexCodec.INDEX_UUID, uuidValue);
             if (attribValue != null) {


Mime
View raw message