-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PSL-1237] implements a service to identify the data to be migrated
- Loading branch information
Showing
4 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
supernode/services/metamigrator/migration_data_identifier.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package metamigrator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/pastelnetwork/gonode/common/log" | ||
"time" | ||
) | ||
|
||
const ( | ||
batchSize = 10000 | ||
) | ||
|
||
var ( | ||
staleTime = time.Now().AddDate(0, -3, 0) | ||
) | ||
|
||
func (task *MetaMigratorTask) IdentifyMigrationData(ctx context.Context) (err error) { | ||
var migrationID int | ||
|
||
migrationID, err = task.service.metaStore.GetPendingMigrationID(ctx) | ||
if err != nil { | ||
log.WithContext(ctx).WithError(err).Error("error retrieving pending migration") | ||
return fmt.Errorf("failed to get pending migration ID: %w", err) | ||
} | ||
|
||
if migrationID == 0 { | ||
log.WithContext(ctx).Info("creating new migration") | ||
|
||
migrationID, err = task.service.metaStore.CreateNewMigration(ctx) | ||
if err != nil { | ||
log.WithContext(ctx).WithError(err).Error("error creating new migration") | ||
return fmt.Errorf("failed to create new migration: %w", err) | ||
} | ||
} | ||
log.WithContext(ctx).WithField("migration_id", migrationID).Info("migration info has been sorted") | ||
|
||
totalCount, err := task.service.metaStore.GetCountOfStaleData(ctx, staleTime) | ||
if err != nil { | ||
log.WithContext(ctx).WithError(err).Error("error retrieving stale data count") | ||
return fmt.Errorf("failed to get count of stale data: %w", err) | ||
} | ||
|
||
if totalCount == 0 { | ||
log.WithContext(ctx).Info("no stale data found to migrate") | ||
return nil | ||
} | ||
log.WithContext(ctx).WithField("total_keys", totalCount).Info("total-data that needs to migrate has been identified") | ||
|
||
numOfBatches := getNumOfBatches(totalCount) | ||
log.WithContext(ctx).WithField("no_of_batches", numOfBatches).Info("batches required to store migration-meta has been calculated") | ||
|
||
for batchNo := 0; batchNo < numOfBatches; batchNo++ { | ||
staleData, err := task.service.metaStore.GetStaleDataInBatches(ctx, batchSize, batchNo, staleTime) | ||
if err != nil { | ||
log.WithContext(ctx).Error("error retrieving batch of stale data") | ||
return fmt.Errorf("failed to get stale data in batch %d: %w", batchNo, err) | ||
} | ||
|
||
if err := task.service.metaStore.InsertMetaMigrationData(ctx, migrationID, staleData); err != nil { | ||
log.WithContext(ctx).Error("error inserting batch of stale data to migration-meta") | ||
return fmt.Errorf("failed to insert stale data for migration %d: %w", migrationID, err) | ||
} | ||
|
||
log.WithContext(ctx).WithField("batch", batchNo).Debug("data added to migration-meta for migration") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getNumOfBatches(totalCount int) int { | ||
numBatches := totalCount / batchSize | ||
if totalCount%batchSize != 0 { | ||
numBatches++ | ||
} | ||
|
||
return numBatches | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package metamigrator | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pastelnetwork/gonode/common/log" | ||
"github.com/pastelnetwork/gonode/common/utils" | ||
"github.com/pastelnetwork/gonode/p2p/kademlia/store/sqlite" | ||
"github.com/pastelnetwork/gonode/supernode/services/common" | ||
) | ||
|
||
const ( | ||
logPrefix = "MetaMigrator" | ||
defaultMetaMigratorDataIdentifier = 24 * time.Hour | ||
lowSpaceThresholdGB = 50 // in GB | ||
|
||
) | ||
|
||
// MetaMigratorService represents the MetaMigrator service. | ||
type MetaMigratorService struct { | ||
*common.SuperNodeService | ||
metaStore *sqlite.MigrationMetaStore | ||
} | ||
|
||
// Run starts the MetaMigrator service task | ||
func (service *MetaMigratorService) Run(ctx context.Context) error { | ||
for { | ||
select { | ||
case <-time.After(defaultMetaMigratorDataIdentifier): | ||
|
||
isLow, err := utils.CheckDiskSpace(lowSpaceThresholdGB) | ||
if err != nil { | ||
log.WithContext(ctx).WithField("method", "MetaMigratorService").WithError(err).Error("check disk space failed") | ||
continue | ||
} | ||
|
||
if !isLow { | ||
continue | ||
} | ||
|
||
task := NewMetaMigratorTask(service) | ||
if err := task.IdentifyMigrationData(ctx); err != nil { | ||
log.WithContext(ctx).WithError(err).Error("failed to identify migration data") | ||
} | ||
case <-ctx.Done(): | ||
log.Println("Context done being called in IdentifyMigrationData loop in service.go") | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// NewService returns a new MetaMigratorService instance. | ||
func NewService(metaStore *sqlite.MigrationMetaStore) *MetaMigratorService { | ||
return &MetaMigratorService{ | ||
SuperNodeService: common.NewSuperNodeService(nil, nil, nil), | ||
metaStore: metaStore, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package metamigrator | ||
|
||
// MetaMigratorTask is the task of identifying migration data and then migrating that data to cloud. | ||
type MetaMigratorTask struct { | ||
service *MetaMigratorService | ||
} | ||
|
||
// NewMetaMigratorTask returns a new MetaMigratorTask instance. | ||
func NewMetaMigratorTask(service *MetaMigratorService) *MetaMigratorTask { | ||
return &MetaMigratorTask{ | ||
service: service, | ||
} | ||
} |