YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/client/rejection_score_source.h
Line
Count
Source
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_CLIENT_REJECTION_SCORE_SOURCE_H
15
#define YB_CLIENT_REJECTION_SCORE_SOURCE_H
16
17
#include <cstdint>
18
#include <cstdlib>
19
#include <string>
20
#include <vector>
21
22
#include <glog/logging.h>
23
24
#include "yb/util/random_util.h"
25
26
namespace yb {
27
namespace client {
28
29
// Provides source for score by attempt num.
30
class RejectionScoreSource {
31
 public:
32
9.22M
  double Get(int attempt_num) {
33
9.22M
    size_t idx = std::min<size_t>(attempt_num - 1, 20);
34
9.22M
    std::lock_guard<std::mutex> lock(mutex_);
35
18.4M
    while (scores_.size() <= idx) {
36
9.23M
      scores_.push_back(RandomUniformReal<double>(0.01, 1.0));
37
9.23M
    }
38
9.22M
    return scores_[idx];
39
9.22M
  }
40
41
 private:
42
  std::mutex mutex_;
43
  std::vector<double> scores_;
44
};
45
46
} // namespace client
47
} // namespace yb
48
49
#endif // YB_CLIENT_REJECTION_SCORE_SOURCE_H