Apache Mesos
registrar.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 __MASTER_REGISTRAR_HPP__
18 #define __MASTER_REGISTRAR_HPP__
19 
20 #include <mesos/mesos.hpp>
21 
22 #include <mesos/state/state.hpp>
23 
24 #include <process/future.hpp>
25 #include <process/owned.hpp>
26 #include <process/pid.hpp>
27 
28 #include <stout/hashset.hpp>
29 
30 #include "master/flags.hpp"
31 #include "master/registry.hpp"
32 
33 namespace mesos {
34 namespace internal {
35 namespace master {
36 
37 // Forward declaration.
38 class RegistrarProcess;
39 
40 // Defines an abstraction for operations that can be applied on the
41 // Registry.
42 // TODO(xujyan): Make RegistryOperation generic so that we can apply
43 // them against a generic "batch operation applier" abstraction, see
44 // the TODO below for more details.
45 class RegistryOperation : public process::Promise<bool>
46 {
47 public:
48  // Attempts to invoke the operation on the registry object.
49  // Aided by accumulator(s):
50  // slaveIDs - is the set of registered slaves.
51  //
52  // Returns whether the operation mutates 'registry', or an error if
53  // the operation cannot be applied successfully.
54  Try<bool> operator()(Registry* registry, hashset<SlaveID>* slaveIDs)
55  {
56  const Try<bool> result = perform(registry, slaveIDs);
57 
58  success = !result.isError();
59 
60  return result;
61  }
62 
63  // Sets the promise based on whether the operation was successful.
64  bool set() { return process::Promise<bool>::set(success); }
65 
66 protected:
67  virtual Try<bool> perform(Registry* registry, hashset<SlaveID>* slaveIDs) = 0;
68 
69 private:
70  bool success = false;
71 };
72 
73 
74 // TODO(xujyan): Add a generic abstraction for applying batches of
75 // operations against State Variables. The Registrar and other
76 // components could leverage this. This abstraction would be
77 // templatized to take the type, along with any accumulators:
78 // template <typename T,
79 // typename X = None,
80 // typename Y = None,
81 // typename Z = None>
82 // T: the data type that the batch operations can be applied on.
83 // X, Y, Z: zero to 3 generic accumulators that facilitate the batch
84 // of operations.
85 // This allows us to reuse the idea of "doing batches of operations"
86 // on other potential new state variables (i.e. repair state, offer
87 // reservations, etc).
88 class Registrar
89 {
90 public:
91  Registrar(const Flags& flags,
92  mesos::state::State* state,
93  const Option<std::string>& authenticationRealm = None());
94  virtual ~Registrar();
95 
96  // Recovers the Registry, persisting the new Master information.
97  // The Registrar must be recovered to allow other operations to
98  // proceed.
99  // TODO(bmahler): Consider a "factory" for constructing the
100  // Registrar, to eliminate the need for passing 'MasterInfo'.
101  // This is required as the Registrar is injected into the Master,
102  // and therefore MasterInfo is unknown during construction.
103  process::Future<Registry> recover(const MasterInfo& info);
104 
105  // Applies an operation on the Registry.
106  // Returns:
107  // true if the operation is permitted.
108  // false if the operation is not permitted.
109  // Failure if the operation fails (possibly lost log leadership),
110  // or recovery failed.
111  virtual process::Future<bool> apply(
113 
114  // Gets the pid of the underlying process.
115  // Used in tests.
116  process::PID<RegistrarProcess> pid() const;
117 
118 private:
119  RegistrarProcess* process;
120 };
121 
122 } // namespace master {
123 } // namespace internal {
124 } // namespace mesos {
125 
126 #endif // __MASTER_REGISTRAR_HPP__
Protocol< RecoverRequest, RecoverResponse > recover
Definition: state.hpp:90
bool set(const T &_t)
Definition: future.hpp:827
Definition: master.hpp:27
Definition: check.hpp:33
Definition: flags.hpp:42
Definition: registrar.hpp:88
Definition: agent.hpp:25
Definition: future.hpp:74
virtual Try< bool > perform(Registry *registry, hashset< SlaveID > *slaveIDs)=0
Try< bool > operator()(Registry *registry, hashset< SlaveID > *slaveIDs)
Definition: registrar.hpp:54
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:289
Definition: none.hpp:27
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:78
Definition: registrar.hpp:45
Definition: owned.hpp:36
Definition: parse.hpp:33
Definition: future.hpp:58