summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/FaultTolerance/FT_Service_Callbacks.cpp
blob: 86e10da47e178e1767ae5eac16a3309e7afa2df4 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// $Id$

#include "FT_Service_Callbacks.h"
#include "FT_Policy_i.h"

#include "tao/MProfile.h"
#include "tao/Tagged_Components.h"
#include "tao/Stub.h"

#include "tao/Invocation.h"
#include "tao/ORB_Core.h"
#include "tao/Client_Strategy_Factory.h"

#if !defined (__ACE_INLINE__)
# include "FT_Service_Callbacks.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(FaultTolerance, FT_Service_Callbacks, "$Id$")

TAO_FT_Service_Callbacks::TAO_FT_Service_Callbacks (
    TAO_ORB_Core *orb_core)

  : orb_core_ (orb_core),
    profile_lock_ (0),
    primary_failed_ (0),
    secondary_set_ (0),
    group_component_ (),
    group_component_flag_ (0)
{
  this->profile_lock_ =
    this->orb_core_->client_factory ()->create_profile_lock ();
}

TAO_FT_Service_Callbacks::~TAO_FT_Service_Callbacks (void)
{
  // Delete the memeory for the lock
  delete this->profile_lock_;
}


CORBA::Boolean
TAO_FT_Service_Callbacks::select_profile (TAO_MProfile *mpfile,
                                          TAO_Profile *&pfile)
{
  // Note: We are grabbing the lock very early. We can still delay
  // that. I will address this when I get around for the next round of
  // improvements.

  // Grab the lock
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock,
                            guard,
                            *this->profile_lock_,
                            0));

  // If we know that the primary has failed just return
  if (this->primary_failed_)
    return 0;

  IOP::TaggedComponent tagged_component;
  tagged_component.tag = IOP::TAG_FT_PRIMARY;

  // Get the current profile
  TAO_Profile *temp_profile =
    mpfile->get_current_profile ();

  // While the profile is not null
  while (temp_profile)
    {
      // Get the tagged component from the  profile
      TAO_Tagged_Components &pfile_tagged =
        temp_profile->tagged_components ();

      // Search for the TaggedComponent that we want
      if (pfile_tagged.get_component (tagged_component) == 1)
        {
          // Set the profile and return success
          pfile = ACE_const_cast (TAO_Profile *,
                                  temp_profile);

          return 1;
        }
      temp_profile = mpfile->get_next ();
    }


  // Return failure. We havent found any
  // @@ Here is where another issue kicks in. Do we flag this
  //    condition as an error. May be not. Probably the client
  //    ORB that is FT compliant, has received an IOR from a
  //    non-compliant ORB. We cannot think this of an error. Rather we
  //    need to fall back on the default methodology. That would mean
  //    that we return 0 and allow the ORB to do the rest.
  return 0;

  // @@ All said and done, what if we receive an IOGR from a
  //    FT-compliant ORB with no primaries?
  //    Ans: The default usage of the profile in the list
  //    as the start point would NOT be a mistake. We should
  //    get a LOCATION_FORWARD or some such thing to get to
  //    the primary finally.
}

CORBA::Boolean
TAO_FT_Service_Callbacks::reselect_profile (TAO_Stub *stub,
                                            TAO_Profile *&pfile)
{
  // Note: We are grabbing the lock very early. We can still delay
  // that. I will address this when I get around for the next round of
  // improvements.

  // Grab the lock
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock,
                            guard,
                            *this->profile_lock_,
                            0));

  // This method is essentially called before the Stub Object gives up
  // on invocation. We should only do this
  // 1) Check whether the first in the list or the profile_in_use ()
  // is NOT a primary. If it had been so, we would have parsed the
  // whole IOR list
  // 2) Then set the first profile for invocation

  // A secondary was already selected, so dont do that again.
  if (this->secondary_set_)
    return 0;

  IOP::TaggedComponent tagged_component;
  tagged_component.tag = IOP::TAG_FT_PRIMARY;

  TAO_Profile *profile = stub->profile_in_use ();

  // Get the tagged component from the  profile
  TAO_Tagged_Components &pfile_tagged =
    profile->tagged_components ();

  // Search for the TaggedComponent that we want
  if (pfile_tagged.get_component (tagged_component) == 1)
    {
      // We just return here as we would have parsed the whole IOR list
      return 0;
    }

  // As the first is not a primary, it should be a secondary, we will
  // set that for invocation.
  pfile = profile;

  // Set the primary_failed_ flag
  this->primary_failed_ = 1;
  this->secondary_set_ = 1;

  return 1;
}

void
TAO_FT_Service_Callbacks::reset_profile_flags (void)
{
  // Grab the lock
  ACE_MT (ACE_GUARD (ACE_Lock,
                     guard,
                     *this->profile_lock_));

  // Reset the flags that we may have
  this->primary_failed_ = 0;
  this->secondary_set_ = 0;
}


