|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.accumulo.test.functional; |
| 20 | + |
| 21 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 22 | +import static org.apache.accumulo.core.fate.ReadOnlyTStore.TStatus.SUBMITTED; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | + |
| 27 | +import java.time.Duration; |
| 28 | +import java.util.Iterator; |
| 29 | +import java.util.Map.Entry; |
| 30 | +import java.util.SortedMap; |
| 31 | +import java.util.TreeMap; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.stream.Stream; |
| 34 | + |
| 35 | +import org.apache.accumulo.core.Constants; |
| 36 | +import org.apache.accumulo.core.client.Accumulo; |
| 37 | +import org.apache.accumulo.core.client.AccumuloClient; |
| 38 | +import org.apache.accumulo.core.client.BatchWriter; |
| 39 | +import org.apache.accumulo.core.client.MutationsRejectedException; |
| 40 | +import org.apache.accumulo.core.client.Scanner; |
| 41 | +import org.apache.accumulo.core.client.TableNotFoundException; |
| 42 | +import org.apache.accumulo.core.conf.Property; |
| 43 | +import org.apache.accumulo.core.data.Key; |
| 44 | +import org.apache.accumulo.core.data.Mutation; |
| 45 | +import org.apache.accumulo.core.data.Value; |
| 46 | +import org.apache.accumulo.core.fate.Fate.TxInfo; |
| 47 | +import org.apache.accumulo.core.fate.ReadOnlyTStore.TStatus; |
| 48 | +import org.apache.accumulo.core.fate.Repo; |
| 49 | +import org.apache.accumulo.core.fate.ZooStore; |
| 50 | +import org.apache.accumulo.core.zookeeper.ZooSession; |
| 51 | +import org.apache.accumulo.harness.MiniClusterConfigurationCallback; |
| 52 | +import org.apache.accumulo.harness.SharedMiniClusterBase; |
| 53 | +import org.apache.accumulo.manager.Manager; |
| 54 | +import org.apache.accumulo.manager.tableOps.ManagerRepo; |
| 55 | +import org.apache.accumulo.miniclusterImpl.MiniAccumuloConfigImpl; |
| 56 | +import org.apache.hadoop.conf.Configuration; |
| 57 | +import org.junit.jupiter.api.AfterAll; |
| 58 | +import org.junit.jupiter.api.BeforeAll; |
| 59 | +import org.junit.jupiter.api.BeforeEach; |
| 60 | +import org.junit.jupiter.api.Test; |
| 61 | + |
| 62 | +public class FateExecutionOrderIT extends SharedMiniClusterBase { |
| 63 | + |
| 64 | + public static class FirstOp extends ManagerRepo { |
| 65 | + |
| 66 | + private static final long serialVersionUID = 1L; |
| 67 | + |
| 68 | + protected static void insertTrackingData(long tid, Manager manager, String step) |
| 69 | + throws TableNotFoundException, MutationsRejectedException { |
| 70 | + try (BatchWriter bw = manager.getContext().createBatchWriter(FATE_TRACKING_TABLE)) { |
| 71 | + Mutation mut = new Mutation(Long.toString(System.currentTimeMillis())); |
| 72 | + mut.put("TXID:" + Long.toString(tid), "", step); |
| 73 | + bw.addMutation(mut); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public long isReady(long tid, Manager manager) throws Exception { |
| 79 | + Thread.sleep(500); |
| 80 | + insertTrackingData(tid, manager, this.getName() + "::isReady"); |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Repo<Manager> call(long tid, Manager manager) throws Exception { |
| 86 | + Thread.sleep(500); |
| 87 | + insertTrackingData(tid, manager, this.getName() + "::call"); |
| 88 | + return new SecondOp(); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + public static class SecondOp extends FirstOp { |
| 94 | + private static final long serialVersionUID = 1L; |
| 95 | + |
| 96 | + @Override |
| 97 | + public Repo<Manager> call(long tid, Manager environment) throws Exception { |
| 98 | + super.call(tid, environment); |
| 99 | + return new LastOp(); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public static class LastOp extends FirstOp { |
| 105 | + private static final long serialVersionUID = 1L; |
| 106 | + |
| 107 | + @Override |
| 108 | + public Repo<Manager> call(long tid, Manager environment) throws Exception { |
| 109 | + super.call(tid, environment); |
| 110 | + return null; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public static class FateExecutionOrderITConfig implements MiniClusterConfigurationCallback { |
| 115 | + |
| 116 | + @Override |
| 117 | + public void configureMiniCluster(MiniAccumuloConfigImpl cfg, Configuration coreSite) { |
| 118 | + cfg.setProperty(Property.MANAGER_FATE_THREADPOOL_SIZE, "1"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static final String FATE_TRACKING_TABLE = "fate_tracking"; |
| 123 | + |
| 124 | + @BeforeAll |
| 125 | + public static void setup() throws Exception { |
| 126 | + SharedMiniClusterBase.startMiniClusterWithConfig(new FateExecutionOrderITConfig()); |
| 127 | + try (AccumuloClient client = Accumulo.newClient().from(getClientProps()).build()) { |
| 128 | + client.tableOperations().create(FATE_TRACKING_TABLE); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @AfterAll |
| 133 | + public static void teardown() throws Exception { |
| 134 | + SharedMiniClusterBase.stopMiniCluster(); |
| 135 | + } |
| 136 | + |
| 137 | + @BeforeEach |
| 138 | + public void before() throws Exception { |
| 139 | + try (AccumuloClient client = Accumulo.newClient().from(getClientProps()).build()) { |
| 140 | + client.tableOperations().deleteRows(FATE_TRACKING_TABLE, null, null); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void waitFor(ZooStore<Manager> store, long txid) throws Exception { |
| 145 | + TStatus status; |
| 146 | + do { |
| 147 | + store.reserve(txid); |
| 148 | + try { |
| 149 | + status = store.getStatus(txid); |
| 150 | + } finally { |
| 151 | + store.unreserve(txid, Duration.ZERO); |
| 152 | + } |
| 153 | + if (status != TStatus.SUCCESSFUL) { |
| 154 | + Thread.sleep(50); |
| 155 | + } |
| 156 | + } while (status != TStatus.SUCCESSFUL); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testDefaultInterleaving() throws Exception { |
| 161 | + |
| 162 | + // Connect to the ZooKeeper that MAC is using and insert FATE operations |
| 163 | + final String path = getCluster().getServerContext().getZooKeeperRoot() + Constants.ZFATE; |
| 164 | + final ZooSession zs = getCluster().getServerContext().getZooSession(); |
| 165 | + final ZooStore<Manager> store = new org.apache.accumulo.core.fate.ZooStore<>(path, zs); |
| 166 | + |
| 167 | + long txid = store.create(); |
| 168 | + store.reserve(txid); |
| 169 | + try { |
| 170 | + store.push(txid, new FirstOp()); |
| 171 | + store.setTransactionInfo(txid, TxInfo.TX_NAME, "TEST_ONE"); |
| 172 | + store.setStatus(txid, SUBMITTED); |
| 173 | + } finally { |
| 174 | + store.unreserve(txid, Duration.ZERO); |
| 175 | + } |
| 176 | + |
| 177 | + long txid2 = store.create(); |
| 178 | + store.reserve(txid2); |
| 179 | + try { |
| 180 | + store.push(txid2, new FirstOp()); |
| 181 | + store.setTransactionInfo(txid2, TxInfo.TX_NAME, "TEST_TWO"); |
| 182 | + store.setStatus(txid2, SUBMITTED); |
| 183 | + } finally { |
| 184 | + store.unreserve(txid2, Duration.ZERO); |
| 185 | + } |
| 186 | + |
| 187 | + long txid3 = store.create(); |
| 188 | + store.reserve(txid3); |
| 189 | + try { |
| 190 | + store.push(txid3, new FirstOp()); |
| 191 | + store.setTransactionInfo(txid3, TxInfo.TX_NAME, "TEST_THREE"); |
| 192 | + store.setStatus(txid3, SUBMITTED); |
| 193 | + } finally { |
| 194 | + store.unreserve(txid3, Duration.ZERO); |
| 195 | + } |
| 196 | + |
| 197 | + waitFor(store, txid); |
| 198 | + waitFor(store, txid2); |
| 199 | + waitFor(store, txid3); |
| 200 | + |
| 201 | + // The execution order of the transactions is not according to their insertion |
| 202 | + // order. However, we do know that the first step of each transaction will be |
| 203 | + // executed before the second steps. |
| 204 | + try (AccumuloClient client = Accumulo.newClient().from(getClientProps()).build()) { |
| 205 | + Scanner scanner = client.createScanner(FATE_TRACKING_TABLE); |
| 206 | + Iterator<Entry<Key,Value>> iter = scanner.iterator(); |
| 207 | + |
| 208 | + SortedMap<Key,Value> subset = new TreeMap<>(); |
| 209 | + AtomicInteger remaining = new AtomicInteger(6); |
| 210 | + |
| 211 | + Stream.generate(() -> null) |
| 212 | + .takeWhile(x -> (iter.hasNext() && remaining.getAndDecrement() > 0)).map(n -> iter.next()) |
| 213 | + .forEach(e -> subset.put(e.getKey(), e.getValue())); |
| 214 | + |
| 215 | + assertTrue( |
| 216 | + subset.values().stream().allMatch(v -> new String(v.get(), UTF_8).startsWith("FirstOp"))); |
| 217 | + assertEquals(2, subset.keySet().stream() |
| 218 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid)).count()); |
| 219 | + assertEquals(2, subset.keySet().stream() |
| 220 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid2)).count()); |
| 221 | + assertEquals(2, subset.keySet().stream() |
| 222 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid3)).count()); |
| 223 | + |
| 224 | + subset.clear(); |
| 225 | + remaining.set(6); |
| 226 | + |
| 227 | + Stream.generate(() -> null) |
| 228 | + .takeWhile(x -> (iter.hasNext() && remaining.getAndDecrement() > 0)).map(n -> iter.next()) |
| 229 | + .forEach(e -> subset.put(e.getKey(), e.getValue())); |
| 230 | + |
| 231 | + assertTrue(subset.values().stream() |
| 232 | + .allMatch(v -> new String(v.get(), UTF_8).startsWith("SecondOp"))); |
| 233 | + assertEquals(2, subset.keySet().stream() |
| 234 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid)).count()); |
| 235 | + assertEquals(2, subset.keySet().stream() |
| 236 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid2)).count()); |
| 237 | + assertEquals(2, subset.keySet().stream() |
| 238 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid3)).count()); |
| 239 | + |
| 240 | + subset.clear(); |
| 241 | + remaining.set(6); |
| 242 | + |
| 243 | + Stream.generate(() -> null) |
| 244 | + .takeWhile(x -> (iter.hasNext() && remaining.getAndDecrement() > 0)).map(n -> iter.next()) |
| 245 | + .forEach(e -> subset.put(e.getKey(), e.getValue())); |
| 246 | + |
| 247 | + assertTrue( |
| 248 | + subset.values().stream().allMatch(v -> new String(v.get(), UTF_8).startsWith("LastOp"))); |
| 249 | + assertEquals(2, subset.keySet().stream() |
| 250 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid)).count()); |
| 251 | + assertEquals(2, subset.keySet().stream() |
| 252 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid2)).count()); |
| 253 | + assertEquals(2, subset.keySet().stream() |
| 254 | + .filter(k -> k.getColumnFamily().toString().equals("TXID:" + txid3)).count()); |
| 255 | + |
| 256 | + assertFalse(iter.hasNext()); |
| 257 | + |
| 258 | + // Print the order to the console |
| 259 | + scanner.iterator() |
| 260 | + .forEachRemaining((e) -> System.out.println(e.getKey() + " -> " + e.getValue())); |
| 261 | + |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | +} |
0 commit comments