Class ResourceAssignment


  • public class ResourceAssignment
    extends java.lang.Object
    Assignments for all of resources. This is NOT a thread-safe class. But in our use case, there is no any concurrent operations to assignments. So here we only use volatile resourceToAssignmentsMap to ensure the reference is up to date. N.B.: This class seems to have thread safety issues. The updateResourceAssignment(ResourceAssignment) function swaps the resourceToAssignmentsMap to another reference, and the two callers of that function wrap its invocation within locks. However, there are many other code paths which perform lock-free accesses to the same map across two functions: containsResource(String) and getPartitionAssignment(String). These other code paths first check if the resource is contained, then if it is, they get it. But there is no guarantee that the reference to the map is the same, since updateResourceAssignment(ResourceAssignment) could have been called in between. Ideally, we would eliminate this "contains, then get" pattern entirely, and instead rely on a single invocation to get the resource (which should be allowed to return null, rather than throw when absent); then the caller can use a null check as the equivalent of today's contains check, which would make the operation atomic and thus impossible to interleave with updateResourceAssignment(ResourceAssignment). TODO: refactor this.
    • Constructor Detail

      • ResourceAssignment

        public ResourceAssignment()
    • Method Detail

      • getPartitionAssignment

        public PartitionAssignment getPartitionAssignment​(java.lang.String resource)
        TODO: Rename this "getPartitionAssignmentOrThrow", and create an equivalent which returns null when absent.
      • setPartitionAssignment

        public void setPartitionAssignment​(java.lang.String resource,
                                           PartitionAssignment partitionAssignment)
      • getPartition

        public Partition getPartition​(java.lang.String resource,
                                      int partitionId)
      • containsResource

        @Deprecated
        public boolean containsResource​(java.lang.String resource)
        Deprecated.
        TODO: Delete this function entirely, to avoid the "contains, then get" anti-pattern.
      • getAssignedResources

        public java.util.Set<java.lang.String> getAssignedResources()
      • updateResourceAssignment

        public ResourceAssignment.ResourceAssignmentChanges updateResourceAssignment​(ResourceAssignment newAssignment)
        Calculate resource assignment changes between the current one and the given one and also swap the current assignment to the given one in the end
        Parameters:
        newAssignment - the given resource assignment
        Returns:
        the changes between 2 assignments