Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: ha flag (#15557)

Danny Xub0a77fa4952c
diff --git a/backend/bin/server/cmd/profile.go b/backend/bin/server/cmd/profile.go
index 90518d114a..be32f4cd28 100644
--- a/backend/bin/server/cmd/profile.go
+++ b/backend/bin/server/cmd/profile.go
@@ -22,6 +22,7 @@ func getBaseProfile(dataDir string) *config.Profile {
Port: flags.port, // Using flags.port as our gRPC server port.
DatastorePort: flags.port + 2, // Using flags.port + 2 as our datastore port.
SampleDatabasePort: sampleDatabasePort,
+ HA: flags.ha,
SaaS: flags.saas,
EnableJSONLogging: flags.enableJSONLogging,
Debug: flags.debug,
diff --git a/backend/bin/server/cmd/root.go b/backend/bin/server/cmd/root.go
index 090f7086f9..8b5da2fe4e 100644
--- a/backend/bin/server/cmd/root.go
+++ b/backend/bin/server/cmd/root.go
@@ -64,13 +64,8 @@ var (
// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
pgURL string
dataDir string
- // When we are running in readonly mode:
- // - The data file will be opened in readonly mode, no applicable migration or seeding will be applied.
- // - Requests other than GET will be rejected
- // - Any operations involving mutation will not start (e.g. Background schema syncer, task scheduler)
- readonly bool
- // saas means the Bytebase is running in SaaS mode, several features is only controlled by us instead of users under this mode.
- saas bool
+ ha bool
+ saas bool
// output logs in json format
enableJSONLogging bool
// demo mode.
@@ -111,8 +106,8 @@ func init() {
// It allows to pass the postgres connection string as an ENV to the service.
rootCmd.PersistentFlags().StringVar(&flags.pgURL, "pg", os.Getenv("PG_URL"), "optional external PostgreSQL instance connection url (must provide dbname); for example postgresql://user:secret@masterhost:5432/dbname?sslrootcert=cert")
rootCmd.PersistentFlags().StringVar(&flags.dataDir, "data", ".", "not recommended for production. Directory where Bytebase stores data if --pg is not specified. If relative path is supplied, then the path is relative to the directory where Bytebase is under")
- rootCmd.PersistentFlags().BoolVar(&flags.readonly, "readonly", false, "whether to run in read-only mode")
- rootCmd.PersistentFlags().BoolVar(&flags.saas, "saas", false, "whether to run in SaaS mode")
+ rootCmd.PersistentFlags().BoolVar(&flags.ha, "ha", false, "run in HA mode")
+ rootCmd.PersistentFlags().BoolVar(&flags.saas, "saas", false, "run in SaaS mode")
rootCmd.PersistentFlags().BoolVar(&flags.enableJSONLogging, "enable-json-logging", false, "enable output logs in bytebase in json format")
// Must be one of the subpath name in the ../migrator/demo directory
rootCmd.PersistentFlags().BoolVar(&flags.demo, "demo", false, "run in demo mode.")
diff --git a/backend/component/config/profile.go b/backend/component/config/profile.go
index a732422a4a..d6a4d5884a 100644
--- a/backend/component/config/profile.go
+++ b/backend/component/config/profile.go
@@ -27,8 +27,6 @@ type Profile struct {
SaaS bool
// When enabled output logs in json format
EnableJSONLogging bool
- // Whether the server is running in a docker container.
- IsDocker bool
// Debug is the startup time debug
Debug bool
// DataDir is the directory stores the data including Bytebase's own database, backups, etc.
@@ -37,6 +35,8 @@ type Profile struct {
ResourceDir string
// Demo mode.
Demo bool
+ // HA replica mode.
+ HA bool
// Version is the bytebase's server version
Version string
@@ -51,6 +51,8 @@ type Profile struct {
LastActiveTs int64
// Unique ID per Bytebase instance run.
DeployID string
+ // Whether the server is running in a docker container.
+ IsDocker bool
// can be set in runtime
RuntimeDebug atomic.Bool
diff --git a/backend/server/server.go b/backend/server/server.go
index 81ceb411b0..cfcccc6593 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -173,7 +173,7 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {
}
// Connect to the instance that stores bytebase's own metadata.
- stores, err := store.New(ctx, pgURL)
+ stores, err := store.New(ctx, pgURL, !profile.HA)
if err != nil {
return nil, errors.Wrapf(err, "failed to new store")
}
diff --git a/backend/store/database.go b/backend/store/database.go
index ba7ffb0eaa..534470971b 100644
--- a/backend/store/database.go
+++ b/backend/store/database.go
@@ -73,7 +73,7 @@ type FindDatabaseMessage struct {
// GetDatabaseV2 gets a database.
func (s *Store) GetDatabaseV2(ctx context.Context, find *FindDatabaseMessage) (*DatabaseMessage, error) {
if find.InstanceID != nil && find.DatabaseName != nil {
- if v, ok := s.databaseCache.Get(getDatabaseCacheKey(*find.InstanceID, *find.DatabaseName)); ok {
+ if v, ok := s.databaseCache.Get(getDatabaseCacheKey(*find.InstanceID, *find.DatabaseName)); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/database_group.go b/backend/store/database_group.go
index 2024a909f2..8fdad72fdf 100644
--- a/backend/store/database_group.go
+++ b/backend/store/database_group.go
@@ -84,7 +84,7 @@ func (s *Store) ListDatabaseGroups(ctx context.Context, find *FindDatabaseGroupM
// GetDatabaseGroup gets a database group.
func (s *Store) GetDatabaseGroup(ctx context.Context, find *FindDatabaseGroupMessage) (*DatabaseGroupMessage, error) {
if find.ProjectID != nil && find.ResourceID != nil {
- if v, ok := s.databaseGroupCache.Get(getDatabaseGroupCacheKey(*find.ProjectID, *find.ResourceID)); ok {
+ if v, ok := s.databaseGroupCache.Get(getDatabaseGroupCacheKey(*find.ProjectID, *find.ResourceID)); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/db_schema.go b/backend/store/db_schema.go
index 0cd247e56f..8b7174a974 100644
--- a/backend/store/db_schema.go
+++ b/backend/store/db_schema.go
@@ -20,7 +20,7 @@ type UpdateDBSchemaMessage struct {
// GetDBSchema gets the schema for a database.
func (s *Store) GetDBSchema(ctx context.Context, instanceID, databaseName string) (*model.DatabaseSchema, error) {
- if v, ok := s.dbSchemaCache.Get(getDatabaseCacheKey(instanceID, databaseName)); ok {
+ if v, ok := s.dbSchemaCache.Get(getDatabaseCacheKey(instanceID, databaseName)); ok && s.enableCache {
return v, nil
}
diff --git a/backend/store/environment.go b/backend/store/environment.go
index 8986b6aa53..3b76cadc17 100644
--- a/backend/store/environment.go
+++ b/backend/store/environment.go
@@ -44,7 +44,7 @@ type UpdateEnvironmentMessage struct {
// GetEnvironmentV2 gets environment by resource ID.
func (s *Store) GetEnvironmentV2(ctx context.Context, find *FindEnvironmentMessage) (*EnvironmentMessage, error) {
if find.ResourceID != nil {
- if v, ok := s.environmentCache.Get(*find.ResourceID); ok {
+ if v, ok := s.environmentCache.Get(*find.ResourceID); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/group.go b/backend/store/group.go
index 8769e08c4b..6082214081 100644
--- a/backend/store/group.go
+++ b/backend/store/group.go
@@ -36,7 +36,7 @@ type GroupMessage struct {
// GetGroup gets a group.
func (s *Store) GetGroup(ctx context.Context, email string) (*GroupMessage, error) {
- if v, ok := s.groupCache.Get(email); ok {
+ if v, ok := s.groupCache.Get(email); ok && s.enableCache {
return v, nil
}
diff --git a/backend/store/idp.go b/backend/store/idp.go
index 151c5d9e52..0b893cc66a 100644
--- a/backend/store/idp.go
+++ b/backend/store/idp.go
@@ -102,7 +102,7 @@ func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProv
// GetIdentityProvider gets an identity provider.
func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProviderMessage) (*IdentityProviderMessage, error) {
if find.ResourceID != nil {
- if v, ok := s.idpCache.Get(*find.ResourceID); ok {
+ if v, ok := s.idpCache.Get(*find.ResourceID); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/instance.go b/backend/store/instance.go
index 8cb9a71efe..a52abe2824 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -44,7 +44,7 @@ type FindInstanceMessage struct {
// GetInstanceV2 gets an instance by the resource_id.
func (s *Store) GetInstanceV2(ctx context.Context, find *FindInstanceMessage) (*InstanceMessage, error) {
if find.ResourceID != nil {
- if v, ok := s.instanceCache.Get(getInstanceCacheKey(*find.ResourceID)); ok {
+ if v, ok := s.instanceCache.Get(getInstanceCacheKey(*find.ResourceID)); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/issue.go b/backend/store/issue.go
index ee9263a7da..424e72dc8a 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -106,12 +106,12 @@ type FindIssueMessage struct {
// GetIssueV2 gets issue by issue UID.
func (s *Store) GetIssueV2(ctx context.Context, find *FindIssueMessage) (*IssueMessage, error) {
if find.UID != nil {
- if v, ok := s.issueCache.Get(*find.UID); ok {
+ if v, ok := s.issueCache.Get(*find.UID); ok && s.enableCache {
return v, nil
}
}
if find.PipelineID != nil {
- if v, ok := s.issueByPipelineCache.Get(*find.PipelineID); ok {
+ if v, ok := s.issueByPipelineCache.Get(*find.PipelineID); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/pipeline.go b/backend/store/pipeline.go
index 08b54b305a..8679335011 100644
--- a/backend/store/pipeline.go
+++ b/backend/store/pipeline.go
@@ -244,7 +244,7 @@ func (*Store) createPipeline(ctx context.Context, txn *sql.Tx, create *PipelineM
// GetPipelineV2ByID gets the pipeline by ID.
func (s *Store) GetPipelineV2ByID(ctx context.Context, id int) (*PipelineMessage, error) {
- if v, ok := s.pipelineCache.Get(id); ok {
+ if v, ok := s.pipelineCache.Get(id); ok && s.enableCache {
return v, nil
}
pipelines, err := s.ListPipelineV2(ctx, &PipelineFind{ID: &id})
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 5b6f106613..da10401478 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -362,7 +362,7 @@ type UpdatePolicyMessage struct {
// GetPolicyV2 gets a policy.
func (s *Store) GetPolicyV2(ctx context.Context, find *FindPolicyMessage) (*PolicyMessage, error) {
if find.ResourceType != nil && find.Resource != nil && find.Type != nil {
- if v, ok := s.policyCache.Get(getPolicyCacheKey(*find.ResourceType, *find.Resource, *find.Type)); ok {
+ if v, ok := s.policyCache.Get(getPolicyCacheKey(*find.ResourceType, *find.Resource, *find.Type)); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/principal.go b/backend/store/principal.go
index 32c1fe32f9..ddf3a4d2dd 100644
--- a/backend/store/principal.go
+++ b/backend/store/principal.go
@@ -85,7 +85,7 @@ func (s *Store) GetSystemBotUser(ctx context.Context) *UserMessage {
// GetUserByID gets the user by ID.
func (s *Store) GetUserByID(ctx context.Context, id int) (*UserMessage, error) {
- if v, ok := s.userIDCache.Get(id); ok {
+ if v, ok := s.userIDCache.Get(id); ok && s.enableCache {
return v, nil
}
@@ -99,7 +99,7 @@ func (s *Store) GetUserByID(ctx context.Context, id int) (*UserMessage, error) {
// GetUserByEmail gets the user by email.
func (s *Store) GetUserByEmail(ctx context.Context, email string) (*UserMessage, error) {
- if v, ok := s.userEmailCache.Get(email); ok {
+ if v, ok := s.userEmailCache.Get(email); ok && s.enableCache {
return v, nil
}
diff --git a/backend/store/project.go b/backend/store/project.go
index 37c75f658b..fe992ecfaa 100644
--- a/backend/store/project.go
+++ b/backend/store/project.go
@@ -51,7 +51,7 @@ type UpdateProjectMessage struct {
// GetProjectV2 gets project by resource ID.
func (s *Store) GetProjectV2(ctx context.Context, find *FindProjectMessage) (*ProjectMessage, error) {
if find.ResourceID != nil {
- if v, ok := s.projectCache.Get(*find.ResourceID); ok {
+ if v, ok := s.projectCache.Get(*find.ResourceID); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/risk.go b/backend/store/risk.go
index 55590060c9..e17b276843 100644
--- a/backend/store/risk.go
+++ b/backend/store/risk.go
@@ -105,7 +105,7 @@ func (s *Store) GetRisk(ctx context.Context, id int64) (*RiskMessage, error) {
// ListRisks lists risks.
// returned risks are sorted by source, level DESC, id.
func (s *Store) ListRisks(ctx context.Context) ([]*RiskMessage, error) {
- if v, ok := s.risksCache.Get(0); ok {
+ if v, ok := s.risksCache.Get(0); ok && s.enableCache {
return v, nil
}
diff --git a/backend/store/role.go b/backend/store/role.go
index 6094fb1dfd..5db18c1280 100644
--- a/backend/store/role.go
+++ b/backend/store/role.go
@@ -68,7 +68,7 @@ func (s *Store) CreateRole(ctx context.Context, create *RoleMessage) (*RoleMessa
// GetRole returns a role by ID.
func (s *Store) GetRole(ctx context.Context, resourceID string) (*RoleMessage, error) {
- if v, ok := s.rolesCache.Get(resourceID); ok {
+ if v, ok := s.rolesCache.Get(resourceID); ok && s.enableCache {
return v, nil
}
query := `
diff --git a/backend/store/setting.go b/backend/store/setting.go
index 6080e4935a..e0d8fe8eb5 100644
--- a/backend/store/setting.go
+++ b/backend/store/setting.go
@@ -191,7 +191,7 @@ func (s *Store) DeleteCache() {
// GetSettingV2 returns the setting by name.
func (s *Store) GetSettingV2(ctx context.Context, name api.SettingName) (*SettingMessage, error) {
- if v, ok := s.settingCache.Get(name); ok {
+ if v, ok := s.settingCache.Get(name); ok && s.enableCache {
return v, nil
}
@@ -293,7 +293,7 @@ func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage)
// CreateSettingIfNotExistV2 creates a new setting only if the named setting doesn't exist.
func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMessage) (*SettingMessage, bool, error) {
- if v, ok := s.settingCache.Get(create.Name); ok {
+ if v, ok := s.settingCache.Get(create.Name); ok && s.enableCache {
return v, false, nil
}
diff --git a/backend/store/sheet.go b/backend/store/sheet.go
index 39de8fabb2..6bdd3c5150 100644
--- a/backend/store/sheet.go
+++ b/backend/store/sheet.go
@@ -64,7 +64,7 @@ type PatchSheetMessage struct {
// GetSheetStatementByID gets the statement of a sheet by ID.
func (s *Store) GetSheetStatementByID(ctx context.Context, id int) (string, error) {
- if v, ok := s.sheetStatementCache.Get(id); ok {
+ if v, ok := s.sheetStatementCache.Get(id); ok && s.enableCache {
return v, nil
}
@@ -85,7 +85,7 @@ func (s *Store) GetSheetStatementByID(ctx context.Context, id int) (string, erro
func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage) (*SheetMessage, error) {
shouldCache := !find.LoadFull && find.UID != nil
if shouldCache {
- if v, ok := s.sheetCache.Get(*find.UID); ok {
+ if v, ok := s.sheetCache.Get(*find.UID); ok && s.enableCache {
return v, nil
}
}
diff --git a/backend/store/store.go b/backend/store/store.go
index 88419c397d..8949f7d750 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -16,10 +16,11 @@ import (
// Store provides database access to all raw objects.
type Store struct {
- db *sql.DB
-
- Secret string
+ db *sql.DB
+ enableCache bool
+ // Cache.
+ Secret string
userIDCache *lru.Cache[int, *UserMessage]
userEmailCache *lru.Cache[string, *UserMessage]
environmentCache *lru.Cache[string, *EnvironmentMessage]
@@ -44,7 +45,7 @@ type Store struct {
}
// New creates a new instance of Store.
-func New(ctx context.Context, pgURL string) (*Store, error) {
+func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {
userIDCache, err := lru.New[int, *UserMessage](32768)
if err != nil {
return nil, err
@@ -144,7 +145,8 @@ func New(ctx context.Context, pgURL string) (*Store, error) {
db.SetMaxOpenConns(maxOpenConns)
return &Store{
- db: db,
+ db: db,
+ enableCache: enableCache,
// Cache.
userIDCache: userIDCache,