Search in sources :

Example 1 with ApproximateKVStats

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;
}
Also used : RegionStats(io.dingodb.store.row.metadata.RegionStats) Peer(io.dingodb.store.row.metadata.Peer) ApproximateKVStats(io.dingodb.store.row.ApproximateKVStats)

Example 2 with ApproximateKVStats

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();
    }
}
Also used : Snapshot(org.rocksdb.Snapshot) Timer(com.codahale.metrics.Timer) ReadOptions(org.rocksdb.ReadOptions) AtomicLong(java.util.concurrent.atomic.AtomicLong) RocksIterator(org.rocksdb.RocksIterator) ApproximateKVStats(io.dingodb.store.row.ApproximateKVStats) Checkpoint(org.rocksdb.Checkpoint) DistributedLock(io.dingodb.store.row.util.concurrent.DistributedLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

ApproximateKVStats (io.dingodb.store.row.ApproximateKVStats)2 Timer (com.codahale.metrics.Timer)1 Peer (io.dingodb.store.row.metadata.Peer)1 RegionStats (io.dingodb.store.row.metadata.RegionStats)1 DistributedLock (io.dingodb.store.row.util.concurrent.DistributedLock)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Checkpoint (org.rocksdb.Checkpoint)1 ReadOptions (org.rocksdb.ReadOptions)1 RocksIterator (org.rocksdb.RocksIterator)1 Snapshot (org.rocksdb.Snapshot)1