yandrey321 commented on PR #11968:
URL: https://github.com/apache/ozone/pull/11968#issuecomment-6898112369
Thanks @sumitagrawl. A write that races with the creation of its container
does succeed, without taking the lock. Two writes to a new container can
interleave in two ways:
1. **The second write reads the map before the first one adds the container
to it.** It does not see the entry, so it takes the auto-create path as before.
`createContainer()` takes the container creation lock in `KeyValueHandler`,
finds the container that the first write created, and returns success.
2. **The second write reads the map after the first one added the container
to it.** This is the new case. Every path adds a container to
`container2BCSIDMap` only after the container is in the container set:
- auto-create calls `createContainer()`, which adds the container to the
set under the creation lock, before `putIfAbsent`;
- an explicit CreateContainer adds it only after the handler returns
`SUCCESS`;
- the commit stage adds it only for a container that exists;
- `updateBCSID` only updates existing entries.
The map is a `ConcurrentHashMap`, so a write that sees the entry also
sees the container in the set. If its first lookup missed the container,
because that lookup ran before the create, the second lookup finds it, and the
write goes on.
A map entry without a container can only mean that the container was
deleted, which is the case this PR refuses. Taking the creation lock for the
second lookup would not change the result, and it would serialize the writes to
a missing container.
`TestHddsDispatcher#testRatisWriteRacingWithContainerCreateSucceeds` covers
case 2:
- a first write creates the container, which adds it to the map;
- for the second write, the first `getContainer` returns null, as it would
if it ran just before the create;
- the write returns `SUCCESS`, `createContainer` is not called, and the
container is the same instance and still open;
- with the second lookup removed, the test fails.
I updated the code comment to say why no lock is needed.
--
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]