Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migration: sector deal ids index cached #210

Closed
wants to merge 65 commits into from

Conversation

snissn
Copy link
Contributor

@snissn snissn commented Aug 29, 2023

  • Add migration cache to newMinerMigrator constructor.
  • Implement sector index caching in migrateSectorsWithCache.
  • Optimize sector migration by leveraging diffs in cached AMT.
  • Manage sector-to-deal mappings using a HAMT structure.

snissn added 20 commits August 22, 2023 19:13
- Introduced `SectorDealIDs` struct to store DealIDs associated with a sector in the market module.
- Implemented CBOR marshaling and unmarshaling for `SectorDealIDs`.
- Updated related references and dependencies in the market package.
- Enhanced `State` struct in the market module to include `SectorDeals`, a mapping of sector IDs to deal IDs grouped by storage provider.
- Updated CBOR serialization logic in `cbor_gen.go`:
  - Adjusted `lengthBufState` to reflect new field.
  - Implemented CBOR marshaling and unmarshaling for `SectorDeals`.
- This addition facilitates more efficient queries for sector-specific deal data.
- Extended sector migration functions in `miner.go` to support `minerAddr`.
- Introduced `sectorToDealIdHamt` to map sector numbers to their associated deal IDs.
- Added console print to log `sectorToDealIdHamtCid` and `minerAddr` for subsequent construction of `sector_deal_ids_index`.
- This is a preliminary commit, and the logging will be removed after constructing the required index.
The HamtCid struct was introduced in the market module, along with the necessary methods to marshal and unmarshal it using CBOR. Since this information is auto-generated, additional detailed comments have been omitted.

- Added HamtCid type definition in market_state.go
- Implemented MarshalCBOR and UnmarshalCBOR methods for HamtCid in cbor_gen.go
- Updated gen.go to include the HamtCid type in the main function
- Add `sectorDeals` field in `minerMigrator` for mapping of sector IDs to deal IDs.
- Modify sector migration methods to be methods of the `minerMigrator` struct.
- Introduce a mechanism to map sector numbers to their associated deal IDs and add to the state tree.
- Correct manifest actor matching for `market11Cid`.
- Initiate a second migration for the market actor post miner migration.
"
- Removed `builtin/v12/migration/market.go` as the migration logic is now centralized in `top.go`.
- Enhanced error messages in the migration process for better debugging.
- Added safety checks before updating sector deal IDs in the market actor.
- Remove v11 market state import.
- Transition from GetActorV4/SetActorV4 to GetActorV5/SetActorV5 methods.
- Use v12 market state type for migration.
- Add address field when updating the market actor in the state tree.
- Add migration cache to newMinerMigrator constructor.
- Implement sector index caching in migrateSectorsWithCache.
- Optimize sector migration by leveraging diffs in cached AMT.
- Manage sector-to-deal mappings using a HAMT structure.
@snissn snissn changed the base branch from mikers/sector_deal_ids_index to master September 1, 2023 23:36
- Rename `outDealHamt` to `outDealHamtCid` for clarity.
- Streamline the function `addSectorToDealIDHamtToSectorDeals` to be called once in `migrateSectorsWithCache`.
- Remove redundant calls to `addSectorToDealIDHamtToSectorDeals` in `migrateSectorsFromScratch` and `migrateSectorsWithDiff`.
- Pass `cid.Cid` directly to `addSectorToDealIDHamtToSectorDeals` to improve type handling.
@snissn snissn requested a review from ZenGround0 September 2, 2023 00:59
@snissn snissn self-assigned this Sep 2, 2023
@snissn snissn marked this pull request as ready for review September 2, 2023 01:00
Copy link
Contributor

@ZenGround0 ZenGround0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I missed a large and important piece of this migration when we first went over the specification of what we want.

This migration creates an index over all deal ids referenced by sector infos. However half of all sector infos are dead and their deal ids are therefore also dead / about to be dead. We want to only add deal ids that are active in the market actor to the index, otherwise they will never be cleaned up.

