|
| 1 | +/* |
| 2 | +2026 © PostgresAI |
| 3 | +*/ |
| 4 | + |
| 5 | +package rdsrefresh |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 14 | + awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 16 | + ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 17 | + |
| 18 | + "gitlab.com/postgres-ai/database-lab/v3/pkg/log" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // rdsInstanceClassPrefix is stripped to derive the EC2 instance type. |
| 23 | + rdsInstanceClassPrefix = "db." |
| 24 | + |
| 25 | + // minParallelJobs is the minimum parallelism level. |
| 26 | + minParallelJobs = 1 |
| 27 | +) |
| 28 | + |
| 29 | +// EC2API defines the interface for EC2 client operations used for vCPU lookup. |
| 30 | +type EC2API interface { |
| 31 | + DescribeInstanceTypes(ctx context.Context, params *ec2.DescribeInstanceTypesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstanceTypesOutput, error) |
| 32 | +} |
| 33 | + |
| 34 | +// ParallelismConfig holds the computed parallelism levels for dump and restore. |
| 35 | +type ParallelismConfig struct { |
| 36 | + DumpJobs int |
| 37 | + RestoreJobs int |
| 38 | +} |
| 39 | + |
| 40 | +// ResolveParallelism determines the optimal parallelism levels for pg_dump and pg_restore. |
| 41 | +// dump parallelism is based on the vCPU count of the RDS clone instance class. |
| 42 | +// restore parallelism is based on the vCPU count of the local machine. |
| 43 | +func ResolveParallelism(ctx context.Context, cfg *Config) (*ParallelismConfig, error) { |
| 44 | + dumpJobs, err := resolveRDSInstanceVCPUs(ctx, cfg) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to resolve RDS instance vCPUs: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + restoreJobs := resolveLocalVCPUs() |
| 50 | + |
| 51 | + log.Msg("auto-parallelism: dump jobs =", dumpJobs, "(RDS clone vCPUs), restore jobs =", restoreJobs, "(local vCPUs)") |
| 52 | + |
| 53 | + return &ParallelismConfig{ |
| 54 | + DumpJobs: dumpJobs, |
| 55 | + RestoreJobs: restoreJobs, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +// resolveRDSInstanceVCPUs looks up the vCPU count for the configured RDS instance class |
| 60 | +// by querying the EC2 DescribeInstanceTypes API. |
| 61 | +func resolveRDSInstanceVCPUs(ctx context.Context, cfg *Config) (int, error) { |
| 62 | + ec2Client, err := newEC2Client(ctx, cfg) |
| 63 | + if err != nil { |
| 64 | + return 0, fmt.Errorf("failed to create EC2 client: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + return lookupInstanceVCPUs(ctx, ec2Client, cfg.RDSClone.InstanceClass) |
| 68 | +} |
| 69 | + |
| 70 | +func newEC2Client(ctx context.Context, cfg *Config) (EC2API, error) { |
| 71 | + awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(cfg.AWS.Region)) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("failed to load AWS config: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + var opts []func(*ec2.Options) |
| 77 | + if cfg.AWS.Endpoint != "" { |
| 78 | + opts = append(opts, func(o *ec2.Options) { |
| 79 | + o.BaseEndpoint = aws.String(cfg.AWS.Endpoint) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + return ec2.NewFromConfig(awsCfg, opts...), nil |
| 84 | +} |
| 85 | + |
| 86 | +// lookupInstanceVCPUs queries EC2 for the vCPU count of the given RDS instance class. |
| 87 | +func lookupInstanceVCPUs(ctx context.Context, client EC2API, rdsInstanceClass string) (int, error) { |
| 88 | + ec2InstanceType, err := rdsClassToEC2Type(rdsInstanceClass) |
| 89 | + if err != nil { |
| 90 | + return 0, err |
| 91 | + } |
| 92 | + |
| 93 | + result, err := client.DescribeInstanceTypes(ctx, &ec2.DescribeInstanceTypesInput{ |
| 94 | + InstanceTypes: []ec2types.InstanceType{ec2types.InstanceType(ec2InstanceType)}, |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return 0, fmt.Errorf("failed to describe EC2 instance type %q: %w", ec2InstanceType, err) |
| 98 | + } |
| 99 | + |
| 100 | + if len(result.InstanceTypes) == 0 { |
| 101 | + return 0, fmt.Errorf("EC2 instance type %q not found", ec2InstanceType) |
| 102 | + } |
| 103 | + |
| 104 | + info := result.InstanceTypes[0] |
| 105 | + if info.VCpuInfo == nil || info.VCpuInfo.DefaultVCpus == nil { |
| 106 | + return 0, fmt.Errorf("vCPU info not available for instance type %q", ec2InstanceType) |
| 107 | + } |
| 108 | + |
| 109 | + vcpus := int(*info.VCpuInfo.DefaultVCpus) |
| 110 | + if vcpus < minParallelJobs { |
| 111 | + return minParallelJobs, nil |
| 112 | + } |
| 113 | + |
| 114 | + return vcpus, nil |
| 115 | +} |
| 116 | + |
| 117 | +// rdsClassToEC2Type converts an RDS instance class (e.g. "db.m5.xlarge") to an EC2 instance type ("m5.xlarge"). |
| 118 | +func rdsClassToEC2Type(rdsClass string) (string, error) { |
| 119 | + if !strings.HasPrefix(rdsClass, rdsInstanceClassPrefix) { |
| 120 | + return "", fmt.Errorf("invalid RDS instance class %q: expected %q prefix", rdsClass, rdsInstanceClassPrefix) |
| 121 | + } |
| 122 | + |
| 123 | + ec2Type := strings.TrimPrefix(rdsClass, rdsInstanceClassPrefix) |
| 124 | + if ec2Type == "" { |
| 125 | + return "", fmt.Errorf("invalid RDS instance class %q: empty after removing prefix", rdsClass) |
| 126 | + } |
| 127 | + |
| 128 | + return ec2Type, nil |
| 129 | +} |
| 130 | + |
| 131 | +// resolveLocalVCPUs returns the number of logical CPUs available on the local machine. |
| 132 | +func resolveLocalVCPUs() int { |
| 133 | + cpus := runtime.NumCPU() |
| 134 | + if cpus < minParallelJobs { |
| 135 | + return minParallelJobs |
| 136 | + } |
| 137 | + |
| 138 | + return cpus |
| 139 | +} |
0 commit comments