summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/examples/FaultTolerance/FLARe/DeCoRAM/src/Forward_Ranking_Scheduler.cpp
blob: e9fcee731c71394dc7ac8e3625afb0937769e5dd (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Foward_Ranking_Scheduler.cpp
 *
 *  $Id$
 *
 *  @author  Friedhelm Wolf (fwolf@dre.vanderbilt.edu)
 */
//=============================================================================

#include <numeric>
#include "Forward_Ranking_Scheduler.h"
#include "FailureAwareWCRT.h"
#include "Combination_T.h"

FailureMapFinder::FailureMapFinder (const REPLICA_GROUPS & rep_groups,
                                    const FAILURE_MAP & failure_map)
  : ReplicaFinder (rep_groups),
    failure_map_ (failure_map)
{
}

PROCESSOR_SET 
FailureMapFinder::operator () (const Task & task) const
{
  PROCESSOR_SET result;
  if (failure_map_.find (task.name.c_str (),
                         result) == 0)
    {
      TRACE (task);
      return result;
    }
  else
    {
      TRACE (task << "delegate");
      return this->ReplicaFinder::operator () (task);
    }
}

Forward_Ranking_Scheduler::Forward_Ranking_Scheduler (
  const PROCESSOR_LIST & processors,
  unsigned int max_failures)
  : Scheduler (processors, max_failures),
    replica_finder_ (replica_groups_,
                     failure_map_)
{
}

double
Forward_Ranking_Scheduler::schedule_task (const Task & task,
                                          const Processor & processor)
{
  TRACE ("(" << task << "," << processor << ")");

  // check if there is already a replica of this task on the processor
  if (this->check_for_existing_replicas (task, processor))
    {
      TRACE ("already contains replica of \"" << primary_name (task) << "\"");
      return .0;
    }

  // add task to local copy of the processor tasks
  TASK_LIST local_tasks = schedule_[processor];
  local_tasks.push_back (task);

  TRACE ("Tasks: " << local_tasks);

  // determine processors that need to fail necessarily to become active
  // this applies only for backups and depends on their rank
  PROCESSOR_SET fixed_failures = this->replica_processors (task);

  TRACE ("Fixed Failures: " << fixed_failures);

  // find other processors that affect the backups on this processor
  // if they fail
  PROCESSOR_SET additional_failures = 
    this->relevant_processors (local_tasks,
                               fixed_failures);

  TRACE ("Additional Failures: " << additional_failures);

  PROCESSOR_SETS failure_scenarios = 
    this->permute_processors (fixed_failures,
                              additional_failures);

  TRACE ("Relevant Failure Scenarios: " << failure_scenarios);

  double wcrt = this->accumulate_wcrt (local_tasks,
                                       failure_scenarios);

  TRACE ("Maximum wcrt: " << wcrt);

  std::cout << "WCRT of task " << task.name << " in pro " << processor << " is " << wcrt << " with fixed pros " << fixed_failures << " and exc pros " << additional_failures << " and failure sets " << failure_scenarios << std::endl;

  return wcrt;
}

void
Forward_Ranking_Scheduler::update_schedule (const ScheduleResult & result)
{
  this->Scheduler::update_schedule (result);

  this->update_failure_map (result);
}

void 
Forward_Ranking_Scheduler::update_failure_map (const ScheduleResult & result)
{
  PROCESSOR_SET proc_dependencies;

  REPLICA_GROUPS::iterator it =
    replica_groups_.find (primary_name (result.task));

  if (it != replica_groups_.end ())
    {
      for (TASK_POSITIONS::iterator tp_it = it->second.begin ();
           tp_it != it->second.begin () + result.task.rank;
           ++tp_it)
        {
          proc_dependencies.insert (tp_it->first);
        }
    }

  failure_map_.bind (result.task.name.c_str (), proc_dependencies);

  TRACE ("Failure Map: " << failure_map_);
}

bool 
Forward_Ranking_Scheduler::check_for_existing_replicas (
  const Task & task,
  const Processor & processor)
{
  TASK_POSITIONS replica_group = 
    replica_groups_[primary_name (task)];
      
  return std::accumulate (replica_group.begin (),
                          replica_group.end (),
                          false,
                          ProcessorNameComparison (processor));
}

PROCESSOR_SET
Forward_Ranking_Scheduler::replica_processors (const Task & task)
{
  return replica_finder_ (task);
}

class RelevantProcessorAccumulator : std::binary_function <PROCESSOR_SET,
                                                           Task,
                                                           PROCESSOR_SET>
{
public:
  RelevantProcessorAccumulator (const ReplicaFinder & rep_finder)
    : rep_finder_ (rep_finder)
  {
  }

  PROCESSOR_SET operator () (const PROCESSOR_SET & previous,
                             const Task & task)
  {
    PROCESSOR_SET result = rep_finder_ (task);

    result.insert (previous.begin (), previous.end ());

    return result;
  }

private:
  const ReplicaFinder & rep_finder_;
};

PROCESSOR_SET 
Forward_Ranking_Scheduler::relevant_processors (
  const TASK_LIST & tasks,
  const PROCESSOR_SET & ignored_processors)
{
  PROCESSOR_SET relevant =  
    std::accumulate (tasks.begin (),
                     tasks.end (),
                     PROCESSOR_SET (),
                     RelevantProcessorAccumulator (replica_finder_));

  PROCESSOR_SET result;

  // remove the processors from the result that should not be
  // permutated here
  std::set_difference (relevant.begin (),
                       relevant.end (),
                       ignored_processors.begin (),
                       ignored_processors.end (),
                       std::inserter (result,
                                      result.begin ()));

  return result;
}

PROCESSOR_SETS
Forward_Ranking_Scheduler::permute_processors (
  const PROCESSOR_SET & fixed,
  const PROCESSOR_SET & exchangeable)
{
  PROCESSOR_SETS failure_sets;

  if (exchangeable.size () > 0)
    {
      // there are multiple backup replicas that are already hosted
      // on the processor we are trying to allocate a primary or a backup
      // replica
      if (fixed.size () == 0)
        {
          // we are adding a primary replica to a processor that
          // already has multiple backup replicas
          if (exchangeable.size () <= max_failures_)
            {
              // we have less than or equal to 'K' processors to check
              // we just check them. We do not need any permutations
              failure_sets.push_back (exchangeable);
            }
          else if (exchangeable.size () > max_failures_)
            {
              // We have more than 'k' processors to check.
              // We need permutations of 'k' size each
              PROCESSOR_LIST failure_combination;
              PROCESSOR_SET::iterator failure_it = exchangeable.begin ();
              for (unsigned int exchange_index = 0;
                   exchange_index < max_failures_;
                   ++exchange_index, ++failure_it)
                {
                  failure_combination.push_back (*failure_it);
                }
              PROCESSOR_LIST exchangeable_elements;
              std::copy (exchangeable.begin (),
                         exchangeable.end (),
                         std::inserter (exchangeable_elements,
                                        exchangeable_elements.begin ()));
              do
                {
                  PROCESSOR_SET proc_fail_set;
                  std::copy (failure_combination.begin (),
                             failure_combination.end (),
                             std::inserter (proc_fail_set,
                                            proc_fail_set.begin ()));
                  failure_sets.push_back (proc_fail_set);
                }
              while (next_combination (exchangeable_elements.begin (),
                                       exchangeable_elements.end (),
                                       failure_combination.begin (),
                                       failure_combination.end ()));
            }
        }
      else if (fixed.size () > 0)
        {
          // we are adding a backup replica to a processor that already
          // has multiple backup replicas
          unsigned int total_proc_size =
            exchangeable.size () + fixed.size ();

          PROCESSOR_SET fixed_exchangeable_set;
          std::copy (exchangeable.begin (),
                     exchangeable.end (),
                     std::inserter (fixed_exchangeable_set,
                                    fixed_exchangeable_set.begin ()));
          std::copy (fixed.begin (),
                     fixed.end (),
                     std::inserter (fixed_exchangeable_set,
                                    fixed_exchangeable_set.begin ()));

          if (total_proc_size <= max_failures_)
            {
              // We have less than or equal to 'k' processors to check
              // We do not need any permutations.
              failure_sets.push_back (fixed_exchangeable_set);
            }
          else if (total_proc_size > max_failures_)
            {
              // We have more than 'k' processors to check.
              // We need permutations of 'k' size each
              PROCESSOR_LIST combination;
              PROCESSOR_SET::iterator it = fixed_exchangeable_set.begin ();
              for (unsigned int comb_index = 0;
                   comb_index < max_failures_; ++comb_index, ++it)
                {
                  combination.push_back (*it);
                }
              PROCESSOR_LIST failure_elements;
              std::copy (fixed_exchangeable_set.begin (),
                         fixed_exchangeable_set.end (),
                         std::inserter (failure_elements,
                                        failure_elements.begin ()));
              do
                {
                  PROCESSOR_SET set;
                  std::copy (combination.begin (),
                             combination.end (),
                             std::inserter (set,
                                            set.begin ()));
                  failure_sets.push_back (set);
                }
              while (next_combination (failure_elements.begin (),
                                       failure_elements.end (),
                                       combination.begin (),
                                       combination.end ()));
            }
        }
    }
  else if (exchangeable.size () == 0)
    {
      // we are adding a backup replica or a primary replica to a procssor
      // that does not have any backup replicas already hosted
      // So we do not need to check any failure scenarios
      if (fixed.size () == 0)
        {
          failure_sets.push_back (exchangeable);
        }
      else if (fixed.size () > 0)
        {
          failure_sets.push_back (fixed);
        }
    }

  return failure_sets;

/*

  if (fixed.size () == max_failures_)
    {
      failure_sets.push_back (fixed);
    }
  else if (fixed.size () < max_failures_)
    {
      unsigned int tupel_size = max_failures_ - fixed.size ();

      if (fixed.size () == 0)
        {
          if (exchangeable.size () <= tupel_size)
            {
              failure_sets.push_back (exchangeable);
            }
          else
            {
              PROCESSOR_LIST failure_combination;
              PROCESSOR_SET::iterator fail_it = exchangeable.begin ();
              for (unsigned int c_index = 0;c_index < tupel_size;
                   ++c_index, ++fail_it)
                {
                  failure_combination.push_back (*fail_it);
                }
              PROCESSOR_LIST fail_elements;
              std::copy (exchangeable.begin (),
                         exchangeable.end (),
                         std::inserter (fail_elements,
                                        fail_elements.begin ()));
              do
                {
                  PROCESSOR_SET fail_set;
                  std::copy (failure_combination.begin (),
                             failure_combination.end (),
                             std::inserter (fail_set,
                                            fail_set.begin ()));
                  std::copy (fixed.begin (),
                             fixed.end (),
                             std::inserter (fail_set,
                                            fail_set.begin ()));

                  failure_sets.push_back (fail_set);
                }
              while (next_combination (fail_elements.begin (),
                                       fail_elements.end (),
                                       failure_combination.begin (),
                                       failure_combination.end ()));
            }
        }
      else if (fixed.size () > 0)
        {
          if (exchangeable.size () <= tupel_size)
            {
              PROCESSOR_SET fixed_exchangeable_set;

              // add the fixed elements
              std::copy (fixed.begin (),
                         fixed.end (),
                         std::inserter (fixed_exchangeable_set,
                                        fixed_exchangeable_set.begin ()));
              
              // add the exchangeable elements
              std::copy (exchangeable.begin (),
                         exchangeable.end (),
                         std::inserter (fixed_exchangeable_set,
                                        fixed_exchangeable_set.begin ()));

              failure_sets.push_back (fixed_exchangeable_set);
            }
          else
            {
              PROCESSOR_LIST combination;
              PROCESSOR_SET::iterator it = exchangeable.begin ();
              for (unsigned int comb_index = 0;comb_index < tupel_size;
                   ++comb_index, ++it)
                {
                  combination.push_back (*it);
                }
              PROCESSOR_LIST failure_elements;
              std::copy (exchangeable.begin (),
                         exchangeable.end (),
                         std::inserter (failure_elements,
                                        failure_elements.begin ())); 
              do
                {
                  PROCESSOR_SET set;
                  std::copy (combination.begin (),
                             combination.end (),
                             std::inserter (set,
                                            set.begin ()));
                  std::copy (fixed.begin (),
                             fixed.end (),
                             std::inserter (set,
                                            set.begin ()));

                  failure_sets.push_back (set);
                }
              while (next_combination (failure_elements.begin (),
                                       failure_elements.end (),
                                       combination.begin (),
                                       combination.end ()));
            }
        }
    }

  return failure_sets;

*/

}

double
Forward_Ranking_Scheduler::accumulate_wcrt (const TASK_LIST & tasks,
                                            const PROCESSOR_SETS & scenarios)
{
  return std::accumulate (scenarios.begin (),
                          scenarios.end (),
                          -1.0,
                          FailureAwareWCRT (tasks,
                                            replica_finder_));
}

std::ostream & operator<< (std::ostream & ostr, 
                           const FAILURE_MAP & fm)
{
  ostr << "{";
  for (FAILURE_MAP::const_iterator it = fm.begin ();
       it != fm.end ();
       ++it)
    {
      ostr << it->key ().c_str () << ": " << it->item () << ", ";
    }
  ostr << "}";

  return ostr;
}