-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: tenant partitioning * fix: rebalance inactive partitions, split into separate partitioner * fix: shutdown partitioner scheduler properly * update config options * fix: config options linting
- Loading branch information
1 parent
68176b7
commit f2c6bc1
Showing
30 changed files
with
91,994 additions
and
81,012 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,6 @@ tasks: | |
- sudo sh ./hack/dev/manage-hosts.sh add 127.0.0.1 app.dev.hatchet-tools.com | ||
prisma-migrate: | ||
cmds: | ||
- go run github.com/steebchen/prisma-client-go migrate dev --create-only --skip-generate --name "{{.CLI_ARGS}}" | ||
- task: generate-sqlc | ||
- sh ./hack/dev/atlas-migrate.sh {{.CLI_ARGS}} | ||
- DATABASE_URL='postgresql://hatchet:[email protected]:5431/hatchet' sh ./hack/db/atlas-apply.sh | ||
|
@@ -67,7 +66,7 @@ tasks: | |
- task: generate-sqlc | ||
seed-dev: | ||
cmds: | ||
- sh ./hack/dev/run-go-with-env.sh run github.com/steebchen/prisma-client-go migrate deploy | ||
- sh ./hack/dev/run-go-with-env.sh run github.com/steebchen/prisma-client-go migrate dev --skip-generate | ||
- SEED_DEVELOPMENT=true sh ./hack/dev/run-go-with-env.sh run ./cmd/hatchet-admin seed | ||
start-dev: | ||
deps: | ||
|
@@ -137,7 +136,7 @@ tasks: | |
- sh ./generate.sh | ||
generate-sqlc: | ||
cmds: | ||
- DATABASE_URL='postgresql://hatchet:[email protected]:5431/shadow' npx --yes prisma migrate deploy | ||
- DATABASE_URL='postgresql://hatchet:[email protected]:5431/shadow' npx --yes prisma migrate dev --skip-generate | ||
- DATABASE_URL='postgresql://hatchet:[email protected]:5431/shadow' npx --yes prisma migrate diff --from-empty --to-schema-datasource prisma/schema.prisma --script > sql/schema/schema.sql | ||
- cp sql/schema/schema.sql pkg/repository/prisma/dbsqlc/schema.sql | ||
- go run github.com/sqlc-dev/sqlc/cmd/[email protected] generate --file pkg/repository/prisma/dbsqlc/sqlc.yaml | ||
|
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
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
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
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,135 @@ | ||
package engine | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-co-op/gocron/v2" | ||
"github.com/google/uuid" | ||
|
||
"github.com/hatchet-dev/hatchet/pkg/repository" | ||
) | ||
|
||
type partitioner struct { | ||
s gocron.Scheduler | ||
repo repository.TenantEngineRepository | ||
} | ||
|
||
func newPartitioner(repo repository.TenantEngineRepository) (partitioner, error) { | ||
s, err := gocron.NewScheduler(gocron.WithLocation(time.UTC)) | ||
|
||
if err != nil { | ||
return partitioner{}, err | ||
} | ||
|
||
return partitioner{s: s, repo: repo}, nil | ||
} | ||
|
||
func (p *partitioner) withControllers(ctx context.Context) (*Teardown, string, error) { | ||
partitionId := uuid.New().String() | ||
|
||
err := p.repo.CreateControllerPartition(ctx, partitionId) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not create engine partition: %w", err) | ||
} | ||
|
||
// rebalance partitions on startup | ||
err = p.repo.RebalanceAllControllerPartitions(ctx) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not rebalance engine partitions: %w", err) | ||
} | ||
|
||
_, err = p.s.NewJob( | ||
gocron.DurationJob(time.Minute*1), | ||
gocron.NewTask( | ||
func() { | ||
rebalanceControllerPartitions(ctx, p.repo) // nolint: errcheck | ||
}, | ||
), | ||
) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not create rebalance controller partitions job: %w", err) | ||
} | ||
|
||
return &Teardown{ | ||
Name: "partition teardown", | ||
Fn: func() error { | ||
deleteCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err := p.repo.DeleteControllerPartition(deleteCtx, partitionId) | ||
|
||
if err != nil { | ||
return fmt.Errorf("could not delete controller partition: %w", err) | ||
} | ||
|
||
return p.repo.RebalanceAllControllerPartitions(deleteCtx) | ||
}, | ||
}, partitionId, nil | ||
} | ||
|
||
func (p *partitioner) withTenantWorkers(ctx context.Context) (*Teardown, string, error) { | ||
partitionId := uuid.New().String() | ||
|
||
err := p.repo.CreateTenantWorkerPartition(ctx, partitionId) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not create engine partition: %w", err) | ||
} | ||
|
||
// rebalance partitions on startup | ||
err = p.repo.RebalanceAllTenantWorkerPartitions(ctx) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not rebalance engine partitions: %w", err) | ||
} | ||
|
||
_, err = p.s.NewJob( | ||
gocron.DurationJob(time.Minute*1), | ||
gocron.NewTask( | ||
func() { | ||
rebalanceTenantWorkerPartitions(ctx, p.repo) // nolint: errcheck | ||
}, | ||
), | ||
) | ||
|
||
if err != nil { | ||
return nil, "", fmt.Errorf("could not create rebalance tenant worker partitions job: %w", err) | ||
} | ||
|
||
return &Teardown{ | ||
Name: "partition teardown", | ||
Fn: func() error { | ||
deleteCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err := p.repo.DeleteTenantWorkerPartition(deleteCtx, partitionId) | ||
|
||
if err != nil { | ||
return fmt.Errorf("could not delete worker partition: %w", err) | ||
} | ||
|
||
return p.repo.RebalanceAllTenantWorkerPartitions(deleteCtx) | ||
}, | ||
}, partitionId, nil | ||
} | ||
|
||
func (p *partitioner) start() { | ||
p.s.Start() | ||
} | ||
|
||
func (p *partitioner) shutdown() error { | ||
return p.s.Shutdown() | ||
} | ||
|
||
func rebalanceControllerPartitions(ctx context.Context, r repository.TenantEngineRepository) error { | ||
return r.RebalanceInactiveControllerPartitions(ctx) | ||
} | ||
|
||
func rebalanceTenantWorkerPartitions(ctx context.Context, r repository.TenantEngineRepository) error { | ||
return r.RebalanceInactiveTenantWorkerPartitions(ctx) | ||
} |
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
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
Oops, something went wrong.