Repository: phoenix Updated Branches: refs/heads/master f12bda4b4 -> caf4616ec
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/caf4616e Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/caf4616e Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/caf4616e Branch: refs/heads/master Commit: caf4616ec7a67897db70605c123856f5d279cfa0 Parents: f12bda4 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:47:41 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/caf4616e/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 70bfdd5..29057db 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())); } @@ -809,4 +812,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/caf4616e/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 57d9b68..0a9b0ae 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; @@ -1052,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; @@ -1076,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) {
