|
| 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.internal.processors.cache; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.UUID; |
| 25 | +import java.util.concurrent.ThreadLocalRandom; |
| 26 | +import org.apache.ignite.IgniteCache; |
| 27 | +import org.apache.ignite.IgniteSystemProperties; |
| 28 | +import org.apache.ignite.cache.CacheAtomicityMode; |
| 29 | +import org.apache.ignite.cache.CacheMode; |
| 30 | +import org.apache.ignite.configuration.CacheConfiguration; |
| 31 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 32 | +import org.apache.ignite.internal.IgniteEx; |
| 33 | +import org.apache.ignite.internal.IgniteNodeAttributes; |
| 34 | +import org.apache.ignite.internal.TestRecordingCommunicationSpi; |
| 35 | +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.junit.runners.Parameterized; |
| 39 | + |
| 40 | +import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC; |
| 41 | + |
| 42 | +/** */ |
| 43 | +@RunWith(Parameterized.class) |
| 44 | +public class MdcCacheReadRequestsRoutingTest extends GridCommonAbstractTest { |
| 45 | + /** */ |
| 46 | + private static final String DC_ID_0 = "DC0"; |
| 47 | + |
| 48 | + /** */ |
| 49 | + private static final String DC_ID_1 = "DC1"; |
| 50 | + |
| 51 | + /** */ |
| 52 | + private static final String KEY = "key"; |
| 53 | + |
| 54 | + /** */ |
| 55 | + private static final String VAL = "val"; |
| 56 | + |
| 57 | + /** */ |
| 58 | + @Parameterized.Parameters(name = "atomicity={0}, replicated={1}") |
| 59 | + public static Iterable<Object[]> data() { |
| 60 | + List<Object[]> res = new ArrayList<>(); |
| 61 | + |
| 62 | + for (CacheAtomicityMode mode : CacheAtomicityMode.values()) { |
| 63 | + for (boolean replicated : new boolean[] {true, false}) { |
| 64 | + res.add(new Object[] {mode, replicated}); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return res; |
| 69 | + } |
| 70 | + |
| 71 | + /** */ |
| 72 | + @Parameterized.Parameter() |
| 73 | + public CacheAtomicityMode atomicityMode; |
| 74 | + |
| 75 | + /** */ |
| 76 | + @Parameterized.Parameter(1) |
| 77 | + public boolean replicated; |
| 78 | + |
| 79 | + /** {@inheritDoc} */ |
| 80 | + @Override protected void afterTest() throws Exception { |
| 81 | + super.afterTest(); |
| 82 | + |
| 83 | + stopAllGrids(); |
| 84 | + |
| 85 | + System.clearProperty(IgniteSystemProperties.IGNITE_DATA_CENTER_ID); |
| 86 | + } |
| 87 | + |
| 88 | + /** {@inheritDoc} */ |
| 89 | + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| 90 | + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); |
| 91 | + |
| 92 | + cfg.setCommunicationSpi(new TestRecordingCommunicationSpi()); |
| 93 | + |
| 94 | + // Enforce different mac adresses to emulate distributed environment by default. |
| 95 | + cfg.setUserAttributes(Collections.singletonMap( |
| 96 | + IgniteNodeAttributes.ATTR_MACS_OVERRIDE, UUID.randomUUID().toString())); |
| 97 | + |
| 98 | + return cfg; |
| 99 | + } |
| 100 | + |
| 101 | + /** */ |
| 102 | + @Test |
| 103 | + public void testReadFromCache() throws Exception { |
| 104 | + ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
| 105 | + |
| 106 | + boolean bool = rnd.nextBoolean(); |
| 107 | + |
| 108 | + int nodes = 20; |
| 109 | + |
| 110 | + for (int i = 0; i < nodes; i += 2) { |
| 111 | + System.setProperty(IgniteSystemProperties.IGNITE_DATA_CENTER_ID, bool ? DC_ID_0 : DC_ID_1); |
| 112 | + |
| 113 | + startGrid(i); |
| 114 | + |
| 115 | + System.setProperty(IgniteSystemProperties.IGNITE_DATA_CENTER_ID, bool ? DC_ID_1 : DC_ID_0); |
| 116 | + |
| 117 | + startGrid(i + 1); |
| 118 | + } |
| 119 | + |
| 120 | + awaitPartitionMapExchange(); |
| 121 | + |
| 122 | + System.setProperty(IgniteSystemProperties.IGNITE_DATA_CENTER_ID, bool ? DC_ID_0 : DC_ID_1); |
| 123 | + |
| 124 | + IgniteEx client = startClientGrid(); |
| 125 | + |
| 126 | + CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME); |
| 127 | + |
| 128 | + ccfg.setAtomicityMode(atomicityMode); |
| 129 | + |
| 130 | + if (replicated) |
| 131 | + ccfg.setCacheMode(CacheMode.REPLICATED); |
| 132 | + else |
| 133 | + ccfg.setBackups(rnd.nextInt(nodes / 2 + 1 /*at least one in every DC*/, nodes /*less than all nodes*/)); |
| 134 | + |
| 135 | + ccfg.setWriteSynchronizationMode(FULL_SYNC); |
| 136 | + |
| 137 | + IgniteCache<Object, Object> cache = client.createCache(ccfg); |
| 138 | + |
| 139 | + cache.put(KEY, VAL); |
| 140 | + |
| 141 | + for (int i = 0; i < nodes; i++) { |
| 142 | + if (Objects.equals(grid(i).localNode().dataCenterId(), bool ? DC_ID_1 : DC_ID_0)) { |
| 143 | + TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(grid(i)); |
| 144 | + |
| 145 | + spi.blockMessages((n, msg) -> true); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + assertEquals(VAL, cache.get(KEY)); |
| 150 | + } |
| 151 | +} |
0 commit comments