0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OperationTimedBarrier.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "OperationProcessor.h"
25 #include "OperationTimedBarrier.h"
26 
27 #include <Common/Error.h>
28 
29 using namespace Hypertable;
30 using namespace std;
31 
33  const String &block_dependency,
34  const String &wakeup_dependency)
35  : OperationEphemeral(context, MetaLog::EntityType::OPERATION_TIMED_BARRIER),
36  m_block_dependency(block_dependency), m_wakeup_dependency(wakeup_dependency) {
37  m_obstructions.insert(block_dependency);
38  m_expire_time = chrono::steady_clock::now();
39 }
40 
42  unique_lock<mutex> lock(m_mutex);
43 
44  HT_INFOF("Entering TimedBarrier-%lld state=%s", (Lld)header.id,
45  OperationState::get_text(m_state));
46 
47  auto now = chrono::steady_clock::now();
48  while (now < m_expire_time && !m_shutdown) {
49  auto diff = chrono::duration_cast<chrono::milliseconds>(m_expire_time-now);
50  HT_INFOF("Barrier for %s will be up for %lld milliseconds",
51  m_block_dependency.c_str(), (Lld)diff.count());
52  m_cond.wait_until(lock, m_expire_time);
53  now = chrono::steady_clock::now();
54  }
55 
56  m_context->op->activate(m_wakeup_dependency);
57 
58  m_state = OperationState::COMPLETE;
59 
60  HT_INFOF("Leaving TimedBarrier-%lld state=%s", (Lld)header.id,
61  OperationState::get_text(m_state));
62 }
63 
65  return "OperationTimedBarrier";
66 }
67 
69  return "TimedBarrier";
70 }
71 
73  lock_guard<mutex> lock(m_mutex);
74  auto new_time = chrono::steady_clock::now() + chrono::milliseconds(millis);
75  if (m_expire_time < new_time)
76  m_expire_time = new_time;
77 }
78 
80  lock_guard<mutex> lock(m_mutex);
81  m_shutdown = true;
82  m_cond.notify_all();
83 }
OperationTimedBarrier(ContextPtr &context, const String &block_dependency, const String &wakeup_dependency)
ContextPtr m_context
Pointer to Master context.
Definition: Operation.h:553
Abstract base class for ephemeral operations.
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
int64_t id
Unique ID of entity.
Declarations for OperationProcessor.
std::mutex m_mutex
Mutex for serializing access to members
virtual const String label()
Human readable label for operation.
STL namespace.
EntityHeader header
Entity header
const char * get_text(int32_t state)
Definition: Operation.cc:609
std::shared_ptr< Context > ContextPtr
Smart pointer to Context.
Definition: Context.h:265
Compatibility Macros for C/C++.
void lock()
Locks the entity's mutex.
Definition: MetaLogEntity.h:97
Hypertable definitions
long long int Lld
Shortcut for printf formats.
Definition: String.h:53
DependencySet m_obstructions
Set of obstructions.
Definition: Operation.h:598
#define HT_INFOF(msg,...)
Definition: Logger.h:272
Error codes, Exception handling, error logging.
std::chrono::steady_clock::time_point m_expire_time
virtual void execute()
Executes (carries out) the operation.
virtual const String name()
Name of operation used for exclusivity.