Uploaded image for project: 'Apache Ozone'
  1. Apache Ozone
  2. HDDS-10210

Ensure atomic update in StateContext#updateCommandStatus

    XMLWordPrintableJSON

Details

    Description

      While tracing the block deletion logic, I found a possible problem regarding command status update logic.

      The cmdStatusMap in StateContext is a ConcurrentHashMap

      Currently the update logic for command status is as follows:

      public boolean updateCommandStatus(Long cmdId,
          Consumer<CommandStatus> cmdStatusUpdater) {
        if (cmdStatusMap.containsKey(cmdId)) {
          cmdStatusUpdater.accept(cmdStatusMap.get(cmdId));
          return true;
        }
        return false;
      } 

      It does not seem correct. Although the get containsKey and get is protected by the ConcurrentHashMap, the CommandStatus retrieved is mutated directly using the cmdStatusUpdater and not protected by the ConcurrentHashMap. This might trigger race condition.

      We should use atomic operation like computeIfPresent.

      Note: The cmdStatusMap is also exposed to public (getCommandStatusMap) which might not be a good idea since we cannot guarantee correct usage of the map. We might need to encapsulate it properly inside StateContext.

      Attachments

        Issue Links

          Activity

            People

              ivanandika Ivan Andika
              ivanandika Ivan Andika
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: