blob: d6a44a2af4230461fb4f26643a3695c869b7012f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
pwyckoff99b000b2008-04-03 19:30:55 +000019
20#include "FacebookBase.h"
21
22using namespace facebook::fb303;
David Reiss858519a2009-02-07 02:36:50 +000023using apache::thrift::concurrency::Guard;
pwyckoff99b000b2008-04-03 19:30:55 +000024
David Reiss0b7d6fa2009-02-07 02:36:35 +000025FacebookBase::FacebookBase(std::string name) :
pwyckoff99b000b2008-04-03 19:30:55 +000026 name_(name) {
zeshuai00726681fb2020-06-03 17:24:38 +080027 aliveSince_ = (int64_t) time(nullptr);
pwyckoff99b000b2008-04-03 19:30:55 +000028}
29
30inline void FacebookBase::getName(std::string& _return) {
31 _return = name_;
32}
33
34void FacebookBase::setOption(const std::string& key, const std::string& value) {
35 Guard g(optionsLock_);
36 options_[key] = value;
37}
38
39void FacebookBase::getOption(std::string& _return, const std::string& key) {
40 Guard g(optionsLock_);
41 _return = options_[key];
42}
43
44void FacebookBase::getOptions(std::map<std::string, std::string> & _return) {
45 Guard g(optionsLock_);
46 _return = options_;
47}
48
49int64_t FacebookBase::incrementCounter(const std::string& key, int64_t amount) {
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020050 counters_.lock();
pwyckoff99b000b2008-04-03 19:30:55 +000051
52 // if we didn't find the key, we need to write lock the whole map to create it
53 ReadWriteCounterMap::iterator it = counters_.find(key);
54 if (it == counters_.end()) {
pwyckoff99b000b2008-04-03 19:30:55 +000055
56 // we need to check again to make sure someone didn't create this key
57 // already while we released the lock
58 it = counters_.find(key);
59 if(it == counters_.end()){
60 counters_[key].value = amount;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020061 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +000062 return amount;
63 }
64 }
65
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020066 it->second.lock();
pwyckoff99b000b2008-04-03 19:30:55 +000067 int64_t count = it->second.value + amount;
68 it->second.value = count;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020069 it->second.unlock();
70 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +000071 return count;
72}
73
74int64_t FacebookBase::setCounter(const std::string& key, int64_t value) {
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020075 counters_.lock();
pwyckoff99b000b2008-04-03 19:30:55 +000076
77 // if we didn't find the key, we need to write lock the whole map to create it
78 ReadWriteCounterMap::iterator it = counters_.find(key);
79 if (it == counters_.end()) {
pwyckoff99b000b2008-04-03 19:30:55 +000080 counters_[key].value = value;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020081 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +000082 return value;
83 }
84
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020085 it->second.lock();
pwyckoff99b000b2008-04-03 19:30:55 +000086 it->second.value = value;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020087 it->second.unlock();
88 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +000089 return value;
90}
91
92void FacebookBase::getCounters(std::map<std::string, int64_t>& _return) {
93 // we need to lock the whole thing and actually build the map since we don't
94 // want our read/write structure to go over the wire
Michael Leinartas3a6f8a22022-06-17 19:40:04 +020095 counters_.lock();
pwyckoff99b000b2008-04-03 19:30:55 +000096 for(ReadWriteCounterMap::iterator it = counters_.begin();
Roger Meier71f2d8a2015-04-26 17:00:04 +020097 it != counters_.end(); ++it)
pwyckoff99b000b2008-04-03 19:30:55 +000098 {
99 _return[it->first] = it->second.value;
100 }
Michael Leinartas3a6f8a22022-06-17 19:40:04 +0200101 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +0000102}
103
104int64_t FacebookBase::getCounter(const std::string& key) {
105 int64_t rv = 0;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +0200106 counters_.lock();
pwyckoff99b000b2008-04-03 19:30:55 +0000107 ReadWriteCounterMap::iterator it = counters_.find(key);
108 if (it != counters_.end()) {
Michael Leinartas3a6f8a22022-06-17 19:40:04 +0200109 it->second.lock();
pwyckoff99b000b2008-04-03 19:30:55 +0000110 rv = it->second.value;
Michael Leinartas3a6f8a22022-06-17 19:40:04 +0200111 it->second.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +0000112 }
Michael Leinartas3a6f8a22022-06-17 19:40:04 +0200113 counters_.unlock();
pwyckoff99b000b2008-04-03 19:30:55 +0000114 return rv;
115}
116
117inline int64_t FacebookBase::aliveSince() {
118 return aliveSince_;
119}
120