2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2012-06-08 10:11:14 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <iostream>
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/status.h"
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
#ifdef USE_HDFS
|
|
|
|
#include "hdfs/hdfs.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
static const std::string kProto = "hdfs://";
|
|
|
|
static const std::string pathsep = "/";
|
|
|
|
|
|
|
|
// Thrown during execution when there is an issue with the supplied
|
|
|
|
// arguments.
|
|
|
|
class HdfsUsageException : public std::exception { };
|
|
|
|
|
|
|
|
// A simple exception that indicates something went wrong that is not
|
|
|
|
// recoverable. The intention is for the message to be printed (with
|
|
|
|
// nothing else) and the process terminate.
|
|
|
|
class HdfsFatalException : public std::exception {
|
|
|
|
public:
|
|
|
|
explicit HdfsFatalException(const std::string& s) : what_(s) { }
|
|
|
|
virtual ~HdfsFatalException() throw() { }
|
|
|
|
virtual const char* what() const throw() {
|
|
|
|
return what_.c_str();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const std::string what_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2013-10-05 07:32:05 +02:00
|
|
|
// The HDFS environment for rocksdb. This class overrides all the
|
2012-06-08 10:11:14 +02:00
|
|
|
// file/dir access methods and delegates the thread-mgmt methods to the
|
|
|
|
// default posix environment.
|
|
|
|
//
|
|
|
|
class HdfsEnv : public Env {
|
|
|
|
|
|
|
|
public:
|
2014-02-26 02:47:37 +01:00
|
|
|
explicit HdfsEnv(const std::string& fsname) : fsname_(fsname) {
|
2012-06-08 10:11:14 +02:00
|
|
|
posixEnv = Env::Default();
|
|
|
|
fileSys_ = connectToPath(fsname_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~HdfsEnv() {
|
|
|
|
fprintf(stderr, "Destroying HdfsEnv::Default()\n");
|
|
|
|
hdfsDisconnect(fileSys_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status NewSequentialFile(const std::string& fname,
|
|
|
|
SequentialFile** result);
|
|
|
|
|
|
|
|
virtual Status NewRandomAccessFile(const std::string& fname,
|
|
|
|
RandomAccessFile** result);
|
|
|
|
|
|
|
|
virtual Status NewWritableFile(const std::string& fname,
|
|
|
|
WritableFile** result);
|
|
|
|
|
2013-10-10 10:01:16 +02:00
|
|
|
virtual Status NewRandomRWFile(const std::string& fname,
|
|
|
|
unique_ptr<RandomRWFile>* result,
|
|
|
|
const EnvOptions& options);
|
|
|
|
|
2014-01-27 20:02:21 +01:00
|
|
|
virtual Status NewDirectory(const std::string& name,
|
|
|
|
unique_ptr<Directory>* result);
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
virtual bool FileExists(const std::string& fname);
|
|
|
|
|
|
|
|
virtual Status GetChildren(const std::string& path,
|
|
|
|
std::vector<std::string>* result);
|
|
|
|
|
|
|
|
virtual Status DeleteFile(const std::string& fname);
|
|
|
|
|
|
|
|
virtual Status CreateDir(const std::string& name);
|
|
|
|
|
2012-11-26 22:56:45 +01:00
|
|
|
virtual Status CreateDirIfMissing(const std::string& name);
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
virtual Status DeleteDir(const std::string& name);
|
|
|
|
|
|
|
|
virtual Status GetFileSize(const std::string& fname, uint64_t* size);
|
|
|
|
|
2012-11-26 22:56:45 +01:00
|
|
|
virtual Status GetFileModificationTime(const std::string& fname,
|
|
|
|
uint64_t* file_mtime);
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
virtual Status RenameFile(const std::string& src, const std::string& target);
|
|
|
|
|
|
|
|
virtual Status LockFile(const std::string& fname, FileLock** lock);
|
|
|
|
|
|
|
|
virtual Status UnlockFile(FileLock* lock);
|
|
|
|
|
|
|
|
virtual Status NewLogger(const std::string& fname, Logger** result);
|
|
|
|
|
2013-09-12 09:53:30 +02:00
|
|
|
virtual void Schedule(void (*function)(void* arg), void* arg,
|
|
|
|
Priority pri = LOW) {
|
|
|
|
posixEnv->Schedule(function, arg, pri);
|
2012-06-08 10:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StartThread(void (*function)(void* arg), void* arg) {
|
|
|
|
posixEnv->StartThread(function, arg);
|
|
|
|
}
|
|
|
|
|
2014-02-26 02:47:37 +01:00
|
|
|
virtual void WaitForJoin() { posixEnv->WaitForJoin(); }
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
virtual Status GetTestDirectory(std::string* path) {
|
|
|
|
return posixEnv->GetTestDirectory(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint64_t NowMicros() {
|
|
|
|
return posixEnv->NowMicros();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SleepForMicroseconds(int micros) {
|
|
|
|
posixEnv->SleepForMicroseconds(micros);
|
|
|
|
}
|
|
|
|
|
2012-09-12 18:54:22 +02:00
|
|
|
virtual Status GetHostName(char* name, uint64_t len) {
|
2012-08-15 00:20:36 +02:00
|
|
|
return posixEnv->GetHostName(name, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status GetCurrentTime(int64_t* unix_time) {
|
2012-08-30 00:21:56 +02:00
|
|
|
return posixEnv->GetCurrentTime(unix_time);
|
2012-08-15 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status GetAbsolutePath(const std::string& db_path,
|
|
|
|
std::string* output_path) {
|
|
|
|
return posixEnv->GetAbsolutePath(db_path, output_path);
|
|
|
|
}
|
|
|
|
|
2013-09-12 09:53:30 +02:00
|
|
|
virtual void SetBackgroundThreads(int number, Priority pri = LOW) {
|
|
|
|
posixEnv->SetBackgroundThreads(number, pri);
|
2012-09-20 00:21:09 +02:00
|
|
|
}
|
2012-08-15 00:20:36 +02:00
|
|
|
|
2012-10-19 23:00:53 +02:00
|
|
|
virtual std::string TimeToString(uint64_t number) {
|
|
|
|
return posixEnv->TimeToString(number);
|
|
|
|
}
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
static uint64_t gettid() {
|
|
|
|
assert(sizeof(pthread_t) <= sizeof(uint64_t));
|
|
|
|
return (uint64_t)pthread_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string fsname_; // string of the form "hdfs://hostname:port/"
|
|
|
|
hdfsFS fileSys_; // a single FileSystem object for all files
|
|
|
|
Env* posixEnv; // This object is derived from Env, but not from
|
|
|
|
// posixEnv. We have posixnv as an encapsulated
|
|
|
|
// object here so that we can use posix timers,
|
|
|
|
// posix threads, etc.
|
|
|
|
|
|
|
|
/**
|
2013-01-07 19:11:18 +01:00
|
|
|
* If the URI is specified of the form hdfs://server:port/path,
|
2012-06-08 10:11:14 +02:00
|
|
|
* then connect to the specified cluster
|
|
|
|
* else connect to default.
|
|
|
|
*/
|
|
|
|
hdfsFS connectToPath(const std::string& uri) {
|
|
|
|
if (uri.empty()) {
|
2014-02-26 02:47:37 +01:00
|
|
|
return nullptr;
|
2012-06-08 10:11:14 +02:00
|
|
|
}
|
|
|
|
if (uri.find(kProto) != 0) {
|
|
|
|
// uri doesn't start with hdfs:// -> use default:0, which is special
|
|
|
|
// to libhdfs.
|
|
|
|
return hdfsConnectNewInstance("default", 0);
|
|
|
|
}
|
|
|
|
const std::string hostport = uri.substr(kProto.length());
|
|
|
|
|
|
|
|
std::vector <std::string> parts;
|
|
|
|
split(hostport, ':', parts);
|
|
|
|
if (parts.size() != 2) {
|
|
|
|
throw HdfsFatalException("Bad uri for hdfs " + uri);
|
|
|
|
}
|
|
|
|
// parts[0] = hosts, parts[1] = port/xxx/yyy
|
|
|
|
std::string host(parts[0]);
|
|
|
|
std::string remaining(parts[1]);
|
|
|
|
|
|
|
|
int rem = remaining.find(pathsep);
|
|
|
|
std::string portStr = (rem == 0 ? remaining :
|
|
|
|
remaining.substr(0, rem));
|
2013-01-07 19:11:18 +01:00
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
tPort port;
|
|
|
|
port = atoi(portStr.c_str());
|
|
|
|
if (port == 0) {
|
|
|
|
throw HdfsFatalException("Bad host-port for hdfs " + uri);
|
|
|
|
}
|
|
|
|
hdfsFS fs = hdfsConnectNewInstance(host.c_str(), port);
|
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
|
2013-01-07 19:11:18 +01:00
|
|
|
void split(const std::string &s, char delim,
|
2012-06-08 10:11:14 +02:00
|
|
|
std::vector<std::string> &elems) {
|
|
|
|
elems.clear();
|
|
|
|
size_t prev = 0;
|
|
|
|
size_t pos = s.find(delim);
|
|
|
|
while (pos != std::string::npos) {
|
|
|
|
elems.push_back(s.substr(prev, pos));
|
|
|
|
prev = pos + 1;
|
|
|
|
pos = s.find(delim, prev);
|
|
|
|
}
|
|
|
|
elems.push_back(s.substr(prev, s.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
#else // USE_HDFS
|
|
|
|
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
static const Status notsup;
|
|
|
|
|
2012-06-08 10:11:14 +02:00
|
|
|
class HdfsEnv : public Env {
|
|
|
|
|
|
|
|
public:
|
2014-02-26 02:47:37 +01:00
|
|
|
explicit HdfsEnv(const std::string& fsname) {
|
2013-10-05 07:32:05 +02:00
|
|
|
fprintf(stderr, "You have not build rocksdb with HDFS support\n");
|
2012-06-08 10:11:14 +02:00
|
|
|
fprintf(stderr, "Please see hdfs/README for details\n");
|
2014-02-26 02:47:37 +01:00
|
|
|
throw std::exception();
|
2012-06-08 10:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~HdfsEnv() {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status NewSequentialFile(const std::string& fname,
|
2013-03-15 01:00:04 +01:00
|
|
|
unique_ptr<SequentialFile>* result,
|
|
|
|
const EnvOptions& options);
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
virtual Status NewRandomAccessFile(const std::string& fname,
|
2013-03-15 01:00:04 +01:00
|
|
|
unique_ptr<RandomAccessFile>* result,
|
|
|
|
const EnvOptions& options) {
|
2013-01-20 11:07:13 +01:00
|
|
|
return notsup;
|
|
|
|
}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
virtual Status NewWritableFile(const std::string& fname,
|
2013-03-15 01:00:04 +01:00
|
|
|
unique_ptr<WritableFile>* result,
|
|
|
|
const EnvOptions& options) {
|
2013-01-20 11:07:13 +01:00
|
|
|
return notsup;
|
|
|
|
}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2013-10-10 10:01:16 +02:00
|
|
|
virtual Status NewRandomRWFile(const std::string& fname,
|
|
|
|
unique_ptr<RandomRWFile>* result,
|
|
|
|
const EnvOptions& options) {
|
|
|
|
return notsup;
|
|
|
|
}
|
|
|
|
|
2014-01-27 20:02:21 +01:00
|
|
|
virtual Status NewDirectory(const std::string& name,
|
|
|
|
unique_ptr<Directory>* result) {
|
|
|
|
return notsup;
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual bool FileExists(const std::string& fname){return false;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
virtual Status GetChildren(const std::string& path,
|
2012-08-29 21:29:43 +02:00
|
|
|
std::vector<std::string>* result){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status DeleteFile(const std::string& fname){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status CreateDir(const std::string& name){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-11-26 22:56:45 +01:00
|
|
|
virtual Status CreateDirIfMissing(const std::string& name){return notsup;}
|
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status DeleteDir(const std::string& name){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status GetFileSize(const std::string& fname, uint64_t* size){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-11-26 22:56:45 +01:00
|
|
|
virtual Status GetFileModificationTime(const std::string& fname,
|
|
|
|
uint64_t* time) {
|
|
|
|
return notsup;
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status RenameFile(const std::string& src, const std::string& target){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status LockFile(const std::string& fname, FileLock** lock){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status UnlockFile(FileLock* lock){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
virtual Status NewLogger(const std::string& fname,
|
|
|
|
shared_ptr<Logger>* result){return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2013-09-12 09:53:30 +02:00
|
|
|
virtual void Schedule(void (*function)(void* arg), void* arg,
|
|
|
|
Priority pri = LOW) {}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
virtual void StartThread(void (*function)(void* arg), void* arg) {}
|
|
|
|
|
2014-02-26 02:47:37 +01:00
|
|
|
virtual void WaitForJoin() {}
|
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status GetTestDirectory(std::string* path) {return notsup;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual uint64_t NowMicros() {return 0;}
|
2012-06-08 10:11:14 +02:00
|
|
|
|
|
|
|
virtual void SleepForMicroseconds(int micros) {}
|
2012-08-15 00:20:36 +02:00
|
|
|
|
2012-09-12 18:54:22 +02:00
|
|
|
virtual Status GetHostName(char* name, uint64_t len) {return notsup;}
|
2012-08-15 00:20:36 +02:00
|
|
|
|
2012-08-29 21:29:43 +02:00
|
|
|
virtual Status GetCurrentTime(int64_t* unix_time) {return notsup;}
|
2012-08-15 00:20:36 +02:00
|
|
|
|
|
|
|
virtual Status GetAbsolutePath(const std::string& db_path,
|
2012-08-29 21:29:43 +02:00
|
|
|
std::string* outputpath) {return notsup;}
|
2012-09-20 00:21:09 +02:00
|
|
|
|
2013-09-12 09:53:30 +02:00
|
|
|
virtual void SetBackgroundThreads(int number, Priority pri = LOW) {}
|
2012-10-19 23:00:53 +02:00
|
|
|
|
|
|
|
virtual std::string TimeToString(uint64_t number) { return "";}
|
2012-06-08 10:11:14 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HDFS
|