Search in sources :

Example 1 with TestWriteOperation

use of org.infinispan.test.op.TestWriteOperation in project infinispan by infinispan.

the class CacheEntryCloudEventsTest method assertEntryEventSent.

private void assertEntryEventSent(Object key, Object value, TestWriteOperation op) {
    byte[] expectedKeyBytes = getKeyBytes(key);
    byte[] expectedValueBytes = getValueBytes(value);
    String type = translateType(op);
    Optional<ProducerRecord<byte[], byte[]>> record = mockSender.getProducer().history().stream().filter(r -> Arrays.equals(r.key(), expectedKeyBytes)).findFirst();
    assertTrue(record.isPresent());
    byte[] eventBytes = record.get().value();
    Json json = Json.read(new String(eventBytes));
    assertEquals("1.0", json.at(SPECVERSION).asString());
    assertEquals(type, json.at(TYPE).asString());
    String source = json.at(SOURCE).asString();
    assertTrue(source.startsWith("/infinispan"));
    assertTrue(source.endsWith("/" + CACHE_NAME));
    Instant.parse(json.at(TIME).asString());
    boolean keyIsBase64 = json.at(INFINISPAN_SUBJECT_ISBASE64, false).asBoolean();
    assertEquals(expectedContentType(keyIsBase64).toString(), json.at(INFINISPAN_SUBJECT_CONTENTTYPE, APPLICATION_JSON_TYPE).asString());
    String subject = json.at(SUBJECT).asString();
    byte[] keyBytes = keyIsBase64 ? Base64.getDecoder().decode(subject) : subject.getBytes(StandardCharsets.UTF_8);
    assertEquals(expectedKeyBytes, keyBytes);
    String data = json.at(DATA).asString();
    boolean valueIsBase64 = json.at(INFINISPAN_DATA_ISBASE64, false).asBoolean();
    assertEquals(expectedContentType(valueIsBase64).toString(), json.at(DATACONTENTTYPE, APPLICATION_JSON_TYPE).asString());
    byte[] valueBytes = valueIsBase64 ? Base64.getDecoder().decode(data) : data.getBytes(StandardCharsets.UTF_8);
    assertEquals(expectedValueBytes, valueBytes);
}
Also used : Arrays(java.util.Arrays) TestGlobalConfigurationBuilder(org.infinispan.commands.module.TestGlobalConfigurationBuilder) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) Cache(org.infinispan.Cache) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) AdvancedCache(org.infinispan.AdvancedCache) DATACONTENTTYPE(org.infinispan.cloudevents.impl.StructuredEventBuilder.DATACONTENTTYPE) APPLICATION_OBJECT(org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT) Map(java.util.Map) TestingUtil(org.infinispan.test.TestingUtil) MagicKey(org.infinispan.distribution.MagicKey) TestDataSCI(org.infinispan.test.TestDataSCI) KafkaEventSender(org.infinispan.cloudevents.impl.KafkaEventSender) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) INFINISPAN_SUBJECT_ISBASE64(org.infinispan.cloudevents.impl.StructuredEventBuilder.INFINISPAN_SUBJECT_ISBASE64) Base64(java.util.Base64) CompletionStage(java.util.concurrent.CompletionStage) Flag(org.infinispan.context.Flag) Optional(java.util.Optional) DataConversion(org.infinispan.encoding.DataConversion) INFINISPAN_SUBJECT_CONTENTTYPE(org.infinispan.cloudevents.impl.StructuredEventBuilder.INFINISPAN_SUBJECT_CONTENTTYPE) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) DataProvider(org.testng.annotations.DataProvider) TYPE(org.infinispan.cloudevents.impl.StructuredEventBuilder.TYPE) PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) INFINISPAN_DATA_ISBASE64(org.infinispan.cloudevents.impl.StructuredEventBuilder.INFINISPAN_DATA_ISBASE64) LinkedHashMap(java.util.LinkedHashMap) APPLICATION_UNKNOWN(org.infinispan.commons.dataconversion.MediaType.APPLICATION_UNKNOWN) MediaType(org.infinispan.commons.dataconversion.MediaType) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) DATA(org.infinispan.cloudevents.impl.StructuredEventBuilder.DATA) Address(org.infinispan.remoting.transport.Address) APPLICATION_PROTOSTREAM(org.infinispan.commons.dataconversion.MediaType.APPLICATION_PROTOSTREAM) StorageType(org.infinispan.configuration.cache.StorageType) APPLICATION_JSON(org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON) MultipleCacheManagersTest(org.infinispan.test.MultipleCacheManagersTest) TIME(org.infinispan.cloudevents.impl.StructuredEventBuilder.TIME) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) SUBJECT(org.infinispan.cloudevents.impl.StructuredEventBuilder.SUBJECT) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) SOURCE(org.infinispan.cloudevents.impl.StructuredEventBuilder.SOURCE) SPECVERSION(org.infinispan.cloudevents.impl.StructuredEventBuilder.SPECVERSION) Json(org.infinispan.commons.dataconversion.internal.Json) ExecutionException(java.util.concurrent.ExecutionException) APPLICATION_JSON_TYPE(org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON_TYPE) CacheMode(org.infinispan.configuration.cache.CacheMode) TestWriteOperation(org.infinispan.test.op.TestWriteOperation) CloudEventsGlobalConfigurationBuilder(org.infinispan.cloudevents.configuration.CloudEventsGlobalConfigurationBuilder) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 2 with TestWriteOperation

