Skip to content

Commit 2e3b2da

Browse files
committed
Add ScratchDir/File methods to CloudEnvImpl
Replaces direct use of /tmp, to fix Windows issues
1 parent 31a9b12 commit 2e3b2da

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

cloud/cloud_env_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ class CloudEnvImpl : public CloudEnv {
321321
bool test_disable_cloud_manifest_{false};
322322

323323
// scratch space in local dir
324-
static constexpr const char* SCRATCH_LOCAL_DIR = "/tmp";
324+
std::string GetScratchDirectory() const;
325+
std::string GetScratchFile() const;
326+
325327
std::mutex files_to_delete_mutex_;
326328
std::chrono::seconds file_deletion_delay_ = std::chrono::hours(1);
327329
std::unordered_map<std::string, int> files_to_delete_;

cloud/purge.cc

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "rocksdb/env.h"
1818
#include "rocksdb/options.h"
1919
#include "rocksdb/status.h"
20+
#include "util/random.h"
2021

2122
namespace ROCKSDB_NAMESPACE {
2223

@@ -189,22 +190,50 @@ Status CloudEnvImpl::FindObsoleteDbid(
189190
return st;
190191
}
191192

193+
std::string CloudEnvImpl::GetScratchDirectory() const {
194+
const char* env = getenv("TMPDIR");
195+
if (env != nullptr) {
196+
return std::string(env);
197+
} else {
198+
#ifdef _WIN32
199+
env = getenv("TEMP");
200+
if (env == nullptr) {
201+
env = getenv("TMP");
202+
}
203+
if (env != nullptr) {
204+
return std::string(env);
205+
} else {
206+
return "c:\\tmp";
207+
}
208+
#else
209+
return "/tmp";
210+
#endif
211+
}
212+
}
213+
214+
std::string CloudEnvImpl::GetScratchFile() const {
215+
int64_t current_time = 0;
216+
auto s = base_env_->GetCurrentTime(&current_time);
217+
if (!s.ok()) {
218+
current_time = static_cast<int64_t>(std::time(0));
219+
}
220+
Random rand(static_cast<uint32_t>(current_time));
221+
const std::string scratch = GetScratchDirectory();
222+
return TempFileName(scratch, rand.Next());
223+
}
224+
192225
//
193226
// For each of the dbids in the list, extract the entire list of
194227
// parent dbids.
195228
Status CloudEnvImpl::extractParents(const std::string& bucket_name_prefix,
196229
const DbidList& dbid_list,
197230
DbidParents* parents) {
198231
const std::string delimiter(DBID_SEPARATOR);
199-
// use current time as seed for random generator
200-
std::srand(static_cast<unsigned int>(std::time(0)));
201-
const std::string random = std::to_string(std::rand());
202-
const std::string scratch(SCRATCH_LOCAL_DIR);
232+
auto localfile = GetScratchFile();
203233
Status st;
204234
for (auto iter = dbid_list.begin(); iter != dbid_list.end(); ++iter) {
205235
// download IDENTITY
206236
std::string cloudfile = iter->second + "/IDENTITY";
207-
std::string localfile = scratch + "/.rockset_IDENTITY." + random;
208237
st = GetCloudEnvOptions().storage_provider->GetCloudObject(
209238
bucket_name_prefix, cloudfile, localfile);
210239
if (!st.ok() && !st.IsNotFound()) {

0 commit comments

Comments
 (0)