errose28 commented on a change in pull request #1435:
URL: https://github.com/apache/hadoop-ozone/pull/1435#discussion_r499585384



##########
File path: 
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/response/key/OMOpenKeysDeleteRequest.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.response.key;
+
+import com.google.common.base.Optional;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.OMMetrics;
+import org.apache.hadoop.ozone.om.OzoneManager;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
+import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerDoubleBufferHelper;
+import org.apache.hadoop.ozone.om.request.key.OMKeyRequest;
+import org.apache.hadoop.ozone.om.request.util.OmResponseUtil;
+import org.apache.hadoop.ozone.om.response.OMClientResponse;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKeyBucket;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKey;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+
+import static 
org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Handles requests to move open keys from the open key table to the delete
+ * table. Modifies the open key table cache only, and no underlying databases.
+ * The delete table cache does not need to be modified since it is not used
+ * for client response validation.
+ */
+public class OMOpenKeysDeleteRequest extends OMKeyRequest {
+
+  private static final Logger LOG =
+          LoggerFactory.getLogger(OMOpenKeysDeleteRequest.class);
+
+  public OMOpenKeysDeleteRequest(OMRequest omRequest) {
+    super(omRequest);
+  }
+
+  public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager,
+      long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
+
+    OMMetrics omMetrics = ozoneManager.getMetrics();
+    omMetrics.incNumOpenKeyDeleteRequests();
+
+    OzoneManagerProtocolProtos.DeleteOpenKeysRequest deleteOpenKeysRequest =
+            getOmRequest().getDeleteOpenKeysRequest();
+
+    List<OpenKeyBucket> submittedOpenKeyBucket =
+            deleteOpenKeysRequest.getOpenKeysPerBucketList();
+
+    long numSubmittedOpenKeys = submittedOpenKeyBucket.stream()
+        .mapToLong(OpenKeyBucket::getKeysCount).sum();
+
+    LOG.debug("{} open keys submitted for deletion.", numSubmittedOpenKeys);
+    omMetrics.incNumOpenKeysSubmittedForDeletion(numSubmittedOpenKeys);
+
+    OzoneManagerProtocolProtos.OMResponse.Builder omResponse =
+            OmResponseUtil.getOMResponseBuilder(getOmRequest());
+
+    IOException exception = null;
+    OMClientResponse omClientResponse = null;
+    Result result = null;
+    Map<String, OmKeyInfo> deletedOpenKeys = new HashMap<>();
+
+    try {
+      // Open keys are grouped by bucket, but there may be multiple buckets
+      // per volume. This maps volume name to volume args to track
+      // all volume updates for this request.
+      Map<String, OmVolumeArgs> modifiedVolumes = new HashMap<>();
+      OMMetadataManager metadataManager = ozoneManager.getMetadataManager();
+
+      for (OpenKeyBucket openKeyBucket: submittedOpenKeyBucket) {
+        // For each bucket where keys will be deleted from,
+        // get its bucket lock and update the cache accordingly.
+        Map<String, OmKeyInfo> deleted = updateOpenKeyTableCache(ozoneManager,
+            trxnLogIndex, openKeyBucket);
+
+        deletedOpenKeys.putAll(deleted);
+
+        // If open keys were deleted from this bucket and its volume still
+        // exists, update the volume's byte usage in the cache.
+        if (!deleted.isEmpty()) {
+          String volumeName = openKeyBucket.getVolumeName();
+          // Returns volume args from the cache if the volume is present,
+          // null otherwise.
+          OmVolumeArgs volumeArgs = getVolumeInfo(metadataManager, volumeName);
+
+          // If this volume still exists, decrement bytes used based on open
+          // keys deleted.
+          // The volume args object being updated is a reference from the
+          // cache, so this serves as a cache update.
+          if (volumeArgs != null) {

Review comment:
       I think we are safe because the bytes used value is stored in a thread 
safe LongAdder internally. See [the original PR where this was 
introduced](https://github.com/apache/hadoop-ozone/pull/1296#discussion_r485570651).
 If there is still an issue with this approach, then most of the request 
classes will need to be modified after HDDS-4053. We should discuss further, as 
this is really an issue with the design already introduced in master for 
HDDS-4053 rather than this PR.

##########
File path: 
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/response/key/OMOpenKeysDeleteRequest.java
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.response.key;
+
+import com.google.common.base.Optional;
+import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
+import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
+import org.apache.hadoop.ozone.om.OMMetadataManager;
+import org.apache.hadoop.ozone.om.OMMetrics;
+import org.apache.hadoop.ozone.om.OzoneManager;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.OmVolumeArgs;
+import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerDoubleBufferHelper;
+import org.apache.hadoop.ozone.om.request.key.OMKeyRequest;
+import org.apache.hadoop.ozone.om.request.util.OmResponseUtil;
+import org.apache.hadoop.ozone.om.response.OMClientResponse;
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKeyBucket;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKey;
+import 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+
+import static 
org.apache.hadoop.ozone.om.lock.OzoneManagerLock.Resource.BUCKET_LOCK;
+
+/**
+ * Handles requests to move open keys from the open key table to the delete
+ * table. Modifies the open key table cache only, and no underlying databases.
+ * The delete table cache does not need to be modified since it is not used
+ * for client response validation.
+ */
+public class OMOpenKeysDeleteRequest extends OMKeyRequest {
+
+  private static final Logger LOG =
+          LoggerFactory.getLogger(OMOpenKeysDeleteRequest.class);
+
+  public OMOpenKeysDeleteRequest(OMRequest omRequest) {
+    super(omRequest);
+  }
+
+  public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager,
+      long trxnLogIndex, OzoneManagerDoubleBufferHelper omDoubleBufferHelper) {
+
+    OMMetrics omMetrics = ozoneManager.getMetrics();
+    omMetrics.incNumOpenKeyDeleteRequests();
+
+    OzoneManagerProtocolProtos.DeleteOpenKeysRequest deleteOpenKeysRequest =
+            getOmRequest().getDeleteOpenKeysRequest();
+
+    List<OpenKeyBucket> submittedOpenKeyBucket =
+            deleteOpenKeysRequest.getOpenKeysPerBucketList();
+
+    long numSubmittedOpenKeys = submittedOpenKeyBucket.stream()
+        .mapToLong(OpenKeyBucket::getKeysCount).sum();
+
+    LOG.debug("{} open keys submitted for deletion.", numSubmittedOpenKeys);
+    omMetrics.incNumOpenKeysSubmittedForDeletion(numSubmittedOpenKeys);
+
+    OzoneManagerProtocolProtos.OMResponse.Builder omResponse =
+            OmResponseUtil.getOMResponseBuilder(getOmRequest());
+
+    IOException exception = null;
+    OMClientResponse omClientResponse = null;
+    Result result = null;
+    Map<String, OmKeyInfo> deletedOpenKeys = new HashMap<>();
+
+    try {
+      // Open keys are grouped by bucket, but there may be multiple buckets
+      // per volume. This maps volume name to volume args to track
+      // all volume updates for this request.
+      Map<String, OmVolumeArgs> modifiedVolumes = new HashMap<>();
+      OMMetadataManager metadataManager = ozoneManager.getMetadataManager();
+
+      for (OpenKeyBucket openKeyBucket: submittedOpenKeyBucket) {
+        // For each bucket where keys will be deleted from,
+        // get its bucket lock and update the cache accordingly.
+        Map<String, OmKeyInfo> deleted = updateOpenKeyTableCache(ozoneManager,
+            trxnLogIndex, openKeyBucket);
+
+        deletedOpenKeys.putAll(deleted);
+
+        // If open keys were deleted from this bucket and its volume still
+        // exists, update the volume's byte usage in the cache.
+        if (!deleted.isEmpty()) {
+          String volumeName = openKeyBucket.getVolumeName();
+          // Returns volume args from the cache if the volume is present,
+          // null otherwise.
+          OmVolumeArgs volumeArgs = getVolumeInfo(metadataManager, volumeName);
+
+          // If this volume still exists, decrement bytes used based on open
+          // keys deleted.
+          // The volume args object being updated is a reference from the
+          // cache, so this serves as a cache update.
+          if (volumeArgs != null) {
+            // If we already encountered the volume, it was a reference to
+            // the same object from the cache, so this will update it.
+            modifiedVolumes.put(volumeName, volumeArgs);

Review comment:
       Just to clarify, is this the execution you are talking about?
   
   1. Request1 deletes key1 from volume1 in cache.
   2. Request2 deletes key2 from volume1 in cache.
   3. Request1 sets cached VolumeArgs object volArgs.bytesUsed -= 
key1.bytesUsed.
       - *divergence 1*: The cache shows key1 and key2 as deleted, but cache 
byte usage only reflects key1's deletion.
   4. Request2 sets cached VolumeArgs object volArgs.bytesUsed -= 
key2.bytesUsed.
       - At this point, byte usage in the cache is consistent with the keys it 
shows as deleted.
   5. Response1 is processed, committing volArgs and the deletion of key1 to 
the DB.
       - *divergence 2*: the DB shows only key1 deleted, but volume byte usage 
has been set as if both key1 and key2 were deleted.
   6. Response2 is processed, committing volArgs to the DB again, and 
committing the deletion of key2 to the DB.
       - Now the keys deleted and bytes used align in the DB.
   
   IIRC the entire volume table is stored in memory and only persisted to the 
DB to save state. Reads only happen from the in memory cache for volume 
metadata. In this case, *divergence 2* will never be detected by callers since 
it only happens at the DB level. *divergence 1* my exist briefly and be 
detected by callers. Again, this is really an issue with all requests modified 
in HDDS-4053 and not just this PR. We should discuss to determine whether the 
slight inconsistency warrants a whole volume lock on all requests that modify 
byte usage.




----------------------------------------------------------------
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:
us...@infra.apache.org



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

Reply via email to