proxygen
Settings.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
17 
18 #include <map>
19 
20 #include <folly/Synchronized.h>
21 
22 namespace folly {
23 namespace settings {
24 namespace detail {
25 namespace {
26 using SettingsMap = std::map<std::string, SettingCoreBase*>;
27 Synchronized<SettingsMap>& settingsMap() {
28  static Indestructible<Synchronized<SettingsMap>> map;
29  return *map;
30 }
31 } // namespace
32 
33 void registerSetting(SettingCoreBase& core) {
34  if (core.meta().project.empty() ||
35  core.meta().project.find('_') != std::string::npos) {
36  throw std::logic_error(
37  "Setting project must be nonempty and cannot contain underscores: " +
38  core.meta().project.str());
39  }
40 
41  auto fullname = core.meta().project.str() + "_" + core.meta().name.str();
42 
43  auto mapPtr = settingsMap().wlock();
44  auto it = mapPtr->find(fullname);
45  if (it != mapPtr->end()) {
46  throw std::logic_error("FOLLY_SETTING already exists: " + fullname);
47  }
48  mapPtr->emplace(std::move(fullname), &core);
49 }
50 
51 } // namespace detail
52 
54  auto mapPtr = detail::settingsMap().rlock();
55  auto it = mapPtr->find(settingName.str());
56  if (it == mapPtr->end()) {
57  return none;
58  }
59  return it->second->meta();
60 }
61 
63  StringPiece settingName,
64  StringPiece newValue,
65  StringPiece reason) {
66  auto mapPtr = detail::settingsMap().rlock();
67  auto it = mapPtr->find(settingName.str());
68  if (it == mapPtr->end()) {
69  return false;
70  }
71  it->second->setFromString(newValue, reason, this);
72  return true;
73 }
74 
76  StringPiece settingName) const {
77  auto mapPtr = detail::settingsMap().rlock();
78  auto it = mapPtr->find(settingName.str());
79  if (it == mapPtr->end()) {
80  return none;
81  }
82  return it->second->getAsString(this);
83 }
84 
86  auto mapPtr = detail::settingsMap().rlock();
87  auto it = mapPtr->find(settingName.str());
88  if (it == mapPtr->end()) {
89  return false;
90  }
91  it->second->resetToDefault(this);
92  return true;
93 }
94 
96  const std::function<void(const SettingMetadata&, StringPiece, StringPiece)>&
97  func) const {
98  detail::SettingsMap map;
99  /* Note that this won't hold the lock over the callback, which is
100  what we want since the user might call other settings:: APIs */
101  map = *detail::settingsMap().rlock();
102  for (const auto& kv : map) {
103  auto value = kv.second->getAsString(this);
104  func(kv.second->meta(), value.first, value.second);
105  }
106 }
107 
108 namespace detail {
109 std::atomic<SettingCoreBase::Version> gGlobalVersion_;
110 
112  static SharedMutex gSavedValuesMutex;
113  return gSavedValuesMutex;
114 }
115 
116 /* Version -> (count of outstanding snapshots, saved setting values) */
117 auto& getSavedValues() {
118  static std::unordered_map<
119  SettingCoreBase::Version,
120  std::pair<size_t, std::unordered_map<SettingCoreBase::Key, BoxedValue>>>
121  gSavedValues;
122  return gSavedValues;
123 }
124 
125 SettingCoreBase::Version nextGlobalVersion() {
126  return gGlobalVersion_.fetch_add(1) + 1;
127 }
128 
130  SettingCoreBase::Key settingKey,
131  SettingCoreBase::Version version,
132  const BoxedValue& value) {
134  for (auto& it : getSavedValues()) {
135  if (version <= it.first) {
136  it.second.second[settingKey] = value;
137  }
138  }
139 }
140 
141 const BoxedValue* FOLLY_NULLABLE
142 getSavedValue(SettingCoreBase::Key settingKey, SettingCoreBase::Version at) {
144  auto it = getSavedValues().find(at);
145  if (it != getSavedValues().end()) {
146  auto jt = it->second.second.find(settingKey);
147  if (jt != it->second.second.end()) {
148  return &jt->second;
149  }
150  }
151  return nullptr;
152 }
153 
154 SnapshotBase::SnapshotBase() {
156  at_ = detail::gGlobalVersion_.load();
157  auto it = detail::getSavedValues().emplace(
158  std::piecewise_construct,
159  std::forward_as_tuple(at_),
160  std::forward_as_tuple());
161  ++it.first->second.first;
162 }
163 
164 SnapshotBase::~SnapshotBase() {
166  auto it = detail::getSavedValues().find(at_);
167  assert(it != detail::getSavedValues().end());
168  --it->second.first;
169  if (!it->second.first) {
170  detail::getSavedValues().erase(at_);
171  }
172 }
173 
174 } // namespace detail
175 
177  for (auto& it : snapshotValues_) {
178  it.second.publish();
179  }
180 }
181 
182 } // namespace settings
183 } // namespace folly
SettingCoreBase::Version nextGlobalVersion()
Definition: Settings.cpp:125
bool setFromString(StringPiece settingName, StringPiece newValue, StringPiece reason) override
Definition: Settings.cpp:62
auto & getSavedValuesMutex()
Definition: Settings.cpp:111
std::string str() const
Definition: Range.h:591
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
internal::KeyMatcher< M > Key(M inner_matcher)
#define FOLLY_NULLABLE
static http_parser_settings settings
Definition: test.c:1529
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void publish() override
Definition: Settings.cpp:176
ProtocolVersion version
void saveValueForOutstandingSnapshots(SettingCoreBase::Key settingKey, SettingCoreBase::Version version, const BoxedValue &value)
Definition: Settings.cpp:129
const BoxedValue *FOLLY_NULLABLE getSavedValue(SettingCoreBase::Key settingKey, SettingCoreBase::Version at)
Definition: Settings.cpp:142
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
static Map map(mapCap)
void forEachSetting(const std::function< void(const SettingMetadata &, StringPiece, StringPiece)> &func) const override
Definition: Settings.cpp:95
void registerSetting(SettingCoreBase &core)
Definition: Settings.cpp:33
std::atomic< SettingCoreBase::Version > gGlobalVersion_
Definition: Settings.cpp:109
Optional< SettingMetadata > getSettingsMeta(StringPiece settingName)
Definition: Settings.cpp:53
Optional< SettingsInfo > getAsString(StringPiece settingName) const override
Definition: Settings.cpp:75
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
bool resetToDefault(StringPiece settingName) override
Definition: Settings.cpp:85
constexpr None none
Definition: Optional.h:87