Coverage Report

Created: 2025-05-09 16:48

/root/doris/be/src/common/utils.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <random>
21
#include <string>
22
23
namespace doris {
24
25
#ifndef ARRAY_SIZE
26
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27
#endif
28
29
struct AuthInfo {
30
    std::string user;
31
    std::string passwd;
32
    std::string cluster;
33
    std::string user_ip;
34
    // -1 as unset
35
    int64_t auth_code = -1; // deprecated
36
    std::string token;
37
};
38
39
template <class T>
40
1
void set_request_auth(T* req, const AuthInfo& auth) {
41
1
    req->user = auth.user; // always set user, because it may be used by FE
42
    // auth code is deprecated and should be removed in 3.1
43
1
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
1
        req->__set_auth_code(auth.auth_code);
46
        // user name and passwd is unused, but they are required field.
47
        // so they have to be set.
48
1
        req->passwd = "";
49
1
    } else if (auth.token != "") {
50
0
        req->__isset.token = true;
51
0
        req->token = auth.token;
52
0
    } else {
53
0
        req->passwd = auth.passwd;
54
0
        if (!auth.cluster.empty()) {
55
0
            req->__set_cluster(auth.cluster);
56
0
        }
57
0
        req->__set_user_ip(auth.user_ip);
58
0
    }
59
1
}
Unexecuted instantiation: _ZN5doris16set_request_authINS_20TLoadTxnBeginRequestEEEvPT_RKNS_8AuthInfoE
Unexecuted instantiation: _ZN5doris16set_request_authINS_18TLoadTxn2PCRequestEEEvPT_RKNS_8AuthInfoE
_ZN5doris16set_request_authINS_21TLoadTxnCommitRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
1
void set_request_auth(T* req, const AuthInfo& auth) {
41
1
    req->user = auth.user; // always set user, because it may be used by FE
42
    // auth code is deprecated and should be removed in 3.1
43
1
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
1
        req->__set_auth_code(auth.auth_code);
46
        // user name and passwd is unused, but they are required field.
47
        // so they have to be set.
48
1
        req->passwd = "";
49
1
    } else if (auth.token != "") {
50
0
        req->__isset.token = true;
51
0
        req->token = auth.token;
52
0
    } else {
53
0
        req->passwd = auth.passwd;
54
0
        if (!auth.cluster.empty()) {
55
0
            req->__set_cluster(auth.cluster);
56
0
        }
57
0
        req->__set_user_ip(auth.user_ip);
58
0
    }
59
1
}
Unexecuted instantiation: _ZN5doris16set_request_authINS_23TLoadTxnRollbackRequestEEEvPT_RKNS_8AuthInfoE
Unexecuted instantiation: _ZN5doris16set_request_authINS_21TStreamLoadPutRequestEEEvPT_RKNS_8AuthInfoE
60
61
// This is the threshold used to periodically release the memory occupied by the expression.
62
// RELEASE_CONTEXT_COUNTER should be power of 2
63
// GCC will optimize the modulo operation to &(release_context_counter - 1)
64
// _conjunct_ctxs will free local alloc after this probe calculations
65
static constexpr int RELEASE_CONTEXT_COUNTER = 1 << 7;
66
static_assert((RELEASE_CONTEXT_COUNTER & (RELEASE_CONTEXT_COUNTER - 1)) == 0,
67
              "should be power of 2");
68
69
template <typename To, typename From>
70
To convert_to(From from) {
71
    union {
72
        From _from;
73
        To _to;
74
    };
75
    _from = from;
76
    return _to;
77
}
78
79
0
inline bool random_bool_slow(double probability_of_true = 0.5) {
80
    // Due to an unknown JNI bug, we cannot use thread_local variables here.
81
0
    static std::random_device seed;
82
0
    static std::mt19937 gen(seed());
83
0
    std::bernoulli_distribution d(probability_of_true);
84
0
    return d(gen);
85
0
}
86
} // namespace doris