Flex  0.17.9
service_utils.h
Go to the documentation of this file.
1 
15 #ifndef SERVICE_UTILS_H
16 #define SERVICE_UTILS_H
17 
18 #include <fcntl.h>
19 #include <rapidjson/pointer.h>
20 #include <rapidjson/rapidjson.h>
21 #include <signal.h>
22 #include <sys/sysinfo.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <cctype>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <exception>
30 #include <filesystem>
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 
36 #include "flex/utils/yaml_utils.h"
37 
38 #include <glog/logging.h>
39 #include <rapidjson/document.h>
40 #include <rapidjson/prettywriter.h>
41 #include <rapidjson/stringbuffer.h>
42 #include <rapidjson/writer.h>
43 #include <boost/filesystem.hpp>
44 
45 namespace gs {
46 
47 static constexpr const char* CODEGEN_BIN = "load_plan_and_gen.sh";
48 
50 
51 inline void blockSignal(int sig) {
52  sigset_t set;
53  sigemptyset(&set);
54  sigaddset(&set, sig);
55  if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) {
56  perror("pthread_sigmask");
57  }
58 }
59 
60 inline int64_t GetCurrentTimeStamp() {
61  return std::chrono::duration_cast<std::chrono::milliseconds>(
62  std::chrono::system_clock::now().time_since_epoch())
63  .count();
64 }
65 
66 inline std::string rapidjson_stringify(const rapidjson::Value& value,
67  int indent = -1) {
68  rapidjson::StringBuffer buffer;
69  if (indent == -1) {
70  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
71  value.Accept(writer);
72  return buffer.GetString();
73  } else {
74  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
75  writer.SetIndent(' ', indent);
76  value.Accept(writer);
77  return buffer.GetString();
78  }
79 }
80 
81 inline std::string toUpper(const std::string str) {
82  std::string upper_str = str;
83  std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(),
84  ::toupper);
85  return upper_str;
86 }
87 
88 // With the help of the following functions, we can serialize and deserialize
89 // by json.get<PropertyType>() and operator <</operator =;
90 // These two functions are inlined to avoid linking library in codegen.
91 inline bool to_json(rapidjson::Document& j, const PropertyType& p) {
92  if (p == PropertyType::Empty()) {
93  j.AddMember("empty", "empty", j.GetAllocator());
94  } else if (p == PropertyType::Bool() || p == PropertyType::UInt8() ||
95  p == PropertyType::UInt16() || p == PropertyType::Int32() ||
96  p == PropertyType::UInt32() || p == PropertyType::Float() ||
97  p == PropertyType::Int64() || p == PropertyType::UInt64() ||
98  p == PropertyType::Double()) {
99  j.AddMember("primitive_type",
101  j.GetAllocator());
102  } else if (p == PropertyType::Date()) {
103  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
104  temporal.AddMember("timestamp", "", j.GetAllocator());
105  j.AddMember("temporal", temporal, j.GetAllocator());
106  } else if (p == PropertyType::Day()) {
107  rapidjson::Document temporal(rapidjson::kObjectType, &j.GetAllocator());
108  temporal.AddMember("date32", "", j.GetAllocator());
109  j.AddMember("temporal", temporal, j.GetAllocator());
110  } else if (p == PropertyType::StringView() ||
111  p == PropertyType::StringMap()) {
112  rapidjson::Document long_text(rapidjson::kObjectType, &j.GetAllocator());
113  long_text.AddMember("long_text", "", j.GetAllocator());
114  j.AddMember("string", long_text, j.GetAllocator());
115  } else if (p.IsVarchar()) {
116  rapidjson::Document string(rapidjson::kObjectType, &j.GetAllocator());
117  rapidjson::Document var_char(rapidjson::kObjectType, &j.GetAllocator());
118  var_char.AddMember("max_length", p.additional_type_info.max_length,
119  j.GetAllocator());
120  string.AddMember("var_char", var_char, j.GetAllocator());
121  j.AddMember("string", string, j.GetAllocator());
122  } else {
123  LOG(ERROR) << "Unknown property type";
124  return false;
125  }
126  return true;
127 }
128 
129 inline rapidjson::Document to_json(
130  const PropertyType& p,
131  rapidjson::Document::AllocatorType* allocator = nullptr) {
132  rapidjson::Document j;
133  if (allocator) {
134  j = rapidjson::Document(rapidjson::kObjectType, allocator);
135  } else {
136  j = rapidjson::Document(rapidjson::kObjectType);
137  }
138  if (!to_json(j, p)) {
139  LOG(ERROR) << "Failed to convert PropertyType to json";
140  }
141  return j;
142 }
143 
144 inline bool from_json(const rapidjson::Value& j, PropertyType& p) {
145  if (j.HasMember("primitive_type")) {
147  j["primitive_type"].GetString());
148  } else if (j.HasMember("string")) {
149  if (j["string"].HasMember("long_text")) {
150  p = PropertyType::String();
151  } else if (j.HasMember("string") && j["string"].HasMember("var_char")) {
152  if (j["string"]["var_char"].HasMember("max_length")) {
154  j["string"]["var_char"]["max_length"].GetInt());
155  } else {
157  }
158  } else {
159  throw std::invalid_argument("Unknown string type: " +
161  }
162  } else if (j.HasMember("temporal")) {
163  if (j["temporal"].HasMember("timestamp")) {
164  p = PropertyType::Date();
165  } else if (j["temporal"].HasMember("date32")) {
166  p = PropertyType::Day();
167  } else {
168  throw std::invalid_argument("Unknown temporal type");
169  }
170  } else {
171  LOG(ERROR) << "Unknown property type";
172  return false;
173  }
174  return true;
175 }
176 
177 inline PropertyType from_json(const rapidjson::Value& j) {
178  PropertyType p;
179  if (!from_json(j, p)) {
180  LOG(ERROR) << "Failed to convert json to PropertyType";
181  }
182  return p;
183 }
184 
185 inline boost::filesystem::path get_current_binary_directory() {
186  return boost::filesystem::canonical("/proc/self/exe").parent_path();
187 }
188 
189 inline std::string jsonToString(const rapidjson::Value& json) {
190  if (json.IsString()) {
191  return json.GetString();
192  } else {
193  return rapidjson_stringify(json);
194  }
195 }
196 
197 // Get the directory of the current executable
198 std::string get_current_dir();
199 
200 std::string find_codegen_bin();
201 
202 std::pair<uint64_t, uint64_t> get_total_physical_memory_usage();
203 
204 void init_cpu_usage_watch();
205 
206 std::pair<double, double> get_current_cpu_usage();
207 
208 std::string memory_to_mb_str(uint64_t mem_bytes);
209 
210 size_t human_readable_to_bytes(const std::string& human_readable);
211 
212 } // namespace gs
213 
214 #endif // SERVICE_UTILS_H
PropertyType StringToPrimitivePropertyType(const std::string &str)
Definition: types.cc:55
std::string PrimitivePropertyTypeToString(PropertyType type)
Definition: types.cc:25
Definition: adj_list.h:23
std::pair< double, double > get_current_cpu_usage()
Definition: service_utils.cc:107
int64_t GetCurrentTimeStamp()
Definition: service_utils.h:60
static constexpr const char * CODEGEN_BIN
Definition: service_utils.h:47
void blockSignal(int sig)
Util functions.
Definition: service_utils.h:51
size_t human_readable_to_bytes(const std::string &human_readable_bytes)
Definition: service_utils.cc:143
std::string rapidjson_stringify(const rapidjson::Value &value, int indent=-1)
Definition: service_utils.h:66
std::string jsonToString(const rapidjson::Value &json)
Definition: service_utils.h:189
bool from_json(const rapidjson::Value &j, PropertyType &p)
Definition: service_utils.h:144
bool to_json(rapidjson::Document &j, const PropertyType &p)
Definition: service_utils.h:91
void init_cpu_usage_watch()
Definition: service_utils.cc:99
std::pair< uint64_t, uint64_t > get_total_physical_memory_usage()
Definition: service_utils.cc:87
std::string find_codegen_bin()
Definition: service_utils.cc:41
boost::filesystem::path get_current_binary_directory()
Definition: service_utils.h:185
std::string memory_to_mb_str(uint64_t mem_bytes)
Definition: service_utils.cc:137
std::string toUpper(const std::string str)
Definition: service_utils.h:81
std::string get_current_dir()
Definition: service_utils.cc:24
Definition: types.h:95
static PropertyType String()
Definition: types.cc:348
static PropertyType Bool()
Definition: types.cc:315
static PropertyType UInt64()
Definition: types.cc:336
static PropertyType Int64()
Definition: types.cc:333
static PropertyType Day()
Definition: types.cc:345
static PropertyType UInt16()
Definition: types.cc:321
static PropertyType Double()
Definition: types.cc:339
static PropertyType Date()
Definition: types.cc:342
static PropertyType Float()
Definition: types.cc:330
static PropertyType StringMap()
Definition: types.cc:354
static PropertyType Varchar(uint16_t max_length)
Definition: types.cc:357
static uint16_t GetStringDefaultMaxLength()
Definition: types.cc:103
impl::AdditionalTypeInfo additional_type_info
Definition: types.h:102
static PropertyType UInt8()
Definition: types.cc:318
static PropertyType StringView()
Definition: types.cc:351
static PropertyType Int32()
Definition: types.cc:324
bool IsVarchar() const
Definition: types.cc:259
static PropertyType UInt32()
Definition: types.cc:327
static PropertyType Empty()
Definition: types.cc:312
uint16_t max_length
Definition: types.h:91