Skip to content

Commit 6a2dc7a

Browse files
committed
separate out network error from invalid object id
1 parent a40374b commit 6a2dc7a

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

flow/connectors/mongo/qrep_partition.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,19 @@ func (c *MongoConnector) minMaxPartitions(
3333
FullTablePartition: true,
3434
}}
3535

36-
minRaw, maxRaw, err := findMinMaxObjectIDs(ctx, collection, c.config.ReadPreference)
36+
minID, maxID, found, err := findMinMaxObjectIDs(ctx, collection, c.config.ReadPreference)
3737
if err != nil {
38-
c.logger.Warn("[mongo] could not find min/max objectIDs, falling back to full table partition",
39-
slog.String("reason", err.Error()))
38+
return nil, err
39+
}
40+
if !found {
41+
c.logger.Info("[mongo] no valid min/max ObjectIDs found, falling back to full table partition")
4042
return fullTablePartition, nil
4143
}
42-
43-
minID := minRaw.ObjectID()
44-
maxID := maxRaw.ObjectID()
45-
4644
minInt := new(big.Int).SetBytes(minID[:])
4745
maxInt := new(big.Int).SetBytes(maxID[:])
4846
intRange := new(big.Int).Sub(maxInt, minInt)
4947
if intRange.Sign() <= 0 {
50-
c.logger.Info("[mongo] min/max ObjectIDs are equal, falling back to full table partition")
48+
c.logger.Info("[mongo] min/max ObjectID range is non-positive, falling back to full table partition")
5149
return fullTablePartition, nil
5250
}
5351

@@ -109,25 +107,24 @@ func findMinMaxObjectIDs(
109107
ctx context.Context,
110108
collection *mongo.Collection,
111109
readPreference protos.ReadPreference,
112-
) (bson.RawValue, bson.RawValue, error) {
110+
) (bson.ObjectID, bson.ObjectID, bool, error) {
113111
minRaw, err := findBoundaryID(ctx, collection, Lower, readPreference)
114112
if err != nil {
115-
return bson.RawValue{}, bson.RawValue{}, fmt.Errorf("failed to find min _id: %w", err)
113+
return bson.ObjectID{}, bson.ObjectID{}, false, fmt.Errorf("failed to find min _id: %w", err)
114+
}
115+
if minRaw.Type != bson.TypeObjectID {
116+
return bson.ObjectID{}, bson.ObjectID{}, false, nil
116117
}
117118

118119
maxRaw, err := findBoundaryID(ctx, collection, Upper, readPreference)
119120
if err != nil {
120-
return bson.RawValue{}, bson.RawValue{}, fmt.Errorf("failed to find max _id: %w", err)
121+
return bson.ObjectID{}, bson.ObjectID{}, false, fmt.Errorf("failed to find max _id: %w", err)
121122
}
122-
123-
// Given MongoDB's type-ordered _id, if both min and max are ObjectID, then every
124-
// document in the collection must have an ObjectID _id.
125-
if minRaw.Type != bson.TypeObjectID || maxRaw.Type != bson.TypeObjectID {
126-
return bson.RawValue{}, bson.RawValue{}, fmt.Errorf(
127-
"_id contains non-ObjectID type (min id type=%s, max id type=%s)",
128-
minRaw.Type.String(), maxRaw.Type.String())
123+
if maxRaw.Type != bson.TypeObjectID {
124+
return bson.ObjectID{}, bson.ObjectID{}, false, nil
129125
}
130-
return minRaw, maxRaw, nil
126+
127+
return minRaw.ObjectID(), maxRaw.ObjectID(), true, nil
131128
}
132129

133130
type Boundary int
@@ -158,11 +155,11 @@ func findBoundaryID(
158155
defer cursor.Close(ctx)
159156

160157
if !cursor.Next(ctx) {
161-
return bson.RawValue{}, fmt.Errorf("collection is empty")
162-
}
163-
rv := cursor.Current.Lookup(DefaultDocumentKeyColumnName)
164-
if rv.IsZero() {
165-
return bson.RawValue{}, fmt.Errorf("document _id is nil")
158+
if err := cursor.Err(); err != nil {
159+
return bson.RawValue{}, fmt.Errorf("cursor error: %w", err)
160+
}
161+
// no results
162+
return bson.RawValue{}, nil
166163
}
167-
return rv, nil
164+
return cursor.Current.Lookup(DefaultDocumentKeyColumnName), nil
168165
}

0 commit comments

Comments
 (0)