Search in sources :

Example 1 with StorageException

use of org.apache.ignite.internal.storage.StorageException in project ignite-3 by apache.

the class ItInternalTableScanTest method invalidRequestNtest.

/**
 * Checks whether {@link IllegalArgumentException} is thrown and inner storage cursor is closes in case of invalid requested amount of
 * items.
 *
 * @param reqAmount  Amount of rows to request at a time.
 * @throws Exception If Any.
 */
private void invalidRequestNtest(int reqAmount) throws InterruptedException {
    // The latch that allows to await Subscriber.onComplete() before asserting test invariants
    // and avoids the race between closing the cursor and stopping the node.
    CountDownLatch subscriberFinishedLatch = new CountDownLatch(2);
    when(mockStorage.scan(any())).thenAnswer(invocation -> {
        var cursor = mock(Cursor.class);
        doAnswer(invocationClose -> {
            subscriberFinishedLatch.countDown();
            return null;
        }).when(cursor).close();
        when(cursor.hasNext()).thenAnswer(hnInvocation -> {
            throw new StorageException("test");
        });
        return cursor;
    });
    AtomicReference<Throwable> gotException = new AtomicReference<>();
    internalTbl.scan(0, null).subscribe(new Subscriber<>() {

        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(reqAmount);
        }

        @Override
        public void onNext(BinaryRow item) {
            fail("Should never get here.");
        }

        @Override
        public void onError(Throwable throwable) {
            gotException.set(throwable);
            subscriberFinishedLatch.countDown();
        }

        @Override
        public void onComplete() {
            fail("Should never get here.");
        }
    });
    subscriberFinishedLatch.await();
    assertThrows(IllegalArgumentException.class, () -> {
        throw gotException.get();
    });
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java.util.concurrent.Flow.Subscription) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 2 with StorageException

use of org.apache.ignite.internal.storage.StorageException in project ignite-3 by apache.

