Skip to content

Commit 1ed5a42

Browse files
authored
remove deprecated mongodb dynconf settings (#4297)
Remove the following env and their corresponding logic: - PEERDB_MONGODB_DIRECT_BSON_CONVERTER - PEERDB_MONGODB_PARALLEL_SNAPSHOTTING Both are performance optimizations and have since stabilized; so we can remove the legacy code.
1 parent 7a046cc commit 1ed5a42

8 files changed

Lines changed: 26 additions & 252 deletions

File tree

flow/connectors/mongo/cdc.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,7 @@ func (c *MongoConnector) PullRecords(
248248
}
249249
}
250250

251-
converter, err := NewBsonConverter(ctx, req.Env)
252-
if err != nil {
253-
return fmt.Errorf("failed to create converter: %w", err)
254-
}
251+
converter := NewDirectBsonConverter()
255252
addRecordItems := func(documentKey bson.Raw, fullDocument bson.Raw, items *model.RecordItems, tableName string) error {
256253
if len(documentKey) > 0 {
257254
rv := documentKey.Lookup(DefaultDocumentKeyColumnName)

flow/connectors/mongo/cdc_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ func TestChangeStreamIdleConnectionAdvancesOffset(t *testing.T) {
143143
TableNameSchemaMapping: map[string]*protos.TableSchema{},
144144
MaxBatchSize: 10000,
145145
IdleTimeout: time.Minute,
146-
Env: map[string]string{"PEERDB_MONGODB_DIRECT_BSON_CONVERTER": "true"},
147146
}
148147
drainMongoCDCRecordsAsync(t, req.RecordStream)
149148

flow/connectors/mongo/codec.go

Lines changed: 0 additions & 126 deletions
This file was deleted.

flow/connectors/mongo/qrep.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/PeerDB-io/peerdb/flow/connectors/utils"
1515
"github.com/PeerDB-io/peerdb/flow/generated/protos"
16-
"github.com/PeerDB-io/peerdb/flow/internal"
1716
"github.com/PeerDB-io/peerdb/flow/model"
1817
"github.com/PeerDB-io/peerdb/flow/otel_metrics"
1918
"github.com/PeerDB-io/peerdb/flow/pkg/common"
@@ -40,15 +39,6 @@ func (c *MongoConnector) GetQRepPartitions(
4039
return fullTablePartition, nil
4140
}
4241

43-
parallelSnapshotting, err := internal.PeerDBMongoDBParallelSnapshotting(ctx, config.Env)
44-
if err != nil {
45-
c.logger.Warn("failed to get parallel snapshotting config", slog.Any("error", err))
46-
}
47-
if !parallelSnapshotting {
48-
c.logger.Info("parallel snapshotting disabled, falling back to full table partition")
49-
return fullTablePartition, nil
50-
}
51-
5242
if config.NumRowsPerPartition <= 0 {
5343
return nil, fmt.Errorf("num rows per partition must be greater than 0")
5444
} else if last != nil && last.Range != nil {
@@ -163,10 +153,7 @@ func (c *MongoConnector) PullQRepRecords(
163153
}
164154
defer cursor.Close(ctx)
165155

166-
converter, err := NewBsonConverter(ctx, config.Env)
167-
if err != nil {
168-
return 0, 0, fmt.Errorf("failed to create bson converter: %w", err)
169-
}
156+
converter := NewDirectBsonConverter()
170157
for cursor.Next(ctx) {
171158
record, err := QValuesFromBsonRaw(cursor.Current, config.Version, converter, config.WatermarkTable)
172159
if err != nil {

flow/connectors/mongo/qvalue_convert.go

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package connmongo
22

33
import (
4-
"context"
54
"encoding/base64"
65
"encoding/hex"
76
"fmt"
@@ -13,80 +12,30 @@ import (
1312
"go.mongodb.org/mongo-driver/v2/bson"
1413
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
1514

16-
"github.com/PeerDB-io/peerdb/flow/internal"
1715
"github.com/PeerDB-io/peerdb/flow/shared"
1816
"github.com/PeerDB-io/peerdb/flow/shared/types"
1917
)
2018

2119
type BsonToQValueConverter interface {
22-
// QValueStringFromId QValueStringFromId converts a raw _id value to a QValueString.
20+
// QValueStringFromId converts a raw _id value to a QValueString.
2321
QValueStringFromId(id bson.RawValue, version uint32) (types.QValueString, error)
2422
// QValueJSONFromDocument converts a raw BSON document to a QValueJSON.
2523
QValueJSONFromDocument(raw bson.Raw) (types.QValueJSON, error)
2624
}
2725

28-
func NewBsonConverter(ctx context.Context, env map[string]string) (BsonToQValueConverter, error) {
29-
direct, err := internal.PeerDBMongoDBDirectBsonConverter(ctx, env)
30-
if err != nil {
31-
return nil, err
32-
}
33-
34-
if !direct {
35-
return &LegacyBsonConverter{
36-
api: CreateExtendedJSONMarshaler(),
37-
}, nil
38-
}
26+
// DirectBsonConverter converts BSON directly to JSON string without intermediate deserialization,
27+
// it uses jsoniter.Stream to build JSON output incrementally into a reusable buffer (to avoid allocation)
28+
type DirectBsonConverter struct {
29+
stream *jsoniter.Stream
30+
}
3931

32+
func NewDirectBsonConverter() *DirectBsonConverter {
4033
return &DirectBsonConverter{
4134
// technically we write JSON directly via raw stream methods and do not use jsoniter's
4235
// config-driven serialization specified here, but this config is applied consistent with our
43-
// custom serialization (and used by LegacyBsonConverter) so specifying it here for consistency
36+
// custom serialization so specifying it here for consistency
4437
stream: jsoniter.NewStream(jsoniter.ConfigCompatibleWithStandardLibrary, nil, 512),
45-
}, nil
46-
}
47-
48-
// LegacyBsonConverter converts BSON to JSON by first deserializing BSON to bson.D,
49-
// and then serialize to JSON string via json-iterator.
50-
type LegacyBsonConverter struct {
51-
api jsoniter.API
52-
}
53-
54-
func (c *LegacyBsonConverter) QValueJSONFromDocument(raw bson.Raw) (types.QValueJSON, error) {
55-
var d bson.D
56-
if err := bson.Unmarshal(raw, &d); err != nil {
57-
return types.QValueJSON{}, fmt.Errorf("failed to unmarshal document: %w", err)
58-
}
59-
jsonb, err := c.api.Marshal(d)
60-
if err != nil {
61-
return types.QValueJSON{}, fmt.Errorf("failed to marshal document: %w", err)
6238
}
63-
return types.QValueJSON{Val: string(jsonb)}, nil
64-
}
65-
66-
func (c *LegacyBsonConverter) QValueStringFromId(id bson.RawValue, version uint32) (types.QValueString, error) {
67-
if version >= shared.InternalVersion_MongoDBIdWithoutRedundantQuotes {
68-
switch id.Type {
69-
case bson.TypeObjectID:
70-
return types.QValueString{Val: id.ObjectID().Hex()}, nil
71-
case bson.TypeString:
72-
return types.QValueString{Val: id.StringValue()}, nil
73-
}
74-
}
75-
var val any
76-
if err := id.Unmarshal(&val); err != nil {
77-
return types.QValueString{}, fmt.Errorf("failed to unmarshal %s: %w", DefaultDocumentKeyColumnName, err)
78-
}
79-
jsonb, err := c.api.Marshal(val)
80-
if err != nil {
81-
return types.QValueString{}, fmt.Errorf("failed to marshal %s: %w", DefaultDocumentKeyColumnName, err)
82-
}
83-
return types.QValueString{Val: string(jsonb)}, nil
84-
}
85-
86-
// DirectBsonConverter converts BSON directly to JSON string without intermediate deserialization,
87-
// it uses jsoniter.Stream to build JSON output incrementally into a reusable buffer (to avoid allocation)
88-
type DirectBsonConverter struct {
89-
stream *jsoniter.Stream
9039
}
9140

9241
func (c *DirectBsonConverter) QValueJSONFromDocument(raw bson.Raw) (types.QValueJSON, error) {
@@ -276,8 +225,15 @@ func rawValueToJSON(v bsoncore.Value, stream *jsoniter.Stream) error {
276225
return nil
277226
}
278227

279-
// writeFloat64JSON matches the encoding behavior of encodeCustom + writeFloat64WithExplicitDecimal:
280-
// NaN/Inf → quoted strings, integer-valued floats → explicit ".0", others → standard notation
228+
// Assume (and test) that values outside of these limits will come out in scientific notation
229+
// and will be parsed as floats either way
230+
var (
231+
floatLimit = math.Pow10(21)
232+
floatNegLimit = -floatLimit
233+
)
234+
235+
// writeFloat64JSON encodes NaN/Inf as quoted strings, integer-valued floats with an explicit
236+
// ".0" suffix (to hint ClickHouse to parse as float), and other values in standard notation.
281237
func writeFloat64JSON(stream *jsoniter.Stream, v float64) {
282238
if math.IsNaN(v) {
283239
stream.WriteRaw(`"NaN"`)

flow/connectors/mongo/codec_test.go renamed to flow/connectors/mongo/qvalue_convert_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package connmongo
22

33
import (
4-
"context"
54
"encoding/base64"
65
"fmt"
76
"math"
@@ -631,22 +630,15 @@ func TestMarshalDocument(t *testing.T) {
631630
},
632631
}
633632

634-
directConverter, err := NewBsonConverter(context.Background(), map[string]string{"PEERDB_MONGODB_DIRECT_BSON_CONVERTER": "true"})
635-
require.NoError(t, err)
636-
legacyConverter, err := NewBsonConverter(context.Background(), map[string]string{"PEERDB_MONGODB_DIRECT_BSON_CONVERTER": "false"})
637-
require.NoError(t, err)
633+
converter := NewDirectBsonConverter()
638634
for _, test := range tests {
639635
t.Run(test.desc, func(t *testing.T) {
640636
inputRaw, err := bson.Marshal(test.input)
641637
require.NoError(t, err)
642638

643-
directResult, err := directConverter.QValueJSONFromDocument(inputRaw)
644-
require.NoError(t, err)
645-
require.Equal(t, test.expected, directResult.Val)
646-
647-
legacyResult, err := legacyConverter.QValueJSONFromDocument(inputRaw)
639+
result, err := converter.QValueJSONFromDocument(inputRaw)
648640
require.NoError(t, err)
649-
require.Equal(t, directResult, legacyResult)
641+
require.Equal(t, test.expected, result.Val)
650642
})
651643
}
652644
}
@@ -664,8 +656,7 @@ func TestMarshalId(t *testing.T) {
664656
require.NoError(t, err)
665657
return bson.Raw(raw).Lookup("_id")
666658
}
667-
converter, err := NewBsonConverter(context.Background(), nil)
668-
require.NoError(t, err)
659+
converter := NewDirectBsonConverter()
669660

670661
objectId, err := bson.ObjectIDFromHex("6893edbecb1f9508891bbb84")
671662
require.NoError(t, err)
@@ -704,8 +695,7 @@ func TestMarshalId(t *testing.T) {
704695
// Tests that floats of all magnitudes are marshaled into a reasonable length and have a signifier
705696
// that they're not integers
706697
func TestMarshalFloatLengths(t *testing.T) {
707-
converter, err := NewBsonConverter(context.Background(), nil)
708-
require.NoError(t, err)
698+
converter := NewDirectBsonConverter()
709699
maxExponent := 309
710700
require.Equal(t, math.Inf(1), math.Pow10(maxExponent), "exponent range should cover +Inf") //nolint:testifylint
711701
require.Equal(t, math.Inf(-1), -math.Pow10(maxExponent), "exponent range should cover -Inf") //nolint:testifylint
@@ -754,8 +744,7 @@ func TestMarshalFloatLengths(t *testing.T) {
754744
}
755745

756746
func TestQValuesFromBsonRawInvalidIds(t *testing.T) {
757-
converter, err := NewBsonConverter(t.Context(), map[string]string{"PEERDB_MONGODB_DIRECT_BSON_CONVERTER": "true"})
758-
require.NoError(t, err)
747+
converter := NewDirectBsonConverter()
759748

760749
t.Run("null _id is rejected", func(t *testing.T) {
761750
raw, err := bson.Marshal(bson.D{{Key: "_id", Value: nil}})

flow/e2e/mongo.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ func (s *MongoSource) GetRows(ctx context.Context, suffix, table, cols string) (
7171
Records: nil,
7272
}
7373

74-
converter, err := connmongo.NewBsonConverter(ctx, nil)
75-
if err != nil {
76-
return nil, err
77-
}
74+
converter := connmongo.NewDirectBsonConverter()
7875

7976
for cursor.Next(ctx) {
8077
record, err := connmongo.QValuesFromBsonRaw(cursor.Current, shared.InternalVersion_Latest, converter, table)

0 commit comments

Comments
 (0)