Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public ReferenceCountedDB getDB(long containerID, String containerDBType,
try {
long start = Time.monotonicNow();
DatanodeStore store = BlockUtils.getUncachedDatanodeStore(containerID,
containerDBPath, schemaVersion, conf);
containerDBPath, schemaVersion, conf, false);
db = new ReferenceCountedDB(store, containerDBPath);
metrics.incDbOpenLatency(Time.monotonicNow() - start);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ private BlockUtils() {
*/
public static DatanodeStore getUncachedDatanodeStore(long containerID,
String containerDBPath, String schemaVersion,
ConfigurationSource conf) throws IOException {
ConfigurationSource conf, boolean readOnly) throws IOException {

DatanodeStore store;
if (schemaVersion.equals(OzoneConsts.SCHEMA_V1)) {
store = new DatanodeStoreSchemaOneImpl(conf,
containerID, containerDBPath);
containerID, containerDBPath, readOnly);
} else if (schemaVersion.equals(OzoneConsts.SCHEMA_V2)) {
store = new DatanodeStoreSchemaTwoImpl(conf,
containerID, containerDBPath);
containerID, containerDBPath, readOnly);
} else {
throw new IllegalArgumentException(
"Unrecognized database schema version: " + schemaVersion);
Expand All @@ -88,11 +88,11 @@ public static DatanodeStore getUncachedDatanodeStore(long containerID,
* @throws IOException
*/
public static DatanodeStore getUncachedDatanodeStore(
KeyValueContainerData containerData, ConfigurationSource conf)
throws IOException {
KeyValueContainerData containerData, ConfigurationSource conf,
boolean readOnly) throws IOException {
return getUncachedDatanodeStore(containerData.getContainerID(),
containerData.getDbFile().getAbsolutePath(),
containerData.getSchemaVersion(), conf);
containerData.getSchemaVersion(), conf, readOnly);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public static void createContainerMetaData(long containerID,
DatanodeStore store;
if (schemaVersion.equals(OzoneConsts.SCHEMA_V1)) {
store = new DatanodeStoreSchemaOneImpl(conf,
containerID, dbFile.getAbsolutePath());
containerID, dbFile.getAbsolutePath(), false);
} else if (schemaVersion.equals(OzoneConsts.SCHEMA_V2)) {
store = new DatanodeStoreSchemaTwoImpl(conf,
containerID, dbFile.getAbsolutePath());
containerID, dbFile.getAbsolutePath(), false);
} else {
throw new IllegalArgumentException(
"Unrecognized schema version for container: " + schemaVersion);
Expand Down Expand Up @@ -192,7 +192,8 @@ public static void parseKVContainerData(KeyValueContainerData kvContainerData,
DatanodeStore store = null;
try {
try {
store = BlockUtils.getUncachedDatanodeStore(kvContainerData, config);
store = BlockUtils.getUncachedDatanodeStore(
kvContainerData, config, true);
} catch (IOException e) {
// If an exception is thrown, then it may indicate the RocksDB is
// already open in the container cache. As this code is only executed at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public abstract class AbstractDatanodeStore implements DatanodeStore {
private static final DBProfile DEFAULT_PROFILE = DBProfile.DISK;
private static final Map<ConfigurationSource, ColumnFamilyOptions>
OPTIONS_CACHE = new ConcurrentHashMap<>();
private final boolean openReadOnly;

/**
* Constructs the metadata store and starts the DB services.
Expand All @@ -85,7 +86,8 @@ public abstract class AbstractDatanodeStore implements DatanodeStore {
* @throws IOException - on Failure.
*/
protected AbstractDatanodeStore(ConfigurationSource config, long containerID,
AbstractDatanodeDBDefinition dbDef) throws IOException {
AbstractDatanodeDBDefinition dbDef, boolean openReadOnly)
throws IOException {

// The same config instance is used on each datanode, so we can share the
// corresponding column family options, providing a single shared cache
Expand All @@ -97,6 +99,7 @@ protected AbstractDatanodeStore(ConfigurationSource config, long containerID,

this.dbDef = dbDef;
this.containerID = containerID;
this.openReadOnly = openReadOnly;
start(config);
}

Expand All @@ -121,6 +124,7 @@ public void start(ConfigurationSource config)
this.store = DBStoreBuilder.newBuilder(config, dbDef)
.setDBOptions(options)
.setDefaultCFOptions(cfOptions)
.setOpenReadOnly(openReadOnly)
.build();

// Use the DatanodeTable wrapper to disable the table iterator on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public class DatanodeStoreSchemaOneImpl extends AbstractDatanodeStore {
* @throws IOException - on Failure.
*/
public DatanodeStoreSchemaOneImpl(ConfigurationSource config,
long containerID, String dbPath)
throws IOException {
super(config, containerID, new DatanodeSchemaOneDBDefinition(dbPath));
long containerID, String dbPath, boolean openReadOnly)
throws IOException {
super(config, containerID, new DatanodeSchemaOneDBDefinition(dbPath),
openReadOnly);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public class DatanodeStoreSchemaTwoImpl extends AbstractDatanodeStore {
* @throws IOException - on Failure.
*/
public DatanodeStoreSchemaTwoImpl(ConfigurationSource config,
long containerID, String dbPath)
throws IOException {
super(config, containerID, new DatanodeSchemaTwoDBDefinition(dbPath));
long containerID, String dbPath, boolean openReadOnly)
throws IOException {
super(config, containerID, new DatanodeSchemaTwoDBDefinition(dbPath),
openReadOnly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class TestContainerCache {
private void createContainerDB(OzoneConfiguration conf, File dbFile)
throws Exception {
DatanodeStore store = new DatanodeStoreSchemaTwoImpl(
conf, 1, dbFile.getAbsolutePath());
conf, 1, dbFile.getAbsolutePath(), false);

// we close since the SCM pre-creates containers.
// we will open and put Db handle into a cache when keys are being created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public final class DBStoreBuilder {
private CodecRegistry registry;
private String rocksDbStat;
private RocksDBConfiguration rocksDBConfiguration;
// Flag to indicate if the RocksDB should be opened readonly.
private boolean openReadOnly = false;

/**
* Create DBStoreBuilder from a generic DBDefinition.
Expand Down Expand Up @@ -187,7 +189,7 @@ public DBStore build() throws IOException {
}

return new RDBStore(dbFile, rocksDBOption, writeOptions, tableConfigs,
registry);
registry, openReadOnly);
}

public DBStoreBuilder setName(String name) {
Expand Down Expand Up @@ -227,6 +229,11 @@ public DBStoreBuilder setPath(Path path) {
return this;
}

public DBStoreBuilder setOpenReadOnly(boolean readOnly) {
this.openReadOnly = readOnly;
return this;
}

/**
* Set the {@link DBOptions} and default {@link ColumnFamilyOptions} based
* on {@code prof}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ public class RDBStore implements DBStore {
@VisibleForTesting
public RDBStore(File dbFile, DBOptions options,
Set<TableConfig> families) throws IOException {
this(dbFile, options, new WriteOptions(), families, new CodecRegistry());
this(dbFile, options, new WriteOptions(), families, new CodecRegistry(),
false);
}

public RDBStore(File dbFile, DBOptions options,
WriteOptions writeOptions, Set<TableConfig> families,
CodecRegistry registry)
CodecRegistry registry, boolean readOnly)
throws IOException {
Preconditions.checkNotNull(dbFile, "DB file location cannot be null");
Preconditions.checkNotNull(families);
Expand Down Expand Up @@ -108,8 +109,13 @@ public RDBStore(File dbFile, DBOptions options,
extraCf.forEach(cf -> columnFamilyDescriptors.add(cf.getDescriptor()));
}

db = RocksDB.open(dbOptions, dbLocation.getAbsolutePath(),
columnFamilyDescriptors, columnFamilyHandles);
if (readOnly) {
db = RocksDB.openReadOnly(dbOptions, dbLocation.getAbsolutePath(),
columnFamilyDescriptors, columnFamilyHandles);
} else {
db = RocksDB.open(dbOptions, dbLocation.getAbsolutePath(),
columnFamilyDescriptors, columnFamilyHandles);
}

for (int x = 0; x < columnFamilyHandles.size(); x++) {
handleTable.put(
Expand Down