kevinrr888 commented on code in PR #4524:
URL: https://github.com/apache/accumulo/pull/4524#discussion_r1705575651


##########
test/src/main/java/org/apache/accumulo/test/fate/MultipleStoresIT.java:
##########
@@ -0,0 +1,553 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.test.fate;
+
+import static 
org.apache.accumulo.test.fate.user.UserFateStoreIT.createFateTable;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Predicate;
+
+import org.apache.accumulo.core.client.Accumulo;
+import org.apache.accumulo.core.clientImpl.ClientContext;
+import org.apache.accumulo.core.conf.DefaultConfiguration;
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.fate.Fate;
+import org.apache.accumulo.core.fate.FateId;
+import org.apache.accumulo.core.fate.FateInstanceType;
+import org.apache.accumulo.core.fate.FateStore;
+import org.apache.accumulo.core.fate.MetaFateStore;
+import org.apache.accumulo.core.fate.ReadOnlyFateStore;
+import org.apache.accumulo.core.fate.Repo;
+import org.apache.accumulo.core.fate.user.UserFateStore;
+import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter;
+import org.apache.accumulo.core.fate.zookeeper.ZooUtil;
+import org.apache.accumulo.harness.SharedMiniClusterBase;
+import org.apache.accumulo.test.util.Wait;
+import org.apache.accumulo.test.zookeeper.ZooKeeperTestingServer;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Sets;
+
+// TODO 4131 could potentially have separate classes for testing MetaFateStore 
and UserFateStore
+// similar to how FateTestRunner is used, however that interface doesn't work 
as nicely here
+// since we are using multiple stores instead of just one. Can do something 
similar to
+// FateTestRunner here if desired
+public class MultipleStoresIT extends SharedMiniClusterBase {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(MultipleStoresIT.class);
+  @TempDir
+  private static File tempDir;
+  private static ZooKeeperTestingServer szk = null;
+  private static ZooReaderWriter zk;
+  private static final String FATE_DIR = "/fate";
+  private ClientContext client;
+
+  @BeforeEach
+  public void beforeEachSetup() {
+    client = (ClientContext) 
Accumulo.newClient().from(getClientProps()).build();
+  }
+
+  @AfterEach
+  public void afterEachTeardown() {
+    client.close();
+  }
+
+  @BeforeAll
+  public static void beforeAllSetup() throws Exception {
+    SharedMiniClusterBase.startMiniCluster();
+    szk = new ZooKeeperTestingServer(tempDir);
+    zk = szk.getZooReaderWriter();
+  }
+
+  @AfterAll
+  public static void afterAllTeardown() throws Exception {
+    SharedMiniClusterBase.stopMiniCluster();
+    szk.close();
+  }
+
+  @Test
+  public void testReserveUnreserve() throws Exception {
+    testReserveUnreserve(FateInstanceType.META);
+    testReserveUnreserve(FateInstanceType.USER);
+  }
+
+  private void testReserveUnreserve(FateInstanceType storeType) throws 
Exception {
+    // reserving/unreserving a FateId should be reflected across instances of 
the stores
+    final String tableName = getUniqueNames(1)[0];
+    final int numFateIds = 500;
+    final FateId fakeFateId = FateId.from(storeType, UUID.randomUUID());
+    final List<FateStore.FateTxStore<SleepingTestEnv>> reservations = new 
ArrayList<>();
+    final boolean isUserStore = storeType.equals(FateInstanceType.USER);
+    final Set<FateId> allIds = new HashSet<>();
+    final FateStore<SleepingTestEnv> store1, store2;
+    final ZooUtil.LockID lock1 = new ZooUtil.LockID("/locks", "L1", 50);
+    final ZooUtil.LockID lock2 = new ZooUtil.LockID("/locks", "L2", 52);
+    Map<FateId,FateStore.FateReservation> activeReservations;
+
+    if (isUserStore) {
+      createFateTable(client, tableName);
+      store1 = new UserFateStore<>(client, tableName, lock1, null);
+      store2 = new UserFateStore<>(client, tableName, lock2, null);
+    } else {
+      store1 = new MetaFateStore<>(FATE_DIR, zk, lock1, null);
+      store2 = new MetaFateStore<>(FATE_DIR, zk, lock2, null);
+    }
+
+    // Create the fate ids using store1
+    for (int i = 0; i < numFateIds; i++) {
+      assertTrue(allIds.add(store1.create()));
+    }
+    assertEquals(numFateIds, allIds.size());
+
+    // Reserve half the fate ids using store1 and rest using store2, after 
reserving a fate id in
+    // one, should not be able to reserve the same in the other. Should also 
not matter that all the
+    // ids were created using store1
+    int count = 0;
+    for (FateId fateId : allIds) {
+      if (count % 2 == 0) {
+        reservations.add(store1.reserve(fateId));
+        assertTrue(store2.tryReserve(fateId).isEmpty());
+      } else {
+        reservations.add(store2.reserve(fateId));
+        assertTrue(store1.tryReserve(fateId).isEmpty());
+      }
+      count++;
+    }
+    // Try to reserve a non-existent fate id
+    assertTrue(store1.tryReserve(fakeFateId).isEmpty());
+    assertTrue(store2.tryReserve(fakeFateId).isEmpty());
+    // Both stores should return the same reserved transactions
+    activeReservations = store1.getActiveReservations();
+    assertEquals(allIds, activeReservations.keySet());
+    activeReservations = store2.getActiveReservations();
+    assertEquals(allIds, activeReservations.keySet());
+
+    // Test setting/getting the TStatus and unreserving the transactions
+    for (int i = 0; i < allIds.size(); i++) {
+      var reservation = reservations.get(i);
+      assertEquals(ReadOnlyFateStore.TStatus.NEW, reservation.getStatus());
+      reservation.setStatus(ReadOnlyFateStore.TStatus.SUBMITTED);
+      assertEquals(ReadOnlyFateStore.TStatus.SUBMITTED, 
reservation.getStatus());
+      reservation.delete();
+      reservation.unreserve(Duration.ofMillis(0));
+      // Attempt to set a status on a tx that has been unreserved (should 
throw exception)
+      assertThrows(IllegalStateException.class,
+          () -> reservation.setStatus(ReadOnlyFateStore.TStatus.NEW));
+    }
+    assertTrue(store1.getActiveReservations().isEmpty());
+    assertTrue(store2.getActiveReservations().isEmpty());
+  }
+
+  @Test
+  public void testReserveNonExistentTxn() throws Exception {
+    testReserveNonExistentTxn(FateInstanceType.META);
+    testReserveNonExistentTxn(FateInstanceType.USER);
+  }
+
+  private void testReserveNonExistentTxn(FateInstanceType storeType) throws 
Exception {
+    // Tests that reserve() doesn't hang indefinitely and instead throws an 
error
+    // on reserve() a non-existent transaction.
+    final FateStore<SleepingTestEnv> store;
+    final boolean isUserStore = storeType.equals(FateInstanceType.USER);
+    final String tableName = getUniqueNames(1)[0];
+    final FateId fakeFateId = FateId.from(storeType, UUID.randomUUID());
+    final ZooUtil.LockID lock = new ZooUtil.LockID("/locks", "L1", 50);
+
+    if (isUserStore) {
+      createFateTable(client, tableName);
+      store = new UserFateStore<>(client, tableName, lock, null);
+    } else {
+      store = new MetaFateStore<>(FATE_DIR, zk, lock, null);
+    }
+
+    assertThrows(IllegalStateException.class, () -> store.reserve(fakeFateId));

Review Comment:
   Added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to