Skip to content

Commit 2aba151

Browse files
committed
encryption: add support for encryption to writeFileSyncing.
`writeFileSyncing()` now will be able to write encrypted content to RocksDB's env. This commit is part of `use encryption for all local disk usage (non-logs)`. Issue: #19783. Release note: None
1 parent 381eb8d commit 2aba151

17 files changed

+259
-22
lines changed

c-deps/libroach/batch.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,18 @@ DBString DBBatch::GetCompactionStats() { return ToDBString("unsupported"); }
515515

516516
DBStatus DBBatch::EnvWriteFile(DBSlice path, DBSlice contents) { return FmtStatus("unsupported"); }
517517

518+
DBStatus DBBatch::EnvOpenFile(DBSlice path, rocksdb::WritableFile** file) {
519+
return FmtStatus("unsupported");
520+
}
521+
522+
DBStatus DBBatch::EnvCloseFile(rocksdb::WritableFile* file) { return FmtStatus("unsupported"); }
523+
524+
DBStatus DBBatch::EnvSyncFile(rocksdb::WritableFile* file) { return FmtStatus("unsupported"); }
525+
526+
DBStatus DBBatch::EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents) {
527+
return FmtStatus("unsupported");
528+
}
529+
518530
DBWriteOnlyBatch::DBWriteOnlyBatch(DBEngine* db) : DBEngine(db->rep, db->iters), updates(0) {}
519531

520532
DBWriteOnlyBatch::~DBWriteOnlyBatch() {}
@@ -582,6 +594,22 @@ DBStatus DBWriteOnlyBatch::EnvWriteFile(DBSlice path, DBSlice contents) {
582594
return FmtStatus("unsupported");
583595
}
584596

597+
DBStatus DBWriteOnlyBatch::EnvOpenFile(DBSlice path, rocksdb::WritableFile** file) {
598+
return FmtStatus("unsupported");
599+
}
600+
601+
DBStatus DBWriteOnlyBatch::EnvCloseFile(rocksdb::WritableFile* file) {
602+
return FmtStatus("unsupported");
603+
}
604+
605+
DBStatus DBWriteOnlyBatch::EnvSyncFile(rocksdb::WritableFile* file) {
606+
return FmtStatus("unsupported");
607+
}
608+
609+
DBStatus DBWriteOnlyBatch::EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents) {
610+
return FmtStatus("unsupported");
611+
}
612+
585613
rocksdb::WriteBatch::Handler* GetDBBatchInserter(::rocksdb::WriteBatchBase* batch) {
586614
return new DBBatchInserter(batch);
587615
}

c-deps/libroach/batch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ struct DBBatch : public DBEngine {
4141
virtual DBStatus GetStats(DBStatsResult* stats);
4242
virtual DBString GetCompactionStats();
4343
virtual DBStatus EnvWriteFile(DBSlice path, DBSlice contents);
44+
virtual DBStatus EnvOpenFile(DBSlice path, rocksdb::WritableFile** file);
45+
virtual DBStatus EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents);
46+
virtual DBStatus EnvSyncFile(rocksdb::WritableFile* file);
47+
virtual DBStatus EnvCloseFile(rocksdb::WritableFile* file);
4448
};
4549

4650
struct DBWriteOnlyBatch : public DBEngine {
@@ -62,6 +66,10 @@ struct DBWriteOnlyBatch : public DBEngine {
6266
virtual DBStatus GetStats(DBStatsResult* stats);
6367
virtual DBString GetCompactionStats();
6468
virtual DBStatus EnvWriteFile(DBSlice path, DBSlice contents);
69+
virtual DBStatus EnvOpenFile(DBSlice path, rocksdb::WritableFile** file);
70+
virtual DBStatus EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents);
71+
virtual DBStatus EnvSyncFile(rocksdb::WritableFile* file);
72+
virtual DBStatus EnvCloseFile(rocksdb::WritableFile* file);
6573
};
6674

6775
// GetDBBatchInserter returns a WriteBatch::Handler that operates on a

c-deps/libroach/db.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,22 @@ DBStatus DBEnvWriteFile(DBEngine* db, DBSlice path, DBSlice contents) {
420420
return db->EnvWriteFile(path, contents);
421421
}
422422

