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

#include "LB_Minimum_Dispersion.h"
#include "LB_Location_Map.h"
#include "LB_ObjectGroup_Map.h"
//#include "ReplicaProxy.h"

ACE_RCSID (LoadBalancing,
           LB_Minimum_Dispersion,
           "$Id$")


TAO_LB_Minimum_Dispersion_Strategy::TAO_LB_Minimum_Dispersion_Strategy (void)
{
}

TAO_LB_Minimum_Dispersion_Strategy::~TAO_LB_Minimum_Dispersion_Strategy (void)
{
}

CORBA::Object_ptr
TAO_LB_Minimum_Dispersion_Strategy::replica (
    TAO_LB_ObjectGroup_Map_Entry *entry,
    CORBA::Environment &ACE_TRY_ENV)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  for ( ; ; )
    {
      ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                        guard,
                        entry->lock,
                        CORBA::Object::_nil ());

      if (entry->replica_infos.is_empty ())
        // @@ What do we do if the set is empty?
        ACE_THROW_RETURN (CORBA::OBJECT_NOT_EXIST (),
                          CORBA::Object::_nil ());

      TAO_LB_ReplicaInfo_Set::iterator begin = entry->replica_infos.begin ();
      TAO_LB_ReplicaInfo_Set::iterator end = entry->replica_infos.end ();

      TAO_LB_ReplicaInfo_Set::iterator i = begin;
      TAO_LB_ReplicaInfo *replica_info = (*i);

      LoadBalancing::LoadList *d =
        replica_info->location_entry->load_list.ptr ();

      for (++i ; i != end; ++i)
        {
          LoadBalancing::LoadList *load =
            (*i)->location_entry->load_list.ptr ();

          // @@ Hardcode one load and don't bother checking the
          // LoadId, for now.  (just to get things going)
          if ((*d)[CORBA::ULong (0)].value > (*load)[CORBA::ULong (0)].value)
            {
              replica_info = *i;
              d = (*i)->location_entry->load_list.ptr ();
            }
        }

      // Before returning an object reference to the client
      // validate it first.
      CORBA::Object_ptr object = replica_info->replica.in ();

      {
        ACE_Reverse_Lock<TAO_SYNCH_MUTEX> reverse_lock (entry->lock);

        ACE_GUARD_RETURN (ACE_Reverse_Lock<TAO_SYNCH_MUTEX>,
                          reverse_guard,
                          reverse_lock,
                          CORBA::Object::_nil ());

        // @@ Ossama: we should setup a timeout policy here...
        ACE_TRY
          {
            CORBA::Boolean non_existent =
              object->_non_existent (ACE_TRY_ENV);
            ACE_TRY_CHECK;
            if (!non_existent)
              {
                return CORBA::Object::_duplicate (object);
              }
          }
        ACE_CATCHANY
          {
            // @@ HACK!  Do the right thing!
            return CORBA::Object::_duplicate (object);
          }
        ACE_ENDTRY;
      }
    }
}

void
TAO_LB_Minimum_Dispersion_Strategy::analyze_loads (
  TAO_LB_Location_Map &location_map,
  CORBA::Environment &ACE_TRY_ENV)
{
  int send_load_advisory = 0;

  {
    ACE_MT (ACE_GUARD (TAO_SYNCH_MUTEX,
                       guard,
                       this->lock_));


    TAO_LB_Location_Map::iterator begin =
      location_map.begin ();

    TAO_LB_Location_Map::iterator end =
      location_map.end ();

    float s = 0;
    CORBA::ULong n = 0;
    TAO_LB_Location_Map::iterator i = begin;
    for ( ; i != end; ++i)
      {
        s += (*i)->int_id_.load_list[0].value;  // @@ Hard coded to
                                                //    get things
                                                //    going.
        n++;
      }

    float avg = (n == 0 ? s : s / n);
    float cl = proxy->current_load ();

    if (avg == 0)
      return;

    float relative_load = cl / avg;

    // @@ Ossama: Make the 1.5 factor adjustable, it is how much
    // dispersion we tolerate before starting to send advisories.
    if (relative_load > 1 + 1.5F / n)
      {
        proxy->has_high_load_ = 1;
        send_load_advisory = 2;  // 2 == Send high load advisory
      }

    if (send_load_advisory == 1 && relative_load < 1 + 0.9F / n)
      {
        proxy->has_high_load_ = 0;
        send_load_advisory = 1;  // 1 == Send nominal load advisory
      }
  }

  // @@ Ossama: no debug messages in production code, my fault....
  // ACE_DEBUG ((LM_DEBUG, "Load[%x] %f %f %f\n",
  //             proxy, cl, avg, relative_load));

  // @@ Ossama: Make the 1.5 factor adjustable, it is how much
  // dispersion we tolerate before starting to send advisories.
  if (send_load_advisory == 2)
    {
      proxy->control_->high_load_advisory (ACE_TRY_ENV);
      ACE_CHECK;

      return;  // We may not throw an exception, so explicitly return.
    }

  // @@ Ossama: notice that we wait until the load is signifcantly
  // lower before sending the nominal load advisory, it does not
  // matter that much because the replicas automatically restart after
  // rejecting one client....
  // @@ Ossama: make the 0.9 factor adjustable, at least at
  // construction time...
  if (send_load_advisory == 1)
    {
      proxy->control_->nominal_load_advisory (ACE_TRY_ENV);
      ACE_CHECK;
    }
}