summaryrefslogtreecommitdiff
path: root/DAnCE/tools/Domain_Validator/CORBA/CORBA_Domain_Validator_impl.cpp
blob: 19feaff84fec0a45375a54e911a85d85bc517ec3 (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
// -*- C++ -*-
// $Id$

#include "CORBA_Domain_Validator_impl.h"

#include "tao/ORB_Core.h"
#include "ace/OS_NS_sys_time.h"
#include "dance/DAnCE_Utility.h"
#include "dance/Logger/Log_Macros.h"
#include "dance/DAnCE_PropertiesC.h"
#include "tools/Config_Handlers/XML_File_Intf.h"

// Implementation skeleton constructor
DAnCE_Domain_Validator_i::DAnCE_Domain_Validator_i (CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb))
{
}

// Implementation skeleton destructor
DAnCE_Domain_Validator_i::~DAnCE_Domain_Validator_i (void)
{
}

bool
DAnCE_Domain_Validator_i::load_domain_from_file (const char * filename)
{
  if (!filename)
    {
      DANCE_ERROR (DANCE_LOG_ERROR,
                   (LM_ERROR, DLINFO
                    ACE_TEXT("DAnCE_Domain_Validator_i::load_domain_from_file - ")
                    ACE_TEXT("Error: Provided with nil filename\n")));
      return false;
    }

  ::DAnCE::Config_Handlers::XML_File_Intf file (filename);

  file.add_search_path (ACE_TEXT ("DANCE_ROOT"), ACE_TEXT ("/docs/schema/"));

  ::Deployment::Domain *plan = file.release_domain ();

  if (!plan)
    {
      DANCE_ERROR (DANCE_LOG_ERROR,
                   (LM_ERROR, DLINFO
                    ACE_TEXT("DAnCE_Domain_Validator_i::load_domain_from_file - ")
                    ACE_TEXT("Error: Processing file <%C>\n"), filename));
      return false;
    }

  this->domain_ = plan;
  return this->create_node_table ();
}

void
DAnCE_Domain_Validator_i::load_domain (const ::Deployment::Domain & domain)
{
  this->domain_ = &domain;
  this->create_node_table ();
}

bool
DAnCE_Domain_Validator_i::create_node_table (void)
{
  this->node_map_.clear ();

  for (CORBA::ULong i=0; i < domain_->node.length (); ++i)
    {
      ::Deployment::Resource resource;

      if (!DAnCE::Utility::get_resource_value (DAnCE::NODE_RESOURCE_TYPE,
                                               domain_->node[i].resource,
                                               resource))
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR,
                        DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                        ACE_TEXT("Error: Resource <%C> not found.\n"),
                        DAnCE::NODE_RESOURCE_TYPE));
          return false;
        }
      const ACE_TCHAR *val = 0;
      if (!::DAnCE::Utility::get_satisfierproperty_value (DAnCE::NODE_IOR,
                                                          resource.property,
                                                          val))
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR,
                        DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                        ACE_TEXT("Error: Property <%C> not found.\n"),
                        DAnCE::NODE_IOR));
          return false;
        }

      std::string destination (domain_->node[i].name);
      std::string ior(val);

      if (destination != "ExecutionManager")
        {
          ior += "/";
          ior += domain_->node[i].name;
          ior += ".NodeManager";
        }
      else
        {
          ior += "/";
          ior += "ExecutionManager";
        }

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_INFO, DLINFO ACE_TEXT("Node_Locator::process_cdd - ")
                    ACE_TEXT("Storing IOR %C for destination %C\n"),
                    ior.c_str (), destination.c_str ()));

      this->node_map_ [destination] = ior;
    }

  return true;
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::validate_node (const char * node_name)
{
  // We need to try to re-resolve the reference here, just in case we were working
  // with a stale NameService reference before.

  NODE_MAP::const_iterator node_loc = this->node_map_.find (node_name);

  if (node_loc == this->node_map_.end ())
    {
      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node")
                    ACE_TEXT ("Invalid node %C\n"),
                    node_name));
      return false;
    }

  try
    {
      CORBA::Object_var object = this->orb_->string_to_object (node_loc->second.c_str ());

      if (CORBA::is_nil (object))
        {
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node - ")
                        ACE_TEXT ("Nil object refernece from string_to_object for node %C\n"),
                        node_name));
          return false;
        }

      bool non_existant = object->_non_existent ();

      if (non_existant)
        {
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node - ")
                        ACE_TEXT ("Resolved object reference not valid for node %C\n"),
                        node_name));
          return false;
        }
    }
  catch (CORBA::Exception &ex)
    {
      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node - ")
                    ACE_TEXT ("Caught CORBA Exception whilst resolving node %C: %C\n"),
                    node_name,
                    ex._info ().c_str ()));
      return false;
    }
  catch (...)
    {
      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node - ")
                    ACE_TEXT ("Caught C++ exception whilst resolving node %C\n"),
                    node_name));
      return false;
    }

  return true;
}

