Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.hadoop.hdds.utils.db;

import org.apache.hadoop.metrics2.MetricsSystem;
import org.apache.hadoop.metrics2.annotation.Metric;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import org.apache.hadoop.metrics2.lib.MutableCounterLong;

import com.google.common.annotations.VisibleForTesting;

/**
* Class to hold RocksDB metrics.
*/
public class RDBMetrics {

private static final String SOURCE_NAME =
RDBMetrics.class.getSimpleName();

public RDBMetrics() {
}

public static RDBMetrics create() {
MetricsSystem ms = DefaultMetricsSystem.instance();
return ms.register(SOURCE_NAME,
"Rocks DB Metrics",
new RDBMetrics());
}

private @Metric MutableCounterLong numDBKeyMayExistChecks;
private @Metric MutableCounterLong numDBKeyMayExistMisses;


public void incNumDBKeyMayExistChecks() {
numDBKeyMayExistChecks.incr();
}

public void incNumDBKeyMayExistMisses() {
numDBKeyMayExistMisses.incr();
}


@VisibleForTesting
public long getNumDBKeyMayExistChecks() {
return numDBKeyMayExistChecks.value();
}

@VisibleForTesting
public long getNumDBKeyMayExistMisses() {
return numDBKeyMayExistMisses.value();
}

public void unRegister() {
MetricsSystem ms = DefaultMetricsSystem.instance();
ms.unregisterSource(SOURCE_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class RDBStore implements DBStore {
private RDBCheckpointManager checkPointManager;
private String checkpointsParentDir;
private List<ColumnFamilyHandle> columnFamilyHandles;
private RDBMetrics rdbMetrics;

@VisibleForTesting
public RDBStore(File dbFile, DBOptions options,
Expand Down Expand Up @@ -131,6 +132,7 @@ public RDBStore(File dbFile, DBOptions options,

//Initialize checkpoint manager
checkPointManager = new RDBCheckpointManager(db, "om");
rdbMetrics = RDBMetrics.create();

} catch (RocksDBException e) {
throw toIOException(
Expand Down Expand Up @@ -252,7 +254,7 @@ public Table<byte[], byte[]> getTable(String name) throws IOException {
if (handle == null) {
throw new IOException("No such table in this DB. TableName : " + name);
}
return new RDBTable(this.db, handle, this.writeOptions);
return new RDBTable(this.db, handle, this.writeOptions, rdbMetrics);
}

@Override
Expand All @@ -274,7 +276,7 @@ public <KEY, VALUE> Table<KEY, VALUE> getTable(String name,
public ArrayList<Table> listTables() throws IOException {
ArrayList<Table> returnList = new ArrayList<>();
for (ColumnFamilyHandle handle : handleTable.values()) {
returnList.add(new RDBTable(db, handle, writeOptions));
returnList.add(new RDBTable(db, handle, writeOptions, rdbMetrics));
}
return returnList;
}
Expand Down Expand Up @@ -378,4 +380,7 @@ public RocksDB getDb() {
return db;
}

public RDBMetrics getMetrics() {
return rdbMetrics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class RDBTable implements Table<byte[], byte[]> {
private final RocksDB db;
private final ColumnFamilyHandle handle;
private final WriteOptions writeOptions;
private final RDBMetrics rdbMetrics;

/**
* Constructs a TableStore.
Expand All @@ -57,10 +58,11 @@ class RDBTable implements Table<byte[], byte[]> {
* @param writeOptions - RocksDB write Options.
*/
RDBTable(RocksDB db, ColumnFamilyHandle handle,
WriteOptions writeOptions) {
WriteOptions writeOptions, RDBMetrics rdbMetrics) {
this.db = db;
this.handle = handle;
this.writeOptions = writeOptions;
this.rdbMetrics = rdbMetrics;
}

/**
Expand Down Expand Up @@ -125,8 +127,17 @@ public boolean isExist(byte[] key) throws IOException {
// RocksDB#keyMayExist
// If the key definitely does not exist in the database, then this
// method returns false, else true.
return db.keyMayExist(handle, key, new StringBuilder())
&& db.get(handle, key) != null;
rdbMetrics.incNumDBKeyMayExistChecks();
boolean keyMayExist = db.keyMayExist(handle, key, new StringBuilder());
if (!keyMayExist) {
return false;
} else {
boolean keyGet = (db.get(handle, key) != null);
if (!keyGet) {
rdbMetrics.incNumDBKeyMayExistMisses();
}
return keyGet;
}
} catch (RocksDBException e) {
throw toIOException(
"Error in accessing DB. ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ public void testIsExist() throws Exception {
byte[] invalidKey =
RandomStringUtils.random(5).getBytes(StandardCharsets.UTF_8);
Assert.assertFalse(testTable.isExist(invalidKey));
RDBMetrics rdbMetrics = rdbStore.getMetrics();
Assert.assertEquals(3, rdbMetrics.getNumDBKeyMayExistChecks());
Assert.assertTrue(rdbMetrics.getNumDBKeyMayExistMisses() <= 1);
}
}

Expand Down