Search in sources :

Example 16 with ByteArray

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

the class WatchAggregatorTest method testHighExactAndLowerRangeCriteriaUnion.

@Test
public void testHighExactAndLowerRangeCriteriaUnion() {
    var watchAggregator = new WatchAggregator();
    watchAggregator.add(new ByteArray("key3"), null);
    watchAggregator.add(new ByteArray("key0"), new ByteArray("key2"), null);
    var keyCriterion = watchAggregator.watch(0, null).get().keyCriterion();
    var expKeyCriterion = new KeyCriterion.RangeCriterion(new ByteArray("key0"), new ByteArray("key4"));
    assertEquals(expKeyCriterion, keyCriterion);
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Example 17 with ByteArray

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

the class WatchAggregatorTest method testAllTypesOfCriteriaUnion.

@Test
public void testAllTypesOfCriteriaUnion() {
    var watchAggregator = new WatchAggregator();
    watchAggregator.add(new ByteArray("key0"), null);
    watchAggregator.add(new ByteArray("key1"), new ByteArray("key2"), null);
    watchAggregator.add(Arrays.asList(new ByteArray("key0"), new ByteArray("key3")), null);
    var keyCriterion = watchAggregator.watch(0, null).get().keyCriterion();
    var expKeyCriterion = new KeyCriterion.RangeCriterion(new ByteArray("key0"), new ByteArray("key4"));
    assertEquals(expKeyCriterion, keyCriterion);
}
Also used : ByteArray(org.apache.ignite.lang.ByteArray) WatchAggregator(org.apache.ignite.internal.metastorage.watch.WatchAggregator) Test(org.junit.jupiter.api.Test)

Example 18 with ByteArray

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

the class DistributedConfigurationStorage method write.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Boolean> write(Map<String, ? extends Serializable> newValues, long curChangeId) {
    assert curChangeId <= changeId.get();
    assert lsnr != null : "Configuration listener must be initialized before write.";
    if (curChangeId < changeId.get()) {
        // updates configuration.
        return CompletableFuture.completedFuture(false);
    }
    Set<Operation> operations = new HashSet<>();
    for (Map.Entry<String, ? extends Serializable> entry : newValues.entrySet()) {
        ByteArray key = new ByteArray(DISTRIBUTED_PREFIX + entry.getKey());
        if (entry.getValue() != null) {
            operations.add(Operations.put(key, ConfigurationSerializationUtil.toBytes(entry.getValue())));
        } else {
            operations.add(Operations.remove(key));
        }
    }
    operations.add(Operations.put(MASTER_KEY, ByteUtils.longToBytes(curChangeId)));
    // Condition for a valid MetaStorage data update. Several possibilities here:
    // - First update ever, MASTER_KEY property must be absent from MetaStorage.
    // - Current node has already performed some updates or received them from MetaStorage watch listener. In this
    // case "curChangeId" must match the MASTER_KEY revision exactly.
    // - Current node has been restarted and received updates from MetaStorage watch listeners after that. Same as
    // above, "curChangeId" must match the MASTER_KEY revision exactly.
    // - Current node has been restarted and have not received any updates from MetaStorage watch listeners yet.
    // In this case "curChangeId" matches APPLIED_REV, which may or may not match the MASTER_KEY revision. Two
    // options here:
    // - MASTER_KEY is missing in local MetaStorage copy. This means that current node have not performed nor
    // observed any configuration changes. Valid condition is "MASTER_KEY does not exist".
    // - MASTER_KEY is present in local MetaStorage copy. The MASTER_KEY revision is unknown but is less than or
    // equal to APPLIED_REV. Obviously, there have been no updates from the future yet. It's also guaranteed
    // that the next received configuration update will have the MASTER_KEY revision strictly greater than
    // current APPLIED_REV. This allows to conclude that "MASTER_KEY revision <= curChangeId" is a valid
    // condition for update.
    // Combining all of the above, it's concluded that the following condition must be used:
    SimpleCondition condition = curChangeId == 0L ? Conditions.notExists(MASTER_KEY) : Conditions.revision(MASTER_KEY).le(curChangeId);
    return metaStorageMgr.invoke(condition, operations, Set.of(Operations.noop()));
}
Also used : SimpleCondition(org.apache.ignite.internal.metastorage.client.SimpleCondition) ByteArray(org.apache.ignite.lang.ByteArray) Operation(org.apache.ignite.internal.metastorage.client.Operation) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 19 with ByteArray

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

the class LocalConfigurationStorage method write.

/**
 * {@inheritDoc}
 */
@Override
public synchronized CompletableFuture<Boolean> write(Map<String, ? extends Serializable> newValues, long sentVersion) {
    assert lsnr != null : "Configuration listener must be initialized before write.";
    if (sentVersion != ver.get()) {
        return CompletableFuture.completedFuture(false);
    }
    Map<ByteArray, byte[]> data = new HashMap<>();
    for (Map.Entry<String, ? extends Serializable> e : newValues.entrySet()) {
        ByteArray key = ByteArray.fromString(LOC_PREFIX + e.getKey());
        data.put(key, e.getValue() == null ? null : ConfigurationSerializationUtil.toBytes(e.getValue()));
    }
    Data entries = new Data(newValues, ver.incrementAndGet());
    // read the 'lsnr' field into a local variable, just in case, to avoid possible race condition on reading
    // it in a lambda below.
    ConfigurationStorageListener localLsnr = lsnr;
    return vaultMgr.putAll(data).thenCompose(v -> localLsnr.onEntriesChanged(entries)).thenApply(v -> true);
}
Also used : VaultManager(org.apache.ignite.internal.vault.VaultManager) ByteArray(org.apache.ignite.lang.ByteArray) Cursor(org.apache.ignite.internal.util.Cursor) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationType(org.apache.ignite.configuration.annotation.ConfigurationType) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Serializable(java.io.Serializable) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConfigurationSerializationUtil(org.apache.ignite.internal.configuration.util.ConfigurationSerializationUtil) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull) VaultEntry(org.apache.ignite.internal.vault.VaultEntry) HashMap(java.util.HashMap) ByteArray(org.apache.ignite.lang.ByteArray) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ByteArray

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

