|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 8 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 9 | + "go.mongodb.org/mongo-driver/v2/mongo/options" |
| 10 | + |
| 11 | + "github.com/percona-lab/percona-mongolink/config" |
| 12 | + "github.com/percona-lab/percona-mongolink/errors" |
| 13 | + "github.com/percona-lab/percona-mongolink/log" |
| 14 | +) |
| 15 | + |
| 16 | +var errConcurrentProcess = errors.New("detected concurrent process") |
| 17 | + |
| 18 | +const heartbeatID = "mongolink" |
| 19 | + |
| 20 | +type StopHeartbeat func(context.Context) error |
| 21 | + |
| 22 | +func RunHeartbeat(ctx context.Context, m *mongo.Client) (StopHeartbeat, error) { |
| 23 | + lastBeat, err := doFirstHeartbeat(ctx, m) |
| 24 | + if err != nil { |
| 25 | + return nil, errors.Wrap(err, "first") |
| 26 | + } |
| 27 | + |
| 28 | + lg := log.New("heartbeat") |
| 29 | + lg.With(log.Int64("hb", lastBeat)).Trace("") |
| 30 | + |
| 31 | + ctx, cancel := context.WithCancel(context.Background()) |
| 32 | + |
| 33 | + go func() { |
| 34 | + lastBeat := lastBeat |
| 35 | + |
| 36 | + for { |
| 37 | + time.Sleep(config.HeartbeatInternal) |
| 38 | + |
| 39 | + savedBeat, err := doHeartbeat(ctx, m, lastBeat) |
| 40 | + if err != nil { |
| 41 | + lg.Error(err, "beat") |
| 42 | + |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + lastBeat = savedBeat |
| 47 | + lg.With(log.Int64("hb", lastBeat)).Trace("") |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + stop := func(ctx context.Context) error { |
| 52 | + cancel() |
| 53 | + |
| 54 | + return DeleteHeartbeat(ctx, m) |
| 55 | + } |
| 56 | + |
| 57 | + return stop, nil |
| 58 | +} |
| 59 | + |
| 60 | +func doFirstHeartbeat(ctx context.Context, m *mongo.Client) (int64, error) { |
| 61 | + timeoutCtx, cancel := context.WithTimeout(ctx, config.HeartbeatTimeout) |
| 62 | + defer cancel() |
| 63 | + |
| 64 | + currBeat := time.Now().Unix() |
| 65 | + |
| 66 | + _, err := m.Database(config.MongoLinkDatabase). |
| 67 | + Collection(config.HeartbeatCollection). |
| 68 | + InsertOne(timeoutCtx, bson.D{{"_id", heartbeatID}, {"time", currBeat}}) |
| 69 | + if err == nil { |
| 70 | + return currBeat, nil |
| 71 | + } |
| 72 | + |
| 73 | + if !mongo.IsDuplicateKeyError(err) { |
| 74 | + return 0, err //nolint:wrapcheck |
| 75 | + } |
| 76 | + |
| 77 | + raw, err := m.Database(config.MongoLinkDatabase). |
| 78 | + Collection(config.HeartbeatCollection). |
| 79 | + FindOne(ctx, bson.D{{"_id", heartbeatID}}). |
| 80 | + Raw() |
| 81 | + if err != nil { |
| 82 | + return 0, errors.Wrap(err, "find") |
| 83 | + } |
| 84 | + |
| 85 | + lastBeat, _ := raw.Lookup("time").AsInt64OK() |
| 86 | + |
| 87 | + if time.Since(time.Unix(lastBeat, 0)) < config.StaleHeartbeatDuration { |
| 88 | + return 0, errConcurrentProcess |
| 89 | + } |
| 90 | + |
| 91 | + currBeat, err = doHeartbeat(ctx, m, lastBeat) |
| 92 | + if err != nil { |
| 93 | + return 0, errors.Wrap(err, "beat") |
| 94 | + } |
| 95 | + |
| 96 | + return currBeat, nil |
| 97 | +} |
| 98 | + |
| 99 | +func doHeartbeat(ctx context.Context, m *mongo.Client, lastBeat int64) (int64, error) { |
| 100 | + timeoutCtx, cancel := context.WithTimeout(ctx, config.HeartbeatTimeout) |
| 101 | + defer cancel() |
| 102 | + |
| 103 | + currBeat := time.Now().Unix() |
| 104 | + |
| 105 | + raw, err := m.Database(config.MongoLinkDatabase). |
| 106 | + Collection(config.HeartbeatCollection). |
| 107 | + FindOneAndUpdate(timeoutCtx, |
| 108 | + bson.D{{"_id", heartbeatID}}, |
| 109 | + bson.D{{"$set", bson.D{{"time", currBeat}}}}, |
| 110 | + options.FindOneAndUpdate().SetReturnDocument(options.Before)). |
| 111 | + Raw() |
| 112 | + if err != nil { |
| 113 | + return 0, err //nolint:wrapcheck |
| 114 | + } |
| 115 | + |
| 116 | + savedBeat, _ := raw.Lookup("time").AsInt64OK() |
| 117 | + if savedBeat != lastBeat { |
| 118 | + return 0, errConcurrentProcess |
| 119 | + } |
| 120 | + |
| 121 | + return currBeat, nil |
| 122 | +} |
| 123 | + |
| 124 | +func DeleteHeartbeat(ctx context.Context, m *mongo.Client) error { |
| 125 | + _, err := m.Database(config.MongoLinkDatabase). |
| 126 | + Collection(config.HeartbeatCollection). |
| 127 | + DeleteOne(ctx, bson.D{{"_id", heartbeatID}}) |
| 128 | + |
| 129 | + return err //nolint:wrapcheck |
| 130 | +} |
0 commit comments