class ProducerPolicy(Response): SCHEMA = Schema(('nearby', Boolean), ('single', Boolean), ('archive', Boolean), ('weight', Array(Weight)), ('blackList', Array(UTF8String)), ('timeout', Int32)) TYPE = 'Producer Policy'
class Topic(Response): SCHEMA = Schema(('topicCode', UTF8String), ('hasProducerPolicy', Boolean), ('producerPolicy', ProducerPolicy), ('hasConsumerPolicy', Boolean), ('consumerPolicy', ConsumerPolicy), ('type', Int8), ('partitionGroups', Array(PartitionGroup)), ('code', Int32)) TYPE = 'Topic' def __init__(self, topic_code, hasProducerPolicy, producerPolicy,hasConsumerPolicy,consumerPolicy,type,partitionGroups,code): self.topicCode = topic_code self.hasProducerPolicy = hasProducerPolicy self.producerPolicy = producerPolicy self.hasConsumerPolicy = hasConsumerPolicy self.consumerPolicy = consumerPolicy self.type = type self.partitionGroups = partitionGroups self.code = code self.encode = WeakMethod(self._encode_self) def _encode_self(self): list = [] list.append(self.SCHEMA.fields[0].encode(self.code)) list.append(self.SCHEMA.fields[1].encode(self.hasProducerPolicy)) if self.hasProducerPolicy: list.append(self.SCHEMA.fields[2].encode(self.producerPolicy)) list.append(self.SCHEMA.fields[3].encode(self.hasConsumerPolicy)) if self.hasConsumerPolicy: list.append(self.SCHEMA.fields[4].encode(self.consumerPolicy)) list.append(self.SCHEMA.fields[5].encode(self.type)) list.append(self.SCHEMA.fields[6].encode(self.partitionGroups)) return b''.join(list) @classmethod def encode(cls, item): it = cls(*item) return it.encode() @classmethod def decode(cls, data): if isinstance(data, bytes): data = BytesIO(data) base_fields = cls.SCHEMA.fields[0:2] topic_code, has_producer_policy = [f.decode(data) for f in base_fields] producerPolicy = None consumerPolicy = None if has_producer_policy: producerPolicy = cls.SCHEMA.fields[2].decode(data) has_consumer_policy = cls.SCHEMA.fields[3].decode(data) if has_consumer_policy: consumerPolicy = cls.SCHEMA.fields[4].decode(data) type, partitionGroups, code = [f.decode(data) for f in cls.SCHEMA.fields[5:]] return cls(topic_code, has_producer_policy, producerPolicy, has_consumer_policy, consumerPolicy, type, partitionGroups, code)
class ProduceMessageRequest(Request): SCHEMA = Schema(('topics', Array(TopicProduceMessage)), ('app', UTF8String)) TYPE = PRODUCE_MESSAGE_REQUEST @classmethod def default_messages_request(cls, topic, producer, msg): msgs = [(msg, 'deault_bussiness_id', None, None, producer)] topic_messages = [(topic, None, 5000, 2, msgs)] return ProduceMessageRequest(topic_messages, producer)
class FetchMessageRequest(Request): SCHEMA = Schema(('topics', Array(FetchTopic)), ('app', UTF8String), ('ackTimeout', Int32), ('longPollTimeout', Int32)) TYPE = FETCH_TOPIC_MESSAGE_REQUEST @classmethod def default_fetch_message_request(cls, topic, app, batch_size): topics = [(topic, batch_size)] fetch_message = (topics, app, 5000, 10000) return FetchMessageRequest(*fetch_message)
class ConsumerPolicy(Response): SCHEMA = Schema(('nearby', Boolean), ('pause', Boolean), ('archive', Boolean), ('retry', Boolean), ('seq', Boolean), ('ackTimeout', Int32), ('backSize', Int16), ('concurrent', Boolean), ('concurrentSize', Int32), ('delay', Int32), ('backList', Array(UTF8String)), ('errTimes', Int32), ('maxPartitionNum', Int32), ('readRetryProportion', Int32)) TYPE = 'Consumer policy'
class ConsumerResponse(Response): SCHEMA = Schema(('consumerIds', Array(ConsumerId))) TYPE = ADD_CONSUMER_RESPONSE
class ProducerResponse(Response): SCHEMA = Schema(('producerIds', Array(ProducerId))) TYPE = ADD_PRODUCER_RESPONSE
class ConsumerRequest(Request): SCHEMA = Schema(('topics', Array(UTF8String)), ('app', UTF8String), ('sequence', Int64)) TYPE = ADD_CONSUMER_REQUEST
class MetadataResponse(Response): SCHEMA = Schema(('topics', Array(Topic)), ('brokers', Array(Broker))) TYPE = FETCH_CLUSTER_RESPONSE
class ProducerRequest(Request): SCHEMA = Schema(('topics', Array(UTF8String)), ('app', UTF8String), ('sequence', Int64)) TYPE = ADD_PRODUCER_REQUEST
class FetchMessageResponse(Response): SCHEMA = Schema(('data', Array(TopicMessage))) TYPE = FETCH_TOPIC_MESSAGE_RESPONSE
class MetadataRequest(Request): # STR = String('utf-8') SCHEMA = Schema(('topics', Array(UTF8String)), ('app', UTF8String)) TYPE = FETCH_CLUSTER_REQUEST
class ProduceMessageResponse(Response): SCHEMA = Schema(('data', Array(TopicProducePartitionAck))) TYPE = PRODUCE_MESSAGE_RESPONSE
def test_array_string(): topics = ['test_topic'] bytes=Array(String()).encode(topics) print(bytes)
class ConsumeAck(Request): SCHEMA = Schema(('topics', Array(ConsumeTopicPartitionGroupAckResponse)), ('app', UTF8String)) TYPE = COMMIT_ACK_REQUEST
class PartitionGroup(Response): SCHEMA = Schema(('id', Int32), ('leader', Int32), ('partitions', Array(Int16))) TYPE = 'Partition Group'
class ConsumeTopicPartitionGroupAckResponse(Response): SCHEMA = Schema(('topic', UTF8String), ('partitions', Array(ConsumePartitionAckResponse))) TYPE = 'Consume topic partition group ack'
class ConsumePartitionAckResponse(Response): SCHEMA = Schema(('partition', Int16), ('data', Array(ConsumePartitionIndexAckResponse))) TYPE = 'Consume partition ack'
class ConsumePartitionGroupAck(Request): SCHEMA = Schema(('partition', Int16), ('data', Array(ConsumePartitionAck))) TYPE = 'Consume partition group ack'
class TopicProducePartitionAck(Request): SCHEMA = Schema(('topic', UTF8String), ('code', Int32), ('result', Array(ProducePartitionAck))) TYPE = 'Topic partition ack'
def test_struct_array(): partition_groups = ((1, 1000000889, (1, 2)), (2, 1000000889, (3, 4))) pg = Array(PartitionGroup) bytes = pg.encode(partition_groups) receive = pg.decode(bytes) assert pg == receive
class ConsumeTopicPartitionAckState(Response): SCHEMA = Schema(('topic', UTF8String), ('partitionData', Array(ConsumePartitionAckState))) TYPE = 'Consume topic partition ack state'
class TopicMessage(Request): SCHEMA = Schema(('topic', UTF8String), ('messages', Array(Message)), ('code', Int32)) TYPE = 'Topic produce message'
class ConsumeAckResponse(Response): SCHEMA = Schema(('data', Array(ConsumeTopicPartitionAckState))) TYPE = COMMIT_ACK_RESPONSE
class TopicProduceMessage(Request): SCHEMA = Schema(('topic', UTF8String), ('txId', UTF8String), ('timeout', Int32), ('qosLevel', Int8), ('messages', Array(Message))) TYPE = 'Topic produce message'