~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/command_log/command_log.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-09-13 01:03:01 UTC
  • mto: (1126.9.2 captain-20090915-01)
  • mto: This revision was merged to the branch mainline in revision 1133.
  • Revision ID: osullivan.padraig@gmail.com-20090913010301-tcvvezipx1124acy
Added calls to the dtrace delete begin/end probes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008-2009 Sun Microsystems
5
 
 *  Copyright (c) 2010 Jay Pipes <jaypipes@gmail.com>
6
 
 *
7
 
 *  Authors:
8
 
 *
9
 
 *  Jay Pipes <jaypipes@gmail.com.com>
10
5
 *
11
6
 *  This program is free software; you can redistribute it and/or modify
12
7
 *  it under the terms of the GNU General Public License as published by
26
21
/**
27
22
 * @file
28
23
 *
29
 
 * Defines the API of the transaction log file descriptor.
 
24
 * Defines the API of the default command log.
 
25
 *
 
26
 * @see drizzled/plugin/replicator.h
 
27
 * @see drizzled/plugin/applier.h
30
28
 *
31
29
 * @details
32
30
 *
33
 
 * Basically, the TransactionLog is a descriptor for a log
34
 
 * file containing transaction messages that is written by
35
 
 * the TransactionLogApplier plugin(s).
 
31
 * The CommandLog applies events it receives from the TransactionServices
 
32
 * server component to a simple log file on disk.
 
33
 * 
 
34
 * Events are received in no guaranteed order and the command log
 
35
 * is in charge of writing these events to the log as they are received.
36
36
 */
37
37
 
38
 
#ifndef PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
39
 
#define PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H
 
38
#ifndef DRIZZLE_PLUGIN_command_log_H
 
39
#define DRIZZLE_PLUGIN_command_log_H
40
40
 
 
41
#include <drizzled/server_includes.h>
41
42
#include <drizzled/atomics.h>
42
 
#include <drizzled/replication_services.h>
43
 
 
44
 
#include "transaction_log_entry.h"
 
43
#include <drizzled/plugin/replicator.h>
 
44
#include <drizzled/plugin/applier.h>
45
45
 
46
46
#include <vector>
47
47
#include <string>
48
48
 
49
 
class TransactionLog
 
49
class CommandLog: public drizzled::plugin::Applier 
50
50
{
51
51
public:
52
 
  static const uint32_t HEADER_TRAILER_BYTES= sizeof(uint32_t) + /* 4-byte msg type header */
53
 
                                              sizeof(uint32_t) + /* 4-byte length header */
54
 
                                              sizeof(uint32_t); /* 4 byte checksum trailer */
55
 
 
56
 
  typedef std::vector<TransactionLogEntry> Entries;
57
 
  typedef std::vector<TransactionLogTransactionEntry> TransactionEntries;
58
 
  /**
59
 
   * The state the log is in
60
 
   */
61
 
  enum Status
 
52
  enum status
62
53
  {
63
54
    CRASHED= 0,
64
55
    OFFLINE, /* Default state, uninited. */
65
56
    ONLINE,
66
57
    WRITING
67
58
  };
68
 
  static const uint32_t SYNC_METHOD_OS= 0; ///< Rely on operating system to sync log file
69
 
  static const uint32_t SYNC_METHOD_EVERY_WRITE= 1; //< Sync on every write to the log file
70
 
  static const uint32_t SYNC_METHOD_EVERY_SECOND= 2; ///< Sync no more than once a second
 
59
private:
 
60
  int log_file; /**< Handle for our log file */
 
61
  enum status state; /**< The state the log is in */
 
62
  drizzled::atomic<bool> is_enabled; /**< Internal toggle. Atomic to support online toggling of command log... */
 
63
  drizzled::atomic<bool> is_active; /**< Internal toggle. If true, log was initialized properly... */
 
64
  const char *log_file_path; /**< Full path to the log file */
 
65
  drizzled::atomic<off_t> log_offset; /**< Offset in log file where log will write next command */
71
66
public:
72
 
  TransactionLog(const std::string in_log_file_path,
73
 
                 uint32_t in_sync_method);
 
67
  CommandLog(const char *in_log_file_path);
74
68
 
75
69
  /** Destructor */
76
 
  ~TransactionLog();
77
 
 
78
 
  /**
79
 
   * Returns the current offset into the log
80
 
   */
81
 
  inline off_t getLogOffset()
82
 
  {
83
 
    return log_offset;
84
 
  }
85
 
 
86
 
  /**
87
 
   * Returns the filename of the transaction log
88
 
   */
89
 
  const std::string &getLogFilename();
90
 
 
91
 
  /**
92
 
   * Returns the filename of the transaction log
93
 
   */
94
 
  const std::string &getLogFilepath();
 
70
  ~CommandLog();
 
71
 
 
72
  /**
 
73
   * Applies a Command to the serial log
 
74
   *
 
75
   * @note
 
76
   *
 
77
   * It is important to note that memory allocation for the 
 
78
   * supplied pointer is not guaranteed after the completion 
 
79
   * of this function -- meaning the caller can dispose of the
 
80
   * supplied message.  Therefore, appliers which are
 
81
   * implementing an asynchronous replication system must copy
 
82
   * the supplied message to their own controlled memory storage
 
83
   * area.
 
84
   *
 
85
   * @param Command message to be replicated
 
86
   */
 
87
  void apply(drizzled::message::Command *to_apply);
95
88
  
 
89
  /** 
 
90
   * Returns whether the command log is active.
 
91
   */
 
92
  bool isActive();
 
93
 
 
94
  /**
 
95
   * Disables the plugin.
 
96
   * Disabled just means that the user has done an online set @command_log_enable= false
 
97
   */
 
98
  inline void disable()
 
99
  {
 
100
    is_enabled= false;
 
101
  }
 
102
 
 
103
  /**
 
104
   * Enables the plugin.  Enabling is a bit different from isActive().
 
105
   * Enabled just means that the user has done an online set global command_log_enable= true
 
106
   * or has manually started up the server with --command-log-enable
 
107
   */
 
108
  inline void enable()
 
109
  {
 
110
    is_enabled= true;
 
111
  }
 
112
 
96
113
  /**
97
114
   * Returns the state that the log is in
98
115
   */
99
 
  inline enum Status getState()
 
116
  inline enum status getState()
100
117
  {
101
118
    return state;
102
119
  }
103
120
 
104
121
  /**
105
 
   * Writes a chunk of data to the log file of a specified
106
 
   * length and returns the offset at which the chunk of
107
 
   * data was written.
108
 
   *
109
 
   * @param[in] Bytes to write
110
 
   * @param[in[ Length of bytes to write
111
 
   *
112
 
   * @retval
113
 
   *  Returns the write offset if the write succeeded, OFF_T_MAX otherwise.
114
 
   */
115
 
  off_t writeEntry(const uint8_t *data, size_t data_length);
116
 
 
117
 
  /**
118
122
   * Truncates the existing log file
119
123
   *
120
124
   * @note 
124
128
   * set to anything other than false, this is called.
125
129
   */
126
130
  void truncate();
127
 
 
128
 
  /**
129
 
   * Takes a global transaction ID and a reference to a string to fill
130
 
   * with the name of the log file which contains the command with the 
131
 
   * transaction ID.  If the transaction ID is contained in a log file, 
132
 
   * the function returns true, false otherwise.
133
 
   *
134
 
   * @param[in] Global transaction ID to search on
135
 
   * @param[inout] String to fill with name of logfile containing command with
136
 
   *               the needed transaction ID
137
 
   *
138
 
   * @retval
139
 
   *  true if found
140
 
   * @retval
141
 
   *  false otherwise
142
 
   */
143
 
  bool findLogFilenameContainingTransactionId(const drizzled::ReplicationServices::GlobalTransactionId &to_find,
144
 
                                              std::string &out_filename) const;
145
 
 
146
 
  /**
147
 
   * Returns whether the log is currently in error.
148
 
   */
149
 
  bool hasError() const;
150
 
 
151
 
  /**
152
 
   * Returns the log's current error message
153
 
   */
154
 
  const std::string &getErrorMessage() const;
155
 
private:
156
 
  /* Don't allows these */
157
 
  TransactionLog();
158
 
  TransactionLog(const TransactionLog &other);
159
 
  TransactionLog &operator=(const TransactionLog &other);
160
 
  /**
161
 
   * Clears the current error message
162
 
   */
163
 
  void clearError();
164
 
  /**
165
 
   * Helper method which synchronizes/flushes the transaction log file
166
 
   * according to the transaction_log_sync_method system variable
167
 
   *
168
 
   * @retval
169
 
   *   0 == Success
170
 
   * @retval
171
 
   *   >0 == Failure. Error code.
172
 
   */
173
 
  int syncLogFile();
174
 
 
175
 
  int log_file; ///< Handle for our log file
176
 
  Status state; ///< The state the log is in
177
 
  const std::string log_file_path; ///< Full path to the log file
178
 
  std::string log_file_name; ///< Name of the log file
179
 
  drizzled::atomic<off_t> log_offset; ///< Offset in log file where log will write next command
180
 
  bool has_error; ///< Is the log in error?
181
 
  std::string error_message; ///< Current error message
182
 
  uint32_t sync_method; ///< Determines behaviour of syncing log file
183
 
  time_t last_sync_time; ///< Last time the log file was synced (only set in SYNC_METHOD_EVERY_SECOND)
184
131
};
185
132
 
186
 
#endif /* PLUGIN_TRANSACTION_LOG_TRANSACTION_LOG_H */
 
133
#endif /* DRIZZLE_PLUGIN_command_log_H */