rippled
Job.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_CORE_JOB_H_INCLUDED
21 #define RIPPLE_CORE_JOB_H_INCLUDED
22 
23 #include <ripple/core/ClosureCounter.h>
24 #include <ripple/core/LoadMonitor.h>
25 #include <functional>
26 
27 #include <functional>
28 
29 namespace ripple {
30 
31 // Note that this queue should only be used for CPU-bound jobs
32 // It is primarily intended for signature checking
33 
34 enum JobType {
35  // Special type indicating an invalid job - will go away soon.
36  jtINVALID = -1,
37 
38  // Job types - the position in this enum indicates the job priority with
39  // earlier jobs having lower priority than later jobs. If you wish to
40  // insert a job at a specific priority, simply add it at the right location.
41 
42  jtPACK, // Make a fetch pack for a peer
43  jtPUBOLDLEDGER, // An old ledger has been accepted
44  jtVALIDATION_ut, // A validation from an untrusted source
45  jtTRANSACTION_l, // A local transaction
46  jtREPLAY_REQ, // Peer request a ledger delta or a skip list
47  jtLEDGER_REQ, // Peer request ledger/txnset data
48  jtPROPOSAL_ut, // A proposal from an untrusted source
49  jtREPLAY_TASK, // A Ledger replay task/subtask
50  jtLEDGER_DATA, // Received data for a ledger we're acquiring
51  jtCLIENT, // A websocket command from the client
52  jtRPC, // A websocket command from the client
53  jtUPDATE_PF, // Update pathfinding requests
54  jtTRANSACTION, // A transaction received from the network
55  jtMISSING_TXN, // Request missing transactions
56  jtREQUESTED_TXN, // Reply with requested transactions
57  jtBATCH, // Apply batched transactions
58  jtADVANCE, // Advance validated/acquired ledgers
59  jtPUBLEDGER, // Publish a fully-accepted ledger
60  jtTXN_DATA, // Fetch a proposed set
61  jtWAL, // Write-ahead logging
62  jtVALIDATION_t, // A validation from a trusted source
63  jtWRITE, // Write out hashed objects
64  jtACCEPT, // Accept a consensus ledger
65  jtPROPOSAL_t, // A proposal from a trusted source
66  jtSWEEP, // Sweep for stale structures
67  jtNETOP_CLUSTER, // NetworkOPs cluster peer report
68  jtNETOP_TIMER, // NetworkOPs net timer processing
69  jtADMIN, // An administrative operation
70 
71  // Special job types which are not dispatched by the job pool
79  jtGENERIC, // Used just to measure time
80 
81  // Node store monitoring
85 };
86 
87 class Job
88 {
89 public:
91 
98  // VFALCO NOTE I'd prefer not to have a default constructed object.
99  // What is the semantic meaning of a Job with no associated
100  // function? Having the invariant "all Job objects refer to
101  // a job" would reduce the number of states.
102  //
103  Job();
104 
105  // Job (Job const& other);
106 
107  Job(JobType type, std::uint64_t index);
108 
110  using CancelCallback = std::function<bool(void)>;
111 
112  // VFALCO TODO try to remove the dependency on LoadMonitor.
113  Job(JobType type,
114  std::string const& name,
115  std::uint64_t index,
116  LoadMonitor& lm,
117  std::function<void(Job&)> const& job,
118  CancelCallback cancelCallback);
119 
120  // Job& operator= (Job const& other);
121 
122  JobType
123  getType() const;
124 
126  getCancelCallback() const;
127 
129  clock_type::time_point const&
130  queue_time() const;
131 
133  bool
134  shouldCancel() const;
135 
136  void
137  doJob();
138 
139  void
140  rename(std::string const& n);
141 
142  // These comparison operators make the jobs sort in priority order
143  // in the job set
144  bool
145  operator<(const Job& j) const;
146  bool
147  operator>(const Job& j) const;
148  bool
149  operator<=(const Job& j) const;
150  bool
151  operator>=(const Job& j) const;
152 
153 private:
160  clock_type::time_point m_queue_time;
161 };
162 
164 
165 } // namespace ripple
166 
167 #endif
ripple::jtTRANSACTION
@ jtTRANSACTION
Definition: Job.h:54
std::chrono::steady_clock
ripple::Job::mName
std::string mName
Definition: Job.h:159
ripple::jtHO_WRITE
@ jtHO_WRITE
Definition: Job.h:78
std::string
STL class.
std::shared_ptr
STL class.
ripple::jtCLIENT
@ jtCLIENT
Definition: Job.h:51
ripple::Job::operator>=
bool operator>=(const Job &j) const
Definition: Job.cpp:111
ripple::jtPATH_FIND
@ jtPATH_FIND
Definition: Job.h:76
ripple::jtACCEPT
@ jtACCEPT
Definition: Job.h:64
functional
ripple::jtHO_READ
@ jtHO_READ
Definition: Job.h:77
ripple::jtMISSING_TXN
@ jtMISSING_TXN
Definition: Job.h:55
ripple::jtREPLAY_TASK
@ jtREPLAY_TASK
Definition: Job.h:49
ripple::Job::mJobIndex
std::uint64_t mJobIndex
Definition: Job.h:156
ripple::jtNETOP_CLUSTER
@ jtNETOP_CLUSTER
Definition: Job.h:67
ripple::Job::queue_time
clock_type::time_point const & queue_time() const
Returns the time when the job was queued.
Definition: Job.cpp:65
ripple::jtNS_SYNC_READ
@ jtNS_SYNC_READ
Definition: Job.h:82
std::function
ripple::jtUPDATE_PF
@ jtUPDATE_PF
Definition: Job.h:53
ripple::jtADMIN
@ jtADMIN
Definition: Job.h:69
ripple::jtNS_WRITE
@ jtNS_WRITE
Definition: Job.h:84
ripple::Job::operator<=
bool operator<=(const Job &j) const
Definition: Job.cpp:135
ripple::jtSWEEP
@ jtSWEEP
Definition: Job.h:66
ripple::jtWRITE
@ jtWRITE
Definition: Job.h:63
ripple::Job::operator<
bool operator<(const Job &j) const
Definition: Job.cpp:123
ripple::jtLEDGER_DATA
@ jtLEDGER_DATA
Definition: Job.h:50
ripple::Job::getCancelCallback
CancelCallback getCancelCallback() const
Definition: Job.cpp:58
ripple::jtBATCH
@ jtBATCH
Definition: Job.h:57
ripple::jtTXN_DATA
@ jtTXN_DATA
Definition: Job.h:60
ripple::jtTRANSACTION_l
@ jtTRANSACTION_l
Definition: Job.h:45
ripple::jtPUBOLDLEDGER
@ jtPUBOLDLEDGER
Definition: Job.h:43
ripple::Job::mJob
std::function< void(Job &)> mJob
Definition: Job.h:157
ripple::jtDISK
@ jtDISK
Definition: Job.h:73
ripple::jtTXN_PROC
@ jtTXN_PROC
Definition: Job.h:74
ripple::Job::Job
Job()
Default constructor.
Definition: Job.cpp:26
ripple::jtPROPOSAL_t
@ jtPROPOSAL_t
Definition: Job.h:65
ripple::jtNETOP_TIMER
@ jtNETOP_TIMER
Definition: Job.h:68
ripple::Job::shouldCancel
bool shouldCancel() const
Returns true if the running job should make a best-effort cancel.
Definition: Job.cpp:71
ripple::jtGENERIC
@ jtGENERIC
Definition: Job.h:79
ripple::jtVALIDATION_t
@ jtVALIDATION_t
Definition: Job.h:62
ripple::jtINVALID
@ jtINVALID
Definition: Job.h:36
ripple::jtOB_SETUP
@ jtOB_SETUP
Definition: Job.h:75
ripple::Job::m_cancelCallback
CancelCallback m_cancelCallback
Definition: Job.h:154
ripple::jtRPC
@ jtRPC
Definition: Job.h:52
ripple::Job
Definition: Job.h:87
ripple::ClosureCounter< void, Job & >
std::uint64_t
ripple::LoadMonitor
Definition: LoadMonitor.h:36
ripple::jtPUBLEDGER
@ jtPUBLEDGER
Definition: Job.h:59
ripple::jtPEER
@ jtPEER
Definition: Job.h:72
ripple::Job::mType
JobType mType
Definition: Job.h:155
ripple::jtREQUESTED_TXN
@ jtREQUESTED_TXN
Definition: Job.h:56
ripple::jtPACK
@ jtPACK
Definition: Job.h:42
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Job::rename
void rename(std::string const &n)
Definition: Job.cpp:93
ripple::jtVALIDATION_ut
@ jtVALIDATION_ut
Definition: Job.h:44
ripple::jtWAL
@ jtWAL
Definition: Job.h:61
ripple::jtREPLAY_REQ
@ jtREPLAY_REQ
Definition: Job.h:46
ripple::jtPROPOSAL_ut
@ jtPROPOSAL_ut
Definition: Job.h:48
ripple::JobType
JobType
Definition: Job.h:34
ripple::Job::getType
JobType getType() const
Definition: Job.cpp:52
ripple::Job::operator>
bool operator>(const Job &j) const
Definition: Job.cpp:99
ripple::jtADVANCE
@ jtADVANCE
Definition: Job.h:58
ripple::Job::doJob
void doJob()
Definition: Job.cpp:79
ripple::Job::m_queue_time
clock_type::time_point m_queue_time
Definition: Job.h:160
ripple::Job::m_loadEvent
std::shared_ptr< LoadEvent > m_loadEvent
Definition: Job.h:158
ripple::jtNS_ASYNC_READ
@ jtNS_ASYNC_READ
Definition: Job.h:83
ripple::jtLEDGER_REQ
@ jtLEDGER_REQ
Definition: Job.h:47