forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cc
More file actions
214 lines (189 loc) · 7.03 KB
/
Copy pathutil.cc
File metadata and controls
214 lines (189 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/testing/util.h"
#include <atomic>
#include <chrono>
#include <cstring>
#include <random>
#include <sstream>
#ifdef _WIN32
// clang-format off
// (prevent include reordering)
#include "arrow/util/windows_compatibility.h"
#include <winsock2.h>
// clang-format on
#else
#include <arpa/inet.h> // IWYU pragma: keep
#include <netinet/in.h> // IWYU pragma: keep
#include <sys/socket.h> // IWYU pragma: keep
#include <sys/stat.h> // IWYU pragma: keep
#include <sys/types.h> // IWYU pragma: keep
#include <sys/wait.h> // IWYU pragma: keep
#include <unistd.h> // IWYU pragma: keep
#endif
#include "arrow/config.h"
#include "arrow/table.h"
#include "arrow/testing/random.h"
#include "arrow/type.h"
#include "arrow/util/io_util.h"
#include "arrow/util/logging.h"
#include "arrow/util/pcg_random.h"
namespace arrow {
using random::pcg32_fast;
uint64_t random_seed() {
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}
void random_null_bytes(int64_t n, double pct_null, uint8_t* null_bytes) {
const int random_seed = 0;
pcg32_fast gen(random_seed);
::arrow::random::uniform_real_distribution<double> d(0.0, 1.0);
std::generate(null_bytes, null_bytes + n,
[&d, &gen, &pct_null] { return d(gen) > pct_null; });
}
void random_is_valid(int64_t n, double pct_null, std::vector<bool>* is_valid,
int random_seed) {
pcg32_fast gen(random_seed);
::arrow::random::uniform_real_distribution<double> d(0.0, 1.0);
is_valid->resize(n, false);
std::generate(is_valid->begin(), is_valid->end(),
[&d, &gen, &pct_null] { return d(gen) > pct_null; });
}
void random_bytes(int64_t n, uint32_t seed, uint8_t* out) {
pcg32_fast gen(seed);
std::uniform_int_distribution<uint32_t> d(0, std::numeric_limits<uint8_t>::max());
std::generate(out, out + n, [&d, &gen] { return static_cast<uint8_t>(d(gen)); });
}
std::string random_string(int64_t n, uint32_t seed) {
std::string s;
s.resize(static_cast<size_t>(n));
random_bytes(n, seed, reinterpret_cast<uint8_t*>(&s[0]));
return s;
}
void random_ascii(int64_t n, uint32_t seed, uint8_t* out) {
rand_uniform_int(n, seed, static_cast<int32_t>('A'), static_cast<int32_t>('z'), out);
}
int64_t CountNulls(const std::vector<uint8_t>& valid_bytes) {
return static_cast<int64_t>(std::count(valid_bytes.cbegin(), valid_bytes.cend(), '\0'));
}
Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool,
std::shared_ptr<ResizableBuffer>* out, uint32_t seed) {
ARROW_ASSIGN_OR_RAISE(auto result, AllocateResizableBuffer(length, pool));
random_bytes(length, seed, result->mutable_data());
*out = std::move(result);
return Status::OK();
}
Status GetTestResourceRoot(std::string* out) {
const char* c_root = std::getenv("ARROW_TEST_DATA");
if (!c_root) {
return Status::IOError(
"Test resources not found, set ARROW_TEST_DATA to <repo root>/testing/data");
}
*out = std::string(c_root);
return Status::OK();
}
std::optional<std::string> GetTestTimezoneDatabaseRoot() {
const char* c_root = std::getenv("ARROW_TIMEZONE_DATABASE");
if (!c_root) {
return std::optional<std::string>();
}
return std::make_optional(std::string(c_root));
}
Status InitTestTimezoneDatabase() {
auto maybe_tzdata = GetTestTimezoneDatabaseRoot();
// If missing, timezone database will default to %USERPROFILE%\Downloads\tzdata
if (!maybe_tzdata.has_value()) return Status::OK();
auto tzdata_path = std::string(maybe_tzdata.value());
arrow::GlobalOptions options = {std::make_optional(tzdata_path)};
ARROW_RETURN_NOT_OK(arrow::Initialize(options));
return Status::OK();
}
int GetListenPort() {
// Get a new available port number by binding a socket to an ephemeral port
// and then closing it. Since ephemeral port allocation tends to avoid
// reusing port numbers, this should give a different port number
// every time, even across processes.
struct sockaddr_in sin;
#ifdef _WIN32
SOCKET sock_fd;
auto sin_len = static_cast<int>(sizeof(sin));
auto errno_message = []() -> std::string {
return internal::WinErrorMessage(WSAGetLastError());
};
#else
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
int sock_fd;
auto sin_len = static_cast<socklen_t>(sizeof(sin));
auto errno_message = []() -> std::string { return internal::ErrnoMessage(errno); };
#endif
#ifdef _WIN32
WSADATA wsa_data;
if (WSAStartup(0x0202, &wsa_data) != 0) {
ARROW_LOG(FATAL) << "Failed to initialize Windows Sockets";
}
#endif
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd == INVALID_SOCKET) {
Status::IOError("Failed to create TCP socket: ", errno_message()).Abort();
}
// First bind to ('0.0.0.0', 0)
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if (bind(sock_fd, reinterpret_cast<struct sockaddr*>(&sin), sin_len) == SOCKET_ERROR) {
Status::IOError("bind() failed: ", errno_message()).Abort();
}
// Then get actual bound port number
if (getsockname(sock_fd, reinterpret_cast<struct sockaddr*>(&sin), &sin_len) ==
SOCKET_ERROR) {
Status::IOError("getsockname() failed: ", errno_message()).Abort();
}
int port = ntohs(sin.sin_port);
#ifdef _WIN32
closesocket(sock_fd);
#else
close(sock_fd);
#endif
return port;
}
std::string GetListenAddress() {
// Using GetListenPort() alone may still suffer from race conditions,
// so this function maximizes the chances of finding an available address
// by using a different loopback address every time (there are 2**24 of them).
std::stringstream ss;
#ifndef __APPLE__
static std::atomic<uint32_t> next_addr{1U}; // start at "127.0.0.1"
const uint32_t addr = next_addr++ & 0xffffffU; // keep bottom 24 bits
// Format loopback IPv4 address
ss << "127";
for (int shift = 16; shift >= 0; shift -= 8) {
const auto byte = (addr >> shift) & 0xFFU;
ss << "." << byte;
}
#else
// On MacOS, only 127.0.0.1 is a valid loopback address by default.
ss << "127.0.0.1";
#endif
// Append port number
ss << ":" << GetListenPort();
return ss.str();
}
const std::vector<std::shared_ptr<DataType>>& all_dictionary_index_types() {
static std::vector<std::shared_ptr<DataType>> types = {
int8(), uint8(), int16(), uint16(), int32(), uint32(), int64(), uint64()};
return types;
}
} // namespace arrow