Coverage Report

Created: 2025-05-21 07:53

/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
5.78k
void set_request_auth(T* req, const AuthInfo& auth) {
41
5.78k
    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
5.78k
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
68
        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
68
        req->passwd = "";
49
5.71k
    } else if (auth.token != "") {
50
280
        req->__isset.token = true;
51
280
        req->token = auth.token;
52
5.43k
    } else {
53
5.43k
        req->passwd = auth.passwd;
54
5.43k
        if (!auth.cluster.empty()) {
55
3
            req->__set_cluster(auth.cluster);
56
3
        }
57
5.43k
        req->__set_user_ip(auth.user_ip);
58
5.43k
    }
59
5.78k
}
_ZN5doris16set_request_authINS_21TStreamLoadPutRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
1.96k
void set_request_auth(T* req, const AuthInfo& auth) {
41
1.96k
    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.96k
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
9
        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
9
        req->passwd = "";
49
1.95k
    } else if (auth.token != "") {
50
75
        req->__isset.token = true;
51
75
        req->token = auth.token;
52
1.87k
    } else {
53
1.87k
        req->passwd = auth.passwd;
54
1.87k
        if (!auth.cluster.empty()) {
55
1
            req->__set_cluster(auth.cluster);
56
1
        }
57
1.87k
        req->__set_user_ip(auth.user_ip);
58
1.87k
    }
59
1.96k
}
_ZN5doris16set_request_authINS_20TLoadTxnBeginRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
1.76k
void set_request_auth(T* req, const AuthInfo& auth) {
41
1.76k
    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.76k
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
0
        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
0
        req->passwd = "";
49
1.76k
    } else if (auth.token != "") {
50
75
        req->__isset.token = true;
51
75
        req->token = auth.token;
52
1.69k
    } else {
53
1.69k
        req->passwd = auth.passwd;
54
1.69k
        if (!auth.cluster.empty()) {
55
1
            req->__set_cluster(auth.cluster);
56
1
        }
57
1.69k
        req->__set_user_ip(auth.user_ip);
58
1.69k
    }
59
1.76k
}
_ZN5doris16set_request_authINS_18TLoadTxn2PCRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
109
void set_request_auth(T* req, const AuthInfo& auth) {
41
109
    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
109
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
0
        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
0
        req->passwd = "";
49
109
    } else if (auth.token != "") {
50
0
        req->__isset.token = true;
51
0
        req->token = auth.token;
52
109
    } else {
53
109
        req->passwd = auth.passwd;
54
109
        if (!auth.cluster.empty()) {
55
0
            req->__set_cluster(auth.cluster);
56
0
        }
57
109
        req->__set_user_ip(auth.user_ip);
58
109
    }
59
109
}
_ZN5doris16set_request_authINS_21TLoadTxnCommitRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
1.68k
void set_request_auth(T* req, const AuthInfo& auth) {
41
1.68k
    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.68k
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
57
        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
57
        req->passwd = "";
49
1.62k
    } else if (auth.token != "") {
50
116
        req->__isset.token = true;
51
116
        req->token = auth.token;
52
1.51k
    } else {
53
1.51k
        req->passwd = auth.passwd;
54
1.51k
        if (!auth.cluster.empty()) {
55
1
            req->__set_cluster(auth.cluster);
56
1
        }
57
1.51k
        req->__set_user_ip(auth.user_ip);
58
1.51k
    }
59
1.68k
}
_ZN5doris16set_request_authINS_23TLoadTxnRollbackRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
40
265
void set_request_auth(T* req, const AuthInfo& auth) {
41
265
    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
265
    if (auth.auth_code != -1) {
44
        // if auth_code is set, no need to set other info
45
2
        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
2
        req->passwd = "";
49
263
    } else if (auth.token != "") {
50
14
        req->__isset.token = true;
51
14
        req->token = auth.token;
52
249
    } else {
53
249
        req->passwd = auth.passwd;
54
249
        if (!auth.cluster.empty()) {
55
0
            req->__set_cluster(auth.cluster);
56
0
        }
57
249
        req->__set_user_ip(auth.user_ip);
58
249
    }
59
265
}
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