Skip to content

Commit 63cfb6e

Browse files
IGNITE-27420 Fix WAL enable/disable command unable to modify groups with multiple caches (#12601)
1 parent 8478994 commit 63cfb6e

File tree

7 files changed

+157
-27
lines changed

7 files changed

+157
-27
lines changed

modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerWalTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Objects;
2121
import java.util.regex.Pattern;
2222
import org.apache.ignite.cluster.ClusterState;
23+
import org.apache.ignite.configuration.CacheConfiguration;
2324
import org.apache.ignite.configuration.DataRegionConfiguration;
2425
import org.apache.ignite.configuration.DataStorageConfiguration;
2526
import org.apache.ignite.configuration.IgniteConfiguration;
@@ -171,6 +172,36 @@ public void testWalStateInMemoryCdcCluster() throws Exception {
171172
assertFalse(testOut.toString().contains("cache3"));
172173
}
173174

175+
/**
176+
* Test WAL mode change for a cache group contains multiple caches.
177+
* @throws Exception If failed.
178+
*/
179+
@Test
180+
public void testWalChangeForMultiCacheGroup() throws Exception {
181+
clusterState = 0; // PDS cluster.
182+
183+
IgniteEx srv = startGrids(2);
184+
srv.cluster().state(ClusterState.ACTIVE);
185+
186+
srv.createCache(new CacheConfiguration<>("cache1")
187+
.setGroupName("testGroup"));
188+
srv.createCache(new CacheConfiguration<>("cache2")
189+
.setGroupName("testGroup"));
190+
191+
assertEquals(EXIT_CODE_OK, execute("--wal", "state", "--groups", "testGroup"));
192+
outputContains(".*testGroup.*true.*true.*true.*true.*false");
193+
194+
assertEquals(EXIT_CODE_OK, execute("--wal", "disable", "--groups", "testGroup"));
195+
196+
assertEquals(EXIT_CODE_OK, execute("--wal", "state", "--groups", "testGroup"));
197+
outputContains(".*testGroup.*true.*false.*true.*true.*false");
198+
199+
assertEquals(EXIT_CODE_OK, execute("--wal", "enable", "--groups", "testGroup"));
200+
201+
assertEquals(EXIT_CODE_OK, execute("--wal", "state", "--groups", "testGroup"));
202+
outputContains(".*testGroup.*true.*true.*true.*true.*false");
203+
}
204+
174205
/** */
175206
private void outputContains(String regexp) {
176207
assertTrue(Pattern.compile(regexp).matcher(testOut.toString()).find());

modules/core/src/main/java/org/apache/ignite/IgniteCluster.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,13 @@ public IgniteFuture<Collection<ClusterStartNodeResult>> startNodesAsync(Collecti
536536
* Cache may be stuck in inconsistent state due to violation of these conditions. It is advised to destroy
537537
* such cache.
538538
*
539-
* @param cacheName Cache name.
539+
* @param cacheOrGrpName Cache or cache group name.
540540
* @return Whether WAL disabled by this call.
541541
* @throws IgniteException If error occurs.
542542
* @see #enableWal(String)
543543
* @see #isWalEnabled(String)
544544
*/
545-
public boolean disableWal(String cacheName) throws IgniteException;
545+
public boolean disableWal(String cacheOrGrpName) throws IgniteException;
546546

547547
/**
548548
* Enables write-ahead logging for specified cache. Restoring crash-recovery guarantees of a previous call to
@@ -560,13 +560,13 @@ public IgniteFuture<Collection<ClusterStartNodeResult>> startNodesAsync(Collecti
560560
* Cache may be stuck in inconsistent state due to violation of these conditions. It is advised to destroy
561561
* such cache.
562562
*
563-
* @param cacheName Cache name.
563+
* @param cacheOrGrpName Cache or cache group name.
564564
* @return Whether WAL enabled by this call.
565565
* @throws IgniteException If error occurs.
566566
* @see #disableWal(String)
567567
* @see #isWalEnabled(String)
568568
*/
569-
public boolean enableWal(String cacheName) throws IgniteException;
569+
public boolean enableWal(String cacheOrGrpName) throws IgniteException;
570570

571571
/**
572572
* Checks if write-ahead logging is enabled for specified cache.

modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ public IgniteClusterAsyncImpl(IgniteClusterImpl cluster) {
345345
}
346346

347347
/** {@inheritDoc} */
348-
@Override public boolean enableWal(String cacheName) throws IgniteException {
349-
return cluster.enableWal(cacheName);
348+
@Override public boolean enableWal(String cacheOrGrpName) throws IgniteException {
349+
return cluster.enableWal(cacheOrGrpName);
350350
}
351351

352352
/** {@inheritDoc} */
353-
@Override public boolean disableWal(String cacheName) throws IgniteException {
354-
return cluster.disableWal(cacheName);
353+
@Override public boolean disableWal(String cacheOrGrpName) throws IgniteException {
354+
return cluster.disableWal(cacheOrGrpName);
355355
}
356356

357357
/** {@inheritDoc} */

modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterImpl.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.ignite.internal.GridKernalContext;
5353
import org.apache.ignite.internal.IgniteComponentType;
5454
import org.apache.ignite.internal.IgniteInternalFuture;
55+
import org.apache.ignite.internal.processors.cache.CacheGroupDescriptor;
5556
import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor;
5657
import org.apache.ignite.internal.processors.cluster.BaselineTopology;
5758
import org.apache.ignite.internal.processors.cluster.baseline.autoadjust.BaselineAutoAdjustStatus;
@@ -631,29 +632,44 @@ private void setBaselineTopology(long topVer, boolean isBaselineAutoAdjust) {
631632
}
632633

633634
/** {@inheritDoc} */
634-
@Override public boolean enableWal(String cacheName) throws IgniteException {
635-
return changeWalMode(cacheName, true);
635+
@Override public boolean enableWal(String cacheOrGrpName) throws IgniteException {
636+
return changeWalMode(cacheOrGrpName, true);
636637
}
637638

638639
/** {@inheritDoc} */
639-
@Override public boolean disableWal(String cacheName) throws IgniteException {
640-
return changeWalMode(cacheName, false);
640+
@Override public boolean disableWal(String cacheOrGrpName) throws IgniteException {
641+
return changeWalMode(cacheOrGrpName, false);
641642
}
642643

643644
/**
644645
* Change WAL mode.
645646
*
646-
* @param cacheName Cache name.
647+
* @param cacheOrGrpName Cache or cache group name.
647648
* @param enabled Enabled flag.
648649
* @return {@code True} if WAL mode was changed as a result of this call.
649650
*/
650-
private boolean changeWalMode(String cacheName, boolean enabled) {
651-
A.notNull(cacheName, "cacheName");
651+
private boolean changeWalMode(String cacheOrGrpName, boolean enabled) {
652+
A.notNull(cacheOrGrpName, "cacheOrGrpName");
652653

653654
guard();
654655

655656
try {
656-
return ctx.cache().context().walState().changeWalMode(Collections.singleton(cacheName), enabled).get();
657+
Collection<String> cacheNames;
658+
659+
int cacheOrGrpId = CU.cacheId(cacheOrGrpName);
660+
CacheGroupDescriptor grpDesc = ctx.cache().cacheGroupDescriptor(cacheOrGrpId);
661+
662+
if (grpDesc != null) {
663+
Map<String, Integer> cachesInGrp = grpDesc.caches();
664+
if (cachesInGrp.isEmpty())
665+
throw new IgniteException("Cache group '" + cacheOrGrpName + "' does not contain any caches.");
666+
else
667+
cacheNames = cachesInGrp.keySet();
668+
}
669+
else
670+
cacheNames = Collections.singleton(cacheOrGrpName);
671+
672+
return ctx.cache().context().walState().changeWalMode(cacheNames, enabled).get();
657673
}
658674
catch (IgniteCheckedException e) {
659675
throw U.convertException(e);

modules/core/src/main/java/org/apache/ignite/internal/management/wal/WalSetStateTask.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.ignite.internal.management.wal.WalDisableCommand.WalDisableCommandArg;
2727
import org.apache.ignite.internal.management.wal.WalEnableCommand.WalEnableCommandArg;
2828
import org.apache.ignite.internal.processors.cache.CacheGroupContext;
29-
import org.apache.ignite.internal.processors.cache.GridCacheContext;
3029
import org.apache.ignite.internal.util.typedef.F;
3130
import org.apache.ignite.internal.visor.VisorJob;
3231
import org.apache.ignite.internal.visor.VisorMultiNodeTask;
@@ -67,15 +66,10 @@ protected WalDisableJob(@Nullable WalDisableCommandArg arg, boolean debug) {
6766
if (grps != null && !grps.contains(grpName))
6867
continue;
6968

70-
GridCacheContext<?, ?> cctx = F.first(gctx.caches());
71-
72-
if (cctx == null)
73-
continue;
74-
7569
if (arg instanceof WalEnableCommandArg)
76-
ignite.cluster().enableWal(cctx.name());
70+
ignite.cluster().enableWal(grpName);
7771
else
78-
ignite.cluster().disableWal(cctx.name());
72+
ignite.cluster().disableWal(grpName);
7973
}
8074

8175
return null;

modules/core/src/test/java/org/apache/ignite/internal/processors/cache/WalModeChangeAdvancedSelfTest.java

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.ignite.Ignite;
2828
import org.apache.ignite.IgniteCache;
2929
import org.apache.ignite.IgniteException;
30+
import org.apache.ignite.configuration.CacheConfiguration;
3031
import org.apache.ignite.internal.IgniteClientReconnectAbstractTest;
3132
import org.apache.ignite.internal.IgniteEx;
3233
import org.apache.ignite.internal.IgniteInternalFuture;
@@ -566,7 +567,8 @@ public void testCacheDestroy() throws Exception {
566567
String msg = e.getMessage();
567568

568569
assert msg.startsWith("Cache doesn't exist") ||
569-
msg.startsWith("Failed to change WAL mode because some caches no longer exist") :
570+
msg.startsWith("Failed to change WAL mode because some caches no longer exist") ||
571+
msg.startsWith("Cache group") && msg.contains("does not contain any caches") :
570572
e.getMessage();
571573
}
572574
finally {
@@ -699,4 +701,91 @@ private static void checkConcurrentOperations(AtomicBoolean done, Ignite node) {
699701
throw new RuntimeException(e);
700702
}
701703
}
704+
705+
/**
706+
* Test that WAL mode change for caches from the same group requires all caches to be specified.
707+
*
708+
* @throws Exception If failed.
709+
*/
710+
@Test
711+
public void testWalModeChangeRequiresAllGroupCaches() throws Exception {
712+
IgniteEx srv = startGrid(config(SRV_1, false, false));
713+
714+
srv.cluster().state(ACTIVE);
715+
716+
CacheConfiguration<Integer, Integer> cacheCfg1 = cacheConfig(CACHE_NAME, PARTITIONED, TRANSACTIONAL);
717+
CacheConfiguration<Integer, Integer> cacheCfg2 = cacheConfig(CACHE_NAME_2, PARTITIONED, TRANSACTIONAL);
718+
719+
cacheCfg1.setGroupName("testGroup");
720+
cacheCfg2.setGroupName("testGroup");
721+
722+
srv.getOrCreateCache(cacheCfg1);
723+
srv.getOrCreateCache(cacheCfg2);
724+
725+
assertForAllNodes(CACHE_NAME, true);
726+
assertForAllNodes(CACHE_NAME_2, true);
727+
728+
assertThrows(
729+
() -> {
730+
srv.cluster().disableWal(CACHE_NAME);
731+
return null;
732+
},
733+
IgniteException.class,
734+
"Cannot change WAL mode because not all cache names belonging to the group are provided"
735+
);
736+
737+
assertThrows(
738+
() -> {
739+
srv.cluster().disableWal(CACHE_NAME_2);
740+
return null;
741+
},
742+
IgniteException.class,
743+
"Cannot change WAL mode because not all cache names belonging to the group are provided"
744+
);
745+
746+
assertForAllNodes(CACHE_NAME, true);
747+
assertForAllNodes(CACHE_NAME_2, true);
748+
749+
assertThrows(
750+
() -> {
751+
srv.cluster().enableWal(CACHE_NAME);
752+
return null;
753+
},
754+
IgniteException.class,
755+
"Cannot change WAL mode because not all cache names belonging to the group are provided"
756+
);
757+
758+
srv.cluster().disableWal("testGroup");
759+
760+
assertForAllNodes(CACHE_NAME, false);
761+
assertForAllNodes(CACHE_NAME_2, false);
762+
763+
assertThrows(
764+
() -> {
765+
srv.cluster().enableWal(CACHE_NAME_2);
766+
return null;
767+
},
768+
IgniteException.class,
769+
"Cannot change WAL mode because not all cache names belonging to the group are provided"
770+
);
771+
772+
srv.cluster().enableWal("testGroup");
773+
774+
assertForAllNodes(CACHE_NAME, true);
775+
assertForAllNodes(CACHE_NAME_2, true);
776+
777+
srv.destroyCache(CACHE_NAME_2);
778+
779+
srv.cluster().disableWal(CACHE_NAME);
780+
assertForAllNodes(CACHE_NAME, false);
781+
782+
srv.cluster().enableWal("testGroup");
783+
assertForAllNodes(CACHE_NAME, true);
784+
785+
srv.cluster().disableWal("testGroup");
786+
assertForAllNodes(CACHE_NAME, false);
787+
788+
srv.cluster().enableWal(CACHE_NAME);
789+
assertForAllNodes(CACHE_NAME, true);
790+
}
702791
}

modules/core/src/test/java/org/apache/ignite/testframework/junits/multijvm/IgniteClusterProcessProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ public IgniteClusterProcessProxy(IgniteProcessProxy proxy) {
168168
}
169169

170170
/** {@inheritDoc} */
171-
@Override public boolean enableWal(String cacheName) throws IgniteException {
171+
@Override public boolean enableWal(String cacheOrGrpName) throws IgniteException {
172172
throw new UnsupportedOperationException("Operation is not supported yet.");
173173
}
174174

175175
/** {@inheritDoc} */
176-
@Override public boolean disableWal(String cacheName) throws IgniteException {
176+
@Override public boolean disableWal(String cacheOrGrpName) throws IgniteException {
177177
throw new UnsupportedOperationException("Operation is not supported yet.");
178178
}
179179

0 commit comments

Comments
 (0)