the class DistributedConfigurationStorageTest method mockMetaStorageManager.

/**
 * Creates a mock implementation of a {@link MetaStorageManager}.
 */
private MetaStorageManager mockMetaStorageManager() {
    var mock = mock(MetaStorageManager.class);
    when(mock.invoke(any(), anyCollection(), anyCollection())).thenAnswer(invocation -> {
        SimpleCondition condition = invocation.getArgument(0);
        Collection<Operation> success = invocation.getArgument(1);
        Collection<Operation> failure = invocation.getArgument(2);
        boolean invokeResult = metaStorage.invoke(toServerCondition(condition), success.stream().map(DistributedConfigurationStorageTest::toServerOperation).collect(toList()), failure.stream().map(DistributedConfigurationStorageTest::toServerOperation).collect(toList()));
        return CompletableFuture.completedFuture(invokeResult);
    });
    try {
        when(mock.range(any(), any())).thenAnswer(invocation -> {
            ByteArray keyFrom = invocation.getArgument(0);
            ByteArray keyTo = invocation.getArgument(1);
            return new CursorAdapter(metaStorage.range(keyFrom.bytes(), keyTo == null ? null : keyTo.bytes()));
        });
    } catch (NodeStoppingException e) {
        throw new RuntimeException(e);
    }
    return mock;
}
Also used : SimpleCondition(org.apache.ignite.internal.metastorage.client.SimpleCondition) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) ByteArray(org.apache.ignite.lang.ByteArray) Operation(org.apache.ignite.internal.metastorage.client.Operation)

Aggregations

ByteArray (org.apache.ignite.lang.ByteArray)51 Test (org.junit.jupiter.api.Test)35 Cursor (org.apache.ignite.internal.util.Cursor)13 List (java.util.List)12 WatchAggregator (org.apache.ignite.internal.metastorage.watch.WatchAggregator)12 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Map (java.util.Map)10 NotNull (org.jetbrains.annotations.NotNull)9 Collection (java.util.Collection)8 Iterator (java.util.Iterator)8 Collectors (java.util.stream.Collectors)8 AfterEach (org.junit.jupiter.api.AfterEach)8 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)8 Assertions.fail (org.junit.jupiter.api.Assertions.fail)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ByteBuffer (java.nio.ByteBuffer)7 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)7 ArrayList (java.util.ArrayList)7 NoSuchElementException (java.util.NoSuchElementException)7 Function.identity (java.util.function.Function.identity)7