forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdfs_internal.cc
557 lines (464 loc) · 18.1 KB
/
hdfs_internal.cc
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// 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.
// This shim interface to libhdfs (for runtime shared library loading) has been
// adapted from the SFrame project, released under the ASF-compatible 3-clause
// BSD license
//
// Using this required having the $JAVA_HOME and $HADOOP_HOME environment
// variables set, so that libjvm and libhdfs can be located easily
// Copyright (C) 2015 Dato, Inc.
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
#include "arrow/io/hdfs_internal.h"
#include <cstdint>
#include <cstdlib>
#include <mutex>
#include <sstream> // IWYU pragma: keep
#include <string>
#include <utility>
#include <vector>
#ifndef _WIN32
#include <dlfcn.h>
#endif
#include "arrow/result.h"
#include "arrow/status.h"
#include "arrow/util/io_util.h"
#include "arrow/util/logging.h"
namespace arrow {
using internal::GetEnvVarNative;
using internal::PlatformFilename;
#ifdef _WIN32
using internal::WinErrorMessage;
#endif
namespace io {
namespace internal {
namespace {
void* GetLibrarySymbol(LibraryHandle handle, const char* symbol) {
if (handle == NULL) return NULL;
#ifndef _WIN32
return dlsym(handle, symbol);
#else
void* ret = reinterpret_cast<void*>(GetProcAddress(handle, symbol));
if (ret == NULL) {
// logstream(LOG_INFO) << "GetProcAddress error: "
// << get_last_err_str(GetLastError()) << std::endl;
}
return ret;
#endif
}
#define GET_SYMBOL_REQUIRED(SHIM, SYMBOL_NAME) \
do { \
if (!SHIM->SYMBOL_NAME) { \
*reinterpret_cast<void**>(&SHIM->SYMBOL_NAME) = \
GetLibrarySymbol(SHIM->handle, "" #SYMBOL_NAME); \
} \
if (!SHIM->SYMBOL_NAME) \
return Status::IOError("Getting symbol " #SYMBOL_NAME "failed"); \
} while (0)
#define GET_SYMBOL(SHIM, SYMBOL_NAME) \
if (!SHIM->SYMBOL_NAME) { \
*reinterpret_cast<void**>(&SHIM->SYMBOL_NAME) = \
GetLibrarySymbol(SHIM->handle, "" #SYMBOL_NAME); \
}
LibraryHandle libjvm_handle = nullptr;
// Helper functions for dlopens
Result<std::vector<PlatformFilename>> get_potential_libjvm_paths();
Result<std::vector<PlatformFilename>> get_potential_libhdfs_paths();
Result<LibraryHandle> try_dlopen(const std::vector<PlatformFilename>& potential_paths,
const char* name);
Result<std::vector<PlatformFilename>> MakeFilenameVector(
const std::vector<std::string>& names) {
std::vector<PlatformFilename> filenames(names.size());
for (size_t i = 0; i < names.size(); ++i) {
ARROW_ASSIGN_OR_RAISE(filenames[i], PlatformFilename::FromString(names[i]));
}
return filenames;
}
void AppendEnvVarFilename(const char* var_name,
std::vector<PlatformFilename>* filenames) {
auto maybe_env_var = GetEnvVarNative(var_name);
if (maybe_env_var.ok()) {
filenames->emplace_back(std::move(*maybe_env_var));
}
}
void AppendEnvVarFilename(const char* var_name, const char* suffix,
std::vector<PlatformFilename>* filenames) {
auto maybe_env_var = GetEnvVarNative(var_name);
if (maybe_env_var.ok()) {
auto maybe_env_var_with_suffix =
PlatformFilename(std::move(*maybe_env_var)).Join(suffix);
if (maybe_env_var_with_suffix.ok()) {
filenames->emplace_back(std::move(*maybe_env_var_with_suffix));
}
}
}
void InsertEnvVarFilename(const char* var_name,
std::vector<PlatformFilename>* filenames) {
auto maybe_env_var = GetEnvVarNative(var_name);
if (maybe_env_var.ok()) {
filenames->emplace(filenames->begin(), PlatformFilename(std::move(*maybe_env_var)));
}
}
Result<std::vector<PlatformFilename>> get_potential_libhdfs_paths() {
std::vector<PlatformFilename> potential_paths;
std::string file_name;
// OS-specific file name
#ifdef _WIN32
file_name = "hdfs.dll";
#elif __APPLE__
file_name = "libhdfs.dylib";
#else
file_name = "libhdfs.so";
#endif
// Common paths
ARROW_ASSIGN_OR_RAISE(auto search_paths, MakeFilenameVector({"", "."}));
// Path from environment variable
AppendEnvVarFilename("ARROW_LIBHDFS3_DIR", &search_paths);
AppendEnvVarFilename("HADOOP_HOME", "lib/native", &search_paths);
AppendEnvVarFilename("ARROW_LIBHDFS_DIR", &search_paths);
// All paths with file name
for (const auto& path : search_paths) {
ARROW_ASSIGN_OR_RAISE(auto full_path, path.Join(file_name));
potential_paths.push_back(std::move(full_path));
}
return potential_paths;
}
Result<std::vector<PlatformFilename>> get_potential_libjvm_paths() {
std::vector<PlatformFilename> potential_paths;
std::vector<PlatformFilename> search_prefixes;
std::vector<PlatformFilename> search_suffixes;
std::string file_name;
// From heuristics
#ifdef _WIN32
ARROW_ASSIGN_OR_RAISE(search_prefixes, MakeFilenameVector({""}));
ARROW_ASSIGN_OR_RAISE(search_suffixes,
MakeFilenameVector({"/jre/bin/server", "/bin/server"}));
file_name = "jvm.dll";
#elif __APPLE__
ARROW_ASSIGN_OR_RAISE(search_prefixes, MakeFilenameVector({""}));
ARROW_ASSIGN_OR_RAISE(search_suffixes,
MakeFilenameVector({"/jre/lib/server", "/lib/server"}));
file_name = "libjvm.dylib";
// SFrame uses /usr/libexec/java_home to find JAVA_HOME; for now we are
// expecting users to set an environment variable
#else
#if defined(__aarch64__)
const std::string prefix_arch{"arm64"};
const std::string suffix_arch{"aarch64"};
#else
const std::string prefix_arch{"amd64"};
const std::string suffix_arch{"amd64"};
#endif
ARROW_ASSIGN_OR_RAISE(
search_prefixes,
MakeFilenameVector({
"/usr/lib/jvm/default-java", // ubuntu / debian distros
"/usr/lib/jvm/java", // rhel6
"/usr/lib/jvm", // centos6
"/usr/lib64/jvm", // opensuse 13
"/usr/local/lib/jvm/default-java", // alt ubuntu / debian distros
"/usr/local/lib/jvm/java", // alt rhel6
"/usr/local/lib/jvm", // alt centos6
"/usr/local/lib64/jvm", // alt opensuse 13
"/usr/local/lib/jvm/java-8-openjdk-" +
prefix_arch, // alt ubuntu / debian distros
"/usr/lib/jvm/java-8-openjdk-" + prefix_arch, // alt ubuntu / debian distros
"/usr/local/lib/jvm/java-7-openjdk-" +
prefix_arch, // alt ubuntu / debian distros
"/usr/lib/jvm/java-7-openjdk-" + prefix_arch, // alt ubuntu / debian distros
"/usr/local/lib/jvm/java-6-openjdk-" +
prefix_arch, // alt ubuntu / debian distros
"/usr/lib/jvm/java-6-openjdk-" + prefix_arch, // alt ubuntu / debian distros
"/usr/lib/jvm/java-7-oracle", // alt ubuntu
"/usr/lib/jvm/java-8-oracle", // alt ubuntu
"/usr/lib/jvm/java-6-oracle", // alt ubuntu
"/usr/local/lib/jvm/java-7-oracle", // alt ubuntu
"/usr/local/lib/jvm/java-8-oracle", // alt ubuntu
"/usr/local/lib/jvm/java-6-oracle", // alt ubuntu
"/usr/lib/jvm/default", // alt centos
"/usr/java/latest" // alt centos
}));
ARROW_ASSIGN_OR_RAISE(
search_suffixes,
MakeFilenameVector({"", "/lib/server", "/jre/lib/" + suffix_arch + "/server",
"/lib/" + suffix_arch + "/server"}));
file_name = "libjvm.so";
#endif
// From direct environment variable
InsertEnvVarFilename("JAVA_HOME", &search_prefixes);
// Generate cross product between search_prefixes, search_suffixes, and file_name
for (auto& prefix : search_prefixes) {
for (auto& suffix : search_suffixes) {
ARROW_ASSIGN_OR_RAISE(auto path, prefix.Join(suffix).Join(file_name));
potential_paths.push_back(std::move(path));
}
}
return potential_paths;
}
#ifndef _WIN32
Result<LibraryHandle> try_dlopen(const std::vector<PlatformFilename>& potential_paths,
const char* name) {
std::string error_message = "unknown error";
LibraryHandle handle;
for (const auto& p : potential_paths) {
handle = dlopen(p.ToNative().c_str(), RTLD_NOW | RTLD_LOCAL);
if (handle != NULL) {
return handle;
} else {
const char* err_msg = dlerror();
if (err_msg != NULL) {
error_message = err_msg;
}
}
}
return Status::IOError("Unable to load ", name, ": ", error_message);
}
#else
Result<LibraryHandle> try_dlopen(const std::vector<PlatformFilename>& potential_paths,
const char* name) {
std::string error_message;
LibraryHandle handle;
for (const auto& p : potential_paths) {
handle = LoadLibraryW(p.ToNative().c_str());
if (handle != NULL) {
return handle;
} else {
error_message = WinErrorMessage(GetLastError());
}
}
return Status::IOError("Unable to load ", name, ": ", error_message);
}
#endif // _WIN32
LibHdfsShim libhdfs_shim;
} // namespace
Status LibHdfsShim::GetRequiredSymbols() {
GET_SYMBOL_REQUIRED(this, hdfsNewBuilder);
GET_SYMBOL_REQUIRED(this, hdfsBuilderSetNameNode);
GET_SYMBOL_REQUIRED(this, hdfsBuilderSetNameNodePort);
GET_SYMBOL_REQUIRED(this, hdfsBuilderSetUserName);
GET_SYMBOL_REQUIRED(this, hdfsBuilderSetKerbTicketCachePath);
GET_SYMBOL_REQUIRED(this, hdfsBuilderSetForceNewInstance);
GET_SYMBOL_REQUIRED(this, hdfsBuilderConfSetStr);
GET_SYMBOL_REQUIRED(this, hdfsBuilderConnect);
GET_SYMBOL_REQUIRED(this, hdfsCreateDirectory);
GET_SYMBOL_REQUIRED(this, hdfsDelete);
GET_SYMBOL_REQUIRED(this, hdfsDisconnect);
GET_SYMBOL_REQUIRED(this, hdfsExists);
GET_SYMBOL_REQUIRED(this, hdfsFreeFileInfo);
GET_SYMBOL_REQUIRED(this, hdfsGetCapacity);
GET_SYMBOL_REQUIRED(this, hdfsGetUsed);
GET_SYMBOL_REQUIRED(this, hdfsGetPathInfo);
GET_SYMBOL_REQUIRED(this, hdfsListDirectory);
GET_SYMBOL_REQUIRED(this, hdfsChown);
GET_SYMBOL_REQUIRED(this, hdfsChmod);
// File methods
GET_SYMBOL_REQUIRED(this, hdfsCloseFile);
GET_SYMBOL_REQUIRED(this, hdfsFlush);
GET_SYMBOL_REQUIRED(this, hdfsOpenFile);
GET_SYMBOL_REQUIRED(this, hdfsRead);
GET_SYMBOL_REQUIRED(this, hdfsSeek);
GET_SYMBOL_REQUIRED(this, hdfsTell);
GET_SYMBOL_REQUIRED(this, hdfsWrite);
return Status::OK();
}
Status ConnectLibHdfs(LibHdfsShim** driver) {
static std::mutex lock;
std::lock_guard<std::mutex> guard(lock);
LibHdfsShim* shim = &libhdfs_shim;
static bool shim_attempted = false;
if (!shim_attempted) {
shim_attempted = true;
shim->Initialize();
ARROW_ASSIGN_OR_RAISE(auto libjvm_potential_paths, get_potential_libjvm_paths());
ARROW_ASSIGN_OR_RAISE(libjvm_handle, try_dlopen(libjvm_potential_paths, "libjvm"));
ARROW_ASSIGN_OR_RAISE(auto libhdfs_potential_paths, get_potential_libhdfs_paths());
ARROW_ASSIGN_OR_RAISE(shim->handle, try_dlopen(libhdfs_potential_paths, "libhdfs"));
} else if (shim->handle == nullptr) {
return Status::IOError("Prior attempt to load libhdfs failed");
}
*driver = shim;
return shim->GetRequiredSymbols();
}
///////////////////////////////////////////////////////////////////////////
// HDFS thin wrapper methods
hdfsBuilder* LibHdfsShim::NewBuilder(void) { return this->hdfsNewBuilder(); }
void LibHdfsShim::BuilderSetNameNode(hdfsBuilder* bld, const char* nn) {
this->hdfsBuilderSetNameNode(bld, nn);
}
void LibHdfsShim::BuilderSetNameNodePort(hdfsBuilder* bld, tPort port) {
this->hdfsBuilderSetNameNodePort(bld, port);
}
void LibHdfsShim::BuilderSetUserName(hdfsBuilder* bld, const char* userName) {
this->hdfsBuilderSetUserName(bld, userName);
}
void LibHdfsShim::BuilderSetKerbTicketCachePath(hdfsBuilder* bld,
const char* kerbTicketCachePath) {
this->hdfsBuilderSetKerbTicketCachePath(bld, kerbTicketCachePath);
}
void LibHdfsShim::BuilderSetForceNewInstance(hdfsBuilder* bld) {
this->hdfsBuilderSetForceNewInstance(bld);
}
hdfsFS LibHdfsShim::BuilderConnect(hdfsBuilder* bld) {
return this->hdfsBuilderConnect(bld);
}
int LibHdfsShim::BuilderConfSetStr(hdfsBuilder* bld, const char* key, const char* val) {
return this->hdfsBuilderConfSetStr(bld, key, val);
}
int LibHdfsShim::Disconnect(hdfsFS fs) { return this->hdfsDisconnect(fs); }
hdfsFile LibHdfsShim::OpenFile(hdfsFS fs, const char* path, int flags, int bufferSize,
short replication, tSize blocksize) { // NOLINT
return this->hdfsOpenFile(fs, path, flags, bufferSize, replication, blocksize);
}
int LibHdfsShim::CloseFile(hdfsFS fs, hdfsFile file) {
return this->hdfsCloseFile(fs, file);
}
int LibHdfsShim::Exists(hdfsFS fs, const char* path) {
return this->hdfsExists(fs, path);
}
int LibHdfsShim::Seek(hdfsFS fs, hdfsFile file, tOffset desiredPos) {
return this->hdfsSeek(fs, file, desiredPos);
}
tOffset LibHdfsShim::Tell(hdfsFS fs, hdfsFile file) { return this->hdfsTell(fs, file); }
tSize LibHdfsShim::Read(hdfsFS fs, hdfsFile file, void* buffer, tSize length) {
return this->hdfsRead(fs, file, buffer, length);
}
bool LibHdfsShim::HasPread() {
GET_SYMBOL(this, hdfsPread);
return this->hdfsPread != nullptr;
}
tSize LibHdfsShim::Pread(hdfsFS fs, hdfsFile file, tOffset position, void* buffer,
tSize length) {
GET_SYMBOL(this, hdfsPread);
DCHECK(this->hdfsPread);
return this->hdfsPread(fs, file, position, buffer, length);
}
tSize LibHdfsShim::Write(hdfsFS fs, hdfsFile file, const void* buffer, tSize length) {
return this->hdfsWrite(fs, file, buffer, length);
}
int LibHdfsShim::Flush(hdfsFS fs, hdfsFile file) { return this->hdfsFlush(fs, file); }
int LibHdfsShim::Available(hdfsFS fs, hdfsFile file) {
GET_SYMBOL(this, hdfsAvailable);
if (this->hdfsAvailable)
return this->hdfsAvailable(fs, file);
else
return 0;
}
int LibHdfsShim::Copy(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
GET_SYMBOL(this, hdfsCopy);
if (this->hdfsCopy)
return this->hdfsCopy(srcFS, src, dstFS, dst);
else
return 0;
}
int LibHdfsShim::Move(hdfsFS srcFS, const char* src, hdfsFS dstFS, const char* dst) {
GET_SYMBOL(this, hdfsMove);
if (this->hdfsMove)
return this->hdfsMove(srcFS, src, dstFS, dst);
else
return 0;
}
int LibHdfsShim::Delete(hdfsFS fs, const char* path, int recursive) {
return this->hdfsDelete(fs, path, recursive);
}
int LibHdfsShim::Rename(hdfsFS fs, const char* oldPath, const char* newPath) {
GET_SYMBOL(this, hdfsRename);
if (this->hdfsRename)
return this->hdfsRename(fs, oldPath, newPath);
else
return 0;
}
char* LibHdfsShim::GetWorkingDirectory(hdfsFS fs, char* buffer, size_t bufferSize) {
GET_SYMBOL(this, hdfsGetWorkingDirectory);
if (this->hdfsGetWorkingDirectory) {
return this->hdfsGetWorkingDirectory(fs, buffer, bufferSize);
} else {
return NULL;
}
}
int LibHdfsShim::SetWorkingDirectory(hdfsFS fs, const char* path) {
GET_SYMBOL(this, hdfsSetWorkingDirectory);
if (this->hdfsSetWorkingDirectory) {
return this->hdfsSetWorkingDirectory(fs, path);
} else {
return 0;
}
}
int LibHdfsShim::MakeDirectory(hdfsFS fs, const char* path) {
return this->hdfsCreateDirectory(fs, path);
}
int LibHdfsShim::SetReplication(hdfsFS fs, const char* path, int16_t replication) {
GET_SYMBOL(this, hdfsSetReplication);
if (this->hdfsSetReplication) {
return this->hdfsSetReplication(fs, path, replication);
} else {
return 0;
}
}
hdfsFileInfo* LibHdfsShim::ListDirectory(hdfsFS fs, const char* path, int* numEntries) {
return this->hdfsListDirectory(fs, path, numEntries);
}
hdfsFileInfo* LibHdfsShim::GetPathInfo(hdfsFS fs, const char* path) {
return this->hdfsGetPathInfo(fs, path);
}
void LibHdfsShim::FreeFileInfo(hdfsFileInfo* hdfsFileInfo, int numEntries) {
this->hdfsFreeFileInfo(hdfsFileInfo, numEntries);
}
char*** LibHdfsShim::GetHosts(hdfsFS fs, const char* path, tOffset start,
tOffset length) {
GET_SYMBOL(this, hdfsGetHosts);
if (this->hdfsGetHosts) {
return this->hdfsGetHosts(fs, path, start, length);
} else {
return NULL;
}
}
void LibHdfsShim::FreeHosts(char*** blockHosts) {
GET_SYMBOL(this, hdfsFreeHosts);
if (this->hdfsFreeHosts) {
this->hdfsFreeHosts(blockHosts);
}
}
tOffset LibHdfsShim::GetDefaultBlockSize(hdfsFS fs) {
GET_SYMBOL(this, hdfsGetDefaultBlockSize);
if (this->hdfsGetDefaultBlockSize) {
return this->hdfsGetDefaultBlockSize(fs);
} else {
return 0;
}
}
tOffset LibHdfsShim::GetCapacity(hdfsFS fs) { return this->hdfsGetCapacity(fs); }
tOffset LibHdfsShim::GetUsed(hdfsFS fs) { return this->hdfsGetUsed(fs); }
int LibHdfsShim::Chown(hdfsFS fs, const char* path, const char* owner,
const char* group) {
return this->hdfsChown(fs, path, owner, group);
}
int LibHdfsShim::Chmod(hdfsFS fs, const char* path, short mode) { // NOLINT
return this->hdfsChmod(fs, path, mode);
}
int LibHdfsShim::Utime(hdfsFS fs, const char* path, tTime mtime, tTime atime) {
GET_SYMBOL(this, hdfsUtime);
if (this->hdfsUtime) {
return this->hdfsUtime(fs, path, mtime, atime);
} else {
return 0;
}
}
} // namespace internal
} // namespace io
} // namespace arrow