Search in sources :

Example 21 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class RocksDbKeyValueStorage method doGetValue.

/**
 * Gets the value by a key and a revision.
 *
 * @param key      Target key.
 * @param revision Target revision.
 * @return Entry.
 */
@NotNull
Entry doGetValue(byte[] key, long revision) {
    if (revision == 0) {
        return Entry.empty(key);
    }
    byte[] valueBytes;
    try {
        valueBytes = data.get(keyToRocksKey(revision, key));
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    }
    if (valueBytes == null || valueBytes.length == 0) {
        return Entry.empty(key);
    }
    Value lastVal = bytesToValue(valueBytes);
    if (lastVal.tombstone()) {
        return Entry.tombstone(key, revision, lastVal.updateCounter());
    }
    return new Entry(key, lastVal.bytes(), revision, lastVal.updateCounter());
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Value(org.apache.ignite.internal.metastorage.server.Value) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class RocksDbKeyValueStorage method putAll.

/**
 * {@inheritDoc}
 */
@Override
public void putAll(List<byte[]> keys, List<byte[]> values) {
    rwLock.writeLock().lock();
    try (WriteBatch batch = new WriteBatch()) {
        long curRev = rev + 1;
        long counter = addAllToBatch(batch, keys, values, curRev);
        for (byte[] key : keys) {
            updateKeysIndex(batch, key, curRev);
        }
        fillAndWriteBatch(batch, curRev, counter);
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) WriteBatch(org.rocksdb.WriteBatch)

Example 23 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class RocksDbKeyValueStorage method getAndPutAll.

/**
 * {@inheritDoc}
 */
@NotNull
@Override
public Collection<Entry> getAndPutAll(List<byte[]> keys, List<byte[]> values) {
    Collection<Entry> res;
    rwLock.writeLock().lock();
    try (WriteBatch batch = new WriteBatch()) {
        long curRev = rev + 1;
        res = doGetAll(keys, curRev);
        long counter = addAllToBatch(batch, keys, values, curRev);
        for (byte[] key : keys) {
            updateKeysIndex(batch, key, curRev);
        }
        fillAndWriteBatch(batch, curRev, counter);
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    } finally {
        rwLock.writeLock().unlock();
    }
    return res;
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Entry(org.apache.ignite.internal.metastorage.server.Entry) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) WriteBatch(org.rocksdb.WriteBatch) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class RocksDbKeyValueStorage method higherOrCeiling.

/**
 * Gets an entry from the keys index with the least key greater than or equal to the specified key, depending on the strictlyHigher
 * parameter.
 *
 * @param key Key.
 * @param strictlyHigher {@code true} for a strictly higher entry, {@code false} for a ceiling one.
 * @return Entry for the least key greater than or equal to the specified key. If no such entry exists returns {@code null}.
 */
@Nullable
private IgniteBiTuple<byte[], long[]> higherOrCeiling(byte[] key, boolean strictlyHigher) {
    try (RocksIterator iterator = index.newIterator()) {
        iterator.seek(key);
        RocksBiPredicate predicate = strictlyHigher ? (k, v) -> CMP.compare(k, key) > 0 : (k, v) -> CMP.compare(k, key) >= 0;
        boolean found = find(iterator, predicate);
        if (!found) {
            return null;
        }
        return new IgniteBiTuple<>(iterator.key(), getAsLongs(iterator.value()));
    } catch (RocksDBException e) {
        throw new IgniteInternalException(e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) RocksBiPredicate(org.apache.ignite.internal.rocksdb.RocksBiPredicate) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RocksIterator(org.rocksdb.RocksIterator) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with IgniteInternalException

use of org.apache.ignite.lang.IgniteInternalException in project ignite-3 by apache.

the class RocksDbKeyValueStorage method snapshot.

/**
 * {@inheritDoc}
 */
@NotNull
@Override
public CompletableFuture<Void> snapshot(Path snapshotPath) {
    Path tempPath = Paths.get(snapshotPath.toString() + TMP_SUFFIX);
    // Create a RocksDB point-in-time snapshot
    Snapshot snapshot = db.getSnapshot();
    return CompletableFuture.runAsync(() -> {
        // (Re)create the temporary directory
        IgniteUtils.deleteIfExists(tempPath);
        try {
            Files.createDirectories(tempPath);
        } catch (IOException e) {
            throw new IgniteInternalException("Failed to create directory: " + tempPath, e);
        }
    }, snapshotExecutor).thenCompose(argVoid -> CompletableFuture.allOf(CompletableFuture.runAsync(() -> createSstFile(data, snapshot, tempPath), snapshotExecutor), CompletableFuture.runAsync(() -> createSstFile(index, snapshot, tempPath), snapshotExecutor))).whenComplete((argVoid, throwable) -> {
        // Release a snapshot
        db.releaseSnapshot(snapshot);
        // Snapshot is not actually closed here, because a Snapshot instance doesn't own a pointer, the
        // database does. Calling close to maintain the AutoCloseable semantics
        snapshot.close();
        if (throwable != null) {
            return;
        }
        // Delete snapshot directory if it already exists
        IgniteUtils.deleteIfExists(snapshotPath);
        try {
            // Rename the temporary directory
            IgniteUtils.atomicMoveFile(tempPath, snapshotPath, null);
        } catch (IOException e) {
            throw new IgniteInternalException("Failed to rename: " + tempPath + " to " + snapshotPath, e);
        }
    });
}
Also used : Path(java.nio.file.Path) Value(org.apache.ignite.internal.metastorage.server.Value) Arrays(java.util.Arrays) StatementResult(org.apache.ignite.internal.metastorage.server.StatementResult) Statement(org.apache.ignite.internal.metastorage.server.Statement) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) RocksStorageUtils.longToBytes(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.longToBytes) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) Entry(org.apache.ignite.internal.metastorage.server.Entry) Map(java.util.Map) RocksDBException(org.rocksdb.RocksDBException) Path(java.nio.file.Path) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReadOptions(org.rocksdb.ReadOptions) Operation(org.apache.ignite.internal.metastorage.server.Operation) KeyValueStorage(org.apache.ignite.internal.metastorage.server.KeyValueStorage) IngestExternalFileOptions(org.rocksdb.IngestExternalFileOptions) Collection(java.util.Collection) Cursor(org.apache.ignite.internal.util.Cursor) WriteOptions(org.rocksdb.WriteOptions) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) WatchEvent(org.apache.ignite.internal.metastorage.server.WatchEvent) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Options(org.rocksdb.Options) ColumnFamily(org.apache.ignite.internal.rocksdb.ColumnFamily) NotNull(org.jetbrains.annotations.NotNull) RocksStorageUtils.getAsLongs(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.getAsLongs) RocksStorageUtils.bytesToValue(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToValue) RocksStorageUtils.bytesToLong(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.bytesToLong) DATA(org.apache.ignite.internal.metastorage.server.persistence.StorageColumnFamilyType.DATA) WriteBatch(org.rocksdb.WriteBatch) CompletableFuture(java.util.concurrent.CompletableFuture) RocksUtils.forEach(org.apache.ignite.internal.rocksdb.RocksUtils.forEach) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RocksUtils.createSstFile(org.apache.ignite.internal.rocksdb.RocksUtils.createSstFile) TreeSet(java.util.TreeSet) RocksIterator(org.rocksdb.RocksIterator) ArrayList(java.util.ArrayList) Condition(org.apache.ignite.internal.metastorage.server.Condition) TOMBSTONE(org.apache.ignite.internal.metastorage.server.Value.TOMBSTONE) RocksDB(org.rocksdb.RocksDB) LONG_EMPTY_ARRAY(org.apache.ignite.internal.util.ArrayUtils.LONG_EMPTY_ARRAY) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) RocksBiPredicate(org.apache.ignite.internal.rocksdb.RocksBiPredicate) RocksUtils.find(org.apache.ignite.internal.rocksdb.RocksUtils.find) ExecutorService(java.util.concurrent.ExecutorService) Files(java.nio.file.Files) DBOptions(org.rocksdb.DBOptions) INDEX(org.apache.ignite.internal.metastorage.server.persistence.StorageColumnFamilyType.INDEX) If(org.apache.ignite.internal.metastorage.server.If) IOException(java.io.IOException) RocksStorageUtils.appendLong(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.appendLong) TimeUnit(java.util.concurrent.TimeUnit) TestOnly(org.jetbrains.annotations.TestOnly) Paths(java.nio.file.Paths) Snapshot(org.rocksdb.Snapshot) Comparator(java.util.Comparator) RocksStorageUtils.keyToRocksKey(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.keyToRocksKey) Collections(java.util.Collections) Update(org.apache.ignite.internal.metastorage.server.Update) RocksStorageUtils.valueToBytes(org.apache.ignite.internal.metastorage.server.persistence.RocksStorageUtils.valueToBytes) Snapshot(org.rocksdb.Snapshot) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)58 RocksDBException (org.rocksdb.RocksDBException)19 IOException (java.io.IOException)11 Path (java.nio.file.Path)10 ArrayList (java.util.ArrayList)10 NotNull (org.jetbrains.annotations.NotNull)10 WriteBatch (org.rocksdb.WriteBatch)9 List (java.util.List)7 Entry (org.apache.ignite.internal.metastorage.server.Entry)7 NoSuchElementException (java.util.NoSuchElementException)6 NetworkAddress (org.apache.ignite.network.NetworkAddress)6 Test (org.junit.jupiter.api.Test)6 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)5 Nullable (org.jetbrains.annotations.Nullable)5 UUID (java.util.UUID)4 TimeoutException (java.util.concurrent.TimeoutException)4 NodeStoppingException (org.apache.ignite.lang.NodeStoppingException)4 ReadOptions (org.rocksdb.ReadOptions)4 RocksIterator (org.rocksdb.RocksIterator)4 CompletableFuture (java.util.concurrent.CompletableFuture)3