ACE_Time_Value
DAnCE_Domain_Validator_i::convert_time_value (const ::DAnCE::Time_Value &time_value)
{
  ACE_Time_Value retval;

  switch (time_value._d ())
    {
    case DAnCE::ttSECONDS:
      retval.set (time_value.seconds (), 0);
      break;

    case DAnCE::ttUSECONDS:
      retval.set (0, time_value.useconds ());
      break;

    case DAnCE::ttFRACTIONAL:
      retval.set (time_value.time ());
      break;

    default:
      break;
    }

  return retval;
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::block_for_node (const char * node_name,
                                          const ::DAnCE::Time_Value & max_block_time)
{
  ACE_Time_Value delay = this->convert_time_value (max_block_time);
  ACE_Time_Value timeout (ACE_OS::gettimeofday () + delay);

  // @@ TODO: We'll probably want a more intelligent quantum.
  ACE_Time_Value retry (0, 1000000 / 4);

  // We need to try to re-resolve the reference here, just in case we were working
  // with a stale NameService reference before.

  NODE_MAP::const_iterator node_loc = this->node_map_.find (node_name);

  if (node_loc == this->node_map_.end ())
    {
      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_Domain_Validator_i::validate_node")
                    ACE_TEXT ("Invalid node %C\n"),
                    node_name));
      return false;
    }

  CORBA::Object_var obj;
  bool first (true);
  ACE_CString last_error;

  do
    {
      try
        {
          if (!first)
            {
              ACE_OS::sleep (retry);
            }
          else first = false;

          obj = orb_->string_to_object (node_loc->second.c_str ());

          if (!CORBA::is_nil (obj))
            break;
        }
      catch (CORBA::Exception &ex)
        {
          DANCE_ERROR (DANCE_LOG_WARNING,
                       (LM_ERROR, DLINFO, ACE_TEXT ("Plan_Launcher - ")
                        ACE_TEXT ("Caught CORBA Exception while resolving Manager object reference: %C\n"),
                        ex._info ().c_str ()));
          last_error = ex._info ();
        }
    } while (ACE_OS::gettimeofday () < timeout);


  bool non_existant (false);
  first = true;

  do {
    try
      {
        if (!first)
          {
            ACE_OS::sleep (retry);
          }
        else first = false;

        non_existant = obj->_non_existent ();

        if (!non_existant)
          break;
      }
    catch (CORBA::Exception &ex)
      {
        DANCE_ERROR (DANCE_LOG_WARNING,
                     (LM_ERROR, DLINFO, ACE_TEXT ("Plan_Launcher - ")
                      ACE_TEXT ("Caught CORBA Exception while resolving Manager object reference: %C\n"),
                      ex._info ().c_str ()));
        last_error = ex._info ();
      }
  } while (ACE_OS::gettimeofday () < timeout);

  if (non_existant)
    {
      DANCE_ERROR (DANCE_LOG_EMERGENCY,
                   (LM_EMERGENCY, DLINFO, ACE_TEXT ("Plan_Launcher - ")
                    ACE_TEXT ("Unable to validate connection to Manager: %C\n"),
                    last_error.c_str ()));
      return false;
    }
  return true;
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::validate_domain (::CORBA::StringSeq_out late_nodes)
{
  bool retval = false;
  late_nodes = new ::CORBA::StringSeq (0);

  for (CORBA::ULong i = 0; i < this->domain_->node.length (); ++i)
    {
      bool node = this->validate_node (this->domain_->node[i].name.in ());

      if (!node)
        {
          retval = false;
          CORBA::ULong pos (late_nodes->length ());
          late_nodes->length (pos + 1);
          late_nodes[pos] = this->domain_->node[i].name.in ();
        }
    }

  return retval;
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::block_for_domain (const ::DAnCE::Time_Value & max_block_time,
                                            ::CORBA::StringSeq_out late_nodes)
{
  ACE_Time_Value tv = this->convert_time_value (max_block_time);
  ACE_Time_Value timeout (ACE_OS::gettimeofday () + ACE_Time_Value (tv));

  late_nodes = new ::CORBA::StringSeq (0);

  NODE_LIST untried_list;
  NODE_LIST retry_list;

  DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
               (LM_DEBUG, DLINFO
                ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                ACE_TEXT ("Starting domain check\n")));

  for (CORBA::ULong i = 0; i < this->domain_->node.length (); ++i)
    {
      untried_list.push_back (this->domain_->node[i].name.in ());
    }

  for (NODE_LIST::iterator i = untried_list.begin ();
       i != untried_list.end ();
       ++i)
    {
      if (ACE_OS::gettimeofday () > timeout)
        {
          DANCE_ERROR (DANCE_LOG_ERROR,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                        ACE_TEXT ("Timeout occurred while performing initial validation\n")));

          this->build_late_list (retry_list, late_nodes);

          return false;
        }

      bool result = this->validate_node (i->c_str ());

      if (!result)
        {
          retry_list.push_back (*i);
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                        ACE_TEXT ("Failed initial check for node %C\n"),
                        i->c_str ()));
        }
      else
        {
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_DEBUG, DLINFO
                        ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                        ACE_TEXT ("Node %C is alive\n"),
                        i->c_str ()));
        }
    }

  bool first = true;
  // @@ TODO: We'll probably want a more intelligent quantum.
  ACE_Time_Value retry (0, 1000000 / 4);

  while ((ACE_OS::gettimeofday () < timeout) && retry_list.size ())
    {
      if (!first)
        {
          ACE_OS::sleep (retry);
        }
      else first = false;

      for (NODE_LIST::iterator i = retry_list.begin ();
           i != retry_list.end ();
           ++i)
        {
          if (ACE_OS::gettimeofday () > timeout)
            {
              DANCE_ERROR (DANCE_LOG_ERROR,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                            ACE_TEXT ("Timeout occurred while performing follow up validation\n")));

              this->build_late_list (retry_list, late_nodes);

              return false;
            }

          bool result = this->validate_node (i->c_str ());

          if (result)
            {
              DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                           (LM_DEBUG, DLINFO
                            ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                            ACE_TEXT ("Node %C is alive\n"),
                            i->c_str ()));
              retry_list.erase (i);
              --i; // need to reposition the iterator so the next ++ will put it in the correct place
            }
        }
    }

  if (retry_list.size () == 0)
    {
      // all nodes are alive.
      DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_Domain_Validator_i::block_for_domain - ")
                    ACE_TEXT ("All nodes are alive in the domain.\n")));
      return true;
    }

  this->build_late_list (retry_list,
                         late_nodes);
  return false;
}

