Skip to content

Commit

Permalink
A really dirty rip-out of the subtree revisions
Browse files Browse the repository at this point in the history
Just to see how deep this goes.
Assuming this does work, then we need to design an upgrade mechanism.
Probably forking this mysql driver / schema to be a mysql2 which is
effectively a different driver.
  • Loading branch information
mhutchinson committed Nov 13, 2023
1 parent 45d4aeb commit d4caed0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion storage/mysql/log_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ type logTreeTX struct {
func (t *logTreeTX) GetMerkleNodes(ctx context.Context, ids []compact.NodeID) ([]tree.Node, error) {
t.treeTX.mu.Lock()
defer t.treeTX.mu.Unlock()
return t.subtreeCache.GetNodes(ids, t.getSubtreesAtRev(ctx, t.readRev))
return t.subtreeCache.GetNodes(ids, t.getSubtreesFunc(ctx))
}

func (t *logTreeTX) DequeueLeaves(ctx context.Context, limit int, cutoffTime time.Time) ([]*trillian.LogLeaf, error) {
Expand Down
3 changes: 1 addition & 2 deletions storage/mysql/schema/storage.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ CREATE TABLE IF NOT EXISTS Subtree(
TreeId BIGINT NOT NULL,
SubtreeId VARBINARY(255) NOT NULL,
Nodes MEDIUMBLOB NOT NULL,
SubtreeRevision INTEGER NOT NULL,
-- Key columns must be in ASC order in order to benefit from group-by/min-max
-- optimization in MySQL.
PRIMARY KEY(TreeId, SubtreeId, SubtreeRevision),
PRIMARY KEY(TreeId, SubtreeId),
FOREIGN KEY(TreeId) REFERENCES Trees(TreeId) ON DELETE CASCADE
);

Expand Down
25 changes: 11 additions & 14 deletions storage/mysql/tree_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,21 @@ import (

// These statements are fixed
const (
insertSubtreeMultiSQL = `INSERT INTO Subtree(TreeId, SubtreeId, Nodes, SubtreeRevision) ` + placeholderSQL
insertSubtreeMultiSQL = `INSERT INTO Subtree(TreeId, SubtreeId, Nodes) ` + placeholderSQL
insertTreeHeadSQL = `INSERT INTO TreeHead(TreeId,TreeHeadTimestamp,TreeSize,RootHash,TreeRevision,RootSignature)
VALUES(?,?,?,?,?,?)`

selectSubtreeSQL = `
SELECT x.SubtreeId, x.MaxRevision, Subtree.Nodes
SELECT x.SubtreeId, Subtree.Nodes
FROM (
SELECT n.TreeId, n.SubtreeId, max(n.SubtreeRevision) AS MaxRevision
SELECT n.TreeId, n.SubtreeId
FROM Subtree n
WHERE n.SubtreeId IN (` + placeholderSQL + `) AND
n.TreeId = ? AND n.SubtreeRevision <= ?
n.TreeId = ?
GROUP BY n.TreeId, n.SubtreeId
) AS x
INNER JOIN Subtree
ON Subtree.SubtreeId = x.SubtreeId
AND Subtree.SubtreeRevision = x.MaxRevision
INNER JOIN Subtree
ON Subtree.SubtreeId = x.SubtreeId
AND Subtree.TreeId = x.TreeId
AND Subtree.TreeId = ?`
placeholderSQL = "<placeholder>"
Expand Down Expand Up @@ -172,7 +171,7 @@ type treeTX struct {
writeRevision int64
}

func (t *treeTX) getSubtrees(ctx context.Context, treeRevision int64, ids [][]byte) ([]*storagepb.SubtreeProto, error) {
func (t *treeTX) getSubtrees(ctx context.Context, ids [][]byte) ([]*storagepb.SubtreeProto, error) {
klog.V(2).Infof("getSubtrees(len(ids)=%d)", len(ids))
klog.V(4).Infof("getSubtrees(")
if len(ids) == 0 {
Expand All @@ -199,7 +198,6 @@ func (t *treeTX) getSubtrees(ctx context.Context, treeRevision int64, ids [][]by
}

args = append(args, t.treeID)
args = append(args, treeRevision)
args = append(args, t.treeID)

rows, err := stx.QueryContext(ctx, args...)
Expand Down Expand Up @@ -340,18 +338,17 @@ func checkResultOkAndRowCountIs(res sql.Result, err error, count int64) error {
return nil
}

// getSubtreesAtRev returns a GetSubtreesFunc which reads at the passed in rev.
func (t *treeTX) getSubtreesAtRev(ctx context.Context, rev int64) cache.GetSubtreesFunc {
// getSubtrees returns a GetSubtreesFunc.
func (t *treeTX) getSubtreesFunc(ctx context.Context) cache.GetSubtreesFunc {
return func(ids [][]byte) ([]*storagepb.SubtreeProto, error) {
return t.getSubtrees(ctx, rev, ids)
return t.getSubtrees(ctx, ids)
}
}

func (t *treeTX) SetMerkleNodes(ctx context.Context, nodes []tree.Node) error {
t.mu.Lock()
defer t.mu.Unlock()
rev := t.writeRevision - 1
return t.subtreeCache.SetNodes(nodes, t.getSubtreesAtRev(ctx, rev))
return t.subtreeCache.SetNodes(nodes, t.getSubtreesFunc(ctx))
}

func (t *treeTX) Commit(ctx context.Context) error {
Expand Down

0 comments on commit d4caed0

Please sign in to comment.