ctubbsii commented on code in PR #5451:
URL: https://github.com/apache/accumulo/pull/5451#discussion_r2049514719


##########
core/src/main/java/org/apache/accumulo/core/util/tables/TableMapping.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.core.util.tables;
+
+import static java.util.Collections.emptySortedMap;
+import static java.util.Objects.requireNonNull;
+import static 
org.apache.accumulo.core.clientImpl.NamespaceMapping.deserializeMap;
+import static 
org.apache.accumulo.core.clientImpl.NamespaceMapping.serializeMap;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.SortedMap;
+import java.util.stream.Stream;
+
+import org.apache.accumulo.core.Constants;
+import 
org.apache.accumulo.core.clientImpl.AcceptableThriftTableOperationException;
+import org.apache.accumulo.core.clientImpl.ClientContext;
+import org.apache.accumulo.core.clientImpl.Namespace;
+import org.apache.accumulo.core.clientImpl.thrift.TableOperation;
+import org.apache.accumulo.core.clientImpl.thrift.TableOperationExceptionType;
+import org.apache.accumulo.core.data.NamespaceId;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter;
+import org.apache.accumulo.core.metadata.AccumuloTable;
+import org.apache.accumulo.core.zookeeper.ZcStat;
+import org.apache.accumulo.core.zookeeper.ZooCache;
+import org.apache.zookeeper.KeeperException;
+
+import com.google.common.collect.ImmutableSortedMap;
+
+public class TableMapping {
+
+  private final ClientContext context;
+  private final NamespaceId namespaceId;
+  private volatile SortedMap<TableId,String> currentTableMap = 
emptySortedMap();
+  private volatile SortedMap<String,TableId> currentTableReverseMap = 
emptySortedMap();
+  private volatile long lastMzxid;
+
+  public TableMapping(ClientContext context, NamespaceId namespaceId) {
+    this.context = context;
+    this.namespaceId = namespaceId;
+  }
+
+  public void put(final ClientContext context, TableId tableId, String 
tableName,
+      TableOperation operation)
+      throws InterruptedException, KeeperException, 
AcceptableThriftTableOperationException {
+    var zoo = context.getZooSession().asReaderWriter();
+    Stream.of(zoo, tableId, namespaceId, 
tableName).forEach(Objects::requireNonNull);
+    String zTableMapPath = getZTableMapPath(namespaceId);
+    if (!zoo.exists(zTableMapPath)) {
+      throw new KeeperException.NoNodeException(zTableMapPath + " does not 
exist in ZooKeeper");
+    }
+    if (isBuiltInZKTable(tableId)) {
+      throw new AssertionError("Putting built-in tables in map should not be 
possible after init");
+    }
+    zoo.mutateExisting(zTableMapPath, data -> {
+      var tables = deserializeMap(data);
+      final String currentName = tables.get(tableId.canonical());
+      if (tableName.equals(currentName)) {
+        return null; // mapping already exists; operation is idempotent, so no 
change needed
+      }
+      if (currentName != null) {
+        throw new AcceptableThriftTableOperationException(null, 
tableId.canonical(), operation,
+            TableOperationExceptionType.EXISTS, "Table Id already exists");
+      }
+      if (tables.containsValue(tableName)) {
+        throw new AcceptableThriftTableOperationException(null, 
tableId.canonical(), operation,
+            TableOperationExceptionType.EXISTS, "Table name already exists");
+      }
+      tables.put(tableId.canonical(), tableName);
+      return serializeMap(tables);
+    });
+  }
+
+  public void remove(final ClientContext context, final TableId tableId)
+      throws InterruptedException, KeeperException, 
AcceptableThriftTableOperationException {
+    var zoo = context.getZooSession().asReaderWriter();
+    Stream.of(zoo, tableId).forEach(Objects::requireNonNull);
+    if (isBuiltInZKTable(tableId)) {
+      throw new AssertionError("Removing built-in tables in map should not be 
possible");
+    }
+    zoo.mutateExisting(getZTableMapPath(getNamespaceOfTableId(zoo, tableId)), 
data -> {
+      var tables = deserializeMap(data);
+      if (!tables.containsKey(tableId.canonical())) {
+        throw new AcceptableThriftTableOperationException(null, 
tableId.canonical(),
+            TableOperation.DELETE, TableOperationExceptionType.NOTFOUND,
+            "Table already removed while processing");
+      }
+      tables.remove(tableId.canonical());
+      return serializeMap(tables);
+    });
+  }
+
+  public void rename(final ClientContext context, final TableId tableId, final 
String oldName,
+      final String newName)
+      throws InterruptedException, KeeperException, 
AcceptableThriftTableOperationException {
+    var zoo = context.getZooSession().asReaderWriter();
+    Stream.of(zoo, tableId, namespaceId, oldName, 
newName).forEach(Objects::requireNonNull);
+    String zTableMapPath = getZTableMapPath(namespaceId);
+    if (!zoo.exists(zTableMapPath)) {
+      throw new KeeperException.NoNodeException(zTableMapPath + " does not 
exist in ZooKeeper");
+    }
+    if (isBuiltInZKTable(tableId)) {
+      throw new AssertionError("Renaming built-in tables in map should not be 
possible");
+    }
+    zoo.mutateExisting(zTableMapPath, current -> {
+      var tables = deserializeMap(current);
+      final String currentName = tables.get(tableId.canonical());
+      if (newName.equals(currentName)) {
+        return null; // assume in this case the operation is running again, so 
we are done
+      }
+      if (!oldName.equals(currentName)) {
+        throw new AcceptableThriftTableOperationException(null, oldName, 
TableOperation.RENAME,
+            TableOperationExceptionType.NOTFOUND, "Name changed while 
processing");
+      }
+      if (tables.containsValue(newName)) {
+        throw new AcceptableThriftTableOperationException(null, newName, 
TableOperation.RENAME,
+            TableOperationExceptionType.EXISTS, "Table name already exists");
+      }
+      tables.put(namespaceId.canonical(), newName);
+      return serializeMap(tables);
+    });
+  }
+
+  private synchronized void update(NamespaceId namespaceId) {
+    final ZooCache zc = context.getZooCache();
+    final ZcStat stat = new ZcStat();
+    final String zTableMapPath = getZTableMapPath(namespaceId);
+
+    byte[] data = zc.get(zTableMapPath, stat);
+    if (stat.getMzxid() > lastMzxid) {
+      if (data == null) {
+        throw new IllegalStateException(zTableMapPath + " node should not be 
null");

Review Comment:
   This is a weird edge case, though... where the namespace existed at the 
start of the operation, but stopped existing (or at least, it's table mapping 
node stopped existing) sometime later. This latest change ensures that you will 
at least see that the namespace no longer has any tables in it in this case, 
rather than failing outright (since it did exist at the start of the operation, 
at least). That would result in a TableNotFoundException, but without a 
specific cause, which is sufficient. If it turns out that the namespace was in 
fact deleted, the next time this is called, it would result in a 
TableNotFoundException caused by a NamespaceNotFoundException, because one 
wouldn't be able to resolve the namespace.



-- 
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