|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.cdc; |
| 19 | + |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.concurrent.CountDownLatch; |
| 24 | +import java.util.concurrent.ThreadLocalRandom; |
| 25 | +import org.apache.ignite.Ignite; |
| 26 | +import org.apache.ignite.binary.BinaryType; |
| 27 | +import org.apache.ignite.configuration.CacheConfiguration; |
| 28 | +import org.apache.ignite.configuration.DataRegionConfiguration; |
| 29 | +import org.apache.ignite.configuration.DataStorageConfiguration; |
| 30 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 31 | +import org.apache.ignite.internal.IgniteInternalFuture; |
| 32 | +import org.apache.ignite.internal.cdc.CdcMain; |
| 33 | +import org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree; |
| 34 | +import org.apache.ignite.internal.util.typedef.internal.CU; |
| 35 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 36 | +import org.apache.ignite.metric.MetricRegistry; |
| 37 | +import org.apache.ignite.testframework.GridTestUtils; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +import static org.apache.ignite.testframework.GridTestUtils.runAsync; |
| 41 | + |
| 42 | +/** */ |
| 43 | +public class CorruptedCdcConsumerStateTest extends AbstractCdcTest { |
| 44 | + /** {@inheritDoc} */ |
| 45 | + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| 46 | + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); |
| 47 | + |
| 48 | + cfg.setCacheConfiguration(new CacheConfiguration<>(DEFAULT_CACHE_NAME)); |
| 49 | + |
| 50 | + cfg.setDataStorageConfiguration( |
| 51 | + new DataStorageConfiguration() |
| 52 | + .setWalForceArchiveTimeout(100) |
| 53 | + .setDefaultDataRegionConfiguration( |
| 54 | + new DataRegionConfiguration().setCdcEnabled(true) |
| 55 | + )); |
| 56 | + |
| 57 | + return cfg; |
| 58 | + } |
| 59 | + |
| 60 | + /** */ |
| 61 | + @Test |
| 62 | + public void testCdcMainClearsCorruptedFiles() throws Exception { |
| 63 | + try (Ignite ign = startGrid(0)) { |
| 64 | + CountDownLatch sendCacheLatch = new CountDownLatch(1); |
| 65 | + |
| 66 | + CdcMain cdc = createCdc(new TestCdcConsumer(sendCacheLatch), ign.configuration()); |
| 67 | + |
| 68 | + IgniteInternalFuture<?> cdcFut = runAsync(cdc); |
| 69 | + |
| 70 | + // Force writing consumer state to ./state directory. |
| 71 | + try { |
| 72 | + ign.getOrCreateCache(DEFAULT_CACHE_NAME).put(0, 0); |
| 73 | + |
| 74 | + U.await(sendCacheLatch); |
| 75 | + } |
| 76 | + finally { |
| 77 | + cdcFut.cancel(); |
| 78 | + } |
| 79 | + |
| 80 | + // Corrupt data. |
| 81 | + NodeFileTree ft = GridTestUtils.getFieldValue(cdc, "ft"); |
| 82 | + |
| 83 | + Path cdcCacheState = ft.cdcCachesState(); |
| 84 | + |
| 85 | + byte[] corrupted = new byte[10]; |
| 86 | + ThreadLocalRandom.current().nextBytes(corrupted); |
| 87 | + |
| 88 | + Files.write(cdcCacheState, corrupted); |
| 89 | + |
| 90 | + sendCacheLatch = new CountDownLatch(1); |
| 91 | + |
| 92 | + cdcFut = runAsync(createCdc(new TestCdcConsumer(sendCacheLatch), ign.configuration())); |
| 93 | + cdcFut.listen(sendCacheLatch::countDown); |
| 94 | + |
| 95 | + // Force writing consumer state to ./state directory. |
| 96 | + try { |
| 97 | + ign.getOrCreateCache(DEFAULT_CACHE_NAME).put(0, 0); |
| 98 | + |
| 99 | + U.await(sendCacheLatch); |
| 100 | + |
| 101 | + assertFalse(cdcFut.isDone()); |
| 102 | + assertNull(cdcFut.error()); |
| 103 | + } |
| 104 | + finally { |
| 105 | + cdcFut.cancel(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** */ |
| 111 | + private static final class TestCdcConsumer implements CdcConsumer { |
| 112 | + /** */ |
| 113 | + private final CountDownLatch latch; |
| 114 | + |
| 115 | + /** */ |
| 116 | + TestCdcConsumer(CountDownLatch latch) { |
| 117 | + this.latch = latch; |
| 118 | + } |
| 119 | + |
| 120 | + /** */ |
| 121 | + @Override public void start(MetricRegistry mreg) { |
| 122 | + // No-op. |
| 123 | + } |
| 124 | + |
| 125 | + /** */ |
| 126 | + @Override public boolean onEvents(Iterator<CdcEvent> evts) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + /** */ |
| 131 | + @Override public void onTypes(Iterator<BinaryType> types) { |
| 132 | + types.forEachRemaining(t -> {}); |
| 133 | + } |
| 134 | + |
| 135 | + /** */ |
| 136 | + @Override public void onMappings(Iterator<TypeMapping> mappings) { |
| 137 | + mappings.forEachRemaining(t -> {}); |
| 138 | + } |
| 139 | + |
| 140 | + /** */ |
| 141 | + @Override public void onCacheChange(Iterator<CdcCacheEvent> cacheEvts) { |
| 142 | + cacheEvts.forEachRemaining(e -> { |
| 143 | + if (e.cacheId() == CU.cacheId(DEFAULT_CACHE_NAME)) |
| 144 | + latch.countDown(); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + /** */ |
| 149 | + @Override public void onCacheDestroy(Iterator<Integer> caches) { |
| 150 | + caches.forEachRemaining(c -> {}); |
| 151 | + } |
| 152 | + |
| 153 | + /** */ |
| 154 | + @Override public void stop() { |
| 155 | + // No-op. |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments