aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/actorid.h
blob: a9cac589b2b02f619cb1f8b765148c241406709d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once

#include "defs.h"
#include <util/stream/output.h> // for IOutputStream
#include <util/generic/hash.h>

namespace NActors {
    // used as global uniq address of actor
    // also could be used to transport service id (12 byte strings placed in hint-localid)
    // highest 1 bit of node - mark of service id
    // next 11 bits of node-id - pool id
    // next 20 bits - node id itself

    struct TActorId {
        static constexpr ui32 MaxServiceIDLength = 12;
        static constexpr ui32 MaxPoolID = 0x000007FF;
        static constexpr ui32 MaxNodeId = 0x000FFFFF;
        static constexpr ui32 PoolIndexShift = 20;
        static constexpr ui32 PoolIndexMask = MaxPoolID << PoolIndexShift;
        static constexpr ui32 ServiceMask = 0x80000000;
        static constexpr ui32 NodeIdMask = MaxNodeId;

    private:
        union {
            struct {
                ui64 LocalId;
                ui32 Hint;
                ui32 NodeId;
            } N;

            struct {
                ui64 X1;
                ui64 X2;
            } X;

            ui8 Buf[16];
        } Raw;

    public:
        TActorId() noexcept {
            Raw.X.X1 = 0;
            Raw.X.X2 = 0;
        }

        explicit TActorId(ui32 nodeId, ui32 poolId, ui64 localId, ui32 hint) noexcept {
            Y_VERIFY_DEBUG(poolId <= MaxPoolID);
            Raw.N.LocalId = localId;
            Raw.N.Hint = hint;
            Raw.N.NodeId = nodeId | (poolId << PoolIndexShift);
        }

        explicit TActorId(ui32 nodeId, const TStringBuf& x) noexcept {
            Y_VERIFY(x.size() <= MaxServiceIDLength, "service id is too long");
            Raw.N.LocalId = 0;
            Raw.N.Hint = 0;
            Raw.N.NodeId = nodeId | ServiceMask;
            memcpy(Raw.Buf, x.data(), x.size());
        }

        explicit TActorId(ui64 x1, ui64 x2) noexcept {
            Raw.X.X1 = x1;
            Raw.X.X2 = x2;
        }

        explicit operator bool() const noexcept {
            return Raw.X.X1 != 0 || Raw.X.X2 != 0;
        }

        ui64 LocalId() const noexcept {
            return Raw.N.LocalId;
        }

        ui32 Hint() const noexcept {
            return Raw.N.Hint;
        }

        ui32 NodeId() const noexcept {
            return Raw.N.NodeId & NodeIdMask;
        }

        bool IsService() const noexcept {
            return (Raw.N.NodeId & ServiceMask);
        }

        TStringBuf ServiceId() const noexcept {
            Y_VERIFY_DEBUG(IsService());
            return TStringBuf((const char*)Raw.Buf, MaxServiceIDLength);
        }

        static ui32 PoolIndex(ui32 nodeid) noexcept {
            return ((nodeid & PoolIndexMask) >> PoolIndexShift);
        }

        ui32 PoolID() const noexcept {
            return PoolIndex(Raw.N.NodeId);
        }

        ui64 RawX1() const noexcept {
            return Raw.X.X1;
        }

        ui64 RawX2() const noexcept {
            return Raw.X.X2;
        }

        bool operator<(const TActorId& x) const noexcept {
            const ui64 s1 = Raw.X.X1;
            const ui64 s2 = Raw.X.X2;
            const ui64 x1 = x.Raw.X.X1;
            const ui64 x2 = x.Raw.X.X2;

            return (s1 != x1) ? (s1 < x1) : (s2 < x2);
        }

        bool operator!=(const TActorId& x) const noexcept {
            return Raw.X.X1 != x.Raw.X.X1 || Raw.X.X2 != x.Raw.X.X2;
        }

        bool operator==(const TActorId& x) const noexcept {
            return !(x != *this);
        }

        ui64 Hash() const noexcept {
            const ui32* x = (const ui32*)Raw.Buf;

            const ui64 x1 = x[0] * 0x001DFF3D8DC48F5Dull;
            const ui64 x2 = x[1] * 0x179CA10C9242235Dull;
            const ui64 x3 = x[2] * 0x0F530CAD458B0FB1ull;
            const ui64 x4 = x[3] * 0xB5026F5AA96619E9ull;

            const ui64 z1 = x1 + x2;
            const ui64 z2 = x3 + x4;

            const ui64 sum = 0x5851F42D4C957F2D + z1 + z2;

            return (sum >> 32) | (sum << 32);
        }

        ui32 Hash32() const noexcept {
            const ui32* x = (const ui32*)Raw.Buf;

            const ui64 x1 = x[0] * 0x001DFF3D8DC48F5Dull;
            const ui64 x2 = x[1] * 0x179CA10C9242235Dull;
            const ui64 x3 = x[2] * 0x0F530CAD458B0FB1ull;
            const ui64 x4 = x[3] * 0xB5026F5AA96619E9ull;

            const ui64 z1 = x1 + x2;
            const ui64 z2 = x3 + x4;

            const ui64 sum = 0x5851F42D4C957F2D + z1 + z2;

            return sum >> 32;
        }

        struct THash {
            ui64 operator()(const TActorId& actorId) const noexcept {
                return actorId.Hash();
            }
        };

        struct THash32 {
            ui64 operator()(const TActorId& actorId) const noexcept {
                return actorId.Hash();
            }
        };

        struct TOrderedCmp {
            bool operator()(const TActorId &left, const TActorId &right) const noexcept {
                Y_VERIFY_DEBUG(!left.IsService() && !right.IsService(), "ordered compare works for plain actorids only");
                const ui32 n1 = left.NodeId();
                const ui32 n2 = right.NodeId();

                return (n1 != n2) ? (n1 < n2) : left.LocalId() < right.LocalId();
            }
        };

        TString ToString() const;
        void Out(IOutputStream& o) const;
        bool Parse(const char* buf, ui32 sz);
    };

    static_assert(sizeof(TActorId) == 16, "expect sizeof(TActorId) == 16");
    static_assert(MaxPools < TActorId::MaxPoolID); // current implementation of united pool has limit MaxPools on pool id
}

template <>
inline void Out<NActors::TActorId>(IOutputStream& o, const NActors::TActorId& x) {
    return x.Out(o);
}

template <>
struct THash<NActors::TActorId> {
    inline ui64 operator()(const NActors::TActorId& x) const {
        return x.Hash();
    }
};

template<> struct std::hash<NActors::TActorId> : THash<NActors::TActorId> {};