void
DAnCE_Domain_Validator_i::build_late_list (const NODE_LIST &nodelist,
                                           ::CORBA::StringSeq_out &latelist)
{
  latelist->length (nodelist.size ());
  CORBA::ULong pos (0);

  for (NODE_LIST::const_iterator i = nodelist.begin ();
       i != nodelist.end ();
       ++i)
    {
      latelist[pos] = i->c_str ();
    }
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::validate_plan (const ::Deployment::DeploymentPlan & plan,
                                         ::CORBA::StringSeq_out late_nodes)
{
  bool retval;
  late_nodes = new ::CORBA::StringSeq (0);

  for (CORBA::ULong i = 0; i < plan.instance.length (); ++i)
    {
      bool node = this->validate_node (plan.instance[i].node.in ());

      if (!node)
        {
          retval = false;
          CORBA::ULong pos (late_nodes->length ());
          late_nodes->length (pos + 1);
          late_nodes[pos] = this->domain_->node[i].name.in ();
        }
    }

  return retval;
}

::CORBA::Boolean
DAnCE_Domain_Validator_i::block_for_plan (const ::Deployment::DeploymentPlan & ,
                                          const ::DAnCE::Time_Value & ,
                                          ::CORBA::StringSeq_out )
{
  // Add your implementation here
  //  ACE_Time_Value tv = this->convert_time_value (max_block_time);
  throw CORBA::NO_IMPLEMENT ();
}