I think the easiest way to do this is create a set of active deal ids by reading the v11 market actor state before the miner migrations, passing this into the miner migrator and then only adding deal ids that are in this set.

@@ -171,11 +213,18 @@ func migrateSectorsWithCache(ctx context.Context, store adt11.Store, cache migra
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get previous outRoot from cache: %w", err)
}

okSectorIndexOut, prevSectorIndexRoot, err := cache.Read(migration.MinerPrevSectorDealIndexKey(inRoot))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to look up prevInRoot here, not inRoot. We won't have a cached result for inRoot but we should have one for prevInRoot.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(By looking up inRoot I think you'll wind up doing the migration from scratch every time)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed here #a008d024f687a6fc24fc0ebac27b4961037343a5

snissn and others added 10 commits September 11, 2023 12:07
…r inRoot

but we should have one for prevInRoot
Co-authored-by: Aayush Rajasekaran <[email protected]>
- Replaced usage of the `builtin.LoadTree`, `builtin.NewTree`, and methods on `builtin.ActorTree` handling with the adt12 package.
- Modified HAMT operations such as loading, creating, and getting the root CID to use the new adt12 methods.
- Adjusted method parameters to fit the new HAMT functions from adt12.
- Refactored addSectorNumberToDealIdHAMT and removeSectorNumberToDealIdFromHAMT functions to use the new HAMT operations for adding and deleting entries.
- Streamlined error messages and handling related to HAMT operations for clarity.
Copy link
Contributor

@arajasek arajasek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More notes.

