1 /*
2    Copyright (c) 2003, 2021, Oracle and/or its affiliates.
3     All rights reserved. Use is subject to license terms.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License, version 2.0,
7    as published by the Free Software Foundation.
8 
9    This program is also distributed with certain software (including
10    but not limited to OpenSSL) that is licensed under separate terms,
11    as designated in a particular file or component or in included license
12    documentation.  The authors of MySQL hereby grant you an additional
13    permission to link the program and your derivative works with the
14    separately licensed software that they have included with MySQL.
15 
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License, version 2.0, for more details.
20 
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 
26 /* NDBT_Error.cpp                         */
27 /* This program deals with error handling */
28 
29 #include <ndb_global.h>
30 #include <NdbMain.h>
31 #include <NdbOut.hpp>
32 #include <NdbTest.hpp>
33 #include <NDBT_Error.hpp>
34 #include <NdbSleep.h>
35 
36 
ErrorData()37 ErrorData::ErrorData()
38 {
39   errorCountArray = new Uint32[6000];
40   resetErrorCounters();
41 
42   key_error = false;
43   temporary_resource_error = true;
44   insufficient_space_error = false;
45   node_recovery_error = true;
46   overload_error = true;
47   timeout_error = true;
48   internal_error = true;
49   user_error = true;
50   application_error = false;
51 }
52 
~ErrorData()53 ErrorData::~ErrorData()
54 {
55   delete [] errorCountArray;
56 }
57 
58 
59 //-------------------------------------------------------------------
60 // Error Handling routines
61 //-------------------------------------------------------------------
62 
handleErrorCommon(const NdbError & error)63 int ErrorData::handleErrorCommon(const NdbError & error)
64 {
65   int retValue = 1;
66   if (error.code > 6000) {
67     if (user_error == true) {
68       retValue = 0;
69     }//if
70     return retValue;
71   }//if
72   errorCountArray[error.code]++;
73   switch(error.classification){
74   case NdbError::NoDataFound:
75   case NdbError::ConstraintViolation:
76     if (key_error == true) {
77       retValue = 0;
78     }//if
79     break;
80   case NdbError::TemporaryResourceError:
81     if (temporary_resource_error == true) {
82       retValue = 0;
83     }//if
84     break;
85   case NdbError::InsufficientSpace:
86     if (insufficient_space_error == true) {
87       retValue = 0;
88     }//if
89     break;
90   case NdbError::NodeRecoveryError:
91     if (node_recovery_error == true) {
92       retValue = 0;
93     }//if
94     break;
95 
96   case NdbError::UnknownResultError:
97     if(error.code == 4012){
98       retValue = 0;
99     }
100     if(error.code == 4115){
101       retValue = 2;
102     }
103     if(error.code == 4007 && node_recovery_error == true){
104       retValue = 3;
105     }
106     break;
107   case NdbError::OverloadError:
108     if (overload_error == true) {
109       NdbSleep_MilliSleep(50);
110       retValue = 0;
111     }//if
112     break;
113   case NdbError::TimeoutExpired:
114     if (timeout_error == true) {
115       retValue = 0;
116     }//if
117     break;
118   case NdbError::InternalError:
119     if (internal_error == true) {
120       retValue = 0;
121     }//if
122     break;
123   case NdbError::ApplicationError:
124     if (application_error == true) {
125       retValue = 0;
126     }//if
127     break;
128   case NdbError::UserDefinedError:
129     if (user_error == true) {
130       retValue = 0;
131     }//if
132     break;
133   default:
134     break;
135   }//switch
136   if(error.status == NdbError::TemporaryError)
137     retValue = 0;
138 
139   return retValue;
140 }//handleErrorCommon()
141 
142 
printErrorCounters(NdbOut & out) const143 void ErrorData::printErrorCounters(NdbOut & out) const
144 {
145   int localLoop;
146   for (localLoop = 0; localLoop < 6000; localLoop++) {
147     int errCount = (int)errorCountArray[localLoop];
148     if (errCount > 0) {
149       out << "NDBT: ErrorCode = " << localLoop << " occurred ";
150       out << errCount << " times" << endl;
151     }//if
152   }//for
153 }//printErrorCounters()
154 
155 
printSettings(NdbOut & out)156 void ErrorData::printSettings(NdbOut & out)
157 {
158   out << "Key Errors are ";
159   if (key_error == false) {
160     out << "disallowed" << endl;
161   } else {
162     out << "allowed" << endl;
163   }//if
164   out << "Temporary Resource Errors are ";
165   if (temporary_resource_error == false) {
166     out << "disallowed" << endl;
167   } else {
168     out << "allowed" << endl;
169   }//if
170   if (internal_error == true) {
171     out << "Insufficient Space Errors are ";
172   }
173   if (insufficient_space_error == false) {
174     out << "disallowed" << endl;
175   } else {
176     out << "allowed" << endl;
177   }//if
178   out << "Node Recovery Errors are ";
179   if (node_recovery_error == false) {
180     out << "disallowed" << endl;
181   } else {
182     out << "allowed" << endl;
183   }//if
184   out << "Overload Errors are ";
185   if (overload_error == false) {
186     out << "disallowed" << endl;
187   } else {
188     out << "allowed" << endl;
189   }//if
190   out << "Timeout Errors are ";
191   if (timeout_error == false) {
192     out << "disallowed" << endl;
193   } else {
194     out << "allowed" << endl;
195   }//if
196   out << "Internal NDB Errors are ";
197   if (internal_error == false) {
198     out << "disallowed" << endl;
199   } else {
200     out << "allowed" << endl;
201   }//if
202   out << "User logic reported Errors are ";
203   if (user_error == false) {
204     out << "disallowed" << endl;
205   } else {
206     out << "allowed" << endl;
207   }//if
208   out << "Application Errors are ";
209   if (application_error == false) {
210     out << "disallowed" << endl;
211   } else {
212     out << "allowed" << endl;
213   }//if
214 }//printSettings
215 
216 
printCmdLineArgs(NdbOut & out)217 void ErrorData::printCmdLineArgs(NdbOut & out)
218 {
219   out << "   -key_err          Allow key errors" << endl;
220   out << "   -no_key_err       Disallow key errors (default)" << endl;
221   out << "   -temp_res_err     Allow temporary resource errors (default)";
222   out << endl;
223   out << "   -no_temp_res_err  Disallow temporary resource errors" << endl;
224   out << "   -ins_space_err    Allow insufficient space errors" << endl;
225   out << "   -no_ins_space_err Disallow insufficient space errors (default)";
226   out << endl;
227   out << "   -noderec_err      Allow Node Recovery errors (default)" << endl;
228   out << "   -no_noderec_err   Disallow Node Recovery errors" << endl;
229   out << "   -overload_err     Allow Overload errors (default)" << endl;
230   out << "   -no_overload_err  Disallow Overload errors" << endl;
231   out << "   -timeout_err      Allow Time-out errors (default)" << endl;
232   out << "   -no_timeout_err   Disallow Time-out errors" << endl;
233   out << "   -internal_err     Allow Internal NDB errors" << endl;
234   out << "   -no_internal_err  Disallow Internal NDB errors (default)";
235   out << "   -user_err         Allow user logic reported errors (default)";
236   out << endl;
237   out << "   -no_user_err      Disallow user logic reported errors";
238   out << endl;
239 
240 }//printCmdLineArgs()
241 
242 
parseCmdLineArg(const char ** argv,int & i)243 bool ErrorData::parseCmdLineArg(const char** argv, int & i)
244 {
245   bool ret_Value = true;
246   if (strcmp(argv[i], "-key_err") == 0){
247     key_error = true;
248   } else if (strcmp(argv[i], "-no_key_err") == 0){
249     key_error = false;
250   } else if (strcmp(argv[i], "-temp_res_err") == 0){
251     temporary_resource_error = true;
252   } else if (strcmp(argv[i], "-no_temp_res_err") == 0){
253     temporary_resource_error = false;
254   } else if (strcmp(argv[i], "-ins_space_err") == 0){
255     insufficient_space_error = true;
256   } else if (strcmp(argv[i], "-no_ins_space_err") == 0){
257     insufficient_space_error = false;
258   } else if (strcmp(argv[i], "-noderec_err") == 0){
259     node_recovery_error = true;
260   } else if (strcmp(argv[i], "-no_noderec_err") == 0){
261     node_recovery_error = false;
262   } else if (strcmp(argv[i], "-overload_err") == 0){
263     overload_error = true;
264   } else if (strcmp(argv[i], "-no_overload_err") == 0){
265     overload_error = false;
266   } else if (strcmp(argv[i], "-timeout_err") == 0){
267     timeout_error = true;
268   } else if (strcmp(argv[i], "-no_timeout_err") == 0){
269     timeout_error = false;
270   } else if (strcmp(argv[i], "-internal_err") == 0){
271     internal_error = true;
272   } else if (strcmp(argv[i], "-no_internal_err") == 0){
273     internal_error = false;
274   } else if (strcmp(argv[i], "-user_err") == 0){
275     user_error = true;
276   } else if (strcmp(argv[i], "-no_user_err") == 0){
277     user_error = false;
278   } else {
279     ret_Value = false;
280   }//if
281   return ret_Value;
282 }//bool parseCmdline
283 
resetErrorCounters()284 void ErrorData::resetErrorCounters()
285 {
286   for (int i = 0; i < 6000; i++){
287     errorCountArray[i] = 0 ;
288   }
289 }
290 
291 
292 
293