Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.0 e9a160d09 -> 528a2b2bc


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/528a2b2b
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/528a2b2b
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/528a2b2b

Branch: refs/heads/4.x-HBase-1.0
Commit: 528a2b2bc4a767e39cc8e29539749c385ab23806
Parents: e9a160d
Author: Thomas D'Silva <[email protected]>
Authored: Fri Jun 24 16:46:52 2016 -0700
Committer: Thomas D'Silva <[email protected]>
Committed: Tue Jun 28 11:48:50 2016 -0700

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


http://git-wip-us.apache.org/repos/asf/phoenix/blob/528a2b2b/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 1434b6c..fada2da 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
@@ -50,6 +50,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;
@@ -92,6 +93,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()));
     }
        
@@ -803,4 +806,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/528a2b2b/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 52d2f64..bf47ab6 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
@@ -87,6 +87,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;
@@ -1053,7 +1054,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;
@@ -1077,8 +1087,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) {

Reply via email to