keith-turner commented on code in PR #4996: URL: https://github.com/apache/accumulo/pull/4996#discussion_r1819370752
########## core/src/main/java/org/apache/accumulo/core/clientImpl/NamespaceMapping.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.clientImpl; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptySortedMap; + +import java.lang.reflect.Type; +import java.util.Map; +import java.util.SortedMap; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.data.NamespaceId; +import org.apache.accumulo.core.fate.zookeeper.ZooCache; +import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter; +import org.apache.zookeeper.KeeperException; + +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; + +public class NamespaceMapping { + private static final Gson gson = new Gson(); + private final ClientContext context; + private volatile SortedMap<NamespaceId,String> currentNamespaceMap = emptySortedMap(); + private volatile SortedMap<String,NamespaceId> currentNamespaceReverseMap = emptySortedMap(); + private volatile long lastMzxid; + + public NamespaceMapping(ClientContext context) { + this.context = context; + } + + public static byte[] initializeNamespaceMap() { + Map<String,String> map = Map.of(Namespace.DEFAULT.id().canonical(), Namespace.DEFAULT.name(), + Namespace.ACCUMULO.id().canonical(), Namespace.ACCUMULO.name()); + return serialize(map); + } + + public static void writeNamespaceToMap(ZooReaderWriter zoo, String zPath, NamespaceId namespaceId, + String namespaceName) + throws InterruptedException, KeeperException, AcceptableThriftTableOperationException { + // The built-in namespaces were already added during init or upgrade and can't be changed + if (!Namespace.DEFAULT.id().equals(namespaceId) + && !Namespace.ACCUMULO.id().equals(namespaceId)) { + zoo.mutateExisting(zPath, data -> { + var namespaces = deserialize(data); + // TODO throw exception if namespace name already exists in map? + namespaces.put(namespaceId.canonical(), namespaceName); + return serialize(namespaces); + }); + } + } + + public static byte[] serialize(Map<String,String> map) { + var sortedMap = ImmutableSortedMap.<String,String>naturalOrder().putAll(map).build(); + String jsonData = gson.toJson(sortedMap); + return jsonData.getBytes(UTF_8); + } + + public static Map<String,String> deserialize(byte[] data) { + if (data == null) { + throw new AssertionError("/namespaces node should not be null"); + } + String jsonData = new String(data, UTF_8); + Type type = new TypeToken<Map<String,String>>() {}.getType(); + return gson.fromJson(jsonData, type); + } + + private synchronized void update() { + final ZooCache zc = context.getZooCache(); + final String zPath = context.getZooKeeperRoot() + Constants.ZNAMESPACES; + final ZooCache.ZcStat stat = new ZooCache.ZcStat(); + + byte[] data = zc.get(zPath, stat); + if (stat.getMzxid() > lastMzxid) { + if (data == null) { + throw new AssertionError("/namespaces node should not be null"); + } else { + Map<String,String> idToName = deserialize(data); + var converted = ImmutableSortedMap.<NamespaceId,String>naturalOrder(); + var convertedReverse = ImmutableSortedMap.<String,NamespaceId>naturalOrder(); + idToName.forEach((idString, name) -> { + var id = NamespaceId.of(idString); + converted.put(id, name); + convertedReverse.put(name, id); + }); + currentNamespaceMap = converted.build(); + currentNamespaceReverseMap = convertedReverse.build(); + } Review Comment: Is it always expected that the desreialized map will contain the accumulo namepsace? If so could check for that when deserializing and throw an exception if its not seen. ########## core/src/main/java/org/apache/accumulo/core/clientImpl/Namespaces.java: ########## @@ -42,7 +38,8 @@ public class Namespaces { public static boolean exists(ClientContext context, NamespaceId namespaceId) { ZooCache zc = context.getZooCache(); - List<String> namespaceIds = zc.getChildren(context.getZooKeeperRoot() + Constants.ZNAMESPACES); + List<String> namespaceIds = new ArrayList<>(NamespaceMapping Review Comment: This whole method could be the following oneliner. ```java return context.getNamespaces().getIdToNameMap().containsKey(namespaceId); ``` This avoid deserializing the json unless needed and it also avoids the linear scan of the list looking for the id instead doing a lookup in the map key set. ########## server/manager/src/main/java/org/apache/accumulo/manager/upgrade/Upgrader11to12.java: ########## @@ -117,6 +122,28 @@ public void upgradeZookeeper(@NonNull ServerContext context) { zrw.overwritePersistentData(rootBase, rtm.toJson().getBytes(UTF_8), stat.getVersion()); log.info("Root metadata in ZooKeeper after upgrade: {}", rtm.toJson()); } + + String zPath = Constants.ZROOT + "/" + context.getInstanceID() + Constants.ZNAMESPACES; + byte[] existingMapData = zrw.getData(zPath); + List<String> namespaceIdList = zrw.getChildren(zPath); + Map<String,String> namespaceMap = null; + if (existingMapData != null) { + namespaceMap = NamespaceMapping.deserialize(existingMapData); + } + if (namespaceMap == null || namespaceMap.isEmpty()) { Review Comment: Seems like an empty map would never be expected here, so should probably no let it silently pass. ########## core/src/main/java/org/apache/accumulo/core/clientImpl/NamespaceMapping.java: ########## @@ -0,0 +1,123 @@ +/* + * 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.clientImpl; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptySortedMap; + +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.Map; +import java.util.SortedMap; + +import org.apache.accumulo.core.Constants; +import org.apache.accumulo.core.data.NamespaceId; +import org.apache.accumulo.core.fate.zookeeper.ZooCache; +import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter; +import org.apache.accumulo.core.fate.zookeeper.ZooUtil; +import org.apache.zookeeper.KeeperException; + +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; + +public class NamespaceMapping { + private static final Gson gson = new Gson(); + private final ClientContext context; + private volatile SortedMap<NamespaceId,String> currentNamespaceMap = emptySortedMap(); + private volatile SortedMap<String,NamespaceId> currentNamespaceReverseMap = emptySortedMap(); + private volatile long lastMzxid; + + public NamespaceMapping(ClientContext context) { + this.context = context; + } + + public static void initializeNamespaceMap(ZooReaderWriter zoo, String zPath) + throws InterruptedException, KeeperException { + Map<String,String> map = Map.of(Namespace.DEFAULT.id().canonical(), Namespace.DEFAULT.name(), + Namespace.ACCUMULO.id().canonical(), Namespace.ACCUMULO.name()); + zoo.putPersistentData(zPath, serialize(map), ZooUtil.NodeExistsPolicy.OVERWRITE); + } + + public static byte[] writeNamespaceToMap(ZooReaderWriter zoo, String zPath, + NamespaceId namespaceId, String namespaceName) throws InterruptedException, KeeperException { + byte[] updatedMap = new byte[0]; + if (!Namespace.DEFAULT.id().equals(namespaceId) Review Comment: If its never expected that this could would be called w/ the accumulo namepsace id or name then it would be better to throw an exception rather than silently ignore it. -- 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]