423+
DBStatus DBEnvOpenFile(DBEngine* db, DBSlice path, DBWritableFile* file) {
424+
return db->EnvOpenFile(path, (rocksdb::WritableFile**)file);
425+
}
426+
427+
DBStatus DBEnvCloseFile(DBEngine* db, DBWritableFile file) {
428+
return db->EnvCloseFile((rocksdb::WritableFile*)file);
429+
}
430+
431+
DBStatus DBEnvSyncFile(DBEngine* db, DBWritableFile file) {
432+
return db->EnvSyncFile((rocksdb::WritableFile*)file);
433+
}
434+
435+
DBStatus DBEnvAppendFile(DBEngine* db, DBWritableFile file, DBSlice contents) {
436+
return db->EnvAppendFile((rocksdb::WritableFile*)file, contents);
437+
}
438+
423439
DBIterator* DBNewIter(DBEngine* db, bool prefix, bool stats) {
424440
rocksdb::ReadOptions opts;
425441
opts.prefix_same_as_start = prefix;

c-deps/libroach/engine.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,38 @@ DBStatus DBImpl::EnvWriteFile(DBSlice path, DBSlice contents) {
223223
return kSuccess;
224224
}
225225

226+
// EnvOpenFile opens a new file in the given engine.
227+
DBStatus DBImpl::EnvOpenFile(DBSlice path, rocksdb::WritableFile** file) {
228+
rocksdb::Status status;
229+
const rocksdb::EnvOptions soptions;
230+
rocksdb::unique_ptr<rocksdb::WritableFile> rocksdb_file;
231+
232+
// Create the file.
233+
status = this->rep->GetEnv()->NewWritableFile(ToString(path), &rocksdb_file, soptions);
234+
if (!status.ok()) {
235+
return ToDBStatus(status);
236+
}
237+
*file = rocksdb_file.release();
238+
return kSuccess;
239+
}
240+
241+
// CloseFile closes the given file in the given engine.
242+
DBStatus DBImpl::EnvCloseFile(rocksdb::WritableFile* file) {
243+
rocksdb::Status status = file->Close();
244+
delete file;
245+
return ToDBStatus(status);
246+
}
247+
248+
// EnvAppendFile appends the given data to the file in the given engine.
249+
DBStatus DBImpl::EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents) {
250+
rocksdb::Status status = file->Append(ToSlice(contents));
251+
return ToDBStatus(status);
252+
}
253+
254+
// EnvSyncFile synchronously writes the data of the file to the disk.
255+
DBStatus DBImpl::EnvSyncFile(rocksdb::WritableFile* file) {
256+
rocksdb::Status status = file->Sync();
257+
return ToDBStatus(status);
258+
}
259+
226260
} // namespace cockroach

c-deps/libroach/engine.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ struct DBEngine {
4242
virtual DBStatus GetStats(DBStatsResult* stats) = 0;
4343
virtual DBString GetCompactionStats() = 0;
4444
virtual DBStatus EnvWriteFile(DBSlice path, DBSlice contents) = 0;
45+
virtual DBStatus EnvOpenFile(DBSlice path, rocksdb::WritableFile** file) = 0;
46+
virtual DBStatus EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents) = 0;
47+
virtual DBStatus EnvSyncFile(rocksdb::WritableFile* file) = 0;
48+
virtual DBStatus EnvCloseFile(rocksdb::WritableFile* file) = 0;
4549

4650
DBSSTable* GetSSTables(int* n);
4751
DBString GetUserProperties();
@@ -78,6 +82,10 @@ struct DBImpl : public DBEngine {
7882
virtual DBStatus GetStats(DBStatsResult* stats);
7983
virtual DBString GetCompactionStats();
8084
virtual DBStatus EnvWriteFile(DBSlice path, DBSlice contents);
85+
virtual DBStatus EnvOpenFile(DBSlice path, rocksdb::WritableFile** file);
86+
virtual DBStatus EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents);
87+
virtual DBStatus EnvSyncFile(rocksdb::WritableFile* file);
88+
virtual DBStatus EnvCloseFile(rocksdb::WritableFile* file);
8189
};
8290

8391
} // namespace cockroach

