Search in sources :

Example 1 with VaultEntry

use of org.apache.ignite.internal.vault.VaultEntry in project ignite-3 by apache.

the class ItPersistencePropertiesVaultServiceTest method testPersistentRestart.

/**
 * Tests that the Vault Service correctly persists data after multiple service restarts.
 */
@Test
void testPersistentRestart() throws Exception {
    var data = Map.of(new ByteArray("key" + 1), fromString("value" + 1), new ByteArray("key" + 2), fromString("value" + 2), new ByteArray("key" + 3), fromString("value" + 3));
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        vaultService.putAll(data).get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        assertThat(vaultService.get(new ByteArray("key" + 1)), willBe(equalTo(new VaultEntry(new ByteArray("key" + 1), fromString("value" + 1)))));
    }
    try (var vaultService = new PersistentVaultService(vaultDir)) {
        vaultService.start();
        try (var cursor = vaultService.range(new ByteArray("key" + 1), new ByteArray("key" + 4))) {
            var actualData = new ArrayList<VaultEntry>();
            cursor.forEachRemaining(actualData::add);
            List<VaultEntry> expectedData = data.entrySet().stream().map(e -> new VaultEntry(e.getKey(), e.getValue())).sorted(Comparator.comparing(VaultEntry::key)).collect(Collectors.toList());
            assertThat(actualData, is(expectedData));
        }
    }
}
Also used : VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ArrayList(java.util.ArrayList) ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 2 with VaultEntry

use of org.apache.ignite.internal.vault.VaultEntry in project ignite-3 by apache.

the class DistributedConfigurationStorage method readAll.

/**
 * {@inheritDoc}
 */
@Override
public Data readAll() throws StorageException {
    var data = new HashMap<String, Serializable>();
    VaultEntry appliedRevEntry = vaultMgr.get(MetaStorageManager.APPLIED_REV).join();
    long appliedRevision = appliedRevEntry.value() == null ? 0L : ByteUtils.bytesToLong(appliedRevEntry.value());
    try (Cursor<VaultEntry> entries = storedDistributedConfigKeys()) {
        for (VaultEntry entry : entries) {
            ByteArray key = entry.key();
            byte[] value = entry.value();
            // vault iterator should not return nulls as values
            assert value != null;
            if (key.equals(MASTER_KEY)) {
                continue;
            }
            String dataKey = key.toString().substring(DISTRIBUTED_PREFIX.length());
            data.put(dataKey, ConfigurationSerializationUtil.fromBytes(value));
        }
    } catch (Exception e) {
        throw new StorageException("Exception when closing a Vault cursor", e);
    }
    assert data.isEmpty() || appliedRevision > 0;
    changeId.set(data.isEmpty() ? 0 : appliedRevision);
    return new Data(data, appliedRevision);
}
Also used : HashMap(java.util.HashMap) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ByteArray(org.apache.ignite.lang.ByteArray)

Example 3 with VaultEntry

use of org.apache.ignite.internal.vault.VaultEntry in project ignite-3 by apache.

the class LocalConfigurationStorage method readAll.

/**
 * Retrieves all data, which keys lie in between {@code [rangeStart, rangeEnd)}.
 */
private Data readAll(ByteArray rangeStart, ByteArray rangeEnd) {
    var data = new HashMap<String, Serializable>();
    try (Cursor<VaultEntry> cursor = vaultMgr.range(rangeStart, rangeEnd)) {
        for (VaultEntry entry : cursor) {
            String key = entry.key().toString().substring(LOC_PREFIX.length());
            byte[] value = entry.value();
            // vault iterator should not return nulls as values
            assert value != null;
            data.put(key, ConfigurationSerializationUtil.fromBytes(value));
        }
    } catch (Exception e) {
        throw new StorageException("Exception when closing a Vault cursor", e);
    }
    // TODO: https://issues.apache.org/jira/browse/IGNITE-14697
    return new Data(data, ver.get());
}
Also used : HashMap(java.util.HashMap) VaultEntry(org.apache.ignite.internal.vault.VaultEntry)

Example 4 with VaultEntry

use of org.apache.ignite.internal.vault.VaultEntry in project ignite-3 by apache.

the class PersistentVaultService method range.

/**
 * {@inheritDoc}
 */
@Override
@NotNull
public Cursor<VaultEntry> range(@NotNull ByteArray fromKey, @NotNull ByteArray toKey) {
    var readOpts = new ReadOptions();
    var upperBound = new Slice(toKey.bytes());
    readOpts.setIterateUpperBound(upperBound);
    RocksIterator it = db.newIterator(readOpts);
    it.seek(fromKey.bytes());
    return new RocksIteratorAdapter<>(it) {

        @Override
        protected VaultEntry decodeEntry(byte[] key, byte[] value) {
            return new VaultEntry(new ByteArray(key), value);
        }

        @Override
        public void close() throws Exception {
            super.close();
            IgniteUtils.closeAll(upperBound, readOpts);
        }
    };
}
Also used : ReadOptions(org.rocksdb.ReadOptions) Slice(org.rocksdb.Slice) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) ByteArray(org.apache.ignite.lang.ByteArray) RocksIterator(org.rocksdb.RocksIterator) RocksIteratorAdapter(org.apache.ignite.internal.rocksdb.RocksIteratorAdapter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

VaultEntry (org.apache.ignite.internal.vault.VaultEntry)4 ByteArray (org.apache.ignite.lang.ByteArray)3 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 RocksIteratorAdapter (org.apache.ignite.internal.rocksdb.RocksIteratorAdapter)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.jupiter.api.Test)1 ReadOptions (org.rocksdb.ReadOptions)1 RocksIterator (org.rocksdb.RocksIterator)1 Slice (org.rocksdb.Slice)1