YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/tserver/tserver_shared_mem.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_TSERVER_TSERVER_SHARED_MEM_H
15
#define YB_TSERVER_TSERVER_SHARED_MEM_H
16
17
#include <atomic>
18
19
#include <boost/asio/ip/tcp.hpp>
20
21
#include "yb/tserver/tserver_util_fwd.h"
22
23
#include "yb/util/atomic.h"
24
#include "yb/util/net/net_fwd.h"
25
#include "yb/util/slice.h"
26
27
namespace yb {
28
namespace tserver {
29
30
class TServerSharedData {
31
 public:
32
11.5k
  TServerSharedData() {
33
    // All atomics stored in shared memory must be lock-free. Non-robust locks
34
    // in shared memory can lead to deadlock if a processes crashes, and memory
35
    // access violations if the segment is mapped as read-only.
36
    // NOTE: this check is NOT sufficient to guarantee that an atomic is safe
37
    // for shared memory! Some atomics claim to be lock-free but still require
38
    // read-write access for a `load()`.
39
    // E.g. for 128 bit objects: https://stackoverflow.com/questions/49816855.
40
0
    LOG_IF(FATAL, !IsAcceptableAtomicImpl(catalog_version_))
41
0
        << "Shared memory atomics must be lock-free";
42
11.5k
    host_[0] = 0;
43
11.5k
  }
44
45
11.2k
  void SetHostEndpoint(const Endpoint& value, const std::string& host) {
46
11.2k
    endpoint_ = value;
47
11.2k
    strncpy(host_, host.c_str(), sizeof(host_) - 1);
48
11.2k
    host_[sizeof(host_) - 1] = 0;
49
11.2k
  }
50
51
1.65k
  const Endpoint& endpoint() const {
52
1.65k
    return endpoint_;
53
1.65k
  }
54
55
0
  Slice host() const {
56
0
    return host_;
57
0
  }
58
59
5.73k
  void SetYSQLCatalogVersion(uint64_t version) {
60
5.73k
    catalog_version_.store(version, std::memory_order_release);
61
5.73k
  }
62
63
158k
  uint64_t ysql_catalog_version() const {
64
158k
    return catalog_version_.load(std::memory_order_acquire);
65
158k
  }
66
67
5.81k
  void SetPostgresAuthKey(uint64_t auth_key) {
68
5.81k
    postgres_auth_key_ = auth_key;
69
5.81k
  }
70
71
470
  uint64_t postgres_auth_key() const {
72
470
    return postgres_auth_key_;
73
470
  }
74
75
 private:
76
  // Endpoint that should be used by local processes to access this tserver.
77
  Endpoint endpoint_;
78
  char host_[255 + 1]; // DNS name max length is 255, but on linux HOST_NAME_MAX is 64.
79
80
  std::atomic<uint64_t> catalog_version_{0};
81
  uint64_t postgres_auth_key_;
82
};
83
84
}  // namespace tserver
85
}  // namespace yb
86
87
#endif // YB_TSERVER_TSERVER_SHARED_MEM_H