Apache Mesos
protobuf.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_STATE_PROTOBUF_HPP__
18 #define __MESOS_STATE_PROTOBUF_HPP__
19 
20 #include <string>
21 
22 #include <mesos/state/state.hpp>
23 #include <mesos/state/storage.hpp>
24 
25 #include <process/future.hpp>
26 
27 #include <stout/lambda.hpp>
28 #include <stout/option.hpp>
29 #include <stout/protobuf.hpp>
30 #include <stout/some.hpp>
31 #include <stout/try.hpp>
32 #include <stout/uuid.hpp>
33 
34 namespace mesos {
35 namespace state {
36 namespace protobuf {
37 
38 class State; // Forward declaration.
39 
40 
41 template <typename T>
42 class Variable
43 {
44 public:
45  T get() const
46  {
47  return t;
48  }
49 
50  Variable mutate(const T& t) const
51  {
52  Variable variable(*this);
53  variable.t = t;
54  return variable;
55  }
56 
57 private:
58  friend class State; // Creates and manages variables.
59 
60  Variable(const mesos::state::Variable& _variable, const T& _t)
61  : variable(_variable), t(_t)
62  {}
63 
64  mesos::state::Variable variable; // Not const to keep Variable assignable.
65  T t;
66 };
67 
68 
69 class State : public mesos::state::State
70 {
71 public:
72  explicit State(mesos::state::Storage* storage)
73  : mesos::state::State(storage) {}
74  virtual ~State() {}
75 
76  // Returns a variable from the state, creating a new one if one
77  // previously did not exist (or an error if one occurs).
78  template <typename T>
79  process::Future<Variable<T>> fetch(const std::string& name);
80 
81  // Returns the variable specified if it was successfully stored in
82  // the state, otherwise returns none if the version of the variable
83  // was no longer valid, or an error if one occurs.
84  template <typename T>
85  process::Future<Option<Variable<T>>> store(const Variable<T>& variable);
86 
87  // Expunges the variable from the state.
88  template <typename T>
89  process::Future<bool> expunge(const Variable<T>& variable);
90 
91 private:
92  // Helpers to handle future results from fetch and swap. We make
93  // these static members of State for friend access to Variable's
94  // constructor.
95  template <typename T>
96  static process::Future<Variable<T>> _fetch(
97  const mesos::state::Variable& variable);
98 
99  template <typename T>
100  static process::Future<Option<Variable<T>>> _store(
101  const T& t,
102  const Option<mesos::state::Variable>& variable);
103 };
104 
105 
106 template <typename T>
108 {
109  return mesos::state::State::fetch(name)
110  .then(lambda::bind(&State::template _fetch<T>, lambda::_1));
111 }
112 
113 
114 template <typename T>
115 process::Future<Variable<T>> State::_fetch(
116  const mesos::state::Variable& variable)
117 {
118  Try<T> t = ::protobuf::deserialize<T>(variable.value());
119  if (t.isError()) {
120  return process::Failure(t.error());
121  }
122 
123  return Variable<T>(variable, t.get());
124 }
125 
126 
127 template <typename T>
129  const Variable<T>& variable)
130 {
131  Try<std::string> value = ::protobuf::serialize(variable.t);
132 
133  if (value.isError()) {
134  return process::Failure(value.error());
135  }
136 
137  return mesos::state::State::store(variable.variable.mutate(value.get()))
138  .then(lambda::bind(&State::template _store<T>, variable.t, lambda::_1));
139 }
140 
141 
142 template <typename T>
143 process::Future<Option<Variable<T>>> State::_store(
144  const T& t,
145  const Option<mesos::state::Variable>& variable)
146 {
147  if (variable.isSome()) {
148  return Some(Variable<T>(variable.get(), t));
149  }
150 
151  return None();
152 }
153 
154 
155 template <typename T>
157 {
158  return mesos::state::State::expunge(variable.variable);
159 }
160 
161 } // namespace protobuf {
162 } // namespace state {
163 } // namespace mesos {
164 
165 #endif // __MESOS_STATE_PROTOBUF_HPP__
process::Future< bool > expunge(const Variable &variable)
Definition: state.hpp:181
process::Future< Option< Variable > > store(const Variable &variable)
Definition: state.hpp:152
State(mesos::state::Storage *storage)
Definition: protobuf.hpp:72
Definition: option.hpp:28
Definition: state.hpp:90
T & get()&
Definition: try.hpp:73
process::Future< Option< Variable< T > > > store(const Variable< T > &variable)
Definition: protobuf.hpp:128
Definition: check.hpp:33
Definition: future.hpp:665
std::string value() const
Definition: state.hpp:67
virtual ~State()
Definition: protobuf.hpp:74
Try< T > fetch(const std::string &value)
Definition: fetch.hpp:38
Try< std::string > serialize(const T &t)
Definition: protobuf.hpp:181
bool isSome() const
Definition: option.hpp:115
Variable mutate(const T &t) const
Definition: protobuf.hpp:50
Definition: protobuf.hpp:69
Definition: state.hpp:64
Variable mutate(const std::string &value) const
Definition: state.hpp:72
process::Future< Variable< T > > fetch(const std::string &name)
Definition: protobuf.hpp:107
Definition: spec.hpp:30
const T & get() const &
Definition: option.hpp:118
Definition: protobuf.hpp:55
static Try error(const E &e)
Definition: try.hpp:42
Definition: storage.hpp:33
process::Future< Variable > fetch(const std::string &name)
Definition: state.hpp:127
process::Future< bool > expunge(const Variable< T > &variable)
Definition: protobuf.hpp:156
_Some< typename std::decay< T >::type > Some(T &&t)
Definition: some.hpp:42
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:71
Definition: protobuf.hpp:42
Try< Nothing > bind(int_fd s, const Address &address)
Definition: network.hpp:46
constexpr const char * name
Definition: shell.hpp:43
Definition: future.hpp:57