1 /*
2    Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "ndb_anyvalue.h"
26 
27 /*
28    AnyValue carries ServerId or Reserved codes
29    Bits from opt_server_id_bits to 30 may carry other data
30    so we ignore them when reading/setting AnyValue.
31 
32    The idea with supporting 'other data' is to allow NdbApi
33    users to tag their NdbApi operations in some way that can
34    be picked up at NdbApi event receivers, *without* interacting
35    badly with / disabling normal binlogging and replication.
36 
37    To achieve this, we have a variable sized mask of bits in the
38    *middle* of the AnyValue word which can be used to mask out
39    the user data for the purpose of the MySQL Server.
40 
41    A better future approach would be to support > 1 tag word
42    per operation.
43 
44 
45    332        21        10        0
46    10987654321098765432109876543210
47    roooooooooooooooooooooooosssssss
48 
49    r = Reserved bit indicates whether
50    bits 0-7+ have ServerId (0) or
51    some special reserved code (1).
52    o = Optional bits, depending on value
53        of server-id-bits will be
54        serverid bits or user-specific
55        data
56    s = Serverid bits or reserved codes
57        At least 7 bits will be available
58        for serverid or reserved codes
59 
60    Implications :
61      Reserved codes can use values between
62      0x80000000 and 0x8000007f inclusive
63      (256 values).
64      0x8000007f was always the 'nologging'
65      code, so the others have started
66      'counting' down from there
67 
68    Examples :
69      opt_server_id_bits= 31
70       - ServerIds can be up to 2^31-1
71       - No user-specific data supported
72       - Reserved codes look like :
73         0x8000007f etc...
74 
75      opt_server_id_bits= 7
76       - ServerIds can be up to 2^7-1
77       - User specific data can be up to 2^24-1
78       - ServerIds have 0 top bit, 24 user bits, then
79         the serverid
80       - Reserved codes have 1 top bit, 24 user bits (prob
81         not used much), then the bottom lsbs of the
82         reserved code.
83 */
84 
85 #include <my_global.h>
86 
87 extern ulong opt_server_id_mask;
88 
89 #define NDB_ANYVALUE_RESERVED_BIT   0x80000000
90 #define NDB_ANYVALUE_RESERVED_MASK  0x8000007f
91 
92 #define NDB_ANYVALUE_NOLOGGING_CODE 0x8000007f
93 
94 #define NDB_ANYVALUE_REFRESH_OP_CODE 0x8000007e
95 #define NDB_ANYVALUE_REFLECT_OP_CODE 0x8000007d
96 #define NDB_ANYVALUE_READ_OP_CODE    0x8000007c
97 
98 /* Next reserved code : 0x8000007c */
99 
100 
101 #ifndef DBUG_OFF
dbug_ndbcluster_anyvalue_set_userbits(Uint32 & anyValue)102 void dbug_ndbcluster_anyvalue_set_userbits(Uint32& anyValue)
103 {
104   /*
105      Set userData part of AnyValue (if there is one) to
106      all 1s to test that it is ignored
107   */
108   const Uint32 userDataMask = ~(opt_server_id_mask |
109                                 NDB_ANYVALUE_RESERVED_BIT);
110 
111   anyValue |= userDataMask;
112 }
113 #endif
114 
ndbcluster_anyvalue_is_reserved(Uint32 anyValue)115 bool ndbcluster_anyvalue_is_reserved(Uint32 anyValue)
116 {
117   return ((anyValue & NDB_ANYVALUE_RESERVED_BIT) != 0);
118 }
119 
ndbcluster_anyvalue_is_nologging(Uint32 anyValue)120 bool ndbcluster_anyvalue_is_nologging(Uint32 anyValue)
121 {
122   return ((anyValue & NDB_ANYVALUE_RESERVED_MASK) ==
123           NDB_ANYVALUE_NOLOGGING_CODE);
124 }
125 
ndbcluster_anyvalue_set_nologging(Uint32 & anyValue)126 void ndbcluster_anyvalue_set_nologging(Uint32& anyValue)
127 {
128   anyValue |= NDB_ANYVALUE_NOLOGGING_CODE;
129 }
130 
ndbcluster_anyvalue_is_refresh_op(Uint32 anyValue)131 bool ndbcluster_anyvalue_is_refresh_op(Uint32 anyValue)
132 {
133   return ((anyValue & NDB_ANYVALUE_RESERVED_MASK) ==
134           NDB_ANYVALUE_REFRESH_OP_CODE);
135 }
136 
ndbcluster_anyvalue_set_refresh_op(Uint32 & anyValue)137 void ndbcluster_anyvalue_set_refresh_op(Uint32& anyValue)
138 {
139   anyValue &= ~NDB_ANYVALUE_RESERVED_MASK;
140   anyValue |= NDB_ANYVALUE_REFRESH_OP_CODE;
141 }
142 
ndbcluster_anyvalue_is_read_op(Uint32 anyValue)143 bool ndbcluster_anyvalue_is_read_op(Uint32 anyValue)
144 {
145   return ((anyValue & NDB_ANYVALUE_RESERVED_MASK) ==
146           NDB_ANYVALUE_READ_OP_CODE);
147 }
148 
ndbcluster_anyvalue_set_read_op(Uint32 & anyValue)149 void ndbcluster_anyvalue_set_read_op(Uint32& anyValue)
150 {
151   anyValue &= ~NDB_ANYVALUE_RESERVED_MASK;
152   anyValue |= NDB_ANYVALUE_READ_OP_CODE;
153 }
154 
ndbcluster_anyvalue_is_reflect_op(Uint32 anyValue)155 bool ndbcluster_anyvalue_is_reflect_op(Uint32 anyValue)
156 {
157   return ((anyValue & NDB_ANYVALUE_RESERVED_MASK) ==
158           NDB_ANYVALUE_REFLECT_OP_CODE);
159 }
160 
ndbcluster_anyvalue_set_reflect_op(Uint32 & anyValue)161 void ndbcluster_anyvalue_set_reflect_op(Uint32& anyValue)
162 {
163   anyValue &= ~NDB_ANYVALUE_RESERVED_MASK;
164   anyValue |= NDB_ANYVALUE_REFLECT_OP_CODE;
165 }
166 
ndbcluster_anyvalue_set_normal(Uint32 & anyValue)167 void ndbcluster_anyvalue_set_normal(Uint32& anyValue)
168 {
169   /* Clear reserved bit and serverid bits */
170   anyValue &= ~(NDB_ANYVALUE_RESERVED_BIT);
171   anyValue &= ~(opt_server_id_mask);
172 }
173 
ndbcluster_anyvalue_is_serverid_in_range(Uint32 serverId)174 bool ndbcluster_anyvalue_is_serverid_in_range(Uint32 serverId)
175 {
176   return ((serverId & ~opt_server_id_mask) == 0);
177 }
178 
ndbcluster_anyvalue_get_serverid(Uint32 anyValue)179 Uint32 ndbcluster_anyvalue_get_serverid(Uint32 anyValue)
180 {
181   assert(! (anyValue & NDB_ANYVALUE_RESERVED_BIT) );
182 
183   return (anyValue & opt_server_id_mask);
184 }
185 
ndbcluster_anyvalue_set_serverid(Uint32 & anyValue,Uint32 serverId)186 void ndbcluster_anyvalue_set_serverid(Uint32& anyValue, Uint32 serverId)
187 {
188   assert(! (anyValue & NDB_ANYVALUE_RESERVED_BIT) );
189   anyValue &= ~(opt_server_id_mask);
190   anyValue |= (serverId & opt_server_id_mask);
191 }
192