xichen01 commented on code in PR #8707: URL: https://github.com/apache/ozone/pull/8707#discussion_r2202835804
########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerStartupCache.java: ########## @@ -0,0 +1,366 @@ +/* + * 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 + * + * http://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.hadoop.ozone.container.common.impl; + +import static org.apache.hadoop.ozone.OzoneConsts.CONTAINER_DB_NAME; +import static org.apache.hadoop.ozone.OzoneConsts.SCHEMA_V3; +import static org.apache.hadoop.ozone.container.common.impl.ContainerData.CHARSET_ENCODING; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.utils.db.RDBStore; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.container.common.interfaces.Container; +import org.apache.hadoop.ozone.container.common.utils.DatanodeStoreCache; +import org.apache.hadoop.ozone.container.common.volume.HddsVolume; +import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainer; +import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Container YAML cache for reducing random IO when reading Container YAML files. + */ +public class ContainerStartupCache { + + private static final Logger LOG = LoggerFactory.getLogger(ContainerStartupCache.class); + private static final String CACHE_FILE_NAME = "container_cache.json"; + private static final String CACHE_FILE_TEMP_SUFFIX = ".tmp"; + private static final String CHECKSUM_PLACEHOLDER = "CHECKSUM_PLACEHOLDER"; + + private final ObjectMapper objectMapper; + + /** + * Versioning for Container Cache format. + */ + public enum CacheVersion { + INITIAL_VERSION(1, "Initial container cache format"), + + FUTURE_VERSION(-1, "Used internally when an unknown cache version is encountered"); + + public static final CacheVersion CURRENT = latest(); + public static final int CURRENT_VERSION = CURRENT.version; + + private final int version; + private final String description; + + CacheVersion(int version, String description) { + this.version = version; + this.description = description; + } + + public String description() { + return description; + } + + public static CacheVersion fromValue(int value) { + for (CacheVersion v : values()) { + if (v.version == value) { + return v; + } + } + return FUTURE_VERSION; + } + + private static CacheVersion latest() { + CacheVersion[] versions = CacheVersion.values(); + // Return the latest version (excluding FUTURE_VERSION which is always last) + return versions[versions.length - 2]; + } + } + + public ContainerStartupCache() { + this.objectMapper = new ObjectMapper(); + this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * Cache metadata for containers in the volume. + */ + public static class CacheMetadata { + private Map<Long, String> containerYamlData; + private long rocksDbSequenceNumber; Review Comment: > threads will be running for delete blocks, and other processes. So might be difficult to ensure db is not modified during shutdown and when this being updated. This `rocksDbSequenceNumber` will be generated at the end of `DatanodeStateMachine#close()`. At this time, the Container-related threads have been closed, including the Container migration thread and the Block deletion thread. Therefore, no Container-related write operations will be generated after the `rocksDbSequenceNumber` is generated. > rocksdb can change offline while fixing issue OR over future requirement like upgrade, and other startup logic. Any write operation will change the RocksDB sequence number, this will let the cache invalid. -- 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: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org