use of io.dingodb.store.row.ApproximateKVStats in project dingo by dingodb.
the class RegionHeartbeatSender method collectRegionStats.
public RegionStats collectRegionStats(final TimeInterval timeInterval) {
final RegionStats stats = new RegionStats();
stats.setRegionId(region.getId());
// Leader Peer sending the heartbeat
stats.setLeader(new Peer(region.getId(), this.storeId, this.regionEngine.getStoreEngine().getSelfEndpoint()));
// Leader considers that these peers are down
// TODO
// Pending peers are the peers that the leader can't consider as working followers
// TODO
// Bytes written for the region during this period
stats.setBytesWritten(bytesWritten());
// Bytes read for the region during this period
stats.setBytesRead(bytesRead());
// Keys written for the region during this period
stats.setKeysWritten(keysWritten());
// Keys read for the region during this period
stats.setKeysRead(keysRead());
// Approximate region size
// TODO very important
// Approximate number of keys
ApproximateKVStats rangeStats = store().getApproximateKVStatsInRange(region.getStartKey(), region.getEndKey());
stats.setApproximateKeys(rangeStats.getKeysCnt());
stats.setApproximateSize(rangeStats.getSizeInBytes());
// Actually reported time interval
stats.setInterval(timeInterval);
if (log.isDebugEnabled()) {
log.info("Collect [RegionStats]: {}.", stats);
}
return stats;
}
use of io.dingodb.store.row.ApproximateKVStats in project dingo by dingodb.
the class RocksRawKVStore method getApproximateKVStatsInRange.
@Override
public ApproximateKVStats getApproximateKVStatsInRange(final byte[] startKey, final byte[] endKey) {
final Timer.Context timeCtx = getTimeContext("APPROXIMATE_KEYS");
final Lock readLock = this.readWriteLock.readLock();
readLock.lock();
final Snapshot snapshot = this.db.getSnapshot();
Long approximateTotalBytes = 0L;
try (final ReadOptions readOptions = new ReadOptions()) {
readOptions.setSnapshot(snapshot);
try (final RocksIterator it = this.db.newIterator(readOptions)) {
if (startKey == null) {
it.seekToFirst();
} else {
it.seek(startKey);
}
long approximateKeys = 0;
for (; ; ) {
// The accuracy is 100, don't ask more
for (int i = 0; i < 100; i++) {
if (!it.isValid()) {
return new ApproximateKVStats(approximateKeys, approximateTotalBytes);
}
approximateTotalBytes += it.key().length;
approximateTotalBytes += it.value().length;
++approximateKeys;
if (endKey != null && BytesUtil.compare(it.key(), endKey) >= 0) {
return new ApproximateKVStats(approximateKeys, approximateTotalBytes);
}
it.next();
}
}
}
} finally {
// Nothing to release, rocksDB never own the pointer for a snapshot.
snapshot.close();
// The pointer to the snapshot is released by the database instance.
this.db.releaseSnapshot(snapshot);
readLock.unlock();
timeCtx.stop();
}
}
Aggregations