node: Avoid duplicate key constraints when changing chain shard#6199
node: Avoid duplicate key constraints when changing chain shard#6199erayack wants to merge 4 commits intographprotocol:masterfrom
Conversation
|
This pull request hasn't had any activity for the last 90 days. If there's no more activity over the course of the next 14 days, it will automatically be closed. |
|
We really need to review this, this shouldn't have been closed |
ed4e528 to
c5439e8
Compare
|
Hi @lutter, I rebased PR #6199 onto current master and resolved the conflict in node/src/manager/commands/chain.rs. The PR is now mergeable. I kept the original fix intent, but ported it to the current async chain-store code, preserved the safe chain-store creation ordering, simplified the backup-name selection flow, and added focused tests for backup-name formatting. |
|
Implemented the behavior you suggested. @dimitrovmaksim Current flow is now:
I also changed the reuse ordering to avoid the unique constraint issue: {chain}-old -> temp, {chain} -> {chain}-old, temp -> {chain} I added focused unit tests for the boundary decision logic. |
334940b to
704e10d
Compare
|
Hi @dimitrovmaksim again. Force-pushed 334940b → 704e10d addressing the latest round:
Tests still pass locally. Ready for another look when you have a moment. |
| ); | ||
| } | ||
|
|
||
| if allocated_chain.is_some() { |
There was a problem hiding this comment.
nit: Same as the allocated_chain = ... comment. I think this print is redundant.
| "Changed block cache shard for {} from {} to {}", | ||
| chain_name, old_shard, shard | ||
| ); | ||
| println!("Latest backup recorded as `{}`", canonical_backup_name); |
There was a problem hiding this comment.
nit: I would remove this println!, seems like a left-over from the sufixed backup names
| }; | ||
|
|
||
| let chain = BlockStore::allocate_chain(conn, &chain_name, &shard, &ident).await?; | ||
| let allocated_chain = if reuse_existing_backup { |
There was a problem hiding this comment.
nit: allocated_chain could be boolean, as it is used only for the conditional printing here https://github.com/erayack/graph-node/blob/8b649af8184d6a83e730b4ddb74289c07d3c5cd8/node/src/manager/commands/chain.rs#L380-L385. Tbf I think this message may be redundant as we already notify the user that the chain has been moved to another shard. Personally i would remove that print and change this to
if !reuse_existing_backup {
let chain = BlockStore::allocate_chain(&mut conn, &chain_name, &target_shard, &ident).await?;
store.add_chain_store(&chain, true).await?;
}
| .ok_or_else(|| anyhow!("unknown chain: {}", &chain_name))?; | ||
| let new_name = format!("{}-old", &chain_name); | ||
| let ident = chain_store.chain_identifier().await?; | ||
| let target_shard = Shard::new(shard.clone())?; |
There was a problem hiding this comment.
nit: You can drop the clone() here and let Shard::new consume the String, then you can use target_shard directly later as print arguments instead of shard
|
Hey @erayack sorry for the delay, I added couple of nit comment. I'll ask another team member to take a look before approving, just to be sure I'm not missing something. Btw could you drop the merge commit and instead rebase your branch ontop of master? 🙏🏻 |
- Implement robust backup naming system to avoid conflicts - Add support for reusing existing backups when appropriate - Preserve previous backups with unique names - Add comprehensive logging for backup operations Fixes graphprotocol#6196
8b649af to
d8ba7e4
Compare
Description
This PR fixes issue #6196 by implementing a robust backup naming system that prevents duplicate key constraint violations when reverting chain shard changes.
Problem
When changing a chain's shard using
graphman chain change-shard, the existing chain is renamed to<chain>-old. However, if a subsequent attempt is made to revert the chain's shard back to the original shard, the operation fails due to a unique constraint violation because<chain>-oldalready exists in the database.Solution
next_backup_name()function that generates unique backup names by appending numeric suffixes when conflicts existChanges
ChainSwapOutcomestruct to track the results of chain swap operationsnext_backup_name()function for conflict-free backup namingchange_block_cache_shard()function with robust backup handling logicTesting
The fix handles the following scenarios:
<chain>-old)Fixes #6196