Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-23110 Disallow IgniteCache#clear() within transaction #11559

Merged
merged 15 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,14 @@ public <T> IgniteFuture<Map<K, EntryProcessorResult<T>>> invokeAllAsync(
*/
public IgniteFuture<Void> removeAllAsync();

/** {@inheritDoc} */
/**
* Clears the contents of the cache, without notifying listeners or {@link CacheWriter}s.
* Entries are cleared only if they are not currently locked, and are not participating in a transaction.
* This operation is not transactional. It calls broadcast closure that deletes all primary keys from remote nodes.
*
* @throws IllegalStateException if the cache is {@link #isClosed()}.
* @throws CacheException if there is a problem during the clear.
*/
@IgniteAsyncSupported
@Override public void clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.function.Function;
import java.util.function.Supplier;
import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
Expand Down Expand Up @@ -1059,6 +1060,10 @@ public List<GridCacheClearAllRunnable<K, V>> splitClearLocally(boolean srv, bool

/** {@inheritDoc} */
@Override public void clear() throws IgniteCheckedException {
if (ctx.transactional() && ctx.grid().transactions().tx() != null)
J-Bakuli marked this conversation as resolved.
Show resolved Hide resolved
J-Bakuli marked this conversation as resolved.
Show resolved Hide resolved
throw new CacheException("Failed to invoke a non-transactional operation within a transaction: " +
"IgniteCache.clear().");

clear((Set<? extends K>)null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.internal.processors.cache.transactions;

import javax.cache.CacheException;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.Test;

import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
import static org.apache.ignite.testframework.GridTestUtils.assertThrows;

/** Checks that non-transactional cache operations fail within a transaction. */
public class NonTransactionalOperationsInTxTest extends GridCommonAbstractTest {
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(gridName)
J-Bakuli marked this conversation as resolved.
Show resolved Hide resolved
.setCacheConfiguration(defaultCacheConfiguration().setAtomicityMode(TRANSACTIONAL));

return cfg;
}

/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
stopAllGrids();
}

/** */
@Test
public void testIgniteCacheClear() throws Exception {
startGrid(0);

checkIgniteCacheClear(grid(0));
}

/** */
@Test
public void testIgniteCacheClearOnClientNode() throws Exception {
startGrid(0);

startClientGrid(1);

checkIgniteCacheClear(grid(1));
}

/**
* It should throw exception.
* @param ignite Ignite.
*/
private void checkIgniteCacheClear(IgniteEx ignite) {
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);

cache.put(1, 1);

assertThrows(null,
() -> doInTransaction(ignite, () -> {
cache.put(2, 2);

cache.clear();

return null;
}),
CacheException.class,
"Failed to invoke a non-transactional operation within a transaction"
);

assertTrue(cache.containsKey(1));

assertFalse(cache.containsKey(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.ignite.internal.processors.cache.persistence.pagemem.ProgressSpeedCalculationTest;
import org.apache.ignite.internal.processors.cache.persistence.pagemem.SegmentedLruPageListTest;
import org.apache.ignite.internal.processors.cache.transactions.AtomicOperationsInTxTest;
import org.apache.ignite.internal.processors.cache.transactions.NonTransactionalOperationsInTxTest;
import org.apache.ignite.internal.processors.cache.transactions.TransactionIntegrityWithSystemWorkerDeathTest;
import org.apache.ignite.internal.processors.cluster.BaselineAutoAdjustMXBeanTest;
import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationInMemoryTest;
Expand Down Expand Up @@ -166,6 +167,7 @@
ExchangeWorkerWaitingForTaskTest.class,

AtomicOperationsInTxTest.class,
NonTransactionalOperationsInTxTest.class,

RebalanceWithDifferentThreadPoolSizeTest.class,

Expand Down