~percona-dev/percona-xtradb-cluster/galera-2.x

« back to all changes in this revision

Viewing changes to gcache/src/gcache_fd.cpp

  • Committer: Alex Yurchenko
  • Date: 2011-08-31 13:30:55 UTC
  • Revision ID: ayurchen@void-20110831133055-6t4oc4zjfvq07e8t
Synced with SVN branch 1.x r2335

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009-2010 Codership Oy <info@codership.com>
 
3
 *
 
4
 */
 
5
 
 
6
#include "gcache_fd.hpp"
 
7
 
 
8
#include <galerautils.hpp>
 
9
 
 
10
#ifndef _XOPEN_SOURCE
 
11
#define _XOPEN_SOURCE 600
 
12
#endif
 
13
 
 
14
#include <cerrno>
 
15
#include <sys/types.h>
 
16
#include <sys/stat.h>
 
17
#include <fcntl.h>
 
18
#include <unistd.h>
 
19
 
 
20
#ifndef O_CLOEXEC // CentOS does not have it
 
21
#define O_CLOEXEC 0
 
22
#endif
 
23
 
 
24
namespace gcache
 
25
{
 
26
    static const int OPEN_FLAGS   = O_RDWR | O_NOATIME | O_CLOEXEC;
 
27
    static const int CREATE_FLAGS = OPEN_FLAGS | O_CREAT | O_TRUNC;
 
28
 
 
29
    FileDescriptor::FileDescriptor (const std::string& fname,
 
30
                                    bool               sync_)
 
31
        throw (gu::Exception)
 
32
        : value (open (fname.c_str(), OPEN_FLAGS, S_IRUSR | S_IWUSR)),
 
33
          name  (fname),
 
34
          size  (lseek (value, 0, SEEK_END)),
 
35
          sync  (sync_)
 
36
    {
 
37
        constructor_common();
 
38
    }
 
39
 
 
40
    FileDescriptor::FileDescriptor (const std::string& fname,
 
41
                                    size_t             length,
 
42
                                    bool               allocate,
 
43
                                    bool               sync_)
 
44
        throw (gu::Exception)
 
45
        : value (open (fname.c_str(), CREATE_FLAGS, S_IRUSR | S_IWUSR)),
 
46
          name  (fname),
 
47
          size  (length),
 
48
          sync  (sync_)
 
49
    {
 
50
        constructor_common();
 
51
 
 
52
        if (allocate)
 
53
        {
 
54
            prealloc ();           // reserve space
 
55
        }
 
56
        else
 
57
        {
 
58
            write_byte (size - 1); // reserve size
 
59
        }
 
60
    }
 
61
 
 
62
    void
 
63
    FileDescriptor::constructor_common() throw (gu::Exception)
 
64
    {
 
65
        if (value < 0) {
 
66
            gu_throw_error(errno) << "Failed to open file '" + name + '\'';
 
67
        }
 
68
/* benefits are questionable
 
69
        int err(posix_fadvise (value, 0, size, POSIX_FADV_SEQUENTIAL));
 
70
 
 
71
        if (err != 0)
 
72
        {
 
73
            log_warn << "Failed to set POSIX_FADV_SEQUENTIAL on "
 
74
                     << name << ": " << err << " (" << strerror(err) << ")";
 
75
        }
 
76
*/
 
77
        log_debug << "Opened file '" << name << "'";
 
78
        log_debug << "File descriptor: " << value;
 
79
    }
 
80
 
 
81
    FileDescriptor::~FileDescriptor ()
 
82
    {
 
83
        if (sync && fsync(value) != 0)
 
84
        {
 
85
            int const err (errno);
 
86
            log_error << "Failed to flush file '" << name << "': "
 
87
                      << gu::to_string(err) << " (" << strerror(err) << '\'';
 
88
        }
 
89
 
 
90
        if (close(value) != 0)
 
91
        {
 
92
            int const err (errno);
 
93
            log_error << "Failed to close file '" << name << "': "
 
94
                      << gu::to_string(err) << " (" << strerror(err) << '\'';
 
95
        }
 
96
        else
 
97
        {
 
98
            log_debug << "Closed  file '" << name << "'";
 
99
        }
 
100
    }
 
101
 
 
102
    void
 
103
    FileDescriptor::flush () const throw (gu::Exception)
 
104
    {
 
105
        log_debug << "Flushing file '" << name << "'";
 
106
 
 
107
        if (fsync (value) < 0) {
 
108
            gu_throw_error(errno) << "fsync() failed on '" + name + '\'';
 
109
        }
 
110
 
 
111
        log_debug << "Flushed file '" << name << "'";
 
112
    }
 
113
 
 
114
    bool
 
115
    FileDescriptor::write_byte (ssize_t offset) throw (gu::Exception)
 
116
    {
 
117
        unsigned char const byte (0);
 
118
 
 
119
        if (lseek (value, offset, SEEK_SET) != offset)
 
120
            gu_throw_error(errno) << "lseek() failed on '" << name << '\'';
 
121
 
 
122
        if (write (value, &byte, sizeof(byte)) != sizeof(byte))
 
123
            gu_throw_error(errno) << "write() failed on '" << name << '\'';
 
124
 
 
125
        return true;
 
126
    }
 
127
 
 
128
#if 0
 
129
    void
 
130
    FileDescriptor::prealloc() throw (gu::Exception)
 
131
    {
 
132
        size_t  const page_size = sysconf (_SC_PAGE_SIZE);
 
133
 
 
134
        size_t  offset = page_size - 1; // last byte of the page
 
135
 
 
136
        log_info << "Preallocating " << size << " bytes in '" << name
 
137
                 << "'...";
 
138
 
 
139
        while (offset < size && write_byte (offset))
 
140
        {
 
141
            offset += page_size;
 
142
        }
 
143
 
 
144
        if (offset > size && write_byte (size - 1) && fsync (value) == 0) {
 
145
            log_info << "Preallocating " << size << " bytes in '" << name
 
146
                     << "' done.";
 
147
            return;
 
148
        }
 
149
 
 
150
        gu_throw_error (errno) << "File preallocation failed";
 
151
    }
 
152
#endif
 
153
    void
 
154
    FileDescriptor::prealloc() throw (gu::Exception)
 
155
    {
 
156
        log_info << "Preallocating " << size << " bytes in '" << name
 
157
                 << "'...";
 
158
 
 
159
        if (0 != posix_fallocate (value, 0, size))
 
160
        {
 
161
            gu_throw_error (errno) << "File preallocation failed";
 
162
        }
 
163
    }
 
164
}