CORBA::Boolean
TAO_FT_Service_Callbacks::object_is_nil (CORBA::Object_ptr obj)
{
  // Get the count
  CORBA::ULong count =
    obj->_stubobj ()->base_profiles ().profile_count ();

  // If the profile count is zero then return true
  if (count == 0)
    return 1;
  else
    {
      // else if each of the profile is nil
      for (CORBA::ULong cnt = 0;
           cnt < count;
           cnt++)
        {
          TAO_Profile *pfile =
            obj->_stubobj ()->base_profiles ().get_profile (cnt);

          if (pfile != 0)
            return 0;
        }
    }

  // If it reaches here then it should be nill
  return 1;

}


void
TAO_FT_Service_Callbacks::service_context_list (
    TAO_Stub *&stub,
    IOP::ServiceContextList &service_list,
    CORBA::Boolean restart,
    CORBA::Environment  &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException))
{

  // If the restart flag is true, then this call for a
  // reinvocation. We need not prepare the Service Context List once
  // again. We can use the already existing one.
  if (!restart)
    {
      // Pack the group version service context
      this->group_version_service_context (stub,
                                           service_list,
                                           ACE_TRY_ENV);
      ACE_CHECK;

      // Pack the request service context
      this->request_service_context (stub,
                                     service_list,
                                     ACE_TRY_ENV);
      ACE_CHECK;
    }

  return;
}

void
TAO_FT_Service_Callbacks::request_service_context (
    TAO_Stub *&stub,
    IOP::ServiceContextList &service_list,
    CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  TAO_OutputCDR cdr;
  if (cdr << ACE_OutputCDR::from_boolean (TAO_ENCAP_BYTE_ORDER)
       == 0)
    ACE_THROW (CORBA::MARSHAL ());

  // Marshall the data in to the CDR streams
  if (cdr <<
      stub->orb_core ()->fault_tolerance_service ().client_id ().c_str ()
      == 0)
    ACE_THROW (CORBA::MARSHAL ());

  if (cdr <<
      stub->orb_core ()->fault_tolerance_service ().retention_id ()
      == 0)
    ACE_THROW (CORBA::MARSHAL ());


  // Check whether the FT::RequestDurationPolicy has been set
  CORBA::Policy_var policy =
    stub->get_policy (FT::REQUEST_DURATION_POLICY,
                      ACE_TRY_ENV);
  ACE_CHECK;

  // @@ There are two possibilities a) Duration policy has not been
  // set b)Duration policy has been set. Point 'b' will give no
  // problems. Some issues kick in for point 'a'. The best is to set a
  // default value. BUT, what is the "right" value? Need to discuss
  // this with Andy when I get a chance. For the present let us assume
  // we have 15 seconds. This is purely an assumption..

  TimeBase::TimeT exp_time = 0;

  // if we have a non-null policy set
  if (!CORBA::is_nil (policy.in ()) &&
      policy->policy_type () == FT::REQUEST_DURATION_POLICY)
    {
      FT::RequestDurationPolicy_var duration_policy =
        FT::RequestDurationPolicy::_narrow (policy.in ());

      exp_time =
        duration_policy->request_duration_value (ACE_TRY_ENV);
      ACE_CHECK;
    }
  else
    {
      // The assumption that we are making
      exp_time = 15 * 10000000;
    }

  // Calculaton of the expiration time

  // @@ Note: This is so poorly defined in the spec. All that the
  // @@ spec says is that add the RequestDurationPolicy value with
  // @@ the 'local clock value', whatever the local clock
  // @@ means. IMHO, we need something like UTC value or a more clear
  // @@ definition to send across the wire.

  // Grab the localtime on the machine where this is running
  ACE_Time_Value time_val = ACE_OS::gettimeofday ();


  TimeBase::TimeT sec_part  = time_val.sec () * 10000000;
  TimeBase::TimeT usec_part = time_val.usec ()* 10;

  // Now we have the total time
  exp_time += sec_part + usec_part;


  // Marshall the TimeBase::TimeT in to the CDR stream
  if (cdr << exp_time == 0)
    ACE_THROW (CORBA::MARSHAL ());

  // Add the CDR encapsulation in to the ServiceContextList
  CORBA::ULong l = service_list.length ();
  service_list.length (l + 1);
  service_list[l].context_id = IOP::FT_REQUEST;

  // Make a *copy* of the CDR stream...
  CORBA::ULong length = cdr.total_length ();
  service_list[l].context_data.length (length);
  CORBA::Octet *buf = service_list[l].context_data.get_buffer ();

  for (const ACE_Message_Block *i = cdr.begin ();
       i != 0;
       i = i->cont ())
    {
      ACE_OS::memcpy (buf, i->rd_ptr (), i->length ());
      buf += i->length ();
    }

  return;
}


