Coverage Report

Created: 2025-07-26 12:05

/root/doris/be/src/common/utils.h
Line
Count
Source
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 <string>
21
22
namespace doris {
23
24
#ifndef ARRAY_SIZE
25
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
26
#endif
27
28
struct AuthInfo {
29
    std::string user;
30
    std::string passwd;
31
    std::string cluster;
32
    std::string user_ip;
33
    // -1 as unset
34
    int64_t auth_code = -1; // deprecated
35
    std::string token;
36
};
37
38
template <class T>
39
8.11k
void set_request_auth(T* req, const AuthInfo& auth) {
40
8.11k
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
8.11k
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
137
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
137
        req->passwd = "";
48
7.98k
    } else if (auth.token != "") {
49
374
        req->__isset.token = true;
50
374
        req->token = auth.token;
51
7.60k
    } else {
52
7.60k
        req->passwd = auth.passwd;
53
7.60k
        if (!auth.cluster.empty()) {
54
6
            req->__set_cluster(auth.cluster);
55
6
        }
56
7.60k
        req->__set_user_ip(auth.user_ip);
57
7.60k
    }
58
8.11k
}
_ZN5doris16set_request_authINS_21TStreamLoadPutRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
39
2.95k
void set_request_auth(T* req, const AuthInfo& auth) {
40
2.95k
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
2.95k
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
18
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
18
        req->passwd = "";
48
2.93k
    } else if (auth.token != "") {
49
104
        req->__isset.token = true;
50
104
        req->token = auth.token;
51
2.83k
    } else {
52
2.83k
        req->passwd = auth.passwd;
53
2.83k
        if (!auth.cluster.empty()) {
54
2
            req->__set_cluster(auth.cluster);
55
2
        }
56
2.83k
        req->__set_user_ip(auth.user_ip);
57
2.83k
    }
58
2.95k
}
_ZN5doris16set_request_authINS_20TLoadTxnBeginRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
39
2.67k
void set_request_auth(T* req, const AuthInfo& auth) {
40
2.67k
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
2.67k
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
0
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
0
        req->passwd = "";
48
2.67k
    } else if (auth.token != "") {
49
104
        req->__isset.token = true;
50
104
        req->token = auth.token;
51
2.57k
    } else {
52
2.57k
        req->passwd = auth.passwd;
53
2.57k
        if (!auth.cluster.empty()) {
54
2
            req->__set_cluster(auth.cluster);
55
2
        }
56
2.57k
        req->__set_user_ip(auth.user_ip);
57
2.57k
    }
58
2.67k
}
_ZN5doris16set_request_authINS_18TLoadTxn2PCRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
39
106
void set_request_auth(T* req, const AuthInfo& auth) {
40
106
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
106
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
0
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
0
        req->passwd = "";
48
106
    } else if (auth.token != "") {
49
0
        req->__isset.token = true;
50
0
        req->token = auth.token;
51
106
    } else {
52
106
        req->passwd = auth.passwd;
53
106
        if (!auth.cluster.empty()) {
54
0
            req->__set_cluster(auth.cluster);
55
0
        }
56
106
        req->__set_user_ip(auth.user_ip);
57
106
    }
58
106
}
_ZN5doris16set_request_authINS_21TLoadTxnCommitRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
39
2.37k
void set_request_auth(T* req, const AuthInfo& auth) {
40
2.37k
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
2.37k
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
115
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
115
        req->passwd = "";
48
2.26k
    } else if (auth.token != "") {
49
166
        req->__isset.token = true;
50
166
        req->token = auth.token;
51
2.09k
    } else {
52
2.09k
        req->passwd = auth.passwd;
53
2.09k
        if (!auth.cluster.empty()) {
54
2
            req->__set_cluster(auth.cluster);
55
2
        }
56
2.09k
        req->__set_user_ip(auth.user_ip);
57
2.09k
    }
58
2.37k
}
_ZN5doris16set_request_authINS_23TLoadTxnRollbackRequestEEEvPT_RKNS_8AuthInfoE
Line
Count
Source
39
6
void set_request_auth(T* req, const AuthInfo& auth) {
40
6
    req->user = auth.user; // always set user, because it may be used by FE
41
    // auth code is deprecated and should be removed in 3.1
42
6
    if (auth.auth_code != -1) {
43
        // if auth_code is set, no need to set other info
44
4
        req->__set_auth_code(auth.auth_code);
45
        // user name and passwd is unused, but they are required field.
46
        // so they have to be set.
47
4
        req->passwd = "";
48
4
    } else if (auth.token != "") {
49
0
        req->__isset.token = true;
50
0
        req->token = auth.token;
51
2
    } else {
52
2
        req->passwd = auth.passwd;
53
2
        if (!auth.cluster.empty()) {
54
0
            req->__set_cluster(auth.cluster);
55
0
        }
56
2
        req->__set_user_ip(auth.user_ip);
57
2
    }
58
6
}
59
60
// This is the threshold used to periodically release the memory occupied by the expression.
61
// RELEASE_CONTEXT_COUNTER should be power of 2
62
// GCC will optimize the modulo operation to &(release_context_counter - 1)
63
// _conjunct_ctxs will free local alloc after this probe calculations
64
static constexpr int RELEASE_CONTEXT_COUNTER = 1 << 7;
65
static_assert((RELEASE_CONTEXT_COUNTER & (RELEASE_CONTEXT_COUNTER - 1)) == 0,
66
              "should be power of 2");
67
68
template <typename To, typename From>
69
To convert_to(From from) {
70
    union {
71
        From _from;
72
        To _to;
73
    };
74
    _from = from;
75
    return _to;
76
}
77
78
} // namespace doris