|
| 1 | +package connmongo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/google/uuid" |
| 12 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 13 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 14 | + "go.mongodb.org/mongo-driver/v2/mongo/options" |
| 15 | + |
| 16 | + "github.com/PeerDB-io/peerdb/flow/connectors/utils" |
| 17 | + "github.com/PeerDB-io/peerdb/flow/generated/protos" |
| 18 | + "github.com/PeerDB-io/peerdb/flow/shared" |
| 19 | +) |
| 20 | + |
| 21 | +// objectIDMinForTimestamp creates an ObjectID with the given timestamp and |
| 22 | +// all trailing bytes set to 0x00, producing the smallest possible ObjectID |
| 23 | +// for that second. |
| 24 | +func objectIDMinForTimestamp(t time.Time) bson.ObjectID { |
| 25 | + var oid [12]byte |
| 26 | + binary.BigEndian.PutUint32(oid[0:4], uint32(t.Unix())) |
| 27 | + return oid |
| 28 | +} |
| 29 | + |
| 30 | +// objectIDMaxForTimestamp creates an ObjectID with the given timestamp and |
| 31 | +// all trailing bytes set to 0xFF, producing the largest possible ObjectID |
| 32 | +// for that second. |
| 33 | +func objectIDMaxForTimestamp(t time.Time) bson.ObjectID { |
| 34 | + var oid [12]byte |
| 35 | + binary.BigEndian.PutUint32(oid[0:4], uint32(t.Unix())) |
| 36 | + for i := 4; i < 12; i++ { |
| 37 | + oid[i] = 0xFF |
| 38 | + } |
| 39 | + return oid |
| 40 | +} |
| 41 | + |
| 42 | +// minMaxPartitions creates partitions by querying only the min and max _id |
| 43 | +// from the collection (leveraging the default _id index), then uniformly |
| 44 | +// dividing the timestamp range encoded in the ObjectIDs. |
| 45 | +func (c *MongoConnector) minMaxPartitions( |
| 46 | + ctx context.Context, |
| 47 | + collection *mongo.Collection, |
| 48 | + numPartitions int64, |
| 49 | +) ([]*protos.QRepPartition, error) { |
| 50 | + fullTablePartition := []*protos.QRepPartition{{ |
| 51 | + PartitionId: utils.FullTablePartitionID, |
| 52 | + Range: nil, |
| 53 | + FullTablePartition: true, |
| 54 | + }} |
| 55 | + if numPartitions == 1 { |
| 56 | + c.logger.Info("[mongo] using full table partition for single partition") |
| 57 | + return fullTablePartition, nil |
| 58 | + } |
| 59 | + |
| 60 | + minID, err := findBoundaryObjectID(ctx, collection, 1) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to find min _id: %w", err) |
| 63 | + } |
| 64 | + maxID, err := findBoundaryObjectID(ctx, collection, -1) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to find max _id: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + tsMin := minID.Timestamp() |
| 70 | + tsMax := maxID.Timestamp() |
| 71 | + tsRange := tsMax.Unix() - tsMin.Unix() |
| 72 | + if tsRange <= 0 { |
| 73 | + c.logger.Info("[mongo] min/max timestamps are equal, returning single partition") |
| 74 | + return fullTablePartition, nil |
| 75 | + } |
| 76 | + |
| 77 | + c.logger.Info("[mongo] using min/max ObjectID timestamp partitioning", |
| 78 | + slog.Time("tsMin", tsMin), |
| 79 | + slog.Time("tsMax", tsMax), |
| 80 | + slog.Int64("numPartitions", numPartitions)) |
| 81 | + |
| 82 | + secondsPerPartition := shared.DivCeil(tsRange, numPartitions) |
| 83 | + partitions := make([]*protos.QRepPartition, 0, numPartitions) |
| 84 | + for i := range numPartitions { |
| 85 | + var start, end bson.ObjectID |
| 86 | + if i == 0 { |
| 87 | + start = minID |
| 88 | + } else { |
| 89 | + start = objectIDMinForTimestamp(time.Unix(tsMin.Unix()+secondsPerPartition*i, 0)) |
| 90 | + } |
| 91 | + if i == numPartitions-1 { |
| 92 | + end = maxID |
| 93 | + } else { |
| 94 | + end = objectIDMaxForTimestamp(time.Unix(tsMin.Unix()+secondsPerPartition*(i+1)-1, 0)) |
| 95 | + } |
| 96 | + partitions = append(partitions, &protos.QRepPartition{ |
| 97 | + PartitionId: uuid.NewString(), |
| 98 | + Range: &protos.PartitionRange{ |
| 99 | + Range: &protos.PartitionRange_ObjectIdRange{ |
| 100 | + ObjectIdRange: &protos.ObjectIdPartitionRange{ |
| 101 | + Start: start.Hex(), |
| 102 | + End: end.Hex(), |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + FullTablePartition: false, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + return partitions, nil |
| 111 | +} |
| 112 | + |
| 113 | +// findBoundaryObjectID returns the min (direction=1) or max (direction=-1) _id |
| 114 | +// in the collection. Because _id is always indexed, this is an efficient O(log n) operation. |
| 115 | +func findBoundaryObjectID( |
| 116 | + ctx context.Context, |
| 117 | + collection *mongo.Collection, |
| 118 | + direction int, |
| 119 | +) (bson.ObjectID, error) { |
| 120 | + findCmd := bson.D{ |
| 121 | + {Key: "find", Value: collection.Name()}, |
| 122 | + {Key: "sort", Value: bson.D{{Key: DefaultDocumentKeyColumnName, Value: direction}}}, |
| 123 | + {Key: "limit", Value: 1}, |
| 124 | + {Key: "projection", Value: bson.D{{Key: DefaultDocumentKeyColumnName, Value: 1}}}, |
| 125 | + } |
| 126 | + |
| 127 | + cursor, err := collection.Database().RunCommandCursor(ctx, findCmd, options.RunCmd()) |
| 128 | + if err != nil { |
| 129 | + return bson.NilObjectID, fmt.Errorf("failed to run find command: %w", err) |
| 130 | + } |
| 131 | + defer cursor.Close(ctx) |
| 132 | + |
| 133 | + if !cursor.Next(ctx) { |
| 134 | + return bson.NilObjectID, fmt.Errorf("collection is empty") |
| 135 | + } |
| 136 | + var doc struct { |
| 137 | + ID bson.ObjectID `bson:"_id"` |
| 138 | + } |
| 139 | + if err := bson.Unmarshal(cursor.Current, &doc); err != nil { |
| 140 | + return bson.NilObjectID, fmt.Errorf("failed to unmarshal boundary document: %w", err) |
| 141 | + } |
| 142 | + return doc.ID, nil |
| 143 | +} |
| 144 | + |
| 145 | +// bucketAutoPartitions uses MongoDB's $bucketAuto aggregation to create partitions. |
| 146 | +// This performs a full collection scan, so it is significantly slower on large collections |
| 147 | +// but produces well-balanced partitions. |
| 148 | +func (c *MongoConnector) bucketAutoPartitions( |
| 149 | + ctx context.Context, |
| 150 | + collection *mongo.Collection, |
| 151 | + watermarkColumn string, |
| 152 | + numPartitions int64, |
| 153 | +) ([]*protos.QRepPartition, error) { |
| 154 | + bucketAutoPipeline := []bson.D{ |
| 155 | + { |
| 156 | + {Key: "$bucketAuto", Value: bson.D{ |
| 157 | + {Key: "groupBy", Value: "$" + watermarkColumn}, |
| 158 | + {Key: "buckets", Value: numPartitions}, |
| 159 | + }}, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + cursor, err := collection.Aggregate(ctx, bucketAutoPipeline) |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("failed to aggregate for bucket partitions: %w", err) |
| 166 | + } |
| 167 | + defer cursor.Close(ctx) |
| 168 | + |
| 169 | + partitions := make([]*protos.QRepPartition, 0, numPartitions) |
| 170 | + for cursor.Next(ctx) { |
| 171 | + var bucket struct { |
| 172 | + ID struct { |
| 173 | + Min bson.ObjectID `bson:"min"` |
| 174 | + Max bson.ObjectID `bson:"max"` |
| 175 | + } `bson:"_id"` |
| 176 | + } |
| 177 | + if err := bson.Unmarshal(cursor.Current, &bucket); err != nil { |
| 178 | + return nil, fmt.Errorf("failed to unmarshal bucket: %w", err) |
| 179 | + } |
| 180 | + |
| 181 | + partitions = append(partitions, &protos.QRepPartition{ |
| 182 | + PartitionId: uuid.NewString(), |
| 183 | + Range: &protos.PartitionRange{ |
| 184 | + Range: &protos.PartitionRange_ObjectIdRange{ |
| 185 | + ObjectIdRange: &protos.ObjectIdPartitionRange{ |
| 186 | + Start: bucket.ID.Min.Hex(), |
| 187 | + End: bucket.ID.Max.Hex(), |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + FullTablePartition: false, |
| 192 | + }) |
| 193 | + } |
| 194 | + if err := cursor.Err(); err != nil { |
| 195 | + if errors.Is(err, context.Canceled) { |
| 196 | + c.logger.Warn("context canceled while performing bucketAuto aggregation") |
| 197 | + } else { |
| 198 | + c.logger.Error("error while performing bucketAuto aggregation", |
| 199 | + slog.String("error", err.Error())) |
| 200 | + } |
| 201 | + return nil, fmt.Errorf("cursor error during bucketAuto aggregation: %w", err) |
| 202 | + } |
| 203 | + |
| 204 | + return partitions, nil |
| 205 | +} |
0 commit comments