journal: synchronize seqnum across files

This commit is contained in:
Lennart Poettering 2011-10-14 05:12:58 +02:00
parent 161e54f871
commit c2373f848d
7 changed files with 43 additions and 11 deletions

View file

@ -364,12 +364,24 @@ int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Objec
return 0;
}
static uint64_t journal_file_seqnum(JournalFile *f) {
static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
uint64_t r;
assert(f);
r = le64toh(f->header->seqnum) + 1;
if (seqnum) {
/* If an external seqno counter was passed, we update
* both the local and the external one, and set it to
* the maximum of both */
if (*seqnum + 1 > r)
r = *seqnum + 1;
*seqnum = r;
}
f->header->seqnum = htole64(r);
return r;
@ -733,6 +745,7 @@ static int journal_file_append_entry_internal(
const dual_timestamp *ts,
uint64_t xor_hash,
const EntryItem items[], unsigned n_items,
uint64_t *seqno,
Object **ret, uint64_t *offset) {
uint64_t np;
uint64_t osize;
@ -749,7 +762,7 @@ static int journal_file_append_entry_internal(
return r;
o->object.type = htole64(OBJECT_ENTRY);
o->entry.seqnum = htole64(journal_file_seqnum(f));
o->entry.seqnum = htole64(journal_file_seqnum(f, seqno));
memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
o->entry.realtime = htole64(ts ? ts->realtime : now(CLOCK_REALTIME));
o->entry.monotonic = htole64(ts ? ts->monotonic : now(CLOCK_MONOTONIC));
@ -769,7 +782,7 @@ static int journal_file_append_entry_internal(
return 0;
}
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset) {
unsigned i;
EntryItem *items;
int r;
@ -794,7 +807,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
items[i].object_offset = htole64(p);
}
r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqno, ret, offset);
finish:
free(items);

View file

@ -63,7 +63,7 @@ int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Objec
uint64_t journal_file_entry_n_items(Object *o);
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset);
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset);
int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset);

View file

@ -50,6 +50,7 @@ int main(int argc, char *argv[]) {
const void *data;
size_t length;
char *cursor;
uint64_t realtime = 0, monotonic = 0;
r = sd_journal_get_cursor(j, &cursor);
if (r < 0) {
@ -60,6 +61,13 @@ int main(int argc, char *argv[]) {
printf("entry: %s\n", cursor);
free(cursor);
sd_journal_get_realtime_usec(j, &realtime);
sd_journal_get_monotonic_usec(j, &monotonic);
printf("realtime: %llu\n"
"monotonic: %llu\n",
(unsigned long long) realtime,
(unsigned long long) monotonic);
SD_JOURNAL_FOREACH_FIELD(j, data, length)
printf("\t%.*s\n", (int) length, (const char*) data);
}

View file

@ -43,6 +43,8 @@ typedef struct Server {
JournalFile *runtime_journal;
JournalFile *system_journal;
Hashmap *user_journals;
uint64_t seqnum;
} Server;
static void fix_perms(JournalFile *f, uid_t uid) {
@ -118,7 +120,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
return s->system_journal;
r = journal_file_open(p, O_RDWR|O_CREAT, 0640, NULL, &f);
r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
free(p);
if (r < 0)
@ -252,7 +254,7 @@ static void process_message(Server *s, const char *buf, struct ucred *ucred, str
if (!f)
log_warning("Dropping message, as we can't find a place to store the data.");
else {
r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
if (r < 0)
log_error("Failed to write entry, ignoring: %s", strerror(-r));

View file

@ -561,3 +561,13 @@ int sd_journal_iterate_fields(sd_journal *j, const void **data, size_t *size) {
return 1;
}
int sd_journal_seek_head(sd_journal *j) {
assert(j);
return -EINVAL;
}
int sd_journal_seek_tail(sd_journal *j) {
assert(j);
return -EINVAL;
}

View file

@ -56,7 +56,6 @@ void sd_journal_flush_matches(sd_journal *j);
int sd_journal_seek_head(sd_journal *j);
int sd_journal_seek_tail(sd_journal *j);
int sd_journal_seek_seqnum(sd_journal *j, uint64_t seqnum);
int sd_journal_seek_monotonic_usec(sd_journal *j, uint64_t usec);
int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);

View file

@ -42,15 +42,15 @@ int main(int argc, char *argv[]) {
iovec.iov_base = (void*) test;
iovec.iov_len = strlen(test);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
iovec.iov_base = (void*) test2;
iovec.iov_len = strlen(test2);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
iovec.iov_base = (void*) test;
iovec.iov_len = strlen(test);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL) == 0);
assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
journal_file_dump(f);