Skip to content

ZOOKEEPER-5129: Don't let container cleanup delete a node that replaced the container - #2504

Open
SpookyJumpyBeans wants to merge 1 commit into
apache:masterfrom
SpookyJumpyBeans:ZOOKEEPER-5129
Open

SpookyJumpyBeans wants to merge 1 commit into
apache:masterfrom
SpookyJumpyBeans:ZOOKEEPER-5129

Conversation

@SpookyJumpyBeans

Copy link
Copy Markdown

https://issues.apache.org/jira/browse/ZOOKEEPER-5129

ContainerManager.checkContainers collects the candidate paths first, then sends a DeleteContainerRequest for each in turn, pacing them by znode.container.maxPerMinute. The request only carried the path. The deleteContainer case in PrepRequestProcessor only refused a node with children or a normal ephemeral node.

So if a candidate was deleted and recreated before its request was processed, the new node was deleted:

  • a persistent node, with its data (EphemeralType.get(0) is VOID, which passed);
  • a new container, before its maxNeverUsedInterval;
  • a new TTL node, or a TTL node modified after it was picked, before its TTL.

Changes

  • ContainerManager checks each candidate again just before sending its request. It skips the candidate if it no longer exists, or isn't a container or TTL node past its grace period. The grace period rules moved into one helper, isPastGracePeriod, which getCandidates uses too.

  • The request carries the node's mzxid, read together with the check under the node's lock. A replacement always has a different mzxid, and so does a modified node. Adding or removing children doesn't change it.

  • PrepRequestProcessor only deletes a container or TTL node, and only if its mzxid is still the one in the request. Prep reads outstanding change records too, so a replacement or a setData that isn't committed yet is also caught:

    EphemeralType ephemeralType = EphemeralType.get(nodeRecord.stat.getEphemeralOwner());
    if (ephemeralType != EphemeralType.CONTAINER && ephemeralType != EphemeralType.TTL) {
        LOG.info("Not deleting {}: it is not a container or TTL node", path);
        throw new KeeperException.BadVersionException(path);
    }
    if (txn.getMzxid() != DeleteContainerRequest.ANY_MZXID && txn.getMzxid() != nodeRecord.stat.getMzxid()) {
        LOG.info("Not deleting {}: it was replaced or modified after it was checked", path);
        throw new KeeperException.BadVersionException(path);
    }
  • DeleteContainerRequest gets an mzxid field. The request is only created by ContainerManager on the leader (or standalone server), and prep turns it into a DeleteTxn. So nothing sent between servers or written to the txn log changes. Clients still can't send it.

Notes:

  • A container modified with setData between the check and the request is now refused. It is deleted at the next check instead.
  • testMaxPerMinute now uses real emptied containers, since a candidate that doesn't exist is no longer sent.
  • Existing issue, not changed here: with audit logging enabled, AuditHelper reads a failed deleteContainer request as a DeleteRequest and logs a ClassCastException. This already happens on master for refused requests, for example a container that has children again. This change adds refusals, so it can show up more often.

Tests

New tests in CreateContainerTest and CreateTTLTest:

Test Replaces or changes the node Guarded by
testDeleteContainerRequestDoesNotDeletePersistentNode path-only request for a persistent node type check
testReplacementContainerIsNotDeleted new container, after the check mzxid check
testModifiedAfterCheckIsNotDeleted setData on a TTL node, after the check mzxid check
testReplacementPersistentNodeIsNotDeleted persistent node with data, after the check type and mzxid checks
testReplacementContainerIsCheckedAgainBeforeDeletion new container, before the check check before sending
testReplacementIsCheckedAgainBeforeDeletion (TTL) TTL node with a longer TTL, before the check check before sending
testDeletedCandidateIsNotSentForDeletion candidate deleted, before the check check before sending

All 7 fail on master. Removing only the type check, only the mzxid check, or only the grace period check makes the tests listed for it fail. CreateContainerTest, CreateTTLTest, Emulate353TTLTest and PrepRequestProcessorTest pass, and so does checkstyle.

SpookyJumpyBeans added a commit to SpookyJumpyBeans/boyidai that referenced this pull request Nov 13, 2026
…ed the container

ContainerManager picks candidate paths, then sends a delete request for
each in turn, pacing them. The request only carried the path, and the
server only refused normal ephemeral nodes. So if a candidate was
deleted and recreated in between, the new node was deleted: any
persistent node, or a new container or TTL node still within its grace
period. A TTL node modified after it was picked, which restarts its TTL,
was deleted too.

ContainerManager now checks each candidate again just before sending its
request, skips it if it no longer exists or is no longer a container or
TTL node past its grace period, and sends the node's mzxid with the
request. The server only deletes a container or TTL node, and only if
its mzxid is unchanged, so it isn't deleted if it is replaced or
modified after that check either.
@JHSUYU

JHSUYU commented Nov 22, 2026

Copy link
Copy Markdown

@kezhuw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants