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
|
#ifndef TARANTOOL_JOURNAL_H_INCLUDED
#define TARANTOOL_JOURNAL_H_INCLUDED
/*
* Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdbool.h>
#include "salad/stailq.h"
#include "fiber.h"
#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */
struct xrow_header;
struct journal_entry;
/**
* An entry for an abstract journal.
* Simply put, a write ahead log request.
*
* In case of synchronous replication, this request will travel
* first to a Raft leader before going to the local WAL.
*/
struct journal_entry {
/** A helper to include requests into a FIFO queue. */
struct stailq_entry fifo;
/**
* On success, contains vclock signature of
* the committed transaction, on error is -1
*/
int64_t res;
/**
* A journal entry completion callback argument.
*/
void *complete_data;
/**
* Approximate size of this request when encoded.
*/
size_t approx_len;
/**
* The number of rows in the request.
*/
int n_rows;
/**
* The rows.
*/
struct xrow_header *rows[];
};
struct region;
/**
* Create a new journal entry.
*
* @return NULL if out of memory, fiber diagnostics area is set
*/
struct journal_entry *
journal_entry_new(size_t n_rows, struct region *region,
void *complete_data);
/**
* An API for an abstract journal for all transactions of this
* instance, as well as for multiple instances in case of
* synchronous replication.
*/
struct journal {
/** Asynchronous write */
int (*write_async)(struct journal *journal,
struct journal_entry *entry);
/** Asynchronous write completion */
void (*write_async_cb)(struct journal_entry *entry);
/** Synchronous write */
int (*write)(struct journal *journal,
struct journal_entry *entry);
};
/**
* Finalize a single entry.
*/
static inline void
journal_async_complete(struct journal *journal, struct journal_entry *entry)
{
assert(journal->write_async_cb != NULL);
journal->write_async_cb(entry);
}
/**
* Depending on the step of recovery and instance configuration
* points at a concrete implementation of the journal.
*/
extern struct journal *current_journal;
/**
* Write a single entry to the journal in synchronous way.
*
* @return 0 if write was processed by a backend or -1 in case of an error.
*/
static inline int
journal_write(struct journal_entry *entry)
{
return current_journal->write(current_journal, entry);
}
/**
* Queue a single entry to the journal in asynchronous way.
*
* @return 0 if write was queued to a backend or -1 in case of an error.
*/
static inline int
journal_write_async(struct journal_entry *entry)
{
return current_journal->write_async(current_journal, entry);
}
/**
* Change the current implementation of the journaling API.
* Happens during life cycle of an instance:
*
* 1. When recovering a snapshot, the log sequence numbers
* don't matter and are not used, transactions
* can be recovered in any order. A stub API simply
* returns 0 for every write request.
*
* 2. When recovering from the local write ahead
* log, the LSN of each entry is already known. In this case,
* the journal API should simply return the existing
* log sequence numbers of records and do nothing else.
*
* 2. After recovery, in wal_mode = NONE, the implementation
* fakes a WAL by using a simple counter to provide
* log sequence numbers.
*
* 3. If the write ahead log is on, the WAL thread
* is issuing the log sequence numbers.
*/
static inline void
journal_set(struct journal *new_journal)
{
current_journal = new_journal;
}
static inline void
journal_create(struct journal *journal,
int (*write_async)(struct journal *journal,
struct journal_entry *entry),
void (*write_async_cb)(struct journal_entry *entry),
int (*write)(struct journal *journal,
struct journal_entry *entry))
{
journal->write_async = write_async;
journal->write_async_cb = write_async_cb;
journal->write = write;
}
/**
* A stub to issue an error in case if asynchronous
* write is diabled in the backend.
*/
extern int
journal_no_write_async(struct journal *journal,
struct journal_entry *entry);
extern void
journal_no_write_async_cb(struct journal_entry *entry);
static inline bool
journal_is_initialized(struct journal *journal)
{
return journal->write != NULL;
}
#if defined(__cplusplus)
} /* extern "C" */
#endif /* defined(__cplusplus) */
#endif /* TARANTOOL_JOURNAL_H_INCLUDED */
|