Skip to content

Commit

Permalink
chore: misc prerelease fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Jan 9, 2025
1 parent b8acc12 commit e3620f6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
25 changes: 4 additions & 21 deletions internal/oplog/sqlitestore/sqlitestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type SqliteStore struct {
dbpool *sqlitex.Pool
lastIDVal atomic.Int64
dblock *flock.Flock
querymu sync.RWMutex

ogidCache *lru.TwoQueueCache[opGroupInfo, int64]

Expand Down Expand Up @@ -222,8 +221,6 @@ func (m *SqliteStore) buildQueryWhereClause(q oplog.Query, includeSelectClauses
}

func (m *SqliteStore) Query(q oplog.Query, f func(*v1.Operation) error) error {
m.querymu.RLock()
defer m.querymu.RUnlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("query: %v", err)
Expand Down Expand Up @@ -251,8 +248,6 @@ func (m *SqliteStore) Query(q oplog.Query, f func(*v1.Operation) error) error {
}

func (m *SqliteStore) QueryMetadata(q oplog.Query, f func(oplog.OpMetadata) error) error {
m.querymu.RLock()
defer m.querymu.RUnlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("query metadata: %v", err)
Expand Down Expand Up @@ -323,16 +318,14 @@ func (m *SqliteStore) findOrCreateGroup(conn *sqlite.Conn, op *v1.Operation) (og
}

func (m *SqliteStore) Transform(q oplog.Query, f func(*v1.Operation) (*v1.Operation, error)) error {
m.querymu.Lock()
defer m.querymu.Unlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("transform: %v", err)
}
defer m.dbpool.Put(conn)

where, args := m.buildQueryWhereClause(q, true)
return withSqliteTransaction(conn, func() error {
return withImmediateSqliteTransaction(conn, func() error {
return sqlitex.ExecuteTransient(conn, "SELECT operations.operation FROM operations JOIN operation_groups ON operations.ogid = operation_groups.ogid WHERE "+where, &sqlitex.ExecOptions{
Args: args,
ResultFunc: func(stmt *sqlite.Stmt) error {
Expand Down Expand Up @@ -391,15 +384,13 @@ func (m *SqliteStore) addInternal(conn *sqlite.Conn, op ...*v1.Operation) error
}

func (m *SqliteStore) Add(op ...*v1.Operation) error {
m.querymu.Lock()
defer m.querymu.Unlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("add operation: %v", err)
}
defer m.dbpool.Put(conn)

return withSqliteTransaction(conn, func() error {
return withImmediateSqliteTransaction(conn, func() error {
for _, o := range op {
o.Id = m.lastIDVal.Add(1)
if o.FlowId == 0 {
Expand All @@ -415,15 +406,13 @@ func (m *SqliteStore) Add(op ...*v1.Operation) error {
}

func (m *SqliteStore) Update(op ...*v1.Operation) error {
m.querymu.Lock()
defer m.querymu.Unlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("update operation: %v", err)
}
defer m.dbpool.Put(conn)

return withSqliteTransaction(conn, func() error {
return withImmediateSqliteTransaction(conn, func() error {
return m.updateInternal(conn, op...)
})
}
Expand Down Expand Up @@ -456,8 +445,6 @@ func (m *SqliteStore) updateInternal(conn *sqlite.Conn, op ...*v1.Operation) err
}

func (m *SqliteStore) Get(opID int64) (*v1.Operation, error) {
m.querymu.RLock()
defer m.querymu.RUnlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return nil, fmt.Errorf("get operation: %v", err)
Expand Down Expand Up @@ -491,16 +478,14 @@ func (m *SqliteStore) Get(opID int64) (*v1.Operation, error) {
}

func (m *SqliteStore) Delete(opID ...int64) ([]*v1.Operation, error) {
m.querymu.Lock()
defer m.querymu.Unlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return nil, fmt.Errorf("delete operation: %v", err)
}
defer m.dbpool.Put(conn)

ops := make([]*v1.Operation, 0, len(opID))
return ops, withSqliteTransaction(conn, func() error {
return ops, withImmediateSqliteTransaction(conn, func() error {
// fetch all the operations we're about to delete
predicate := []string{"operations.id IN ("}
args := []any{}
Expand Down Expand Up @@ -548,8 +533,6 @@ func (m *SqliteStore) Delete(opID ...int64) ([]*v1.Operation, error) {
}

func (m *SqliteStore) ResetForTest(t *testing.T) error {
m.querymu.Lock()
defer m.querymu.Unlock()
conn, err := m.dbpool.Take(context.Background())
if err != nil {
return fmt.Errorf("reset for test: %v", err)
Expand Down
23 changes: 23 additions & 0 deletions internal/oplog/sqlitestore/sqlutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@ import (
"zombiezen.com/go/sqlite/sqlitex"
)

// withSqliteTransaction should be used when the function only executes reads
func withSqliteTransaction(conn *sqlite.Conn, f func() error) error {
var err error
endFunc := sqlitex.Transaction(conn)
err = f()
endFunc(&err)
return err
}

func withImmediateSqliteTransaction(conn *sqlite.Conn, f func() error) error {
var err error
endFunc, err := sqlitex.ImmediateTransaction(conn)
if err != nil {
return err
}
err = f()
endFunc(&err)
return err
}

func withExclusiveSqliteTransaction(conn *sqlite.Conn, f func() error) error {
var err error
endFunc, err := sqlitex.ExclusiveTransaction(conn)
if err != nil {
return err
}
err = f()
endFunc(&err)
return err
}
11 changes: 7 additions & 4 deletions webui/src/components/OperationTreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ export const OperationTreeView = ({
const otherTrees: React.ReactNode[] = [];

for (const instance of Object.keys(backupsByInstance)) {
const instanceOps = backupsByInstance[instance];
const instanceBackups = backupsByInstance[instance];
const instTree = (
<DisplayOperationTree
operations={backups}
operations={instanceBackups}
isPlanView={isPlanView}
onSelect={(flow) => {
setSelectedBackupId(flow ? flow.flowID : null);
}}
expand={instance === config!.instance}
/>
);

Expand Down Expand Up @@ -205,10 +206,12 @@ const DisplayOperationTree = ({
operations,
isPlanView,
onSelect,
expand,
}: {
operations: FlowDisplayInfo[];
isPlanView?: boolean;
onSelect?: (flow: FlowDisplayInfo | null) => any;
expand?: boolean;
}) => {
const [treeData, setTreeData] = useState<{
tree: OpTreeNode[];
Expand Down Expand Up @@ -237,7 +240,7 @@ const DisplayOperationTree = ({
<Tree<OpTreeNode>
treeData={treeData.tree}
showIcon
defaultExpandedKeys={treeData.expanded}
defaultExpandedKeys={expand ? treeData.expanded : []}
onSelect={(keys, info) => {
if (info.selectedNodes.length === 0) return;
const backup = info.selectedNodes[0].backup;
Expand Down Expand Up @@ -433,7 +436,7 @@ const buildTree = (
expanded = expandTree(tree, 5, 0, 2);
} else {
tree = buildTreePlan(operations);
expanded = expandTree(tree, 5, 2, 4);
expanded = expandTree(tree, 5, 1, 3);
}
return { tree, expanded };
};
Expand Down

0 comments on commit e3620f6

Please sign in to comment.