From 981dede1e946b78cff5877b9740c3f5f1e387eee Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Tue, 24 Nov 2026 18:35:13 -0800 Subject: [PATCH 1/5] PHOENIX-8086 MetaDataEndpointImpl.createTable reports failure after a successful commit createTable commits the catalog rows and then reads the new table back. If the region moves, this read can fail with NotServingRegionException. The endpoint then reports a failure, and the client retry gets TABLE_ALREADY_EXISTS for a table that the same statement created. Now a failed read after the commit logs a warning and returns success without the table. The client builds the table from its own metadata, without LAST_DDL_TIMESTAMP and the external schema id. Views and indexes get these values on the next refresh. A base table keeps them null in the client cache until its next DDL or cache eviction. Co-Authored-By: Claude Opus 5.5 --- .../phoenix/coprocessor/MetaDataEndpointImpl.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index bd38a263fed..ac8659c9e3b 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -2981,7 +2981,16 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, // used by buildTable does not include 't' due to how SCN is implemented. clientTimeStamp += 1; } - PTable newTable = buildTable(tableKey, cacheKey, region, clientTimeStamp, clientVersion); + // The catalog rows are already committed here. If the table read fails, for example + // because the region moves, do not report a failure. The client then builds the table + // from its own metadata. + PTable newTable = null; + try { + newTable = buildTable(tableKey, cacheKey, region, clientTimeStamp, clientVersion); + } catch (IOException | SQLException e) { + LOGGER.warn("Could not read {} {} after create, key {}; returning success without it", + tableType, fullTableName, Bytes.toStringBinary(tableKey), e); + } if (newTable != null) { builder.setTable(PTableImpl.toProto(newTable, clientVersion)); } From 0f1803688581a92cefa98f34f66bc8b7ece55dd4 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 3 Dec 2026 18:40:29 -0800 Subject: [PATCH 2/5] PHOENIX-8086 Build the created table from its committed rows after a failed read When the read after the commit fails, the endpoint now builds the new table from the catalog rows that it has just committed, the same way schema export already does. These rows carry the server-generated DDL timestamp and, with change detection, the external schema id, so the client caches the same values as after a normal read. If this build also fails, the endpoint returns success without the table, as before. Co-Authored-By: Claude Opus 5.5 --- .../coprocessor/MetaDataEndpointImpl.java | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index ac8659c9e3b..3c6d2f0f0cf 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -2981,15 +2981,26 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, // used by buildTable does not include 't' due to how SCN is implemented. clientTimeStamp += 1; } - // The catalog rows are already committed here. If the table read fails, for example - // because the region moves, do not report a failure. The client then builds the table - // from its own metadata. + // The catalog rows are already committed here, so a failed read must not report a + // failure. If the read fails, for example because the region moves, build the table from + // the committed mutations. These mutations carry the server-side values that the client + // needs, such as the last DDL timestamp and the external schema id. If that build also + // fails, return success without the table. The client then builds the table from its own + // metadata. PTable newTable = null; try { newTable = buildTable(tableKey, cacheKey, region, clientTimeStamp, clientVersion); } catch (IOException | SQLException e) { - LOGGER.warn("Could not read {} {} after create, key {}; returning success without it", + LOGGER.warn("Could not read {} {} after create, key {}; building it from mutations", tableType, fullTableName, Bytes.toStringBinary(tableKey), e); + try { + newTable = getTableFromMutations(getTableRowMutations(tableMetadata, tableKey), + clientTimeStamp, clientVersion, null); + } catch (Exception buildException) { + LOGGER.warn( + "Could not build {} {} from mutations, key {}; returning success without it", + tableType, fullTableName, Bytes.toStringBinary(tableKey), buildException); + } } if (newTable != null) { builder.setTable(PTableImpl.toProto(newTable, clientVersion)); @@ -3019,8 +3030,11 @@ private void updateCreateTableDdlSuccessMetrics(PTableType tableType) { } } - private void exportSchema(List tableMetadata, byte[] tableKey, long clientTimestamp, - int clientVersion, PTable oldTable) throws SQLException, IOException { + /** + * Builds a PTable from the catalog mutations of a DDL operation, without a read from the region. + */ + private PTable getTableFromMutations(List tableMetadata, long clientTimeStamp, + int clientVersion, PTable oldTable) throws IOException, SQLException { List tableCellList = MetaDataUtil.getTableCellsFromMutations(tableMetadata); List> allColumnsCellList = @@ -3030,9 +3044,34 @@ private void exportSchema(List tableMetadata, byte[] tableKey, long cl for (List columnCellList : allColumnsCellList) { Collections.sort(columnCellList, KeyValue.COMPARATOR); } + return getTableFromCells(tableCellList, allColumnsCellList, clientTimeStamp, clientVersion, + oldTable); + } + + /** + * Returns the mutations for the header, column and link rows of one table. A scan of the table + * key reads the same rows. + */ + private static List getTableRowMutations(List tableMetadata, + byte[] tableKey) { + List tableRows = new ArrayList<>(tableMetadata.size()); + for (Mutation m : tableMetadata) { + byte[] row = m.getRow(); + if ( + Bytes.equals(row, tableKey) + || (row.length > tableKey.length && Bytes.startsWith(row, tableKey) + && row[tableKey.length] == QueryConstants.SEPARATOR_BYTE) + ) { + tableRows.add(m); + } + } + return tableRows; + } - PTable newTable = getTableFromCells(tableCellList, allColumnsCellList, clientTimestamp, - clientVersion, oldTable); + private void exportSchema(List tableMetadata, byte[] tableKey, long clientTimestamp, + int clientVersion, PTable oldTable) throws SQLException, IOException { + PTable newTable = + getTableFromMutations(tableMetadata, clientTimestamp, clientVersion, oldTable); PTable parentTable = null; // if this is a view, we need to get the columns from its parent table / view if (newTable != null && newTable.getType().equals(PTableType.VIEW)) { From 76529c2ee92a6b227d6283670b66bdcf056873ab Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 3 Dec 2026 19:31:37 -0800 Subject: [PATCH 3/5] PHOENIX-8086 Test the create table response after a failed read A test hook makes the read after a create fail one time. It can also make the build from mutations fail. New tests check that the create succeeds in both cases. The client keeps the server DDL timestamp only when the response carries the table. The fallback now catches only the I/O, SQL and runtime exceptions. Co-Authored-By: Claude Opus 5.5 --- .../coprocessor/MetaDataEndpointImpl.java | 21 +++++++- .../end2end/MetaDataEndpointImplIT.java | 54 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index 3c6d2f0f0cf..ad8978c666b 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -622,6 +622,10 @@ public static PName newPName(byte[] keyBuffer, int keyOffset, int keyLength) { } private static boolean failConcurrentMutateAddColumnOneTimeForTesting = false; + // A test names a table here to make the read after its create fail one time. The test can also + // make the build from mutations fail, so that the create returns without the table. + private static volatile String failReadAfterCreateTableForTesting; + private static volatile boolean failBuildAfterCreateTableForTesting; private static final String FORCE_INDEX_MUTATE_METADATA_AS_ATTRIB = String.valueOf(Integer.MAX_VALUE); private RegionCoprocessorEnvironment env; @@ -648,6 +652,12 @@ public static void setFailConcurrentMutateAddColumnOneTimeForTesting(boolean fai failConcurrentMutateAddColumnOneTimeForTesting = fail; } + public static void setFailReadAfterCreateTableForTesting(String fullTableName, + boolean failBuild) { + failBuildAfterCreateTableForTesting = failBuild; + failReadAfterCreateTableForTesting = fullTableName; + } + /** * Stores a reference to the coprocessor environment provided by the * {@link org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost} from the region where this @@ -2988,15 +2998,24 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, // fails, return success without the table. The client then builds the table from its own // metadata. PTable newTable = null; + boolean failBuildForTesting = false; try { + if (fullTableName.equals(failReadAfterCreateTableForTesting)) { + failReadAfterCreateTableForTesting = null; + failBuildForTesting = failBuildAfterCreateTableForTesting; + throw new IOException("Read failure injected for testing"); + } newTable = buildTable(tableKey, cacheKey, region, clientTimeStamp, clientVersion); } catch (IOException | SQLException e) { LOGGER.warn("Could not read {} {} after create, key {}; building it from mutations", tableType, fullTableName, Bytes.toStringBinary(tableKey), e); try { + if (failBuildForTesting) { + throw new IOException("Build failure injected for testing"); + } newTable = getTableFromMutations(getTableRowMutations(tableMetadata, tableKey), clientTimeStamp, clientVersion, null); - } catch (Exception buildException) { + } catch (IOException | SQLException | RuntimeException buildException) { LOGGER.warn( "Could not build {} {} from mutations, key {}; returning success without it", tableType, fullTableName, Bytes.toStringBinary(tableKey), buildException); diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndpointImplIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndpointImplIT.java index 85a9cd2cddf..d18f8e39102 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndpointImplIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndpointImplIT.java @@ -19,10 +19,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +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.sql.SQLException; import java.util.Arrays; import java.util.List; @@ -30,11 +33,13 @@ import java.util.Properties; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.TableName; +import org.apache.phoenix.coprocessor.MetaDataEndpointImpl; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.jdbc.PhoenixConnection; import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; import org.apache.phoenix.schema.PColumn; import org.apache.phoenix.schema.PTable; +import org.apache.phoenix.schema.PTableKey; import org.apache.phoenix.schema.TableNotFoundException; import org.apache.phoenix.util.PhoenixRuntime; import org.apache.phoenix.util.PropertiesUtil; @@ -381,6 +386,55 @@ public void testWhereClause() throws Exception { assertNotNull(grandChildViewTable.getColumnForColumnName("A2").getViewConstant()); } + @Test + public void testCreateTableAfterFailedReadReturnsTableFromMutations() throws Exception { + assertCreateTableAfterFailedRead(false); + } + + @Test + public void testCreateTableAfterFailedReadAndBuildReturnsSuccess() throws Exception { + assertCreateTableAfterFailedRead(true); + } + + /** + * Makes the server read after a create fail, and optionally the build from mutations too. The + * create must succeed in both cases. The client keeps the server DDL timestamp only when the + * response carries the table. + */ + private void assertCreateTableAfterFailedRead(boolean failBuild) throws Exception { + String tableName = generateUniqueName(); + try (PhoenixConnection conn = + DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class)) { + MetaDataEndpointImpl.setFailReadAfterCreateTableForTesting(tableName, failBuild); + try { + conn.createStatement() + .execute("CREATE TABLE " + tableName + " (K VARCHAR PRIMARY KEY, V VARCHAR)"); + } finally { + MetaDataEndpointImpl.setFailReadAfterCreateTableForTesting(null, false); + } + PTable cached = conn.getTableRef(new PTableKey(null, tableName)).getTable(); + // Read the stored value from the catalog. A metadata refresh keeps the cached table + // because the table did not change on the server. + ResultSet catalog = conn.createStatement() + .executeQuery("SELECT LAST_DDL_TIMESTAMP FROM SYSTEM.CATALOG WHERE TENANT_ID IS NULL" + + " AND TABLE_SCHEM IS NULL AND TABLE_NAME = '" + tableName + "'" + + " AND COLUMN_NAME IS NULL AND COLUMN_FAMILY IS NULL"); + assertTrue(catalog.next()); + long stored = catalog.getLong(1); + assertTrue(stored > 0); + if (failBuild) { + assertNull(cached.getLastDDLTimestamp()); + } else { + assertEquals(Long.valueOf(stored), cached.getLastDDLTimestamp()); + } + conn.createStatement().execute("UPSERT INTO " + tableName + " VALUES ('a', 'b')"); + conn.commit(); + ResultSet rs = conn.createStatement().executeQuery("SELECT V FROM " + tableName); + assertTrue(rs.next()); + assertEquals("b", rs.getString(1)); + } + } + private void assertColumnNamesEqual(PTable table, String... cols) { List actual = Lists.newArrayList(); for (PColumn column : table.getColumns()) { From 588e2fd85b050a334a1dc10f2732c2db848020d7 Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Thu, 3 Dec 2026 20:09:47 -0800 Subject: [PATCH 4/5] PHOENIX-8086 Fix the checkstyle and spotbugs warnings in the create fallback A static helper now clears the test flag, so the endpoint instance does not write to a static field. The row filter names its child row test, so it needs no parentheses. Co-Authored-By: Claude Opus 5.5 --- .../coprocessor/MetaDataEndpointImpl.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index ad8978c666b..2f857eecb81 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -658,6 +658,15 @@ public static void setFailReadAfterCreateTableForTesting(String fullTableName, failReadAfterCreateTableForTesting = fullTableName; } + /** Returns true one time for the table that a test names. */ + private static boolean consumeFailReadAfterCreateTableForTesting(String fullTableName) { + if (!fullTableName.equals(failReadAfterCreateTableForTesting)) { + return false; + } + failReadAfterCreateTableForTesting = null; + return true; + } + /** * Stores a reference to the coprocessor environment provided by the * {@link org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost} from the region where this @@ -3000,8 +3009,7 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, PTable newTable = null; boolean failBuildForTesting = false; try { - if (fullTableName.equals(failReadAfterCreateTableForTesting)) { - failReadAfterCreateTableForTesting = null; + if (consumeFailReadAfterCreateTableForTesting(fullTableName)) { failBuildForTesting = failBuildAfterCreateTableForTesting; throw new IOException("Read failure injected for testing"); } @@ -3076,11 +3084,9 @@ private static List getTableRowMutations(List tableMetadata, List tableRows = new ArrayList<>(tableMetadata.size()); for (Mutation m : tableMetadata) { byte[] row = m.getRow(); - if ( - Bytes.equals(row, tableKey) - || (row.length > tableKey.length && Bytes.startsWith(row, tableKey) - && row[tableKey.length] == QueryConstants.SEPARATOR_BYTE) - ) { + boolean childRow = row.length > tableKey.length && Bytes.startsWith(row, tableKey) + && row[tableKey.length] == QueryConstants.SEPARATOR_BYTE; + if (childRow || Bytes.equals(row, tableKey)) { tableRows.add(m); } } From 9d420956da8fe94c4ccc697e844937c1f3b97bfd Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Fri, 4 Dec 2026 12:44:12 -0800 Subject: [PATCH 5/5] PHOENIX-8086 Treat a failed read after create the same way for all failures The read after the commit now catches the same failures as the build from mutations, so an unchecked failure of the read also falls back to that build. The comments now say that the build reads no region rows only for a base table. For a view or an index it still reads parent and link rows. Co-Authored-By: Claude Opus 5.5 --- .../phoenix/coprocessor/MetaDataEndpointImpl.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java index 2f857eecb81..1bcb519233b 100644 --- a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java +++ b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java @@ -3003,9 +3003,10 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, // The catalog rows are already committed here, so a failed read must not report a // failure. If the read fails, for example because the region moves, build the table from // the committed mutations. These mutations carry the server-side values that the client - // needs, such as the last DDL timestamp and the external schema id. If that build also - // fails, return success without the table. The client then builds the table from its own - // metadata. + // needs, such as the last DDL timestamp and the external schema id. For a base table this + // build needs no read. For a view or an index it still reads parent and link rows, so it + // can fail too. If that build also fails, return success without the table. The client + // then builds the table from its own metadata. PTable newTable = null; boolean failBuildForTesting = false; try { @@ -3014,7 +3015,7 @@ Collections. emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE, throw new IOException("Read failure injected for testing"); } newTable = buildTable(tableKey, cacheKey, region, clientTimeStamp, clientVersion); - } catch (IOException | SQLException e) { + } catch (IOException | SQLException | RuntimeException e) { LOGGER.warn("Could not read {} {} after create, key {}; building it from mutations", tableType, fullTableName, Bytes.toStringBinary(tableKey), e); try { @@ -3058,7 +3059,8 @@ private void updateCreateTableDdlSuccessMetrics(PTableType tableType) { } /** - * Builds a PTable from the catalog mutations of a DDL operation, without a read from the region. + * Builds a PTable from the catalog mutations of a DDL operation. For a base table this needs no + * read from the region. For a view or an index the build still reads parent and link rows. */ private PTable getTableFromMutations(List tableMetadata, long clientTimeStamp, int clientVersion, PTable oldTable) throws IOException, SQLException {