-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support cross cell reads #77
Changes from all commits
98e4a6a
4be4738
094d892
f330192
2d13726
a3d04ad
1c90630
dff4e22
b9f927f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"io" | ||
"net/http" | ||
"slices" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -170,6 +171,25 @@ func (p PlanetScaleEdgeDatabase) ListShards(ctx context.Context, psc PlanetScale | |
return p.Mysql.GetVitessShards(ctx, psc) | ||
} | ||
|
||
func (p PlanetScaleEdgeDatabase) ListCells(ctx context.Context, psc PlanetScaleSource) ([]string, error) { | ||
var cells []string | ||
tablets, err := p.Mysql.GetVitessTablets(ctx, psc) | ||
|
||
if err != nil { | ||
return cells, err | ||
} | ||
|
||
for _, vttablet := range tablets { | ||
if strings.EqualFold(vttablet.Keyspace, psc.Database) { | ||
if !slices.Contains(cells, vttablet.Cell) { | ||
cells = append(cells, vttablet.Cell) | ||
} | ||
} | ||
} | ||
|
||
return cells, nil | ||
} | ||
|
||
// Read streams rows from a table given a starting cursor. | ||
// 1. We will get the latest vgtid for a given table in a shard when a sync session starts. | ||
// 2. This latest vgtid is now the stopping point for this sync session. | ||
|
@@ -188,13 +208,20 @@ func (p PlanetScaleEdgeDatabase) Read(ctx context.Context, w io.Writer, ps Plane | |
tabletType = psdbconnect.TabletType_replica | ||
} | ||
|
||
cells, err := p.ListCells(ctx, ps) | ||
if err != nil { | ||
return currentSerializedCursor, err | ||
} | ||
|
||
p.Logger.Log(LOGLEVEL_INFO, fmt.Sprintf("Syncing from tabletType \"%v\", from cells: %v", TabletTypeToString(tabletType), cells)) | ||
|
||
currentPosition := lastKnownPosition | ||
table := s.Stream | ||
readDuration := 1 * time.Minute | ||
preamble := fmt.Sprintf("[%v:%v:%v shard : %v] ", table.Namespace, TabletTypeToString(tabletType), table.Name, currentPosition.Shard) | ||
for { | ||
p.Logger.Log(LOGLEVEL_INFO, preamble+"peeking to see if there's any new rows") | ||
latestCursorPosition, lcErr := p.getLatestCursorPosition(ctx, currentPosition.Shard, currentPosition.Keyspace, table, ps, tabletType) | ||
latestCursorPosition, lcErr := p.getLatestCursorPosition(ctx, currentPosition.Shard, currentPosition.Keyspace, table, ps, tabletType, cells) | ||
if lcErr != nil { | ||
return currentSerializedCursor, errors.Wrap(err, "Unable to get latest cursor position") | ||
} | ||
|
@@ -207,7 +234,7 @@ func (p PlanetScaleEdgeDatabase) Read(ctx context.Context, w io.Writer, ps Plane | |
p.Logger.Log(LOGLEVEL_INFO, fmt.Sprintf("new rows found, syncing rows for %v", readDuration)) | ||
p.Logger.Log(LOGLEVEL_INFO, fmt.Sprintf(preamble+"syncing rows with cursor [%v]", currentPosition)) | ||
|
||
currentPosition, err = p.sync(ctx, currentPosition, latestCursorPosition, table, ps, tabletType, readDuration) | ||
currentPosition, err = p.sync(ctx, currentPosition, latestCursorPosition, table, ps, tabletType, cells, readDuration) | ||
if currentPosition.Position != "" { | ||
currentSerializedCursor, sErr = TableCursorToSerializedCursor(currentPosition) | ||
if sErr != nil { | ||
|
@@ -235,7 +262,7 @@ func (p PlanetScaleEdgeDatabase) Read(ctx context.Context, w io.Writer, ps Plane | |
} | ||
} | ||
|
||
func (p PlanetScaleEdgeDatabase) sync(ctx context.Context, tc *psdbconnect.TableCursor, stopPosition string, s Stream, ps PlanetScaleSource, tabletType psdbconnect.TabletType, readDuration time.Duration) (*psdbconnect.TableCursor, error) { | ||
func (p PlanetScaleEdgeDatabase) sync(ctx context.Context, tc *psdbconnect.TableCursor, stopPosition string, s Stream, ps PlanetScaleSource, tabletType psdbconnect.TabletType, cells []string, readDuration time.Duration) (*psdbconnect.TableCursor, error) { | ||
defer p.Logger.Flush() | ||
ctx, cancel := context.WithTimeout(ctx, readDuration) | ||
defer cancel() | ||
|
@@ -276,7 +303,9 @@ func (p PlanetScaleEdgeDatabase) sync(ctx context.Context, tc *psdbconnect.Table | |
TableName: s.Name, | ||
Cursor: tc, | ||
TabletType: tabletType, | ||
Cells: cells, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Phanatic Would it be better to change how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, we have an |
||
} | ||
p.Logger.Log(LOGLEVEL_INFO, fmt.Sprintf("DEBUG: SyncRequest.Cells = %v", sReq.GetCells())) | ||
|
||
c, err := client.Sync(ctx, sReq) | ||
if err != nil { | ||
|
@@ -329,7 +358,7 @@ func (p PlanetScaleEdgeDatabase) sync(ctx context.Context, tc *psdbconnect.Table | |
} | ||
} | ||
|
||
func (p PlanetScaleEdgeDatabase) getLatestCursorPosition(ctx context.Context, shard, keyspace string, s Stream, ps PlanetScaleSource, tabletType psdbconnect.TabletType) (string, error) { | ||
func (p PlanetScaleEdgeDatabase) getLatestCursorPosition(ctx context.Context, shard, keyspace string, s Stream, ps PlanetScaleSource, tabletType psdbconnect.TabletType, cells []string) (string, error) { | ||
defer p.Logger.Flush() | ||
timeout := 45 * time.Second | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
|
@@ -368,6 +397,7 @@ func (p PlanetScaleEdgeDatabase) getLatestCursorPosition(ctx context.Context, sh | |
Position: "current", | ||
}, | ||
TabletType: tabletType, | ||
Cells: cells, | ||
} | ||
|
||
c, err := client.Sync(ctx, sReq) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mcrauwel we need to filter out by tablet type here, you can use the
psc.UseReplica
here to filter.If we pass along all the cells here, vstream might pick a primary tablet that isn't in the right cell from the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to @Phanatic, we just list all the possible cells and we request a
REPLICA
tabletThe tablet_picker just request an tablet to stream from in the list of cell with the
tabletType == 'replica'
but if you think it's necessary, it's fairly easy to add, before we call this function we already decided the tabletType so I can just pass that along as a param...