NdbEventOperationImpl.hpp 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef NdbEventOperationImpl_H
#define NdbEventOperationImpl_H

20 21 22
#include <NdbEventOperation.hpp>
#include <signaldata/SumaImpl.hpp>
#include <transporter/TransporterDefinitions.hpp>
unknown's avatar
unknown committed
23 24 25 26 27
#include <NdbRecAttr.hpp>

#define NDB_EVENT_OP_MAGIC_NUMBER 0xA9F301B4

class NdbEventOperationImpl;
28

unknown's avatar
unknown committed
29 30 31 32
struct EventBufData
{
  union {
    SubTableData *sdata;
33
    Uint32 *memory;
unknown's avatar
unknown committed
34 35 36 37 38
  };
  LinearSectionPtr ptr[3];
  unsigned sz;
  NdbEventOperationImpl *m_event_op;
  EventBufData *m_next; // Next wrt to global order
39 40
  EventBufData *m_next_hash; // Next in per-GCI hash
  Uint32 m_pkhash; // PK hash (without op) for fast compare
unknown's avatar
unknown committed
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
};

class EventBufData_list
{
public:
  EventBufData_list();
  ~EventBufData_list();

  void remove_first();
  void append(EventBufData *data);
  void append(const EventBufData_list &list);

  int is_empty();

  EventBufData *m_head, *m_tail;
  unsigned m_count;
  unsigned m_sz;
};

inline
EventBufData_list::EventBufData_list()
  : m_head(0), m_tail(0),
    m_count(0),
    m_sz(0)
{
}

inline
EventBufData_list::~EventBufData_list()
{
}


inline
int EventBufData_list::is_empty()
{
  return m_head == 0;
}

inline
void EventBufData_list::remove_first()
{
  m_count--;
  m_sz-= m_head->sz;
  m_head= m_head->m_next;
  if (m_head == 0)
    m_tail= 0;
}

inline
void EventBufData_list::append(EventBufData *data)
{
  data->m_next= 0;
  if (m_tail)
    m_tail->m_next= data;
  else
  {
#ifdef VM_TRACE
    assert(m_count == 0);
    assert(m_sz == 0);
#endif
    m_head= data;
  }
  m_tail= data;

  m_count++;
  m_sz+= data->sz;
}

inline
void EventBufData_list::append(const EventBufData_list &list)
{
  if (m_tail)
    m_tail->m_next= list.m_head;
  else
    m_head= list.m_head;
  m_tail= list.m_tail;
  m_count+= list.m_count;
  m_sz+= list.m_sz;
}

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
// GCI bucket has also a hash over data, with key event op, table PK.
// It can only be appended to and is invalid after remove_first().
class EventBufData_hash
{
public:
  struct Pos { // search result
    Uint32 index;       // index into hash array
    EventBufData* data; // non-zero if found
    Uint32 pkhash;      // PK hash
  };

  static Uint32 getpkhash(NdbEventOperationImpl* op, LinearSectionPtr ptr[3]);
  static bool getpkequal(NdbEventOperationImpl* op, LinearSectionPtr ptr1[3], LinearSectionPtr ptr2[3]);

  void search(Pos& hpos, NdbEventOperationImpl* op, LinearSectionPtr ptr[3]);
  void append(Pos& hpos, EventBufData* data);

  enum { GCI_EVENT_HASH_SIZE = 101 };
  EventBufData* m_hash[GCI_EVENT_HASH_SIZE];
};

inline
void EventBufData_hash::append(Pos& hpos, EventBufData* data)
{
  data->m_next_hash = m_hash[hpos.index];
  m_hash[hpos.index] = data;
}

unknown's avatar
unknown committed
150 151 152 153 154 155 156 157 158 159 160
struct Gci_container
{
  enum State 
  {
    GC_COMPLETE = 0x1 // GCI is complete, but waiting for out of order
  };
  
  Uint32 m_state;
  Uint32 m_gcp_complete_rep_count; // Remaining SUB_GCP_COMPLETE_REP until done
  Uint64 m_gci;                    // GCI
  EventBufData_list m_data;
161
  EventBufData_hash m_data_hash;
unknown's avatar
unknown committed
162
};
163

164 165 166 167
class NdbEventOperationImpl : public NdbEventOperation {
public:
  NdbEventOperationImpl(NdbEventOperation &N,
			Ndb *theNdb, 
unknown's avatar
unknown committed
168 169 170
			const char* eventName);
  NdbEventOperationImpl(NdbEventOperationImpl&); //unimplemented
  NdbEventOperationImpl& operator=(const NdbEventOperationImpl&); //unimplemented
171 172 173 174 175 176 177 178
  ~NdbEventOperationImpl();

  NdbEventOperation::State getState();

  int execute();
  int stop();
  NdbRecAttr *getValue(const char *colName, char *aValue, int n);
  NdbRecAttr *getValue(const NdbColumnImpl *, char *aValue, int n);
unknown's avatar
unknown committed
179 180 181
  int receive_event();
  Uint64 getGCI();
  Uint64 getLatestGCI();
182 183 184 185 186 187

  NdbDictionary::Event::TableEvent getEventType();