@@ -55,6 +55,10 @@ type State struct {

// Verified registry allocation IDs for deals that are not yet activated.
PendingDealAllocationIds cid.Cid // HAMT[DealID]AllocationID

// New mapping of sector IDs to deal IDS, grouped by storage provider.
ProviderSectors cid.Cid // HAMT[Address]HAMT[SectorNumber]SectorDeals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ProviderSectors cid.Cid // HAMT[Address]HAMT[SectorNumber]SectorDeals
ProviderSectors cid.Cid // HAMT[Address]HAMT[SectorNumber]SectorDealIDs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think?

// Create the new state
newMarketState := market12.State{
Proposals: oldMarketState.Proposals,
States: oldMarketState.States, // FIXME migrate deal states to include sector number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but as far as I understand, this work is still outstanding. We need to do the second portion of the migration described here (and to add a sector number to and remove allocation ID from each DealState).

@@ -120,7 +149,7 @@ func (m minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, i

wrappedStore := adt11.WrapStore(ctx, store)

newSectors, err := migrateSectorsWithCache(ctx, wrappedStore, in.Cache, in.Address, inState.Sectors)
newSectors, err := m.migrateSectorsWithCache(ctx, wrappedStore, in.Cache, in.Address, inState.Sectors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snissn Can you make this change too (ideally as a pre-factor that we land first, but otherwise just as part of this PR)?

@@ -120,7 +149,7 @@ func (m minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, i

wrappedStore := adt11.WrapStore(ctx, store)

newSectors, err := migrateSectorsWithCache(ctx, wrappedStore, in.Cache, in.Address, inState.Sectors)
newSectors, err := m.migrateSectorsWithCache(ctx, wrappedStore, in.Cache, in.Address, inState.Sectors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about forgetting about it.

@@ -67,12 +74,34 @@ func newMinerMigrator(ctx context.Context, store cbor.IpldStore, outCode cid.Cid

}

//load sector index from datastore, or create a new one if not found
okSectorIndex, prevSectorIndexRoot, err := cache.Read(migration.MarketSectorIndexKey())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider reading this in top.go, and just passing prevSectorIndexRoot here so that we don't pass the entire cache here. I don't think it matters too much.

if err != nil {
return nil, xerrors.Errorf("reading cached sectorDeals tree: %w", err)
}
fmt.Println("Loaded HAMT from cache: ", prevSectorIndexRoot)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop?

// New mapping of sector IDs to deal IDS, grouped by storage provider.
sectorDeals, err = adt12.MakeEmptyMap(ctxStore, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("creating new state tree: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad error message

//add to new address sector deal id hamt index
err = m.addSectorToDealIDHamtToSectorDeals(sectorToDealIdHamtCid, in.Address)
if err != nil {
return nil, xerrors.Errorf("failed to add sector to deal id hamt to for minerAddr to HAMT: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad error msg

}

var sectorInfo miner11.SectorOnChainInfo
if err = inArray.ForEach(&sectorInfo, func(k int64) error {

err = addSectorNumberToDealIdHAMT(sectorToDealIdHamt, sectorInfo, store)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check error here

- Introduced a new `dealToSectorIndex` map in the `minerMigrator` structure to map deal IDs to sector numbers.
- Modified methods to update both the HAMT and the newly introduced deal-to-sector index.
- Started the foundation for market state migration logic by setting up placeholders and TODOs.
- Introduced the `MarketState` type in `miner_types.go` for better type clarity.

Note: More work is needed to complete the market state migration, as indicated by the TODOs.
- Added imports for v12 market and utilities
- Removed unused market and miner v12 imports
- Migrated `oldMarketState.States` from v11 to v12:
  - Iterated through each old market state, used `miner.dealToSectorIndex` to update the sector number.
  - Created a new ADT array for the deal states in v12 and populated it with the updated deal states.
  - Replaced the old market state's `States` with the new deal states CID in `newMarketState`.
- Updated field name in CBOR serialization and deserialization in `cbor_gen.go`.
- Adjusted invariant checks to reference 'DealStates' in `invariants.go`.
- Modified type definition in `market_state.go` to reflect the renaming.
- Ensured migration logic in `top.go` uses the new 'DealStates' field.
go: downloading google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
chain/actors/builtin/market/v12.go:80:18: s.State.States undefined (type "github.com/filecoin-project/go-state-types/builtin/v12/market".State has no field or method States)
chain/actors/builtin/market/v12.go:80:51: otherState12.State.States undefined (type "github.com/filecoin-project/go-state-types/builtin/v12/market".State has no field or method States)
chain/actors/builtin/market/v12.go:84:52: s.State.States undefined (type "github.com/filecoin-project/go-state-types/builtin/v12/market".State has no field or method States)
chain/actors/builtin/market/v12.go:197:53: v12.VerifiedClaim undefined (type "github.com/filecoin-project/go-state-types/builtin/v12/market".DealState has no field or method VerifiedClaim)
make: *** [Makefile:89: lotus] Error 1

Revert "Rename 'States' field to 'DealStates' in v12 market"

This reverts commit 4e83d01.
- Modified `dealToSectorIndex` from a pointer to a map to a pointer to a `sync.Map`.
- Adjusted relevant methods to use `Load` and `Store` from `sync.Map` for concurrency safety.
- Updated logic in `MigrateStateTree` to handle loading sector numbers using the `sync.Map`.
Copy link
Contributor

@arajasek arajasek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(forgot to submit review yday)

sectorDeals *adt12.Map
OutCodeCID cid.Cid
marketSectorDealsIndexLock *sync.Mutex
dealToSectorIndex *map[uint64]uint64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dealToSectorIndex *map[uint64]uint64
dealToSectorIndex map[uint64]uint64

should be fine?

return xerrors.Errorf("failed to delete sector from sectorToDealIdHamt index: %w", err)
}

//note that this is very slow because the index only goes one direction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be fine not removing the deal from the map here, so long as we only use this map as a lookup, and not the "list" of deals. That is, if we iterate over some other struct as the list of deals, and only use this for the sectornumber lookup. That way it would be fine to have some extra info here, so long as we aren't missing any deals (which we shouldn't be).

@arajasek
Copy link
Contributor

Deferred to nv22.

@arajasek arajasek closed this Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

5 participants