the class RocksDbPartitionStorage method invoke.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public <T> T invoke(SearchRow key, InvokeClosure<T> clo) throws StorageException {
    try {
        byte[] partitionKey = partitionKey(key);
        byte[] existingDataBytes = data.get(partitionKey);
        clo.call(existingDataBytes == null ? null : new DelegatingDataRow(key, existingDataBytes));
        switch(clo.operationType()) {
            case WRITE:
                DataRow newRow = clo.newRow();
                assert newRow != null;
                byte[] value = newRow.valueBytes();
                assert value != null;
                data.put(partitionKey, value);
                break;
            case REMOVE:
                data.delete(partitionKey);
                break;
            case NOOP:
                break;
            default:
                throw new UnsupportedOperationException(String.valueOf(clo.operationType()));
        }
        return clo.result();
    } catch (RocksDBException e) {
        throw new StorageException("Failed to access data in the storage", e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) StorageException(org.apache.ignite.internal.storage.StorageException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with StorageException

use of org.apache.ignite.internal.storage.StorageException in project ignite-3 by apache.

the class RocksDbPartitionStorage method insertAll.

/**
 * {@inheritDoc}
 */
@Override
public Collection<DataRow> insertAll(List<? extends DataRow> rows) throws StorageException {
    List<DataRow> cantInsert = new ArrayList<>();
    try (var batch = new WriteBatch();
        var opts = new WriteOptions()) {
        for (DataRow row : rows) {
            byte[] partitionKey = partitionKey(row);
            if (data.get(partitionKey) == null) {
                byte[] value = row.valueBytes();
                assert value != null;
                data.put(batch, partitionKey, value);
            } else {
                cantInsert.add(row);
            }
        }
        db.write(opts, batch);
    } catch (RocksDBException e) {
        throw new StorageException("Filed to write data to the storage", e);
    }
    return cantInsert;
}
Also used : WriteOptions(org.rocksdb.WriteOptions) RocksDBException(org.rocksdb.RocksDBException) ArrayList(java.util.ArrayList) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) WriteBatch(org.rocksdb.WriteBatch) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 4 with StorageException

use of org.apache.ignite.internal.storage.StorageException in project ignite-3 by apache.

the class RocksDbPartitionStorage method writeAll.

/**
 * {@inheritDoc}
 */
@Override
public void writeAll(List<? extends DataRow> rows) throws StorageException {
    try (WriteBatch batch = new WriteBatch();
        WriteOptions opts = new WriteOptions()) {
        for (DataRow row : rows) {
            byte[] value = row.valueBytes();
            assert value != null;
            data.put(batch, partitionKey(row), value);
        }
        db.write(opts, batch);
    } catch (RocksDBException e) {
        throw new StorageException("Filed to write data to the storage", e);
    }
}
Also used : WriteOptions(org.rocksdb.WriteOptions) RocksDBException(org.rocksdb.RocksDBException) WriteBatch(org.rocksdb.WriteBatch) DelegatingDataRow(org.apache.ignite.internal.storage.basic.DelegatingDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) StorageException(org.apache.ignite.internal.storage.StorageException)

Example 5 with StorageException

use of org.apache.ignite.internal.storage.StorageException in project ignite-3 by apache.

the class RocksDbTableStorage method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws StorageException {
    try {
        Files.createDirectories(tablePath);
    } catch (IOException e) {
        throw new StorageException("Failed to create a directory for the table storage", e);
    }
    List<ColumnFamilyDescriptor> cfDescriptors = getExistingCfDescriptors();
    List<ColumnFamilyHandle> cfHandles = new ArrayList<>(cfDescriptors.size());
    DBOptions dbOptions = new DBOptions().setCreateIfMissing(true).setWriteBufferManager(dataRegion.writeBufferManager());
    try {
        db = RocksDB.open(dbOptions, tablePath.toAbsolutePath().toString(), cfDescriptors, cfHandles);
    } catch (RocksDBException e) {
        throw new StorageException("Failed to initialize RocksDB instance", e);
    }
    addToCloseableResources(db::closeE);
    // read all existing Column Families from the db and parse them according to type: meta, partition data or index.
    for (int i = 0; i < cfHandles.size(); i++) {
        ColumnFamilyHandle cfHandle = cfHandles.get(i);
        ColumnFamilyDescriptor cfDescriptor = cfDescriptors.get(i);
        String handleName = cfHandleName(cfHandle);
        ColumnFamily cf = new ColumnFamily(db, cfHandle, handleName, cfDescriptor.getOptions(), null);
        switch(columnFamilyType(handleName)) {
            case META:
                meta = addToCloseableResources(new RocksDbMetaStorage(cf));
                break;
            case PARTITION:
                partitionCf = addToCloseableResources(cf);
                break;
            case SORTED_INDEX:
                String indexName = sortedIndexName(handleName);
                var indexDescriptor = new SortedIndexDescriptor(indexName, tableCfg.value());
                sortedIndices.put(indexName, new RocksDbSortedIndexStorage(cf, indexDescriptor));
                break;
            default:
                throw new StorageException("Unidentified column family [name=" + handleName + ", table=" + tableCfg.name() + ']');
        }
    }
    if (partitionCf == null) {
        partitionCf = addToCloseableResources(createColumnFamily(PARTITION_CF_NAME, partitionCfDescriptor()));
    }
    partitions = new AtomicReferenceArray<>(tableCfg.value().partitions());
    for (int partId : meta.getPartitionIds()) {
        partitions.set(partId, new RocksDbPartitionStorage(threadPool, partId, db, partitionCf));
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) ArrayList(java.util.ArrayList) RocksDbSortedIndexStorage(org.apache.ignite.internal.storage.rocksdb.index.RocksDbSortedIndexStorage) IOException(java.io.IOException) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) SortedIndexDescriptor(org.apache.ignite.internal.storage.index.SortedIndexDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) ColumnFamily(org.apache.ignite.internal.rocksdb.ColumnFamily) DBOptions(org.rocksdb.DBOptions) StorageException(org.apache.ignite.internal.storage.StorageException)

Aggregations

StorageException (org.apache.ignite.internal.storage.StorageException)14 RocksDBException (org.rocksdb.RocksDBException)11 ArrayList (java.util.ArrayList)6 DataRow (org.apache.ignite.internal.storage.DataRow)5 DelegatingDataRow (org.apache.ignite.internal.storage.basic.DelegatingDataRow)5 SimpleDataRow (org.apache.ignite.internal.storage.basic.SimpleDataRow)5 WriteBatch (org.rocksdb.WriteBatch)4 WriteOptions (org.rocksdb.WriteOptions)4 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Subscription (java.util.concurrent.Flow.Subscription)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Stream (java.util.stream.Stream)1 ColumnFamily (org.apache.ignite.internal.rocksdb.ColumnFamily)1 PartitionStorage (org.apache.ignite.internal.storage.PartitionStorage)1 SearchRow (org.apache.ignite.internal.storage.SearchRow)1 SortedIndexDescriptor (org.apache.ignite.internal.storage.index.SortedIndexDescriptor)1 RocksDbSortedIndexStorage (org.apache.ignite.internal.storage.rocksdb.index.RocksDbSortedIndexStorage)1