  void print();
  void printAll();

unknown's avatar
unknown committed
188 189 190
  NdbEventOperation *m_facade;
  Uint32 m_magic_number;

191 192 193
  const NdbError & getNdbError() const;
  NdbError m_error;

194 195 196
  Ndb *m_ndb;
  NdbEventImpl *m_eventImpl;

197 198 199 200
  NdbRecAttr *theFirstPkAttrs[2];
  NdbRecAttr *theCurrentPkAttrs[2];
  NdbRecAttr *theFirstDataAttrs[2];
  NdbRecAttr *theCurrentDataAttrs[2];
201

unknown's avatar
unknown committed
202 203 204 205
  NdbEventOperation::State m_state; /* note connection to mi_type */
  Uint32 mi_type; /* should be == 0 if m_state != EO_EXECUTING
		   * else same as in EventImpl
		   */
206
  Uint32 m_eventId;
unknown's avatar
unknown committed
207
  Uint32 m_oid;
208

209
  bool m_mergeEvents;
unknown's avatar
unknown committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  
  EventBufData *m_data_item;

  void *m_custom_data;
  int m_has_error;

#ifdef VM_TRACE
  Uint32 m_data_done_count;
  Uint32 m_data_count;
#endif

  // managed by the ndb object
  NdbEventOperationImpl *m_next;
  NdbEventOperationImpl *m_prev;
private:
  void receive_data(NdbRecAttr *r, const Uint32 *data, Uint32 sz);
226 227
};

unknown's avatar
unknown committed
228 229

class NdbEventBuffer {
230
public:
unknown's avatar
unknown committed
231 232
  NdbEventBuffer(Ndb*);
  ~NdbEventBuffer();
233

unknown's avatar
unknown committed
234 235 236 237 238 239
  const Uint32 &m_system_nodes;
  Vector<Gci_container> m_active_gci;
  NdbEventOperation *createEventOperation(const char* eventName,
					  NdbError &);
  void dropEventOperation(NdbEventOperation *);
  static NdbEventOperationImpl* getEventOperationImpl(NdbEventOperation* tOp);
240 241 242

  void add_drop_lock();
  void add_drop_unlock();
unknown's avatar
unknown committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256
  void lock();
  void unlock();

  void add_op();
  void remove_op();
  void init_gci_containers();

  // accessed from the "receive thread"
  int insertDataL(NdbEventOperationImpl *op,
		  const SubTableData * const sdata,
		  LinearSectionPtr ptr[3]);
  void execSUB_GCP_COMPLETE_REP(const SubGcpCompleteRep * const rep);
  void complete_outof_order_gcis();
  
unknown's avatar
unknown committed
257
  void report_node_failure(Uint32 node_id);
unknown's avatar
unknown committed
258 259 260 261 262
  void completeClusterFailed();

  // used by user thread 
  Uint64 getLatestGCI();
  Uint32 getEventId(int bufferId);
263

unknown's avatar
unknown committed
264 265 266 267 268
  int pollEvents(int aMillisecondNumber, Uint64 *latestGCI= 0);
  NdbEventOperation *nextEvent();

  NdbEventOperationImpl *move_data();

269 270 271 272 273 274 275 276 277
  // routines to copy/merge events
  EventBufData* alloc_data();
  int alloc_mem(EventBufData* data, LinearSectionPtr ptr[3]);
  int copy_data(const SubTableData * const sdata,
                LinearSectionPtr ptr[3],
                EventBufData* data);
  int merge_data(const SubTableData * const sdata,
                 LinearSectionPtr ptr[3],
                 EventBufData* data);
278

unknown's avatar
unknown committed
279
  void free_list(EventBufData_list &list);
280

unknown's avatar
unknown committed
281
  void reportStatus();
282 283

  // Global Mutex used for some things
unknown's avatar
unknown committed
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
  static NdbMutex *p_add_drop_mutex;

#ifdef VM_TRACE
  const char *m_latest_command;
#endif

  Ndb *m_ndb;
  Uint64 m_latestGCI;           // latest "handover" GCI
  Uint64 m_latest_complete_GCI; // latest complete GCI (in case of outof order)

  NdbMutex *m_mutex;
  struct NdbCondition *p_cond;

  // receive thread
  Gci_container m_complete_data;
  EventBufData *m_free_data;
#ifdef VM_TRACE
  unsigned m_free_data_count;
#endif
  unsigned m_free_data_sz;

  // user thread
  EventBufData_list m_available_data;
  EventBufData_list m_used_data;

  unsigned m_total_alloc; // total allocated memory

  // threshholds to report status
312
  unsigned m_free_thresh, m_min_free_thresh, m_max_free_thresh;
unknown's avatar
unknown committed
313 314 315 316 317 318 319 320 321 322 323
  unsigned m_gci_slip_thresh;

  NdbError m_error;
private:
  int expand(unsigned sz);

  // all allocated data
  struct EventBufData_chunk
  {
    unsigned sz;
    EventBufData data[1];
324
  };
unknown's avatar
unknown committed
325 326 327 328 329 330
  Vector<EventBufData_chunk *> m_allocated_data;
  unsigned m_sz;

  // dropped event operations that have not yet
  // been deleted
  NdbEventOperationImpl *m_dropped_ev_op;
331 332

  Uint32 m_active_op_count;
333
};
unknown's avatar
unknown committed
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

inline
NdbEventOperationImpl*
NdbEventBuffer::getEventOperationImpl(NdbEventOperation* tOp)
{
  return &tOp->m_impl;
}

inline void
NdbEventOperationImpl::receive_data(NdbRecAttr *r,
				    const Uint32 *data,
				    Uint32 sz)
{
  r->receive_data(data,sz);
#if 0
  if (sz)
  {
    assert((r->attrSize() * r->arraySize() + 3) >> 2 == sz);
    r->theNULLind= 0;
    memcpy(r->aRef(), data, 4 * sz);
    return;
  }
  r->theNULLind= 1;
#endif
}

360
#endif