-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logical: Add support for backfilling
This change adds an explicit API for logical dialects which can support faster backfilling of data. The goal is to use fan mode during a backfill and then switch to a fully-transactional mode once the data load has been completed. The backfill mode is gated behind an --backfillWindow flag, to ensure that the operator is aware that the database may not be transactionally-consistent with respect to the source if replication lags by more than the specified window. A memoizing logical.Factory type is added to support a future use case that is best expressed as a collection of independent replication loops that share the target stack. The logical.Backfiller interface is also added to support future work. The loop type no longer implements the loop.Event interface. Instead, we introduce a fan- and a serial-mode implementation, which are chosen based on the mode in which the loop is operating. Wrappers are added to suport chaos and metrics collection. The pglogical and mylogical packages are updated to support the revised API.
- Loading branch information
Showing
25 changed files
with
912 additions
and
468 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package logical | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/cockroachdb/cdc-sink/internal/target/apply/fan" | ||
"github.com/cockroachdb/cdc-sink/internal/types" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
) | ||
|
||
// Factory supports uses cases where it is desirable to have multiple, | ||
// independent logical loops that share common resources. | ||
type Factory struct { | ||
appliers types.Appliers | ||
cfg *Config | ||
fans *fan.Fans | ||
memo types.Memo | ||
pool *pgxpool.Pool | ||
|
||
mu struct { | ||
sync.Mutex | ||
cancels []func() | ||
loops map[string]*Loop | ||
} | ||
} | ||
|
||
// Close terminates all running loops and waits for them to shut down. | ||
func (f *Factory) Close() { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
for _, cancel := range f.mu.cancels { | ||
// These are just context-cancellations functions that return | ||
// immediately, so we need to wait for the loops to stop below. | ||
cancel() | ||
} | ||
for _, loop := range f.mu.loops { | ||
<-loop.Stopped() | ||
} | ||
f.mu.cancels = nil | ||
f.mu.loops = make(map[string]*Loop) | ||
} | ||
|
||
// Get constructs or retrieves the named Loop. | ||
func (f *Factory) Get(ctx context.Context, name string, dialect Dialect) (*Loop, error) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
|
||
if found, ok := f.mu.loops[name]; ok { | ||
return found, nil | ||
} | ||
|
||
cfg := f.cfg.Copy() | ||
if cfg.LoopName == "" { | ||
cfg.LoopName = name | ||
} else { | ||
cfg.LoopName = fmt.Sprintf("%s-%s", cfg.LoopName, name) | ||
} | ||
|
||
ret, cancel, err := ProvideLoop(ctx, f.appliers, cfg, dialect, f.fans, f.memo, f.pool) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.mu.loops[name] = ret | ||
f.mu.cancels = append(f.mu.cancels, cancel) | ||
return ret, nil | ||
} |
Oops, something went wrong.