def test_assignment_change_size(assignments: Assignments): with pytest.raises(ValueError): changed = assignments.changeMaxMembers(0) changed = assignments.changeMaxMembers(8) assert changed changed = assignments.changeMaxMembers(8) assert not changed
def test_assignment_get_member_assignment(assignments: Assignments): assert assignments.getMemberAssignment(9) is None, 'Unknown member ID should return None' mbrAssign = assignments.getMemberAssignment(0) assert mbrAssign is not None assert mbrAssign.memberId == 0 assert mbrAssign.topics == {'locations': [0, 1]} mbrAssign = assignments.getMemberAssignment(5) assert mbrAssign is not None assert mbrAssign.memberId == 5 assert mbrAssign.topics == {'locations': [10, 11]}
def test_assignment_json_serde(): asns = Assignments('test', 8, tp1, 12345, 1) jsonOut = asns.asJson() print(jsonOut) asnsDeser = Assignments.fromJson(jsonOut) assert asns.version == asnsDeser.version assert asns.group == asnsDeser.group assert asns.maxMembers == asnsDeser.maxMembers assert asns.configVersion == asnsDeser.configVersion assert len(asns.memberAssignments) == len(asnsDeser.memberAssignments) assert len(asns.topics) == len(asnsDeser.topics)
def _doReassignment(self, newAssignments: Assignments) -> bool: # Reassigns the consumer given the new assignments if possible. # if false is returned then reassignment failed and the consumer may be # out of sync with the rest of the group if self._memberId is None: memberAssignments = None else: memberAssignments = newAssignments.getMemberAssignment( self._memberId) if memberAssignments is not None: try: logger.debug('Received new assignment. | memberAssignments=%s', memberAssignments) self._cons.assign(memberAssignments) self._assignments = newAssignments self._memberAssignment = memberAssignments self._meta(True) except Exception: logger.exception( 'Failed to perform reassignment. assignments: %s', memberAssignments) return False else: logger.warning( 'Current member ID not found in new assignments. memberId: %s, assignments: %s', self._memberId, newAssignments, ) return False return True
def test_static_membership_assigned_memberid_suddenly_invalid( validMockedMembership): asn, cons, coord, sm = validMockedMembership newAsn = Assignments(asn.group, 4, asn.topics, asn.configVersion, asn.version + 1) test_static_membership_assigned(5, validMockedMembership) coord.assignments.return_value = newAsn state = sm.step() assert state == sm.STATE_NEW
def _updateAssignments(self): # potentially generate new member assignments based on our `StaticConfig` topics = self._fetchAllTopicMetadata(self._conf.topics) if self._assignments is None: assignments = self._coord.assignments(self._meta()) else: assignments = self._assignments # this may be the first run ever for this group so no assignments are available if assignments is None: assignments = Assignments( group=self._conf.group, maxMembers=self._conf.maxGroupSize, topics=topics, configVersion=self._conf.configVersion, version=0, ) changed = True else: # partition counts can change outside of the configuration updates changed = assignments.changeTopicPartitions(topics) # Update the assignments if our configVersion is greater than the one # provided by the coordinator if self._conf.configVersion > assignments.configVersion: # may or may not cause a recalculation of the assignments assignments.changeMaxMembers(self._conf.maxGroupSize) # this must be updated though assignments.configVersion = self._conf.configVersion changed = True if changed: if assignments.version < 1: assignments.version = 1 else: assignments.version += 1 self._coord.updateAssignments(self._meta(), assignments) return assignments
def _processAssignmentsData(self, rawData): if rawData is not None: return Assignments.fromJson(rawData.decode('utf-8'))
def _innerUpdateAssignment(self, assignment: Assignments): self._ensureAssignmentsPath() self.zk.set(self._assignmentsPath, assignment.asJson().encode('utf-8')) logger.info('Assignments updated. | assignments=%s', assignment)
def test_assignment_str(assignments: Assignments): assert 'memberId: 2,' in f'{assignments}' asn = Assignments() assert 'assignments:' in f'{[asn]}'
def test_assignment_change_topics(assignments: Assignments): changed = assignments.changeTopicPartitions(tp2) assert changed changed = assignments.changeTopicPartitions(tp2) assert not changed
def test_assignment_version(assignments: Assignments): av = assignments.assignmentVersion() assert av.configVersion == assignments.configVersion assert av.group == assignments.group assert av.version == assignments.version
def test_assignment_json_bad_input(jsonStr): rs = Assignments.fromJson(jsonStr) assert rs is None
def test_assignment_json_bad(): rs = Assignments.fromJson('{some: bad json}') assert rs is None
def assignments(): return Assignments.fromJson(good_assignment_json)