-
Notifications
You must be signed in to change notification settings - Fork 5
Add Airbyte protocol v2 compliance (stream status, per-stream state) #149
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
Open
Smidge
wants to merge
4
commits into
planetscale:main
Choose a base branch
from
anam-org:upstream-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a564922
Add stream status trace messages for Airbyte protocol v2
Smidge 03b5915
Emit per-stream status and state in read loop, handle v2 state input
Smidge de00c59
Add tests for Airbyte protocol v2 compliance
Smidge fa86cd4
Fix lost shard progress and silent success on stream errors
Smidge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,32 +22,33 @@ func init() { | |
|
|
||
| func ReadCommand(ch *Helper) *cobra.Command { | ||
| readCmd := &cobra.Command{ | ||
| Use: "read", | ||
| Short: "Converts rows from a PlanetScale database into AirbyteRecordMessages", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| Use: "read", | ||
| Short: "Converts rows from a PlanetScale database into AirbyteRecordMessages", | ||
| SilenceUsage: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
|
|
||
| ch.Logger = internal.NewLogger(cmd.OutOrStdout()) | ||
| if readSourceConfigFilePath == "" { | ||
| fmt.Fprintf(cmd.ErrOrStderr(), "Please pass path to a valid source config file via the [%v] argument", "config") | ||
| os.Exit(1) | ||
| return fmt.Errorf("missing config file path") | ||
| } | ||
|
|
||
| if readSourceCatalogPath == "" { | ||
| fmt.Fprintf(cmd.OutOrStdout(), "Please pass path to a valid source catalog file via the [%v] argument", "config") | ||
| os.Exit(1) | ||
| return fmt.Errorf("missing catalog file path") | ||
| } | ||
|
|
||
| psc, err := parseSource(ch.FileReader, readSourceConfigFilePath) | ||
| if err != nil { | ||
| fmt.Fprintln(cmd.OutOrStdout(), "Please provide path to a valid configuration file") | ||
| return | ||
| return err | ||
| } | ||
|
|
||
| ch.Logger.Log(internal.LOGLEVEL_INFO, "Ensure database") | ||
| if err := ch.EnsureDB(psc); err != nil { | ||
| fmt.Fprintln(cmd.OutOrStdout(), "Unable to connect to PlanetScale Database") | ||
| return | ||
| return err | ||
| } | ||
|
|
||
| defer func() { | ||
|
|
@@ -60,19 +61,19 @@ func ReadCommand(ch *Helper) *cobra.Command { | |
| cs, err := checkConnectionStatus(ctx, ch.Database, psc) | ||
| if err != nil { | ||
| ch.Logger.ConnectionStatus(cs) | ||
| return | ||
| return err | ||
| } | ||
|
|
||
| ch.Logger.Log(internal.LOGLEVEL_INFO, "Reading catalog") | ||
| catalog, err := readCatalog(readSourceCatalogPath) | ||
| if err != nil { | ||
| ch.Logger.Error(fmt.Sprintf("Unable to read catalog: %+v", err)) | ||
| os.Exit(1) | ||
| return fmt.Errorf("unable to read catalog: %w", err) | ||
| } | ||
|
|
||
| if len(catalog.Streams) == 0 { | ||
| ch.Logger.Log(internal.LOGLEVEL_ERROR, "Catalog has no streams") | ||
| return | ||
| return nil | ||
| } | ||
|
|
||
| state := "" | ||
|
|
@@ -81,7 +82,7 @@ func ReadCommand(ch *Helper) *cobra.Command { | |
| b, err := os.ReadFile(stateFilePath) | ||
| if err != nil { | ||
| ch.Logger.Error(fmt.Sprintf("Unable to read state : %v", err)) | ||
| os.Exit(1) | ||
| return fmt.Errorf("unable to read state: %w", err) | ||
| } | ||
| state = string(b) | ||
| } | ||
|
|
@@ -90,16 +91,17 @@ func ReadCommand(ch *Helper) *cobra.Command { | |
| shards, err := ch.Database.ListShards(ctx, psc) | ||
| if err != nil { | ||
| ch.Logger.Error(fmt.Sprintf("Unable to list shards : %v", err)) | ||
| os.Exit(1) | ||
| return fmt.Errorf("unable to list shards: %w", err) | ||
| } | ||
|
|
||
| ch.Logger.Log(internal.LOGLEVEL_INFO, "Reading state") | ||
| syncState, err := readState(state, psc, catalog.Streams, shards, ch.Logger) | ||
| if err != nil { | ||
| ch.Logger.Error(fmt.Sprintf("Unable to read state : %v", err)) | ||
| os.Exit(1) | ||
| return fmt.Errorf("unable to read state: %w", err) | ||
| } | ||
|
|
||
| var readErr error | ||
| for _, configuredStream := range catalog.Streams { | ||
| keyspaceOrDatabase := configuredStream.Stream.Namespace | ||
| if keyspaceOrDatabase == "" { | ||
|
|
@@ -109,33 +111,49 @@ func ReadCommand(ch *Helper) *cobra.Command { | |
| streamState, ok := syncState.Streams[streamStateKey] | ||
| if !ok { | ||
| ch.Logger.Error(fmt.Sprintf("Unable to read state for stream %v", streamStateKey)) | ||
| os.Exit(1) | ||
| ch.Logger.StreamStatus(keyspaceOrDatabase, configuredStream.Stream.Name, internal.STREAM_STATUS_INCOMPLETE) | ||
| return fmt.Errorf("unable to read state for stream %v", streamStateKey) | ||
| } | ||
|
|
||
| ch.Logger.StreamStatus(keyspaceOrDatabase, configuredStream.Stream.Name, internal.STREAM_STATUS_STARTED) | ||
|
|
||
| streamFailed := false | ||
| for shardName, shardState := range streamState.Shards { | ||
| var tc *psdbconnectv1alpha1.TableCursor | ||
|
|
||
| tc, err = shardState.SerializedCursorToTableCursor(configuredStream) | ||
| ch.Logger.Log(internal.LOGLEVEL_INFO, fmt.Sprintf("Using serialized cursor for stream %s", streamStateKey)) | ||
| if err != nil { | ||
| ch.Logger.Error(fmt.Sprintf("Invalid serialized cursor for stream %v, failed with [%v]", streamStateKey, err)) | ||
| os.Exit(1) | ||
| streamFailed = true | ||
| break | ||
| } | ||
|
|
||
| sc, err := ch.Database.Read(ctx, cmd.OutOrStdout(), psc, configuredStream, tc) | ||
| if err != nil { | ||
| ch.Logger.Error(err.Error()) | ||
| os.Exit(1) | ||
| streamFailed = true | ||
| break | ||
| } | ||
|
|
||
| if sc != nil { | ||
| // if we get any new state, we assign it here. | ||
| // otherwise, the older state is round-tripped back to Airbyte. | ||
| syncState.Streams[streamStateKey].Shards[shardName] = sc | ||
| } | ||
| ch.Logger.State(syncState) | ||
| } | ||
|
|
||
| // Always emit state to checkpoint whatever progress was made, | ||
| // including partial progress when only some shards succeeded. | ||
| ch.Logger.StreamState(keyspaceOrDatabase, configuredStream.Stream.Name, syncState.Streams[streamStateKey]) | ||
|
|
||
| if streamFailed { | ||
| ch.Logger.StreamStatus(keyspaceOrDatabase, configuredStream.Stream.Name, internal.STREAM_STATUS_INCOMPLETE) | ||
| readErr = fmt.Errorf("read failed for stream %v", streamStateKey) | ||
| } else { | ||
| ch.Logger.StreamStatus(keyspaceOrDatabase, configuredStream.Stream.Name, internal.STREAM_STATUS_COMPLETE) | ||
| } | ||
| } | ||
|
|
||
| return readErr | ||
| }, | ||
| } | ||
| readCmd.Flags().StringVar(&readSourceCatalogPath, "catalog", "", "Path to the PlanetScale catalog configuration") | ||
|
|
@@ -153,9 +171,26 @@ func readState(state string, psc internal.PlanetScaleSource, streams []internal. | |
| Streams: map[string]internal.ShardStates{}, | ||
| } | ||
| if state != "" { | ||
| err := json.Unmarshal([]byte(state), &syncState) | ||
| if err != nil { | ||
| return syncState, err | ||
| // Try parsing as Airbyte v2 per-stream state array first | ||
| var perStreamStates []internal.AirbyteState | ||
| if err := json.Unmarshal([]byte(state), &perStreamStates); err == nil && len(perStreamStates) > 0 && perStreamStates[0].Type == internal.STATE_TYPE_STREAM { | ||
| logger.Log(internal.LOGLEVEL_INFO, fmt.Sprintf("Parsing Airbyte v2 per-stream state (%d streams)", len(perStreamStates))) | ||
| for _, s := range perStreamStates { | ||
| if s.Stream != nil && s.Stream.StreamState != nil { | ||
| ns := s.Stream.StreamDescriptor.Namespace | ||
| if ns == "" { | ||
| ns = psc.Database | ||
| } | ||
| key := ns + ":" + s.Stream.StreamDescriptor.Name | ||
|
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. Nit: there's a few areas where this key logic is duplicated. It would be nice to extract it to a shared helper to reduce the duplication and minimize the risk of drift between them. |
||
| syncState.Streams[key] = *s.Stream.StreamState | ||
| } | ||
| } | ||
| } else { | ||
| // Fall back to legacy global state format | ||
| err := json.Unmarshal([]byte(state), &syncState) | ||
| if err != nil { | ||
| return syncState, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If one shard errors and
breaks here, we skip all other shards. We shouldcontinuehere instead so we can process the other shards.Somewhat related to this, when an error occurs,
ch.Database.Readreturns both the error and the "progress-so-far" cursorsc. Since we check the error first, webreakbefore updating the stream's state withsc, which might have advanced from previous iterations before a later one times out. We should move theif sc != nil {branch below to before the error check.