void
TAO_FT_Service_Callbacks::group_version_service_context (
    TAO_Stub *&stub,
    IOP::ServiceContextList &service_list,
    CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  TAO_OutputCDR cdr;
  if (cdr << ACE_OutputCDR::from_boolean (TAO_ENCAP_BYTE_ORDER)
       == 0)
    ACE_THROW (CORBA::MARSHAL ());

  if (!this->group_component_flag_)
    this->get_object_group_version (stub->profile_in_use ());

  // Marshall the data in to the CDR streams
  // Marshall the TimeBase::TimeT in to the CDR stream
  if (cdr << this->group_component_ == 0)
    ACE_THROW (CORBA::MARSHAL ());

  // Add the CDR encapsulation in to the ServiceContextList
  CORBA::ULong l = service_list.length ();
  service_list.length (l + 1);
  service_list[l].context_id = IOP::FT_GROUP_VERSION;

  // Make a *copy* of the CDR stream...
  CORBA::ULong length = cdr.total_length ();
  service_list[l].context_data.length (length);
  CORBA::Octet *buf = service_list[l].context_data.get_buffer ();

  for (const ACE_Message_Block *i = cdr.begin ();
       i != 0;
       i = i->cont ())
    {
      ACE_OS::memcpy (buf, i->rd_ptr (), i->length ());
      buf += i->length ();
    }

  return;
}


void
TAO_FT_Service_Callbacks::get_object_group_version (TAO_Profile *profile)
{
  // For the group version service context, we need to get the
  // ObjectGroupRefVersion. So we just look at the profile in
  // use. Look at the TaggedComponents in the profile in use, look for
  // the TAG_FT_GROUP and get the properties. This is the general idea.

  // Get the TAO_TaggedComponents from the profile
  const TAO_Tagged_Components &tagged_components =
    profile->tagged_components ();

  IOP::TaggedComponent tagged;
  tagged.tag = IOP::TAG_FT_GROUP;

  // Get the TaggedComponent
  // If it doesn't exist then just return
  // It just means that the IOR that we got is not from a FT ORB.
  if (tagged_components.get_component (tagged) != 1)
    return;

  TAO_InputCDR cdr (ACE_reinterpret_cast (const char*,
                                          tagged.component_data.get_buffer ()
                                          ),
                    tagged.component_data.length ());
  CORBA::Boolean byte_order;
  if ((cdr >> ACE_InputCDR::to_boolean (byte_order)) == 0)
    return;
  cdr.reset_byte_order (ACE_static_cast(int,byte_order));

  // Extract the group component
  cdr >> this->group_component_;

  // Set the flag
  this->group_component_flag_ = 1;
}


int
TAO_FT_Service_Callbacks::raise_comm_failure (
    TAO_GIOP_Invocation *invoke,
    TAO_Profile *profile,
    CORBA::Environment &ACE_TRY_ENV)
{
  if (restart_policy_check (invoke->service_info (),
                            profile))
    {
      return TAO_INVOKE_RESTART;
    }

  // As the right tags are not found close the connection and throw an
  // exception
  invoke->close_connection ();
  ACE_THROW_RETURN (CORBA::COMM_FAILURE (
      CORBA_SystemException::_tao_minor_code (
          TAO_INVOCATION_RECV_REQUEST_MINOR_CODE,
          errno),
      CORBA::COMPLETED_MAYBE),
      TAO_INVOKE_EXCEPTION);
}

int
TAO_FT_Service_Callbacks::raise_transient_failure (
    TAO_GIOP_Invocation *invoke,
    TAO_Profile *profile,
    CORBA::Environment &ACE_TRY_ENV)
{
  if (restart_policy_check (invoke->service_info (),
                            profile))
    {
      return TAO_INVOKE_RESTART;
    }

  // As the right tags are not found close the connection and throw an
  // exception
  ACE_THROW_RETURN (CORBA::TRANSIENT (
      CORBA_SystemException::_tao_minor_code (
          TAO_INVOCATION_RECV_REQUEST_MINOR_CODE,
          errno),
      CORBA::COMPLETED_MAYBE),
      TAO_INVOKE_EXCEPTION);
}

CORBA::Boolean
TAO_FT_Service_Callbacks::restart_policy_check (
    IOP::ServiceContextList &service_list,
    const TAO_Profile *profile)
{
  // Check whether the IOP::FT_REQUEST exists within the service
  // context list and FT::FT_GROUP exists within the profile that we
  // have
  IOP::TaggedComponent tagged_component;
  tagged_component.tag = IOP::TAG_FT_GROUP;

  if (profile->tagged_components ().get_component (tagged_component)
      == 1)
    {
      // Look for the FT_REQUEST context id
      for (CORBA::ULong i = 0;
           i < service_list.length ();
           i++)
        {
          if (service_list[i].context_id == IOP::FT_REQUEST)
            {
              // Success
              return 1;
            }
        }
    }

  // Failure
  return 0;
}


void
TAO_FT_Service_Callbacks::service_log_msg_rcv (
    TAO_Message_State_Factory & /*state*/)
{
  // Oscar & ANDY to fill up
  return;
}

void
TAO_FT_Service_Callbacks::service_log_msg_pre_upcall (
    TAO_ServerRequest & /*req*/)
{
  // Oscar & ANDY to fill up
  return;
}

void
TAO_FT_Service_Callbacks::service_log_msg_post_upcall (
    TAO_ServerRequest & /*req*/)
{
  // Oscar & ANDY to fill up
  return;
}