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