rippled
NodeStoreScheduler.cpp
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 #include <ripple/app/main/NodeStoreScheduler.h>
21 #include <cassert>
22 
23 namespace ripple {
24 
26  : Stoppable("NodeStoreScheduler", parent)
27 {
28 }
29 
30 void
32 {
33  m_jobQueue = &jobQueue;
34 }
35 
36 void
38 {
39 }
40 
41 void
43 {
44  assert(m_taskCount == 0);
45  stopped();
46 }
47 
48 void
50 {
51  ++m_taskCount;
52  if (!m_jobQueue->addJob(jtWRITE, "NodeObject::store", [this, &task](Job&) {
53  doTask(task);
54  }))
55  {
56  // Job not added, presumably because we're shutting down.
57  // Recover by executing the task synchronously.
58  doTask(task);
59  }
60 }
61 
62 void
64 {
65  task.performScheduledTask();
66 
67  // NOTE: It feels a bit off that there are two different methods that
68  // call stopped(): onChildrenStopped() and doTask(). There's a
69  // suspicion that, as long as the Stoppable tree is configured
70  // correctly, this call to stopped() in doTask() can never occur.
71  //
72  // However, until we increase our confidence that the suspicion is
73  // correct, we will leave this code in place.
74  if ((--m_taskCount == 0) && isStopping() && areChildrenStopped())
75  stopped();
76 }
77 
78 void
80 {
81  if (report.wentToDisk)
84  1,
85  report.elapsed);
86 }
87 
88 void
90 {
92 }
93 
94 } // namespace ripple
ripple::Stoppable::stopped
void stopped()
Called by derived classes to indicate that the stoppable has stopped.
Definition: Stoppable.cpp:72
ripple::NodeStoreScheduler::scheduleTask
void scheduleTask(NodeStore::Task &task) override
Schedules a task.
Definition: NodeStoreScheduler.cpp:49
ripple::NodeStoreScheduler::onFetch
void onFetch(NodeStore::FetchReport const &report) override
Reports completion of a fetch Allows the scheduler to monitor the node store's performance.
Definition: NodeStoreScheduler.cpp:79
ripple::NodeStoreScheduler::onBatchWrite
void onBatchWrite(NodeStore::BatchWriteReport const &report) override
Reports the completion of a batch write Allows the scheduler to monitor the node store's performance.
Definition: NodeStoreScheduler.cpp:89
ripple::NodeStoreScheduler::NodeStoreScheduler
NodeStoreScheduler(Stoppable &parent)
Definition: NodeStoreScheduler.cpp:25
ripple::jtNS_SYNC_READ
@ jtNS_SYNC_READ
Definition: Job.h:77
ripple::NodeStore::FetchReport
Contains information about a fetch operation.
Definition: ripple/nodestore/Scheduler.h:30
ripple::JobQueue::addJob
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
ripple::jtNS_WRITE
@ jtNS_WRITE
Definition: Job.h:79
ripple::NodeStoreScheduler::doTask
void doTask(NodeStore::Task &task)
Definition: NodeStoreScheduler.cpp:63
ripple::jtWRITE
@ jtWRITE
Definition: Job.h:58
ripple::NodeStoreScheduler::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: NodeStoreScheduler.cpp:37
ripple::NodeStoreScheduler::m_jobQueue
JobQueue * m_jobQueue
Definition: NodeStoreScheduler.h:58
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::NodeStore::Task
Derived classes perform scheduled tasks.
Definition: Task.h:27
ripple::Stoppable::areChildrenStopped
bool areChildrenStopped() const
Returns true if all children have stopped.
Definition: Stoppable.cpp:66
ripple::Job
Definition: Job.h:82
ripple::NodeStore::FetchReport::isAsync
bool isAsync
Definition: ripple/nodestore/Scheduler.h:35
ripple::NodeStore::BatchWriteReport
Contains information about a batch write operation.
Definition: ripple/nodestore/Scheduler.h:41
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::NodeStoreScheduler::setJobQueue
void setJobQueue(JobQueue &jobQueue)
Definition: NodeStoreScheduler.cpp:31
ripple::NodeStoreScheduler::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: NodeStoreScheduler.cpp:42
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::Task::performScheduledTask
virtual void performScheduledTask()=0
Performs the task.
cassert
ripple::NodeStoreScheduler::m_taskCount
std::atomic< int > m_taskCount
Definition: NodeStoreScheduler.h:59
ripple::NodeStore::BatchWriteReport::writeCount
int writeCount
Definition: ripple/nodestore/Scheduler.h:46
ripple::NodeStore::BatchWriteReport::elapsed
std::chrono::milliseconds elapsed
Definition: ripple/nodestore/Scheduler.h:45
ripple::JobQueue::addLoadEvents
void addLoadEvents(JobType t, int count, std::chrono::milliseconds elapsed)
Add multiple load events.
Definition: JobQueue.cpp:193
ripple::NodeStore::FetchReport::wentToDisk
bool wentToDisk
Definition: ripple/nodestore/Scheduler.h:36
ripple::NodeStore::FetchReport::elapsed
std::chrono::milliseconds elapsed
Definition: ripple/nodestore/Scheduler.h:34
ripple::jtNS_ASYNC_READ
@ jtNS_ASYNC_READ
Definition: Job.h:78
ripple::Stoppable::isStopping
bool isStopping() const
Returns true if the stoppable should stop.
Definition: Stoppable.cpp:54