You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2020/09/21 14:33:48 UTC

[GitHub] [hadoop-ozone] linyiqun commented on a change in pull request #1437: HDDS-4222: [OzoneFS optimization] Provide a mechanism for efficient path lookup

linyiqun commented on a change in pull request #1437:
URL: https://github.com/apache/hadoop-ozone/pull/1437#discussion_r492061634



##########
File path: hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/OMConfigKeys.java
##########
@@ -246,4 +246,15 @@ private OMConfigKeys() {
       "ozone.om.enable.filesystem.paths";
   public static final boolean OZONE_OM_ENABLE_FILESYSTEM_PATHS_DEFAULT =
       false;
+
+  public static final String OZONE_OM_CACHE_DIR_POLICY =
+          "ozone.om.metadata.cache.directory";
+  public static final String OZONE_OM_CACHE_DIR_DEFAULT = "DIR_LRU";

Review comment:
       I prefer rename these two:
   
   * ozone.om.metadata.cache.directory -> ozone.om.metadata.cache.directory.policy
   * OZONE_OM_CACHE_DIR_DEFAULT  -> OZONE_OM_CACHE_DIR_POLICY_DEFAULT

##########
File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/cache/CacheEntity.java
##########
@@ -0,0 +1,48 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.om.cache;
+
+/**
+ * Entities that are to be cached.
+ */
+public enum CacheEntity {
+
+  DIR("directory");
+  // This is extendable and one can add more entities for
+  // caching based on demand. For example, define new entities like FILE
+  // ("file"), LISTING("listing") cache etc.
+
+  CacheEntity(String entity) {
+    this.entityName = entity;
+  }
+
+  private String entityName;
+
+  public String getName() {
+    return entityName;
+  }
+
+  public static CacheEntity getEntity(String entityStr) {
+    for (CacheEntity entity : CacheEntity.values()) {
+      if (entityStr.equalsIgnoreCase(entity.getName())) {

Review comment:
       Can you change the order of the comparison (to **entity.getName().equalsIgnoreCase(entityStr)**) in case entityStr passed as null that will lead NPE error.
   

##########
File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/cache/CacheStore.java
##########
@@ -0,0 +1,72 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.om.cache;
+
+/**
+ * Cache used for traversing path components from parent node to the leaf node.
+ * <p>
+ * Basically, its a write-through cache and ensures that no-stale entries in
+ * the cache.
+ * <p>
+ * TODO: can define specific 'CacheLoader' to handle the OM restart and
+ *       define cache loading strategies. It can be NullLoader, LazyLoader,
+ *       LevelLoader etc.
+ *
+ * @param <CACHEKEY>
+ * @param <CACHEVALUE>
+ */
+public interface CacheStore<CACHEKEY extends OMCacheKey,

Review comment:
       Besides basical put/get/remove interface, we should also have other interface defined like cache hit/miss count for cache store in the future..

##########
File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/cache/OMMetadataCacheFactory.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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.om.cache;
+
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides different caching policies for cache entities. This can be
+ * extended by adding more entities and their caching policies into it.
+ * <p>
+ * For example, for the directory cache user has to configure following
+ * property with cache type. OM will creates specific cache store for the
+ * directory based on the configured cache policy.
+ * ozone.om.metadata.cache.directory = DIR_LRU
+ * <p>
+ * One can add new directory policy to OM by defining new cache type say
+ * "DIR_LFU" and implements new CacheStore as DirectoryLFUCacheStore.
+ * <p>
+ * One can add new entity to OM, let's say file to be cached by configuring the
+ * property like below and implement specific provider to instantiate the
+ * fileCacheStore.
+ * ozone.om.metadata.cache.file = FILE_LRU
+ */
+public final class OMMetadataCacheFactory {
+  private static final Logger LOG =
+          LoggerFactory.getLogger(OMMetadataCacheFactory.class);
+
+  /**
+   * Private constructor, class is not meant to be initialized.
+   */
+  private OMMetadataCacheFactory() {
+  }
+
+  public static CacheStore getCache(String configuredCachePolicy,
+                                    String defaultValue,
+                                    OzoneConfiguration config) {
+    String cachePolicy = config.get(configuredCachePolicy, defaultValue);
+    LOG.info("Configured {} with {}", configuredCachePolicy, cachePolicy);
+    CacheEntity entity = getCacheEntity(configuredCachePolicy);
+
+    switch (entity) {
+    case DIR:
+      OMMetadataCacheProvider provider = new OMDirectoryCacheProvider(config,
+              cachePolicy);
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("CacheStore initialized with {}:" + provider.getEntity());
+      }
+      return provider.getCache();
+    default:
+      return null;

Review comment:
       How about throw error when cache store cannot be initialized with given cache policy? We would be better not let cache store as null returned. If cache store returned null, that means all operations from this cache store will be failed. Otherwise, we have to do empty check for each operation when cache store is used.

##########
File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/cache/DirectoryLRUCacheStore.java
##########
@@ -0,0 +1,87 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.om.cache;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.om.OMConfigKeys;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Directory LRUCache: cache directories based on LRU (Least Recently Used)
+ * cache eviction strategy, wherein if the cache size has reached the maximum
+ * allocated capacity, the least recently used objects in the cache will be
+ * evicted.
+ * <p>
+ * TODO: Add cache metrics - occupancy, hit, miss, evictions etc
+ */
+public class DirectoryLRUCacheStore implements CacheStore {
+
+  private static final Logger LOG =
+          LoggerFactory.getLogger(DirectoryLRUCacheStore.class);
+
+  // Initialises Guava based LRU cache.
+  private Cache<OMCacheKey, OMCacheValue> mCache;
+
+  /**
+   * @param configuration ozone config
+   */
+  public DirectoryLRUCacheStore(OzoneConfiguration configuration) {
+    LOG.info("Initializing DirectoryLRUCacheStore..");
+    // defaulting to 1000,00
+    int initSize = configuration.getInt(
+            OMConfigKeys.OZONE_OM_CACHE_DIR_INIT_CAPACITY,
+            OMConfigKeys.OZONE_OM_CACHE_DIR_INIT_CAPACITY_DEFAULT);
+    // defaulting to 5000,000
+    long maxSize = configuration.getLong(
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY,
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY_DEFAULT);
+    LOG.info("Configured {} with {}",
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY, maxSize);
+    mCache = CacheBuilder.newBuilder()
+            .initialCapacity(initSize)
+            .maximumSize(maxSize)
+            .build();
+  }
+
+  @Override
+  public void put(OMCacheKey key, OMCacheValue value) {
+    mCache.put(key, value);
+  }
+
+  @Override
+  public OMCacheValue get(OMCacheKey key) {
+    return mCache.getIfPresent(key);
+  }
+
+  @Override
+  public void remove(OMCacheKey key) {

Review comment:
       Same comment like above.

##########
File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/cache/DirectoryLRUCacheStore.java
##########
@@ -0,0 +1,87 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.om.cache;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.om.OMConfigKeys;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Directory LRUCache: cache directories based on LRU (Least Recently Used)
+ * cache eviction strategy, wherein if the cache size has reached the maximum
+ * allocated capacity, the least recently used objects in the cache will be
+ * evicted.
+ * <p>
+ * TODO: Add cache metrics - occupancy, hit, miss, evictions etc
+ */
+public class DirectoryLRUCacheStore implements CacheStore {
+
+  private static final Logger LOG =
+          LoggerFactory.getLogger(DirectoryLRUCacheStore.class);
+
+  // Initialises Guava based LRU cache.
+  private Cache<OMCacheKey, OMCacheValue> mCache;
+
+  /**
+   * @param configuration ozone config
+   */
+  public DirectoryLRUCacheStore(OzoneConfiguration configuration) {
+    LOG.info("Initializing DirectoryLRUCacheStore..");
+    // defaulting to 1000,00
+    int initSize = configuration.getInt(
+            OMConfigKeys.OZONE_OM_CACHE_DIR_INIT_CAPACITY,
+            OMConfigKeys.OZONE_OM_CACHE_DIR_INIT_CAPACITY_DEFAULT);
+    // defaulting to 5000,000
+    long maxSize = configuration.getLong(
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY,
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY_DEFAULT);
+    LOG.info("Configured {} with {}",
+            OMConfigKeys.OZONE_OM_CACHE_DIR_MAX_CAPACITY, maxSize);
+    mCache = CacheBuilder.newBuilder()
+            .initialCapacity(initSize)
+            .maximumSize(maxSize)
+            .build();
+  }
+
+  @Override
+  public void put(OMCacheKey key, OMCacheValue value) {
+    mCache.put(key, value);

Review comment:
       Can we add the null check for key before putting it into cache?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: ozone-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: ozone-issues-help@hadoop.apache.org