/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 | 17.3k | 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 | 17.3k | LOG_IF(FATAL, !IsAcceptableAtomicImpl(catalog_version_)) |
41 | 0 | << "Shared memory atomics must be lock-free"; |
42 | 17.3k | host_[0] = 0; |
43 | 17.3k | } |
44 | | |
45 | 16.7k | void SetHostEndpoint(const Endpoint& value, const std::string& host) { |
46 | 16.7k | endpoint_ = value; |
47 | 16.7k | strncpy(host_, host.c_str(), sizeof(host_) - 1); |
48 | 16.7k | host_[sizeof(host_) - 1] = 0; |
49 | 16.7k | } |
50 | | |
51 | 6.09k | const Endpoint& endpoint() const { |
52 | 6.09k | return endpoint_; |
53 | 6.09k | } |
54 | | |
55 | 6 | Slice host() const { |
56 | 6 | return host_; |
57 | 6 | } |
58 | | |
59 | 19.3k | void SetYSQLCatalogVersion(uint64_t version) { |
60 | 19.3k | catalog_version_.store(version, std::memory_order_release); |
61 | 19.3k | } |
62 | | |
63 | 442k | uint64_t ysql_catalog_version() const { |
64 | 442k | return catalog_version_.load(std::memory_order_acquire); |
65 | 442k | } |
66 | | |
67 | 8.74k | void SetPostgresAuthKey(uint64_t auth_key) { |
68 | 8.74k | postgres_auth_key_ = auth_key; |
69 | 8.74k | } |
70 | | |
71 | 3.91k | uint64_t postgres_auth_key() const { |
72 | 3.91k | return postgres_auth_key_; |
73 | 3.91k | } |
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 |