Skip to content

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

7.5 KB
Raw Blame
// Package store is the implementation for managing Bytebase's own metadata in a PostgreSQL database.
package store

import (
	"sync"
	"sync/atomic"

	"context"
	"database/sql"
	"fmt"
	"time"

	lru "github.com/hashicorp/golang-lru/v2"
	"github.com/hashicorp/golang-lru/v2/expirable"

	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
	"github.com/bytebase/bytebase/backend/store/model"
)

// Store provides database access to all raw objects.
type Store struct {
	dbConnManager *DBConnectionManager
	enableCache   bool

	// Cache.
	authSecret     atomic.Pointer[string]
	userEmailCache *expirable.LRU[string, *UserMessage]
	instanceCache  *lru.Cache[string, *InstanceMessage]
	databaseCache  *lru.Cache[string, *DatabaseMessage]
	projectCache   *lru.Cache[string, *ProjectMessage]
	policyCache    *expirable.LRU[string, *PolicyMessage]
	settingCache   *expirable.LRU[string, *SettingMessage]

	// projectPublishMu prevents a cache fill that read an old database snapshot
	// from publishing after a concurrent project update invalidates the cache.
	projectPublishMu sync.Mutex
	// settingPublishMu serializes all setting cache publications (writer
	// republishes, cache-miss fills, invalidations). Publishers re-read the
	// row under the mutex, so whichever publishes last publishes current
	// truth. It must be acquired with no transaction, row lock, or pooled
	// connection held — writers commit first, then publish — so the
	// mutex/pool wait graph stays acyclic against the bounded metadata pool.
	settingPublishMu sync.Mutex
	// settingPublishHookForTest, when set, runs between commit and cache
	// publish so tests can deterministically expose the ordering window.
	settingPublishHookForTest func()
	// settingMissHookForTest and settingFillHookForTest, when set, run after a
	// setting read misses the cache and before it waits for the publish mutex,
	// and right before a cache-miss fill reads the database.
	settingMissHookForTest func()
	settingFillHookForTest func()
	rolesCache             *expirable.LRU[string, *RoleMessage]
	groupCache             *expirable.LRU[string, *GroupMessage]
	groupMembersCache      *expirable.LRU[string, map[string]bool]
	memberGroupsCache      *expirable.LRU[string, []string]
	dbSchemaCache          *expirable.LRU[string, *model.DatabaseMetadata]
	iamPolicyCache         *expirable.LRU[string, *IamPolicyMessage]

	// Large objects.
	sheetFullCache *lru.Cache[string, *SheetMessage]
}

// OutOfBandCacheLifetime bounds how long a cached user, setting or policy
// misses a change another process made to its row, such as `bytebase recovery`
// running beside the server. An entry expires this long after it is stored; a
// read must not extend it.
const OutOfBandCacheLifetime = time.Minute

// New creates a new instance of Store.
// pgURL can be either a direct PostgreSQL URL or a file path containing the URL.
func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {
	return newStore(ctx, pgURL, enableCache, OutOfBandCacheLifetime)
}

