summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/FaultTolerance/FT_Invocation_Endpoint_Selectors.cpp
blob: a97c0e3d383ca95949e8eb268762135016e1c3f9 (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
// -*- C++ -*-

#include "FT_Invocation_Endpoint_Selectors.h"
#include "tao/Invocation.h"
#include "tao/Stub.h"
#include "tao/Profile.h"
#include "tao/Endpoint.h"
#include "tao/Base_Transport_Property.h"


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

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



TAO_FT_Invocation_Endpoint_Selector::TAO_FT_Invocation_Endpoint_Selector (void)
  : TAO_Default_Endpoint_Selector (),
    is_primary_alive_ (1),
    is_rewound_ (0)
{
}

TAO_FT_Invocation_Endpoint_Selector::~TAO_FT_Invocation_Endpoint_Selector (void)
{
}

void
TAO_FT_Invocation_Endpoint_Selector::select_endpoint (
  TAO_GIOP_Invocation *invocation
  ACE_ENV_ARG_DECL)
{

  int retval = this->select_endpoint_i (invocation
                                        ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (retval == 0)
    {
      if (TAO_debug_level > 6)
        ACE_DEBUG ((LM_DEBUG,
                    "(%P|%t) TAO-FT - Primary doesnt exist.",
                    "Falling back on the default selection routines \n"));

      TAO_Default_Endpoint_Selector::select_endpoint (invocation
                                                      ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }

  return;
}

// @@ RTCORBA_Subsetting - next should be deprecated...
void
TAO_FT_Invocation_Endpoint_Selector::next (TAO_GIOP_Invocation *
                                     ACE_ENV_ARG_DECL_NOT_USED)
{
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("This method is DEPRECATED!\n")));
  // if (invocation->stub_->next_profile_retry () == 0)
  //     ACE_THROW (CORBA::TRANSIENT (CORBA::OMGVMCID | 2,
  //                                  CORBA::COMPLETED_NO));
}

void
TAO_FT_Invocation_Endpoint_Selector::forward (TAO_GIOP_Invocation
                                        *invocation,
                                        const TAO_MProfile &mprofile
                                        ACE_ENV_ARG_DECL)
{
  invocation->stub ()->add_forward_profiles (mprofile);
  // This has to be and is thread safe.
  // TAO_Stub::add_forward_profiles() already makes a copy of the
  // MProfile, so do not make a copy here.


  // We may not need to do this since TAO_GIOP_Invocations
  // get created on a per-call basis. For now we'll play it safe.
  if (invocation->stub ()->next_profile () == 0)
    ACE_THROW (CORBA::TRANSIENT (
                                 CORBA_SystemException::_tao_minor_code (
                                       TAO_INVOCATION_LOCATION_FORWARD_MINOR_CODE,
                                       errno),
                                 CORBA::COMPLETED_NO));
}

void
TAO_FT_Invocation_Endpoint_Selector::success (TAO_GIOP_Invocation *invocation)
{
  invocation->stub ()->set_valid_profile ();
}

void
TAO_FT_Invocation_Endpoint_Selector::close_connection (TAO_GIOP_Invocation *invocation)
{
  // Get rid of any forwarding profiles and reset
  // the profile list to point to the first profile!
  // FRED For now we will not deal with recursive forwards!
  invocation->stub ()->reset_profiles ();
}


int
TAO_FT_Invocation_Endpoint_Selector::select_endpoint_i(
    TAO_GIOP_Invocation *invocation
    ACE_ENV_ARG_DECL)
{
  int retval =
    this->select_primary (invocation
                          ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);


  // We havent got any information on the primary ie. we dont have a
  // primary in the profile at all or we have a primary selected. We
  // return now.
  if (retval == 0 || retval == 1)
    return retval;

  // Ok, to the tricky portion. Primary is dead.
  if (TAO_debug_level > 2)
    ACE_DEBUG ((LM_DEBUG,
                "(%P|%t) The primary is dead,"
                " selecting a secondary \n"));

  // @@ todo: We could hold & release the locks a couple of times
  // here. We need to look into this after 1.2.1 for better
  // performance.
  if (this->is_rewound_ == 0)
    {
      // .. grab the lock
      ACE_MT (ACE_GUARD_RETURN (ACE_Lock,
                                guard,
                                *(invocation->stub ()->profile_lock ()),
                                0));

      // DOUBLE checked optimization pattern
      if (this->is_rewound_ == 0)
        {
          // Reset the profile list
          invocation->stub ()->reset_profiles_i ();

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

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

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

      // If it is a primary just skip to the next profile. We know
      // that the primary has failed anyway..
      if (pfile_tagged.get_component (tagged_component) != 1)
        {
          invocation->profile (invocation->stub ()->profile_in_use ());
          invocation->endpoint (invocation->profile ()->endpoint ());

          int status =
            this->endpoint_from_profile (invocation
                                         ACE_ENV_ARG_PARAMETER);
          ACE_CHECK_RETURN (-1);

          if (status == 1)
            return 1;
        }
    }
  while (invocation->stub ()->next_profile_retry () != 0);

  // If we get here, we completely failed to find an endpoint selector
  // that we know how to use, so throw an exception.
  ACE_THROW_RETURN (CORBA::TRANSIENT (CORBA::OMGVMCID | 2,
                                      CORBA::COMPLETED_NO),
                    0);
}

int
TAO_FT_Invocation_Endpoint_Selector::select_primary (
    TAO_GIOP_Invocation *invoc
    ACE_ENV_ARG_DECL)
{
  // Get the current profile
  TAO_Profile *temp_profile =
    invoc->stub ()->base_profiles ().get_current_profile ();

  int retval = 0;

  if (temp_profile)
    {
      // Check whether we have a primary profile in hand.
      retval =
        this->check_profile_for_primary (invoc,
                                         temp_profile
                                         ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);
    }
  else
    return 0;

  // If we have a primary just return
  if (retval == 1)
    return retval;

  // If we have detected a failure
  if (retval == -1)
    {
      // .. grab the lock
      ACE_MT (ACE_GUARD_RETURN (ACE_Lock,
                                guard,
                                *invoc->stub ()->profile_lock (),
                                0));

      // set the primary flag to be dead..
      this->is_primary_alive_ = 0;

      // .. and return
      return retval;
    }


  // Doing the above helps us to avoid a lock for every
  // invocation. The funda is this. Try to select the first profile in
  // the list of profiles. If it is a primary, life is just great from
  // there. If not, hold a lock and go through the list to pick
  // one. The second invocation would just use that profile as the
  // starting point. Theoretically we should have just one lock if the
  // primary is hidden somewhere else.

  // If we know that the primary is dead return a -1, to choose a
  // secondary.
  if (this->is_primary_alive_ == 0)
    return -1;

  // Now grab the lock
  ACE_MT (ACE_GUARD_RETURN (ACE_Lock,
                            guard,
                            *invoc->stub ()->profile_lock (),
                            0));

  temp_profile =
    invoc->stub ()->base_profiles ().get_next ();

  // While the profile is not null
  while (temp_profile)
    {
      // Check whether we have a primary profile in hand.
      retval =
        this->check_profile_for_primary (invoc,
                                         temp_profile
                                         ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      if (retval == 1 || retval == -1)
        return retval;

      temp_profile =
        invoc->stub ()->base_profiles ().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.
}


int
TAO_FT_Invocation_Endpoint_Selector::check_profile_for_primary (
    TAO_GIOP_Invocation *invocation,
    TAO_Profile*pfile
    ACE_ENV_ARG_DECL)
{
  IOP::TaggedComponent tagged_component;
  tagged_component.tag = IOP::TAG_FT_PRIMARY;

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

  // Search for the TaggedComponent that we want
  if (pfile_tagged.get_component (tagged_component) == 1)
    {
      if (TAO_debug_level > 2)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) TAO-FT - Got a primary component \n"));
        }

      invocation->profile (pfile);
      invocation->endpoint (invocation->profile ()->endpoint ());

      int status =
        this->endpoint_from_profile (invocation
                                     ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      if (status == 1)
        {
          if (TAO_debug_level > 4)
            ACE_DEBUG ((LM_DEBUG,
                        "(P|%t) TAO-FT - Got a valid primary \n"));

          return 1;
        }
      else
        {
          if (TAO_debug_level > 2)
            ACE_ERROR ((LM_ERROR,
                        "(P|%t) TAO-FT - We have a primary profile,"
                        " but it is unreachable \n"));
          return -1;
        }

    }

  if (TAO_debug_level > 2)
    ACE_DEBUG ((LM_DEBUG,
                "(P|%t) TAO-FT - Not a primary profile  \n"));
  return 0;
}