summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/LoadBalancing/GenericFactory/DeadMemberDetection_App_Ctrl/client.cpp
blob: de52cba81eb6b14c2d58b338adb33259d2a63859 (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "TestC.h"
#include "ace/Get_Opt.h"
#include "ace/Vector_T.h"
#include "ace/OS_NS_strings.h"
#include "ace/OS_NS_unistd.h"

const ACE_TCHAR *group_file = ACE_TEXT("group.ior");
const ACE_TCHAR *group_ior = ACE_TEXT("file://group.ior");
const ACE_TCHAR *direct_obj_file = ACE_TEXT("direct_obj2.ior");
const ACE_TCHAR *direct_obj_ior = ACE_TEXT("file://direct_obj2.ior");
const ACE_TCHAR *strategy = ACE_TEXT("roundrobin");

bool ping_timedout2 = false;


int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:j:ls:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        group_ior = get_opts.opt_arg ();
        break;
      case 'j':
        direct_obj_ior = get_opts.opt_arg ();
        break;
      case 'l':
        ping_timedout2 = true;
        break;
      case 's':
        strategy = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-k <ior> -j <direct ior> -l -s <strategy>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates successful parsing of the command line
  return 0;
}



int dead_member_rr_test (CORBA::ORB_ptr orb,
                         const ACE_TCHAR *group_ior,
                         Test::Basic_ptr direct_basic)
{
  int status = 0;

  for (int iter = 0; iter < 4; iter++)
  {
    try {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request %d \n", iter));
      if (iter != 1)
      {
        CORBA::Object_var tmp =
          orb->string_to_object (group_ior);

        Test::Basic_var basic =
          Test::Basic::_narrow (tmp.in ());

        if (CORBA::is_nil (basic.in ()))
          {
            ACE_ERROR ((LM_DEBUG,
                              "Nil Test::Basic reference <%s>\n",
                              group_ior));
            status = 1;
          }

        CORBA::String_var the_string =
          basic->get_string ();

        ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request handled by object at <%s>\n",
          the_string.in ()));

        if ((iter == 0 && ACE_OS::strcmp (the_string.in(), "MyLocation 1"))
          || (iter == 2 && ACE_OS::strcmp (the_string.in(), "MyLocation 3")))
        {
          status = 1;
        }

        basic->exit ();
      }
      else
        direct_basic->exit ();

      if (iter == 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not complete.\n"), iter));
        status = 1;
      }

      // Delay more than LB validate member interval so
      // the server shutdown can be detected by LB.
      ACE_OS::sleep (10);
    }
    catch (const ::CORBA::OBJECT_NOT_EXIST& /*ex*/)
    {
      // When no member available, the LB service raise OBJECT_NOT_EXIST exception.
      if (iter < 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("OBJECT_NOT_EXIST exception\n"), iter));
        status = 1;
      }
    }
    catch (...)
    {
      throw;
    }
  }

  return status;
}


int hang_member_rr_test (CORBA::ORB_ptr orb,
                         const ACE_TCHAR *group_ior,
                         Test::Basic_ptr /*direct_basic*/)
{
  int status = 0;

  for (int iter = 0; iter < 4; iter++)
  {
    try {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request %d \n", iter));

      CORBA::Object_var tmp =
        orb->string_to_object (group_ior);

      Test::Basic_var basic =
        Test::Basic::_narrow (tmp.in ());

      if (CORBA::is_nil (basic.in ()))
        {
          ACE_ERROR ((LM_DEBUG,
                             "Nil Test::Basic reference <%s>\n",
                             group_ior));
          status = 1;
        }

      CORBA::String_var the_string =
        basic->get_string ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request handled by object at <%s>\n",
        the_string.in ()));

      if ((iter == 0 && ACE_OS::strcmp (the_string.in(), "MyLocation 1"))
        || (iter == 1 && ACE_OS::strcmp (the_string.in(), "MyLocation 2"))
        || (iter == 2 && ACE_OS::strcmp (the_string.in(), "MyLocation 3")))
      {
        status = 1;
      }

      basic->exit ();

      if (iter == 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not complete.\n"), iter));
        status = 1;
      }

      ACE_OS::sleep (10);
    }
    catch (const ::CORBA::OBJECT_NOT_EXIST& /*ex*/)
    {
      // When no member available, the LB service raise OBJECT_NOT_EXIST exception.
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("OBJECT_NOT_EXIST exception\n"), iter));
        status = 1;
      }
    }
    catch (const ::CORBA::COMM_FAILURE& /*ex*/)
    {
      // It is possible that the LB has not detected the server 2 exit as it hangs
      // and returns the reference to the hang server(server 2) for 4th iteration.
      // So the 4th operation could get the COMM_FAILURE or TRANSIENT exceptions.
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("COMM_FAILURE exception\n"), iter));
        status = 1;
      }
    }
    catch (const ::CORBA::TRANSIENT& /*ex*/)
    {
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("TRANSIENT exception\n"), iter));
        status = 1;
      }
    }
    catch (...)
    {
      throw;
    }
  }

  return status;
}