c-deps/libroach/include/libroach.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct {
6161
typedef struct DBCache DBCache;
6262
typedef struct DBEngine DBEngine;
6363
typedef struct DBIterator DBIterator;
64+
typedef void* DBWritableFile;
6465

6566
// DBOptions contains local database options.
6667
typedef struct {
@@ -359,6 +360,19 @@ void DBRunLDB(int argc, char** argv);
359360
// DBEnvWriteFile writes the given data as a new "file" in the given engine.
360361
DBStatus DBEnvWriteFile(DBEngine* db, DBSlice path, DBSlice contents);
361362

363+
// DBEnvOpenFile opens a DBWritableFile as a new "file" in the given engine.
364+
DBStatus DBEnvOpenFile(DBEngine* db, DBSlice path, DBWritableFile* file);
365+
366+
// DBEnvAppendFile appends the given data to the given DBWritableFile in the
367+
// given engine.
368+
DBStatus DBEnvAppendFile(DBEngine* db, DBWritableFile file, DBSlice contents);
369+
370+
// DBEnvSyncFile synchronously writes the data of the file to the disk.
371+
DBStatus DBEnvSyncFile(DBEngine* db, DBWritableFile file);
372+
373+
// DBEnvCloseFile closes the given DBWritableFile in the given engine.
374+
DBStatus DBEnvCloseFile(DBEngine* db, DBWritableFile file);
375+
362376
// DBFileLock contains various parameters set during DBLockFile and required for DBUnlockFile.
363377
typedef void* DBFileLock;
364378

c-deps/libroach/snapshot.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,16 @@ DBStatus DBSnapshot::EnvWriteFile(DBSlice path, DBSlice contents) {
5757
return FmtStatus("unsupported");
5858
}
5959

60+
DBStatus DBSnapshot::EnvOpenFile(DBSlice path, rocksdb::WritableFile** file) {
61+
return FmtStatus("unsupported");
62+
}
63+
64+
DBStatus DBSnapshot::EnvCloseFile(rocksdb::WritableFile* file) { return FmtStatus("unsupported"); }
65+
66+
DBStatus DBSnapshot::EnvSyncFile(rocksdb::WritableFile* file) { return FmtStatus("unsupported"); }
67+
68+
DBStatus DBSnapshot::EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents) {
69+
return FmtStatus("unsupported");
70+
}
71+
6072
} // namespace cockroach

c-deps/libroach/snapshot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ struct DBSnapshot : public DBEngine {
3838
virtual DBStatus GetStats(DBStatsResult* stats);
3939
virtual DBString GetCompactionStats();
4040
virtual DBStatus EnvWriteFile(DBSlice path, DBSlice contents);
41+
virtual DBStatus EnvOpenFile(DBSlice path, rocksdb::WritableFile** file);
42+
virtual DBStatus EnvAppendFile(rocksdb::WritableFile* file, DBSlice contents);
43+
virtual DBStatus EnvSyncFile(rocksdb::WritableFile* file);
44+
virtual DBStatus EnvCloseFile(rocksdb::WritableFile* file);
4145
};
4246

4347
} // namespace cockroach

pkg/storage/client_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ type multiTestContext struct {
202202
dbs []*client.DB
203203
gossips []*gossip.Gossip
204204
storePools []*storage.StorePool
205+
dirCleanups []func()
206+
caches []engine.RocksDBCache
205207
// We use multiple stoppers so we can restart different parts of the
206208
// test individually. transportStopper is for 'transport', and the
207209
// 'stoppers' slice corresponds to the 'stores'.
@@ -339,6 +341,14 @@ func (m *multiTestContext) Stop() {
339341
}
340342
m.transportStopper.Stop(context.TODO())
341343

344+
for _, cleanup := range m.dirCleanups {
345+
cleanup()
346+
}
347+
348+
for _, cache := range m.caches {
349+
cache.Release()
350+
}
351+
342352
for _, s := range m.engineStoppers {
343353
s.Stop(context.TODO())
344354
}
@@ -693,7 +703,20 @@ func (m *multiTestContext) addStore(idx int) {
693703
} else {
694704
engineStopper := stop.NewStopper()
695705
m.engineStoppers = append(m.engineStoppers, engineStopper)
696-
eng = engine.NewInMem(roachpb.Attributes{}, 1<<20)
706+
707+
dir, cleanup := testutils.TempDir(m.t)
708+
cache := engine.NewRocksDBCache(1 << 20)
709+
var err error
710+
eng, err = engine.NewRocksDB(engine.RocksDBConfig{
711+
Dir: dir,
712+
MustExist: false,
713+
}, cache)
714+
if err != nil {
715+
m.t.Fatal(err)
716+
}
717+
718+
m.dirCleanups = append(m.dirCleanups, cleanup)
719+
m.caches = append(m.caches, cache)
697720
engineStopper.AddCloser(eng)
698721
m.engines = append(m.engines, eng)
699722
needBootstrap = true

pkg/storage/engine/engine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ type Engine interface {
283283
// that the key range is compacted all the way to the bottommost level of
284284
// SSTables, which is necessary to pick up changes to bloom filters.
285285
CompactRange(start, end roachpb.Key, forceBottommost bool) error
286+
// OpenFile opens a DBFile with the given filename.
287+
OpenFile(filename string) (DBFile, error)
286288
}
287289

288290
// WithSSTables extends the Engine interface with a method to get info

0 commit comments

Comments
 (0)