0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ScannerMap.cc
Go to the documentation of this file.
1 /*
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 
26 
27 #include <Common/Compat.h>
28 
29 #include "ScannerMap.h"
30 
31 #include <Common/Time.h>
32 
33 using namespace Hypertable;
34 using namespace std;
35 
36 atomic<int> ScannerMap::ms_next_id {0};
37 
41  const TableIdentifier &table, ProfileDataScanner &profile_data) {
42  lock_guard<mutex> lock(m_mutex);
43  ScanInfo scaninfo;
44  scaninfo.scanner = scanner;
45  scaninfo.range = range;
46  scaninfo.last_access_millis = get_timestamp_millis();
47  scaninfo.table= table;
48  scaninfo.profile_data = profile_data;
49  int32_t id = ++ms_next_id;
50  m_scanner_map[id] = scaninfo;
51  return id;
52 }
53 
54 
55 
58 bool
59 ScannerMap::get(int32_t id, MergeScannerRangePtr &scanner, RangePtr &range,
60  TableIdentifierManaged &table,ProfileDataScanner *profile_data){
61  lock_guard<mutex> lock(m_mutex);
62  auto iter = m_scanner_map.find(id);
63  if (iter == m_scanner_map.end())
64  return false;
65  (*iter).second.last_access_millis = get_timestamp_millis();
66  scanner = (*iter).second.scanner;
67  range = (*iter).second.range;
68  table = (*iter).second.table;
69  *profile_data = (*iter).second.profile_data;
70  return true;
71 }
72 
73 
74 
77 bool ScannerMap::remove(int32_t id) {
78  lock_guard<mutex> lock(m_mutex);
79  return (m_scanner_map.erase(id) == 0) ? false : true;
80 }
81 
82 
83 void ScannerMap::purge_expired(int32_t max_idle_millis) {
84  lock_guard<mutex> lock(m_mutex);
85  int64_t now_millis = get_timestamp_millis();
86  auto iter = m_scanner_map.begin();
87  while (iter != m_scanner_map.end()) {
88  if ((now_millis - (*iter).second.last_access_millis) > (int64_t)max_idle_millis) {
89  auto tmp_iter = iter;
90  HT_WARNF("Destroying scanner %d because it has not been used in %u "
91  "milliseconds", (*iter).first, max_idle_millis);
92  ++iter;
93  (*tmp_iter).second.scanner = 0;
94  (*tmp_iter).second.range = 0;
95  m_scanner_map.erase(tmp_iter);
96  }
97  else
98  ++iter;
99  }
100 
101 }
102 
103 
104 void ScannerMap::get_counts(int32_t *totalp, CstrToInt32Map &table_scanner_count_map) {
105  lock_guard<mutex> lock(m_mutex);
106  CstrToInt32Map::iterator tsc_iter;
107 
108  *totalp = m_scanner_map.size();
109 
110  for (auto & entry : m_scanner_map) {
111  if ((tsc_iter = table_scanner_count_map.find(entry.second.table.id)) != table_scanner_count_map.end())
112  table_scanner_count_map[entry.second.table.id]++;
113  }
114 
115 }
116 
117 void ScannerMap::update_profile_data(int32_t id, ProfileDataScanner &profile_data) {
118  lock_guard<mutex> lock(m_mutex);
119  auto iter = m_scanner_map.find(id);
120  if (iter == m_scanner_map.end())
121  HT_WARNF("Unable to locate scanner ID %u in scanner map", (unsigned)id);
122  else
123  iter->second.profile_data = profile_data;
124 }
125 
126 
128  return get_ts64() / 1000000LL;
129 }
void purge_expired(int32_t max_idle_ms)
This method iterates through the scanner map purging mappings that have not been referenced for max_i...
Definition: ScannerMap.cc:83
#define HT_WARNF(msg,...)
Definition: Logger.h:290
int64_t last_access_millis
Last access time in milliseconds since epoch.
Definition: ScannerMap.h:133
ProfileDataScanner profile_data
Accumulated profile data.
Definition: ScannerMap.h:137
STL namespace.
Wrapper for TableIdentifier providing member storage.
MergeScannerRangePtr scanner
Scanner.
Definition: ScannerMap.h:129
std::shared_ptr< MergeScannerRange > MergeScannerRangePtr
Smart pointer to MergeScannerRange.
int32_t put(MergeScannerRangePtr &scanner, RangePtr &range, const TableIdentifier &table, ProfileDataScanner &profile_data)
This method computes a unique scanner ID and puts the given scanner and range pointers into a map usi...
Definition: ScannerMap.cc:40
Holds scanner information.
Definition: ScannerMap.h:127
Compatibility Macros for C/C++.
static std::atomic< int > ms_next_id
Next available scanner ID.
Definition: ScannerMap.h:121
Time related declarations.
Hypertable definitions
std::shared_ptr< Range > RangePtr
Smart pointer to Range.
Definition: Range.h:404
bool remove(int32_t id)
This method removes the entry in the scanner map corresponding to the given id.
Definition: ScannerMap.cc:77
void get_counts(int32_t *totalp, CstrToInt32Map &table_scanner_count_map)
This method retrieves outstanding scanner counts.
Definition: ScannerMap.cc:104
bool get(int32_t id, MergeScannerRangePtr &scanner, RangePtr &range, TableIdentifierManaged &table, ProfileDataScanner *profile_data)
This method retrieves the scanner and range mapped to the given scanner id.
Definition: ScannerMap.cc:59
TableIdentifierManaged table
Table identifier.
Definition: ScannerMap.h:135
Declarations for ScannerMap.
int64_t get_ts64()
Returns the current time in nanoseconds as a 64bit number.
Definition: Time.cc:40
void update_profile_data(int32_t id, ProfileDataScanner &profile_data)
Updates profile data of a scanner in the map.
Definition: ScannerMap.cc:117
int64_t get_timestamp_millis()
Returns the number of milliseconds since the epoch.
Definition: ScannerMap.cc:127
std::map< const char *, int32_t, LtCstr > CstrToInt32Map
STL map from c-style string to int32_t.
Definition: StringExt.h:55