func newStore(ctx context.Context, pgURL string, enableCache bool, outOfBandCacheLifetime time.Duration) (*Store, error) {
	instanceCache, err := lru.New[string, *InstanceMessage](32768)
	if err != nil {
		return nil, err
	}
	databaseCache, err := lru.New[string, *DatabaseMessage](32768)
	if err != nil {
		return nil, err
	}
	projectCache, err := lru.New[string, *ProjectMessage](32768)
	if err != nil {
		return nil, err
	}
	rolesCache := expirable.NewLRU[string, *RoleMessage](128, nil, time.Minute)
	sheetFullCache, err := lru.New[string, *SheetMessage](10)
	if err != nil {
		return nil, err
	}
	groupCache := expirable.NewLRU[string, *GroupMessage](1024, nil, time.Minute)
	groupMembersCache := expirable.NewLRU[string, map[string]bool](1024, nil, time.Minute)
	memberGroupsCache := expirable.NewLRU[string, []string](4096, nil, time.Minute)
	dbSchemaCache := expirable.NewLRU[string, *model.DatabaseMetadata](128, nil, 5*time.Minute)
	iamPolicyCache := expirable.NewLRU[string, *IamPolicyMessage](1024, nil, time.Minute)

	// Initialize database connection (handles both direct URL and file-based)
	dbConnManager := NewDBConnectionManager(pgURL)
	if err := dbConnManager.Initialize(ctx); err != nil {
		return nil, err
	}

	s := &Store{
		dbConnManager: dbConnManager,
		enableCache:   enableCache,

		// Cache.
		instanceCache:     instanceCache,
		databaseCache:     databaseCache,
		projectCache:      projectCache,
		rolesCache:        rolesCache,
		sheetFullCache:    sheetFullCache,
		groupCache:        groupCache,
		groupMembersCache: groupMembersCache,
		memberGroupsCache: memberGroupsCache,
		dbSchemaCache:     dbSchemaCache,
		iamPolicyCache:    iamPolicyCache,
		userEmailCache:    expirable.NewLRU[string, *UserMessage](32768, nil, outOfBandCacheLifetime),
		settingCache:      expirable.NewLRU[string, *SettingMessage](1024, nil, outOfBandCacheLifetime),
		policyCache:       expirable.NewLRU[string, *PolicyMessage](4096, nil, outOfBandCacheLifetime),
	}

	return s, nil
}

// Close closes underlying db.
func (s *Store) Close() error {
	return s.dbConnManager.Close()
}

func (s *Store) GetDB() *sql.DB {
	return s.dbConnManager.GetDB()
}

// DeleteCache deletes the cache.
func (s *Store) DeleteCache() {
	// The setting cache purge participates in the publish-ordering invariant
	// so an in-flight fill cannot republish a just-purged snapshot.
	s.settingPublishMu.Lock()
	s.settingCache.Purge()
	s.settingPublishMu.Unlock()
	s.policyCache.Purge()
	s.userEmailCache.Purge()
}

// PurgeGroupCaches purges all group-related caches.
func (s *Store) PurgeGroupCaches() {
	s.groupCache.Purge()
	s.groupMembersCache.Purge()
	s.memberGroupsCache.Purge()
}

// PurgeIamPolicyCaches purges all IAM policy caches.
func (s *Store) PurgeIamPolicyCaches() {
	s.iamPolicyCache.Purge()
}

// removeGroupMembersCache removes all possible cache entries for a group's members,
// covering both groups/{email} and groups/{id} lookups.
func (s *Store) removeGroupMembersCache(workspace string, group *GroupMessage) {
	cacheKey := getGroupMembersCacheKey(workspace, formatGroupName(group))
	s.groupMembersCache.Remove(cacheKey)
}

func getInstanceCacheKey(instanceID string) string {
	return instanceID
}

func getSettingCacheKey(workspace string, name storepb.SettingName) string {
	return fmt.Sprintf("workspaces/%s/settings/%s", workspace, name.String())
}

// formatGroupName returns "groups/{email}" if email is set, otherwise "groups/{id}".
// This mirrors utils.FormatGroupName but avoids a circular import.
func formatGroupName(group *GroupMessage) string {
	if group.Email != "" {
		return "groups/" + group.Email
	}
	return "groups/" + group.ID
}

func getGroupCacheKey(workspace string, group *GroupMessage) string {
	return fmt.Sprintf("workspaces/%s/%s", workspace, formatGroupName(group))
}

func getGroupMembersCacheKey(workspace, groupName string) string {
	return fmt.Sprintf("workspaces/%s/%s/members", workspace, groupName)
}

func getMemberGroupsCacheKey(workspace, userName string) string {
	return fmt.Sprintf("workspaces/%s/memberGroups/%s", workspace, userName)
}

func getPolicyCacheKey(workspace string, resourceType storepb.Policy_Resource, resource string, policyType storepb.Policy_Type) string {
	return fmt.Sprintf("workspaces/%s/policies/%s/%s/%s", workspace, resourceType.String(), resource, policyType.String())
}

func getDatabaseCacheKey(workspace, instanceID, databaseName string) string {
	return fmt.Sprintf("workspaces/%s/%s/%s", workspace, instanceID, databaseName)
}

func getDBSchemaCacheKey(instanceID, databaseName string) string {
	return fmt.Sprintf("%s/%s", instanceID, databaseName)
}