~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/serial_event_log/serial_event_log.cc

MergeĀ fromĀ lp:~jaypipes/drizzle/serial_event_log

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 *
29
29
 * @details
30
30
 *
31
 
 * Currently, this is a very simple implementation.  We have a global
32
 
 * lock on the file writer and write the events as the are received, 
33
 
 * first writing a 64-bit length and then the serialized transaction/command.
 
31
 * Currently, the log file uses this implementation:
 
32
 *
 
33
 * We have an atomic off_t called log_offset which keeps track of the 
 
34
 * offset into the log file for writing the next Command.
 
35
 *
 
36
 * When writing a Command to the log, we calculate the length of the 
 
37
 * Command to be written.  We then increment log_offset by the length
 
38
 * of the Command plus sizeof(uint64_t) and store this new offset in a 
 
39
 * local off_t called cur_offset (see SerialEventLog::apply().  This 
 
40
 * compare and set is done in an atomic instruction.
 
41
 *
 
42
 * We then adjust the local off_t (cur_offset) back to the original
 
43
 * offset by subtracting the length and sizeof(uint64_t).
 
44
 *
 
45
 * We then first write a 64-bit length and then the serialized transaction/command
 
46
 * to our log file at our local cur_offset.
34
47
 *
35
48
 * @todo
36
49
 *
104
117
void SerialEventLog::apply(drizzled::message::Command *to_apply)
105
118
{
106
119
  std::string buffer; /* Buffer we will write serialized command to */
107
 
  size_t length;
108
 
  size_t written;
 
120
  ssize_t length;
 
121
  ssize_t written;
109
122
  off_t cur_offset;
110
123
 
111
124
  to_apply->SerializeToString(&buffer);
112
 
  length= buffer.length();
 
125
  length= (ssize_t) buffer.length();
113
126
 
114
127
  /*
115
128
   * Do an atomic increment on the offset of the log file position
116
129
   */
117
 
  cur_offset= log_offset+= (sizeof(uint64_t) + length);
 
130
  cur_offset= log_offset+= (off_t) (sizeof(ssize_t) + length);
118
131
  /** 
119
132
   * @TODO
120
133
   *
136
149
   * We adjust cur_offset back to the original log_offset before
137
150
   * the increment above...
138
151
   */
139
 
  cur_offset-= (sizeof(uint64_t) + length);
 
152
  cur_offset-= (off_t) (sizeof(ssize_t) + length);
140
153
 
141
154
  /* 
142
155
   * Quick safety...if an error occurs below, the log file will
147
160
    return;
148
161
 
149
162
  written= pwrite(log_file, &length, sizeof(uint64_t), cur_offset);
150
 
  cur_offset+= written;
151
 
  if (unlikely(written != sizeof(uint64_t)))
 
163
  cur_offset+= (off_t) written;
 
164
  if (unlikely(written != sizeof(ssize_t)))
152
165
  {
153
166
    errmsg_printf(ERRMSG_LVL_ERROR, 
154
167
                  _("Failed to write full size of command.  Tried to write %" PRId64 ", but only wrote %" PRId64 ".  Error: %s"), 
155
 
                  (uint64_t) length, 
156
 
                  (uint64_t) written, 
 
168
                  (int64_t) length, 
 
169
                  (int64_t) written, 
157
170
                  strerror(errno));
158
171
    state= CRASHED;
159
172
    is_active= false;
172
185
  {
173
186
    errmsg_printf(ERRMSG_LVL_ERROR, 
174
187
                  _("Failed to write full serialized command.  Tried to write %" PRId64 ", but only wrote %" PRId64 ".  Error: %s"), 
175
 
                  (uint64_t) length, 
176
 
                  (uint64_t) written, 
 
188
                  (int64_t) length, 
 
189
                  (int64_t) written, 
177
190
                  strerror(errno));
178
191
    state= CRASHED;
179
192
    is_active= false;
180
 
    return;
181
193
  }
182
194
}
183
195