ptlrs commented on code in PR #12139:
URL: https://github.com/apache/ozone/pull/12139#discussion_r4684863788


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java:
##########
@@ -2539,21 +2542,24 @@ private void deleteInternal(Container container, 
boolean force)
         HddsVolume hddsVolume = keyValueContainerData.getVolume();
 
         // Steps to delete
-        // 1. container marked deleted
-        // 2. container is removed from container set
+        // 1. witnessed container DB entry set to DELETED, container is 
removed from container set
+        // 2. container marked deleted
         // 3. container db handler and content removed from db
         // 4. container moved to tmp folder
         // 5. container content deleted from tmp folder
+        long waitTime = clock.millis() - startTime;
+        if (waitTime > maxDeleteLockWaitMs) {
+          LOG.warn("An attempt to delete container {} took {} ms acquiring 
locks and pre-checks. " +
+                  "The delete has been skipped and should be retried 
automatically by SCM.",
+              containerLocked.getContainerData().getContainerID(), waitTime);
+          return;
+        }
+        // Step 1 is first and outside the try below. If the entry cannot be 
written, nothing has changed yet: the
+        // delete fails with the witnessed container DB error and the replica 
stays as it was. If the datanode stops
+        // after step 1, the replica on disk is intact and is loaded again at 
startup.
+        containerSet.removeContainer(containerId);

Review Comment:
   Yes, this is what we originally thought about doing but to provide better 
exception messages, we have now changed the order. 
   
   See this thread for context 
https://github.com/apache/ozone/pull/12139#discussion_r4674020780
   



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerSet.java:
##########
@@ -404,10 +441,11 @@ private boolean removeContainer(long containerId, boolean 
markMissing, boolean r
     }
   }
 
