Skip to content

Commit 252b8a1

Browse files
committed
Fix schema version mismatch for index restore with AlterTableIndex
Problem: - After restoring index impl tables, schema version mismatch occurred - Index metadata expected version X but impl table had version X+1 - Tests failed with: "schema version mismatch during metadata loading" Root Cause: - Index impl table restore incremented the impl table's schema version - Parent index metadata AlterVersion was not updated - This caused a mismatch when querying the restored table Solution: - Added AlterTableIndex operation after each index impl table restore - Mirrors CDC stream pattern for avoiding additional alterVersion - AlterTableIndex operation syncs index metadata with impl table version - Sets index state to Ready, which increments the index AlterVersion The fix creates two operations per index restore: 1. Restore impl table (increments impl table schema version) 2. AlterTableIndex at parent table (syncs index metadata AlterVersion) Reference pattern from schemeshard__operation_create_cdc_stream.cpp: When working on index impl tables, create AlterTableIndex operation to avoid schema version mismatch between index and impl table.
1 parent 0197f82 commit 252b8a1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ydb/core/tx/schemeshard/schemeshard_incremental_restore_scan.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,39 @@ void TSchemeShard::CreateSingleIndexRestoreOperation(
857857
// Send the request (parallel with table operations)
858858
LOG_I("Sending index restore operation for: " << dstIndexImplPath);
859859
Send(SelfId(), indexRequest.Release());
860+
861+
// Create AlterTableIndex operation to sync index metadata version with impl table
862+
// This prevents schema version mismatch between index and its impl table
863+
// (similar to CDC stream pattern - see schemeshard__operation_create_cdc_stream.cpp)
864+
{
865+
auto alterIndexRequest = MakeHolder<TEvSchemeShard::TEvModifySchemeTransaction>();
866+
auto& alterIndexRecord = alterIndexRequest->Record;
867+
868+
TTxId alterIndexTxId = GetCachedTxId(ctx);
869+
alterIndexRecord.SetTxId(ui64(alterIndexTxId));
870+
871+
auto& alterIndexTx = *alterIndexRecord.AddTransaction();
872+
alterIndexTx.SetOperationType(NKikimrSchemeOp::ESchemeOpAlterTableIndex);
873+
alterIndexTx.SetInternal(true);
874+
alterIndexTx.SetWorkingDir(targetTablePath);
875+
876+
auto& alterIndex = *alterIndexTx.MutableAlterTableIndex();
877+
alterIndex.SetName(indexName);
878+
alterIndex.SetState(NKikimrSchemeOp::EIndexStateReady);
879+
880+
// Track this AlterTableIndex operation
881+
TOperationId alterIndexOpId(alterIndexTxId, 0);
882+
IncrementalRestoreOperationToState[alterIndexOpId] = operationId;
883+
TxIdToIncrementalRestore[alterIndexTxId] = operationId;
884+
885+
if (stateIt != IncrementalRestoreStates.end()) {
886+
stateIt->second.InProgressOperations.insert(alterIndexOpId);
887+
LOG_I("Tracking AlterTableIndex operation " << alterIndexOpId << " for schema version sync");
888+
}
889+
890+
LOG_I("Sending AlterTableIndex operation for: " << targetTablePath << "/" << indexName);
891+
Send(SelfId(), alterIndexRequest.Release());
892+
}
860893
}
861894

862895
// Notification function for operation completion

0 commit comments

Comments
 (0)