use of org.infinispan.test.op.TestWriteOperation in project infinispan by infinispan.

the class StateTransferOverwritingValueTest method doTest.

private void doTest(final TestWriteOperation op) throws Exception {
    // Test scenario:
    // cache0 is the only member in the cluster, cache1 joins
    // Key k is in the cache, and is transferred to cache1
    // A user operation/tx also modifies key k
    // The user tx must update k even if it doesn't find key k in the data container.
    final AdvancedCache<Object, Object> cache0 = advancedCache(0);
    final String key = "key";
    // Prepare for replace/remove: put a previous value in cache0
    final Object previousValue = op.getPreviousValue();
    if (previousValue != null) {
        cache0.put(key, previousValue);
        assertEquals(previousValue, cache0.get(key));
        log.tracef("Previous value inserted: %s = %s", key, previousValue);
    }
    int preJoinTopologyId = cache0.getDistributionManager().getCacheTopology().getTopologyId();
    // Block any state response commands on cache0
    CheckPoint checkPoint = new CheckPoint();
    ControlledRpcManager blockingRpcManager0 = replaceRpcManager(cache0);
    blockingRpcManager0.excludeCommands(WriteCommand.class, BackupWriteCommand.class, RevokeBiasCommand.class, InvalidateVersionsCommand.class, AbstractTransactionBoundaryCommand.class, TxCompletionNotificationCommand.class);
    int rebalanceTopologyId = preJoinTopologyId + 1;
    // Block the rebalance confirmation on cache0
    blockRebalanceConfirmation(manager(0), checkPoint, rebalanceTopologyId);
    // Start the joiner
    log.tracef("Starting the cache on the joiner");
    ConfigurationBuilder c = getConfigurationBuilder();
    c.clustering().stateTransfer().awaitInitialTransfer(false).timeout(30, SECONDS);
    addClusterEnabledCacheManager(c);
    final AdvancedCache<Object, Object> cache1 = advancedCache(1);
    // Wait for joiner to finish requesting segments, so that write commands are not blocked
    StateTransferLock stateTransferLock1 = TestingUtil.extractComponent(cache1, StateTransferLock.class);
    stateTransferLock1.transactionDataFuture(rebalanceTopologyId).toCompletableFuture().get(10, SECONDS);
    assertEquals(2, cache1.getRpcManager().getMembers().size());
    // Every PutKeyValueCommand will be blocked before committing the entry on cache1
    CyclicBarrier beforeCommitCache1Barrier = new CyclicBarrier(2);
    // Scattered cache mode uses only PKVC or RemoveCommands for backup
    BlockingInterceptor<?> blockingInterceptor1 = new BlockingInterceptor<>(beforeCommitCache1Barrier, true, false, cacheMode.isScattered() ? t -> t instanceof PutKeyValueCommand || t instanceof RemoveCommand : t -> t.getClass().equals(op.getCommandClass()));
    AsyncInterceptorChain interceptorChain1 = TestingUtil.extractInterceptorChain(cache1);
    Class<? extends EntryWrappingInterceptor> ewi = interceptorChain1.findInterceptorExtending(EntryWrappingInterceptor.class).getClass();
    assertTrue(interceptorChain1.addInterceptorAfter(blockingInterceptor1, ewi));
    // Wait for cache0 to collect the state to send to cache1 (including our previous value).
    ControlledRpcManager.BlockedRequest<?> blockedStateResponse = blockingRpcManager0.expectCommand(StateResponseCommand.class);
    // Put/Replace/Remove from cache0 with cache0 as primary owner, cache1 will become a backup owner for the retry
    // The put command will be blocked on cache1 just before committing the entry.
    Future<Object> future = fork(() -> op.perform(cache0, key));
    // Wait for the entry to be wrapped on cache1
    beforeCommitCache1Barrier.await(10, TimeUnit.SECONDS);
    // Stop blocking, otherwise we'll block the state transfer put commands as well
    blockingInterceptor1.suspend(true);
    // Allow the state to be applied on cache1 (writing the old value for our entry)
    blockedStateResponse.send().receiveAll();
    if (cacheMode.isScattered()) {
        blockingRpcManager0.expectCommand(StateResponseCommand.class).send().receiveAll();
    }
    // Wait for cache1 to finish applying the state, but don't allow the rebalance confirmation to be processed.
    // (It would change the topology and it would trigger a retry for the command.)
    checkPoint.awaitStrict("pre_rebalance_confirmation_" + rebalanceTopologyId + "_from_" + address(1), 10, SECONDS);
    // Now allow the command to commit on cache1
    beforeCommitCache1Barrier.await(10, TimeUnit.SECONDS);
    // Wait for the command to finish and check that it didn't fail
    Object result = future.get(10, TimeUnit.SECONDS);
    assertEquals(op.getReturnValue(), result);
    log.tracef("%s operation is done", op);
    // Allow the rebalance confirmation to proceed and wait for the topology to change everywhere
    checkPoint.trigger("resume_rebalance_confirmation_" + rebalanceTopologyId + "_from_" + address(0));
    checkPoint.trigger("resume_rebalance_confirmation_" + rebalanceTopologyId + "_from_" + address(1));
    TestingUtil.waitForNoRebalance(cache0, cache1);
    // Check the value on all the nodes
    assertEquals(op.getValue(), cache0.get(key));
    assertEquals(op.getValue(), cache1.get(key));
    blockingRpcManager0.stopBlocking();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) WriteCommand(org.infinispan.commands.write.WriteCommand) InvalidateVersionsCommand(org.infinispan.commands.write.InvalidateVersionsCommand) EntryWrappingInterceptor(org.infinispan.interceptors.impl.EntryWrappingInterceptor) CheckPoint(org.infinispan.test.fwk.CheckPoint) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Test(org.testng.annotations.Test) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) Answer(org.mockito.stubbing.Answer) Future(java.util.concurrent.Future) AdvancedCache(org.infinispan.AdvancedCache) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestingUtil(org.infinispan.test.TestingUtil) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Address(org.infinispan.remoting.transport.Address) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) RemoveCommand(org.infinispan.commands.write.RemoveCommand) CyclicBarrier(java.util.concurrent.CyclicBarrier) MultipleCacheManagersTest(org.infinispan.test.MultipleCacheManagersTest) ClusterTopologyManager(org.infinispan.topology.ClusterTopologyManager) StateResponseCommand(org.infinispan.commands.statetransfer.StateResponseCommand) LockingMode(org.infinispan.transaction.LockingMode) AbstractTransactionBoundaryCommand(org.infinispan.commands.tx.AbstractTransactionBoundaryCommand) ControlledRpcManager(org.infinispan.util.ControlledRpcManager) NoOpGlobalConfigurationManager(org.infinispan.globalstate.NoOpGlobalConfigurationManager) AdditionalAnswers(org.mockito.AdditionalAnswers) TimeUnit(java.util.concurrent.TimeUnit) ControlledRpcManager.replaceRpcManager(org.infinispan.util.ControlledRpcManager.replaceRpcManager) TxCompletionNotificationCommand(org.infinispan.commands.remote.recovery.TxCompletionNotificationCommand) CacheMode(org.infinispan.configuration.cache.CacheMode) IsolationLevel(org.infinispan.util.concurrent.IsolationLevel) BackupWriteCommand(org.infinispan.commands.triangle.BackupWriteCommand) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) TestWriteOperation(org.infinispan.test.op.TestWriteOperation) RevokeBiasCommand(org.infinispan.commands.remote.RevokeBiasCommand) Mockito.withSettings(org.mockito.Mockito.withSettings) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) RemoveCommand(org.infinispan.commands.write.RemoveCommand) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) BlockingInterceptor(org.infinispan.distribution.BlockingInterceptor) AsyncInterceptorChain(org.infinispan.interceptors.AsyncInterceptorChain) EntryWrappingInterceptor(org.infinispan.interceptors.impl.EntryWrappingInterceptor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CheckPoint(org.infinispan.test.fwk.CheckPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) ControlledRpcManager(org.infinispan.util.ControlledRpcManager) CheckPoint(org.infinispan.test.fwk.CheckPoint) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Aggregations

SECONDS (java.util.concurrent.TimeUnit.SECONDS)2 AdvancedCache (org.infinispan.AdvancedCache)2 CacheMode (org.infinispan.configuration.cache.CacheMode)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 StandardCharsets (java.nio.charset.StandardCharsets)1 Instant (java.time.Instant)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 Cache (org.infinispan.Cache)1