YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tserver/heartbeater.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_TSERVER_HEARTBEATER_H
33
#define YB_TSERVER_HEARTBEATER_H
34
35
#include <memory>
36
37
#include "yb/server/server_base_options.h"
38
39
#include "yb/gutil/macros.h"
40
#include "yb/master/master_fwd.h"
41
#include "yb/master/master_heartbeat.fwd.h"
42
#include "yb/tserver/tserver_fwd.h"
43
#include "yb/util/status_fwd.h"
44
#include "yb/util/net/net_util.h"
45
46
namespace yb {
47
namespace tserver {
48
49
// Interface data providers to be used for filling data into heartbeat request.
50
// Data provider could fill in data into TSHeartbeatRequestPB that will be send by Heartbeater
51
// after that.
52
class HeartbeatDataProvider {
53
 public:
54
5.81k
  explicit HeartbeatDataProvider(TabletServer* server) : server_(*CHECK_NOTNULL(server)) {}
55
73
  virtual ~HeartbeatDataProvider() {}
56
57
  // Add data to heartbeat, provider could skip and do nothing if is it too early for example for
58
  // periodical provider.
59
  // Called on every heartbeat from Heartbeater::Thread::TryHeartbeat.
60
  virtual void AddData(
61
      const master::TSHeartbeatResponsePB& last_resp, master::TSHeartbeatRequestPB* req) = 0;
62
63
  const std::string& LogPrefix() const;
64
65
189k
  TabletServer& server() { return server_; }
66
67
 private:
68
  TabletServer& server_;
69
};
70
71
// Component of the Tablet Server which is responsible for heartbeating to the
72
// leader master.
73
//
74
// TODO: send heartbeats to non-leader masters.
75
class Heartbeater {
76
 public:
77
  Heartbeater(
78
      const TabletServerOptions& options, TabletServer* server,
79
      std::vector<std::unique_ptr<HeartbeatDataProvider>>&& data_providers);
80
  Heartbeater(const Heartbeater& other) = delete;
81
  void operator=(const Heartbeater& other) = delete;
82
83
  CHECKED_STATUS Start();
84
  CHECKED_STATUS Stop();
85
86
  // Trigger a heartbeat as soon as possible, even if the normal
87
  // heartbeat interval has not expired.
88
  void TriggerASAP();
89
90
  void set_master_addresses(server::MasterAddressesPtr master_addresses);
91
92
  ~Heartbeater();
93
94
 private:
95
  class Thread;
96
  std::unique_ptr<Thread> thread_;
97
};
98
99
class PeriodicalHeartbeatDataProvider : public HeartbeatDataProvider {
100
 public:
101
  PeriodicalHeartbeatDataProvider(TabletServer* server, const MonoDelta& period) :
102
5.81k
    HeartbeatDataProvider(server), period_(period) {}
103
104
  void AddData(
105
      const master::TSHeartbeatResponsePB& last_resp, master::TSHeartbeatRequestPB* req) override;
106
107
37.7k
  CoarseTimePoint prev_run_time() const { return prev_run_time_; }
108
109
 private:
110
  virtual void DoAddData(
111
      const master::TSHeartbeatResponsePB& last_resp, master::TSHeartbeatRequestPB* req) = 0;
112
113
  MonoDelta period_;
114
  CoarseTimePoint prev_run_time_;
115
};
116
117
} // namespace tserver
118
} // namespace yb
119
120
#endif /* YB_TSERVER_HEARTBEATER_H */