int dead_member_rand_test (CORBA::ORB_ptr orb,
                           const ACE_TCHAR *group_ior,
                           Test::Basic_ptr /*direct_basic*/)
{
  int status = 0;

  ACE_Vector <ACE_CString> locations;

  for (int iter = 0; iter < 4; iter ++)
  {
    try {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request %d \n", iter));

      CORBA::Object_var tmp =
        orb->string_to_object (group_ior);

      Test::Basic_var basic =
        Test::Basic::_narrow (tmp.in ());

      if (CORBA::is_nil (basic.in ()))
        {
          ACE_ERROR ((LM_DEBUG,
                             "Nil Test::Basic reference <%s>\n",
                             group_ior));
          status = 1;
        }

      CORBA::String_var the_string =
        basic->get_string ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request handled by object at <%s>\n",
        the_string.in ()));

      // Any picked member will not be picked again since they exit right after
      // this request.
      for (size_t j = 0; j < locations.size (); ++j)
      {
        if (ACE_OS::strcmp (the_string.in (), locations[j].c_str ()) == 0)
        {
          status = 1;
        }
      }

      if (status == 0)
        locations.push_back (the_string.in ());

      if (iter == 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not complete\n"), iter));
        status = 1;
      }

      basic->exit ();

      ACE_OS::sleep (10);
    }
    catch (const ::CORBA::OBJECT_NOT_EXIST& /*ex*/)
    {
      // Only upon 4th iteration, there is no member available,
      // the LB service raise OBJECT_NOT_EXIST exception.
      if (iter < 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("OBJECT_NOT_EXIST exception \n"), iter));
        status = 1;
      }
    }
    catch (...)
    {
      throw;
    }
  }

  return status;
}


int hang_member_rand_test (CORBA::ORB_ptr orb,
                           const ACE_TCHAR *group_ior,
                           Test::Basic_ptr /*direct_basic*/)
{
  int status = 0;

  ACE_Vector <ACE_CString> locations;

  for (int iter = 0; iter < 4; iter++)
  {
    try {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request %d \n", iter));

      CORBA::Object_var tmp =
        orb->string_to_object (group_ior);

      Test::Basic_var basic =
        Test::Basic::_narrow (tmp.in ());

      if (CORBA::is_nil (basic.in ()))
        {
          ACE_ERROR ((LM_DEBUG,
                             "Nil Test::Basic reference <%s>\n",
                             group_ior));
          status = 1;
        }

      CORBA::String_var the_string =
        basic->get_string ();

      ACE_DEBUG ((LM_DEBUG, "(%P|%t)%T - Client request handled by object at <%s>\n",
        the_string.in ()));

      for (size_t j = 0; j < locations.size (); ++j)
      {
        if (ACE_OS::strcmp (the_string.in (), locations[j].c_str ()) == 0)
        {
          status = 1;
        }
      }

      if (status == 0)
        locations.push_back (the_string.in ());

     if (iter == 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not complete\n"), iter));
        status = 1;
      }

      basic->exit ();

      ACE_OS::sleep (10);
    }
    catch (const ::CORBA::OBJECT_NOT_EXIST& /*ex*/)
    {
      // When no member available, the LB service raise OBJECT_NOT_EXIST exception.
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("OBJECT_NOT_EXIST exception"), iter));
        // no member available.
        status = 1;
      }
    }
    catch (const ::CORBA::COMM_FAILURE& /*ex*/)
    {
      // It is possible that the LB has not detected the server 2 exit as it hangs
      // and returns the reference to the hang server(server 2) for 4th iteration.
      // So the 4th operation could get the COMM_FAILURE or TRANSIENT exceptions.
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("COMM_FAILURE exception\n"), iter));
        status = 1;
      }
    }
    catch (const ::CORBA::TRANSIENT& /*ex*/)
    {
      if (iter != 3)
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Request %d should not catch ")
          ACE_TEXT ("TRANSIENT exception\n"), iter));
        status = 1;
      }
    }
    catch (...)
    {
      throw;
    }
  }

  return status;
}


int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  int status = 0;
  try
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv);

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var tmp =
        orb->string_to_object (group_ior);

      Test::Basic_var basic =
        Test::Basic::_narrow (tmp.in ());

      if (CORBA::is_nil (basic.in ()))
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Nil Test::Basic reference <%s>\n",
                             group_ior),
                            1);
        }

      tmp = orb->string_to_object (direct_obj_ior);

      Test::Basic_var direct_basic =
        Test::Basic::_narrow (tmp.in ());

      if (CORBA::is_nil (direct_basic.in ()))
        {
          ACE_ERROR_RETURN ((LM_DEBUG,
                             "Nil Test::Basic direct reference <%s>\n",
                             direct_obj_ior),
                            1);
        }

      if (ACE_OS::strcasecmp (strategy, ACE_TEXT("roundrobin")) == 0)
      {
        if (ping_timedout2)
          status = hang_member_rr_test (orb.in (), group_ior, direct_basic.in ());
        else
          status = dead_member_rr_test (orb.in (), group_ior, direct_basic.in ());
      }
      else if (ACE_OS::strcasecmp (strategy, ACE_TEXT("random")) == 0)
        if (ping_timedout2)
          status = hang_member_rand_test (orb.in (), group_ior, direct_basic.in ());
        else
          status = dead_member_rand_test (orb.in (), group_ior, direct_basic.in ());
      else
      {
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t)Invalid strategy \n")));
        status = 1;
      }

      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught in client.cpp:");
      status = 1;
    }

  return status;
}