Skip to content

Commit 5b0ffb0

Browse files
committed
Reorder code
1 parent 19c30f3 commit 5b0ffb0

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

flow/cmd/validate_mirror.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/PeerDB-io/peerdb/flow/generated/protos"
1717
"github.com/PeerDB-io/peerdb/flow/internal"
1818
"github.com/PeerDB-io/peerdb/flow/pkg/common"
19-
mysql_validation "github.com/PeerDB-io/peerdb/flow/pkg/mysql"
2019
"github.com/PeerDB-io/peerdb/flow/shared"
2120
"github.com/PeerDB-io/peerdb/flow/shared/types"
2221
)
@@ -188,25 +187,6 @@ func (h *FlowRequestHandler) checkSourcePeerReuse(
188187
return nil
189188
}
190189

191-
// beyond other PeerDB mirrors (checked below), the pinned server_id must not be already registered
192-
// as a replica on the source DB itself.
193-
mysqlConn, mysqlClose, err := connectors.GetAs[*connmysql.MySqlConnector](ctx, cfg.Env, peer)
194-
if err != nil {
195-
return NewInternalApiError(fmt.Errorf("failed to create MySQL connector for source peer %s: %w", cfg.SourceName, err))
196-
}
197-
defer mysqlClose(ctx)
198-
199-
if inUse, err := mysql_validation.HasReplicaWithServerId(mysqlConn.Conn(), *mysqlCfg.ServerId); err != nil {
200-
return NewInternalApiError(fmt.Errorf("failed to check replicas registered on source peer %s: %w", cfg.SourceName, err))
201-
} else if inUse {
202-
return NewFailedPreconditionApiError(
203-
fmt.Errorf("source peer %q pins server_id=%d, which is already in use by a replica registered on the source database",
204-
cfg.SourceName, *mysqlCfg.ServerId),
205-
NewMirrorErrorInfo(map[string]string{
206-
common.ErrorMetadataOffendingField: "source_name",
207-
}))
208-
}
209-
210190
// CDC flows for this source peer other than the mirror being validated.
211191
// query_string IS NULL filters out QRep flows, which do
212192
// not stream the binlog.
@@ -249,6 +229,25 @@ func (h *FlowRequestHandler) checkSourcePeerReuse(
249229
return NewInternalApiError(fmt.Errorf("failed to iterate flows while checking peer reuse for %s: %w", cfg.SourceName, err))
250230
}
251231

232+
// Beyond other PeerDB mirrors (checked above), the pinned server_id must not be already
233+
// registered as a replica on the source DB itself.
234+
mysqlConn, mysqlClose, err := connectors.GetAs[*connmysql.MySqlConnector](ctx, cfg.Env, peer)
235+
if err != nil {
236+
return NewInternalApiError(fmt.Errorf("failed to create MySQL connector for source peer %s: %w", cfg.SourceName, err))
237+
}
238+
defer mysqlClose(ctx)
239+
240+
if inUse, err := mysqlConn.HasReplicaWithServerId(ctx, *mysqlCfg.ServerId); err != nil {
241+
return NewInternalApiError(fmt.Errorf("failed to check replicas registered on source peer %s: %w", cfg.SourceName, err))
242+
} else if inUse {
243+
return NewFailedPreconditionApiError(
244+
fmt.Errorf("source peer %q pins server_id=%d, which is already in use by a replica registered on the source database",
245+
cfg.SourceName, *mysqlCfg.ServerId),
246+
NewMirrorErrorInfo(map[string]string{
247+
common.ErrorMetadataOffendingField: "source_name",
248+
}))
249+
}
250+
252251
return nil
253252
}
254253

flow/connectors/mysql/validate.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ func (c *MySqlConnector) ValidateMirrorSource(ctx context.Context, cfg *protos.F
138138
return nil
139139
}
140140

141+
// HasReplicaWithServerId reports whether the source lists a replica registered with serverID.
142+
func (c *MySqlConnector) HasReplicaWithServerId(ctx context.Context, serverID uint32) (bool, error) {
143+
conn, err := c.connect(ctx)
144+
if err != nil {
145+
return false, fmt.Errorf("failed to connect: %w", err)
146+
}
147+
return mysql_validation.HasReplicaWithServerId(conn, serverID)
148+
}
149+
141150
func (c *MySqlConnector) ValidateCheck(ctx context.Context) error {
142151
if c.config.Flavor == protos.MySqlFlavor_MYSQL_UNKNOWN {
143152
return fmt.Errorf("flavor is set to unknown")

0 commit comments

Comments
 (0)