-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default logrepl autoCleanup mode to remove replication slots and …
…publications (#139) * Add default logrepl autoCleanup mode to remove replication slots and publications The new option `logrepl.autoCleanup` is not only valid in the context of logical replication. The default is set to true and will remove the replication slot and publication when the connector is deleted. This change is a departure from the current implementation, in the following areas: 1. Replication slots are not `temporary` any longer and will persist through pipeline state changes. 2. An implication of 1) the slot will need to be cleaned up, hence why the `autoCleanup` option is set to true. In cases where the replication slots and publications are pre-created, ensure `logrepl.autoCleanup` is set to `false`. * cr; add note in docs about slots/pub being deleted on connector deletion
- Loading branch information
Showing
9 changed files
with
272 additions
and
14 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,83 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logrepl | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/conduitio/conduit-connector-postgres/source/logrepl/internal" | ||
sdk "github.com/conduitio/conduit-connector-sdk" | ||
"github.com/jackc/pglogrepl" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
) | ||
|
||
type CleanupConfig struct { | ||
URL string | ||
SlotName string | ||
PublicationName string | ||
} | ||
|
||
// Cleanup drops the provided replication and publication. | ||
func Cleanup(ctx context.Context, c CleanupConfig) error { | ||
pgconfig, err := pgconn.ParseConfig(c.URL) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse config URL: %w", err) | ||
} | ||
|
||
if pgconfig.RuntimeParams == nil { | ||
pgconfig.RuntimeParams = make(map[string]string) | ||
} | ||
pgconfig.RuntimeParams["replication"] = "database" | ||
|
||
conn, err := pgconn.ConnectConfig(ctx, pgconfig) | ||
if err != nil { | ||
return fmt.Errorf("could not establish replication connection: %w", err) | ||
} | ||
defer conn.Close(ctx) | ||
|
||
var errs []error | ||
|
||
if c.SlotName != "" { | ||
if err := pglogrepl.DropReplicationSlot( | ||
ctx, | ||
conn, | ||
c.SlotName, | ||
pglogrepl.DropReplicationSlotOptions{}, | ||
); err != nil { | ||
errs = append(errs, fmt.Errorf("failed to clean up replication slot %q: %w", c.SlotName, err)) | ||
} | ||
} else { | ||
sdk.Logger(ctx).Warn(). | ||
Msg("cleanup: skipping replication slot cleanup, name is empty") | ||
} | ||
|
||
if c.PublicationName != "" { | ||
if err := internal.DropPublication( | ||
ctx, | ||
conn, | ||
c.PublicationName, | ||
internal.DropPublicationOptions{IfExists: true}, | ||
); err != nil { | ||
errs = append(errs, fmt.Errorf("failed to clean up publication %q: %w", c.PublicationName, err)) | ||
} | ||
} else { | ||
sdk.Logger(ctx).Warn(). | ||
Msg("cleanup: skipping publication cleanup, name is empty") | ||
} | ||
|
||
return errors.Join(errs...) | ||
} |
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,111 @@ | ||
// Copyright © 2024 Meroxa, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logrepl | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/conduitio/conduit-connector-postgres/test" | ||
"github.com/matryer/is" | ||
) | ||
|
||
func Test_Cleanup(t *testing.T) { | ||
conn := test.ConnectSimple(context.Background(), t, test.RepmgrConnString) | ||
|
||
tests := []struct { | ||
desc string | ||
setup func(t *testing.T) | ||
conf CleanupConfig | ||
|
||
wantErr error | ||
}{ | ||
{ | ||
desc: "drops slot and pub", | ||
conf: CleanupConfig{ | ||
URL: test.RepmgrConnString, | ||
SlotName: "conduitslot1", | ||
PublicationName: "conduitpub1", | ||
}, | ||
setup: func(t *testing.T) { | ||
test.CreatePublication(t, conn, "conduitpub1") | ||
test.CreateReplicationSlot(t, conn, "conduitslot1") | ||
}, | ||
}, | ||
{ | ||
desc: "drops pub, slot unspecified", | ||
conf: CleanupConfig{ | ||
URL: test.RepmgrConnString, | ||
PublicationName: "conduitpub2", | ||
}, | ||
setup: func(t *testing.T) { | ||
test.CreatePublication(t, conn, "conduitpub2") | ||
}, | ||
}, | ||
{ | ||
desc: "drops slot, pub unspecified", | ||
conf: CleanupConfig{ | ||
URL: test.RepmgrConnString, | ||
SlotName: "conduitslot3", | ||
}, | ||
setup: func(t *testing.T) { | ||
test.CreateReplicationSlot(t, conn, "conduitslot3") | ||
}, | ||
}, | ||
{ | ||
desc: "drops pub, slot missing", | ||
conf: CleanupConfig{ | ||
URL: test.RepmgrConnString, | ||
SlotName: "conduitslot4", | ||
PublicationName: "conduitpub4", | ||
}, | ||
setup: func(t *testing.T) { | ||
test.CreatePublication(t, conn, "conduitpub4") | ||
}, | ||
wantErr: errors.New(`replication slot "conduitslot4" does not exist`), | ||
}, | ||
{ | ||
desc: "drops slot, pub missing", // no op | ||
conf: CleanupConfig{ | ||
URL: test.RepmgrConnString, | ||
SlotName: "conduitslot5", | ||
PublicationName: "conduitpub5", | ||
}, | ||
setup: func(t *testing.T) { | ||
test.CreateReplicationSlot(t, conn, "conduitslot5") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
is := is.New(t) | ||
|
||
if tc.setup != nil { | ||
tc.setup(t) | ||
} | ||
|
||
err := Cleanup(context.Background(), tc.conf) | ||
|
||
if tc.wantErr != nil { | ||
is.True(strings.Contains(err.Error(), tc.wantErr.Error())) | ||
} else { | ||
is.NoErr(err) | ||
} | ||
}) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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