Apache Mesos
log.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_LOG_LOG_HPP__
18 #define __MESOS_LOG_LOG_HPP__
19 
20 #include <stdint.h>
21 
22 #include <list>
23 #include <set>
24 #include <string>
25 
27 
28 #include <process/future.hpp>
29 #include <process/process.hpp>
30 
31 #include <stout/duration.hpp>
32 #include <stout/none.hpp>
33 #include <stout/option.hpp>
34 
35 namespace mesos {
36 namespace internal {
37 namespace log {
38 
39 // Forward declarations.
40 class LogProcess;
41 class LogReaderProcess;
42 class LogWriterProcess;
43 
44 } // namespace log {
45 } // namespace internal {
46 } // namespace mesos {
47 
48 
49 namespace mesos {
50 namespace log {
51 
52 class Log
53 {
54 public:
55  // Forward declarations.
56  class Reader;
57  class Writer;
58 
59  class Position
60  {
61  public:
62  bool operator==(const Position& that) const
63  {
64  return value == that.value;
65  }
66 
67  bool operator<(const Position& that) const
68  {
69  return value < that.value;
70  }
71 
72  bool operator<=(const Position& that) const
73  {
74  return value <= that.value;
75  }
76 
77  bool operator>(const Position& that) const
78  {
79  return value > that.value;
80  }
81 
82  bool operator>=(const Position& that) const
83  {
84  return value >= that.value;
85  }
86 
87  // Returns an "identity" off this position, useful for serializing
88  // to logs or across communication mediums.
89  std::string identity() const
90  {
91  CHECK(sizeof(value) == 8);
92  char bytes[8];
93  bytes[0] =(0xff & (value >> 56));
94  bytes[1] = (0xff & (value >> 48));
95  bytes[2] = (0xff & (value >> 40));
96  bytes[3] = (0xff & (value >> 32));
97  bytes[4] = (0xff & (value >> 24));
98  bytes[5] = (0xff & (value >> 16));
99  bytes[6] = (0xff & (value >> 8));
100  bytes[7] = (0xff & value);
101  return std::string(bytes, sizeof(bytes));
102  }
103 
104  private:
105  friend class Log;
106  friend class Writer;
109 
110  /*implicit*/ Position(uint64_t _value) : value(_value) {}
111 
112  uint64_t value;
113  };
114 
115  class Entry
116  {
117  public:
119  std::string data;
120 
121  private:
123 
124  Entry(const Position& _position, const std::string& _data)
125  : position(_position), data(_data) {}
126  };
127 
128  class Reader
129  {
130  public:
131  explicit Reader(Log* log);
132  ~Reader();
133 
134  // Returns all entries between the specified positions, unless
135  // those positions are invalid, in which case returns an error.
137  const Position& from,
138  const Position& to);
139 
140  // Returns the beginning position of the log from the perspective
141  // of the local replica (which may be out of date if the log has
142  // been opened and truncated while this replica was partitioned).
144 
145  // Returns the ending (i.e., last) position of the log from the
146  // perspective of the local replica (which may be out of date if
147  // the log has been opened and appended to while this replica was
148  // partitioned).
150 
151  // Launches the catch-up process. Returns the ending position of
152  // the caught-up range.
154 
155  private:
157  };
158 
159  class Writer
160  {
161  public:
162  // Creates a new writer associated with the specified log. Only
163  // one writer (local or remote) can be valid at any point in
164  // time. A writer becomes invalid if either Writer::append or
165  // Writer::truncate return None, in which case, the writer (or
166  // another writer) must be restarted.
167  explicit Writer(Log* log);
168  ~Writer();
169 
170  // Attempts to get a promise (from the log's replicas) for
171  // exclusive writes, i.e., no other writer's will be able to
172  // perform append and truncate operations. Returns the ending
173  // position of the log or none if the promise to exclusively write
174  // could not be attained but may be retried.
176 
177  // Attempts to append the specified data to the log. Returns the
178  // new ending position of the log or 'none' if this writer has
179  // lost its promise to exclusively write (which can be reacquired
180  // by invoking Writer::start).
181  process::Future<Option<Position>> append(const std::string& data);
182 
183  // Attempts to truncate the log up to but not including the
184  // specificed position. Returns the new ending position of the log
185  // or 'none' if this writer has lost its promise to exclusively
186  // write (which can be reacquired by invoking Writer::start).
188 
189  private:
191  };
192 
193  // Creates a new replicated log that assumes the specified quorum
194  // size, is backed by a file at the specified path, and coordinates
195  // with other replicas via the set of process PIDs.
196  Log(int quorum,
197  const std::string& path,
198  const std::set<process::UPID>& pids,
199  bool autoInitialize = false,
200  const Option<std::string>& metricsPrefix = None());
201 
202  // Creates a new replicated log that assumes the specified quorum
203  // size, is backed by a file at the specified path, and coordinates
204  // with other replicas associated with the specified ZooKeeper
205  // servers, timeout, and znode.
206  Log(int quorum,
207  const std::string& path,
208  const std::string& servers,
209  const Duration& timeout,
210  const std::string& znode,
211  const Option<zookeeper::Authentication>& auth = None(),
212  bool autoInitialize = false,
213  const Option<std::string>& metricsPrefix = None());
214 
215  ~Log();
216 
217  // Returns a position based off of the bytes recovered from
218  // Position.identity().
219  Position position(const std::string& identity) const
220  {
221  CHECK(identity.size() == 8);
222  const char* bytes = identity.c_str();
223  uint64_t value =
224  ((uint64_t) (bytes[0] & 0xff) << 56) |
225  ((uint64_t) (bytes[1] & 0xff) << 48) |
226  ((uint64_t) (bytes[2] & 0xff) << 40) |
227  ((uint64_t) (bytes[3] & 0xff) << 32) |
228  ((uint64_t) (bytes[4] & 0xff) << 24) |
229  ((uint64_t) (bytes[5] & 0xff) << 16) |
230  ((uint64_t) (bytes[6] & 0xff) << 8) |
231  ((uint64_t) (bytes[7] & 0xff));
232  return Position(value);
233  }
234 
235 private:
238 
240 };
241 
242 } // namespace log {
243 } // namespace mesos {
244 
245 #endif // __MESOS_LOG_LOG_HPP__
Log(int quorum, const std::string &path, const std::set< process::UPID > &pids, bool autoInitialize=false, const Option< std::string > &metricsPrefix=None())
std::string data
Definition: log.hpp:119
bool operator<=(const Position &that) const
Definition: log.hpp:72
process::Future< Position > catchup()
process::Future< std::list< Entry > > read(const Position &from, const Position &to)
process::Future< Position > beginning()
bool operator==(const Position &that) const
Definition: log.hpp:62
bool operator>(const Position &that) const
Definition: log.hpp:77
bool operator>=(const Position &that) const
Definition: log.hpp:82
std::string identity() const
Definition: log.hpp:89
Definition: duration.hpp:32
Definition: log.hpp:128
Position position(const std::string &identity) const
Definition: log.hpp:219
Definition: log.hpp:52
process::Future< Position > ending()
process::Future< Option< Position > > truncate(const Position &to)
process::Future< Option< Position > > append(const std::string &data)
Definition: log.hpp:159
Position position
Definition: log.hpp:118
Definition: log.hpp:43
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
Definition: log.hpp:59
Definition: log.hpp:115
bool operator<(const Position &that) const
Definition: log.hpp:67
process::Future< Option< Position > > start()
Try< std::set< pid_t > > pids()
Definition: freebsd.hpp:62
Definition: future.hpp:57