-  private void deleteFromContainerTable(long containerId) throws 
StorageContainerException {
+  private void markDeletedInContainerTable(long containerId) throws 
StorageContainerException {

Review Comment:
   I reverted the change that I had previously done to the order of operations 
as per the review comment. We now preserve the upstream behaviour of marking 
the container first on-disk for deletion and only then updating the witness db. 
   
   As such we can't simply have `ContainerReader#cleanupContainer` update the 
witness db at DN startup time because it conflicts with the disk balancer. Disk 
Balancer marks a container as deleted as well. 
   
   We will now have to read all containers and only if a moved copy of the 
replica doesn't exist on some another volume can we conclude that the container 
is actually deleted. Otherwise we may incorrectly mark a container as deleted 
in the witness db. 
   
   This will require some more work and should be handled in a separate PR.



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java:
##########
@@ -504,6 +504,9 @@ ContainerCommandResponseProto handleCreateContainer(
     containerIdLock.lock();
     try {
       if (containerSet.getContainer(containerID) == null) {
+        if (containerState != RECOVERING) {

Review Comment:
   Is the question about checking for the recovering state or checking for the 
container in the witness db? 
   The addContainer methods do call ensureContainerNotMissing but intentional 
deletes will not be part of the missing set. 
   
   This check's goal is to ensure that a new container is not created unless 
there is no entry in the witness db or the container is in recovering state. 



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerSet.java:
##########
@@ -666,6 +734,10 @@ public <T> void buildMissingContainerSetAndValidate(Map<T, 
Long> container2BCSID
     container2BCSIDMap.entrySet().parallelStream().forEach((mapEntry) -> {
       final long id = getId.applyAsLong(mapEntry.getKey());
       if (!containerMap.containsKey(id)) {
+        if (isDeletedInContainerTable(id)) {

Review Comment:
   I agree, we can optimize this but let's handle this in a future PR as it not 
strictly necessary for the scope of this PR



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerSet.java:
##########
@@ -226,14 +259,30 @@ private boolean addContainer(Container<?> container, 
boolean overwrite) throws
     }
   }
 
-  private void updateContainerIdTable(long containerId, ContainerData 
containerData) throws StorageContainerException {
+  /**
+   * Records the replica that is being added in the witnessed container DB. 
The entry is written when:
+   * <ul>
+   *   <li>there is none: this is the first replica of the container on this 
datanode;</li>
+   *   <li>it is DELETED: this datanode deleted its replica, and now loads one 
from disk at startup or gets one through
+   *   repair. An ordinary create does not get here for a DELETED entry, 
KeyValueHandler refuses it;</li>
+   *   <li>it has no replica index: it was written in the older table 
format;</li>
+   *   <li>{@code replaceReplicaIndex} is set and the replica has another 
index than the entry: a repair replaces the
+   *   replica that this datanode held.</li>
+   * </ul>
+   * In all other cases the entry is kept as it is.
+   */
+  private void updateContainerIdTable(long containerId, ContainerData 
containerData, boolean replaceReplicaIndex)
+      throws StorageContainerException {
     if (null != containerMetadataStore) {
       try {
         ContainerID containerIdObj = ContainerID.valueOf(containerId);
         Table<ContainerID, ContainerCreateInfo> containerCreateInfoTable =
             containerMetadataStore.getContainerCreateInfoTable();
         ContainerCreateInfo containerCreateInfo = 
containerCreateInfoTable.get(containerIdObj);
-        if (containerCreateInfo == null || 
containerCreateInfo.getReplicaIndex() == INVALID_REPLICA_INDEX) {
+        if (containerCreateInfo == null
+            || containerCreateInfo.getState() == State.DELETED

Review Comment:
   We are also allowing recovering EC containers to get created provided the 
indexes are different. 



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerSet.java:
##########
@@ -148,6 +148,31 @@ public void ensureContainerNotMissing(long containerId, 
State state) throws Stor
     }
   }
 
+  /**
+   * Fails if the witnessed container DB has an entry for the container: it 
was already created on this datanode,
+   * possibly deleted since, and must not be created again by an ordinary 
create.
+   * @param containerId ID of the container to be created
+   * @throws StorageContainerException with {@code CLOSED_CONTAINER_IO} if 
there is an entry, or with
+   *                                   {@code IO_EXCEPTION} if the entry 
cannot be read
+   */
+  public void ensureContainerNotWitnessed(long containerId) throws 
StorageContainerException {

Review Comment:
   The witnessdb is not the source of truth for container states. It doesn't 
attempt to keep the states synchronized with the transitions in any container's 
states. 



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/ContainerSet.java:
##########
@@ -384,11 +434,12 @@ private boolean removeContainer(long containerId, boolean 
markMissing, boolean r
     if (markMissing) {
       missingContainerSet.add(containerId);
     }
+    // Keep the witnessed entry as DELETED before anything is removed, so a 
failed write leaves the replica loaded.
+    if (markDeleted) {
+      markDeletedInContainerTable(containerId);

Review Comment:
   Yes agree, although I don't want to add any check at this time which 
prevents us from recording a deleted state in the witness db. 
   
   I am not confident that there are no race conditions such that absence of an 
entry in the in-memory map guarantees absence of the container in practice. 
   
   A penalty of recording never-before-existed containers as `DELETED` in the 
witness db should be benign. A different state may result in them being marked 
as missing.



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/impl/HddsDispatcher.java:
##########
@@ -348,6 +348,18 @@ && getMissingContainerSet().contains(containerID)) {
         StorageContainerException sce = new StorageContainerException(
             "ContainerID " + containerID + " does not exist",
             ContainerProtos.Result.CONTAINER_NOT_FOUND);
+        if (isWriteCommitStage) {
+          // The write stage of this WriteChunk was refused if this datanode 
held the container before and deleted it.
+          // Answer the commit stage the same way, as a closed replica does, 
so that a late write does not close the
+          // pipeline.
+          try {
+            containerSet.ensureContainerNotWitnessed(containerID);

Review Comment:
   It is for the commit stage of the same WriteChunk. The write stage is 
refused with CLOSED_CONTAINER_IO, which the state machine tolerates, so the 
entry is still applied. The commit stage does not create containers and closes 
the pipeline if no container is found. 



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to