class DefaultPartitioner implements Partitioner {

    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);// 先从集群中取出所有分区
        int numPartitions = partitions.size();
        if (keyBytes == null) {            // 没有key的情况走这个分支
            int nextValue = counter.getAndIncrement(); // 计数服务+1
            List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic);// 取出可用的分区
            if (availablePartitions.size() > 0) {
                int part = DefaultPartitioner.toPositive(nextValue) % availablePartitions.size();
                return availablePartitions.get(part)
                                          .partition();
            } else {
                // no partitions are available, give a non-available partition
                // 没有可用的分区，只能返回一个不可用的分区
                return DefaultPartitioner.toPositive(nextValue) % numPartitions;
            }
        } else {
            // hash the keyBytes to choose a partition
            return DefaultPartitioner.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
        }
    }

}