Skip to content

Commit

Permalink
HDDS-3244. Open Rocksdb in createContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
runzhiwang committed Mar 27, 2020
1 parent a6340a6 commit a145399
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
import org.apache.hadoop.ozone.container.common.transport.server.ratis
.DispatcherContext;
import org.apache.hadoop.ozone.container.common.volume.VolumeSet;
import org.apache.hadoop.ozone.container.keyvalue.helpers.BlockUtils;
import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos
.ContainerCommandRequestProto;
Expand Down Expand Up @@ -165,20 +163,6 @@ public ContainerCommandResponseProto dispatch(
}
}

private void createRocksDBAndPutCache(Container container)
throws IOException {
if (container == null) {
return;
}

KeyValueContainerData kvContainerData =
(KeyValueContainerData)container.getContainerData();
if (!BlockUtils.isDBExist(kvContainerData, conf)) {
BlockUtils.createDBAndPutCache(kvContainerData.getContainerDBType(),
kvContainerData.getDbFile().getAbsolutePath(), conf);
}
}

@SuppressWarnings("methodlength")
private ContainerCommandResponseProto dispatchRequest(
ContainerCommandRequestProto msg, DispatcherContext dispatcherContext) {
Expand Down Expand Up @@ -229,15 +213,6 @@ private ContainerCommandResponseProto dispatchRequest(
container2BCSIDMap = dispatcherContext.getContainer2BCSIDMap();
}
if (isWriteCommitStage) {
try {
createRocksDBAndPutCache(container);
} catch (IOException ioe) {
StorageContainerException sce = new StorageContainerException(
"Create RocksDB failed. " + ioe.getMessage(), ioe,
Result.UNABLE_TO_READ_METADATA_DB);
return ContainerUtils.logAndReturnError(LOG, sce, msg);
}

// check if the container Id exist in the loaded snapshot file. if
// it does not , it infers that , this is a restart of dn where
// the we are reapplying the transaction which was not captured in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,6 @@ public void removeDB(String containerDBPath) {
}
}

/**
* Check whether a DB handler exists in cache.
*
* @param containerDBPath - DB path of the container.
*/
public boolean isDBExist(String containerDBPath) {
lock.lock();
try {
return this.get(containerDBPath) != null;
} finally {
lock.unlock();
}
}

/**
* Add a DB handler into cache.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
import org.apache.hadoop.hdds.scm.container.common.helpers.StorageContainerException;
import org.apache.hadoop.hdds.utils.MetadataStore;
import org.apache.hadoop.hdds.utils.MetadataStoreBuilder;
import org.apache.hadoop.ozone.container.common.helpers.BlockData;
import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData;
import org.apache.hadoop.ozone.container.common.utils.ContainerCache;
import org.apache.hadoop.ozone.container.common.utils.ReferenceCountedDB;

import java.io.File;
import java.io.IOException;

import static org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos
Expand Down Expand Up @@ -99,41 +96,16 @@ public static void shutdownCache(ContainerCache cache) {
}

/**
* Check whether a DB handler exists in cache.
* Add a DB handler into cache.
*
* @param containerData containerData.
* @param conf configuration.
*/
public static boolean isDBExist(KeyValueContainerData containerData,
Configuration conf) {
Preconditions.checkNotNull(containerData);
ContainerCache cache = ContainerCache.getInstance(conf);
Preconditions.checkNotNull(cache);
Preconditions.checkNotNull(containerData.getDbFile());
return cache.isDBExist(containerData.getDbFile().getAbsolutePath());
}

/**
* Create a DB handler and put it into cache.
*
* @param containerDBType - DB type of the container.
* @param db - DB handler.
* @param containerDBPath - DB path of the container.
* @param conf configuration.
*/
public static void createDBAndPutCache(String containerDBType,
String containerDBPath, Configuration conf) throws IOException {
public static void addDB(ReferenceCountedDB db, String containerDBPath,
Configuration conf) {
ContainerCache cache = ContainerCache.getInstance(conf);
Preconditions.checkNotNull(cache);

MetadataStore metadataStore =
MetadataStoreBuilder.newBuilder()
.setDbFile(new File(containerDBPath))
.setCreateIfMissing(true)
.setConf(conf)
.setDBType(containerDBType)
.build();
ReferenceCountedDB db =
new ReferenceCountedDB(metadataStore, containerDBPath);
cache.addDB(containerDBPath, db);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.apache.hadoop.ozone.container.common.helpers.BlockData;
import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData;
import org.apache.hadoop.hdds.utils.MetadataKeyFilters;
import org.apache.hadoop.hdds.utils.MetadataStore;
import org.apache.hadoop.hdds.utils.MetadataStoreBuilder;

import com.google.common.base.Preconditions;
import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -72,6 +74,13 @@ public static void createContainerMetaData(File containerMetaDataPath, File
" Path: " + containerMetaDataPath);
}

MetadataStore store = MetadataStoreBuilder.newBuilder().setConf(conf)
.setCreateIfMissing(true).setDbFile(dbFile).build();
ReferenceCountedDB db =
new ReferenceCountedDB(store, dbFile.getAbsolutePath());
//add db handler into cache
BlockUtils.addDB(db, dbFile.getAbsolutePath(), conf);

if (!chunksPath.mkdirs()) {
LOG.error("Unable to create chunks directory Container {}",
chunksPath);
Expand Down

0 comments on commit a145399

Please sign in to comment.