PyFlink Job slowing down by 66% when increasing pa...
# troubleshooting
r
PyFlink Job slowing down by 66% when increasing parallelism: I am working on a flink cluster with 2 nodes. I am running a simple job of reading a kafka stream, loading a 6kb kmeans pickle running it on a field, and then sinking back to kafka. When I submit the job with parallelism of 1 it processes ~30,000 records/min When I submit the job with parallelism of 2, if all task slots are assigned to same task manager it process ~10,000 records/min When I submit the job with parallelism of 2, if one task slots is on taskmanager01 and the other task slot is on taskmanager02 it processes ~60,000 records/min When I submit the job with parallelism of 4, if all task slots are assigned to same task manager it processes ~10,000 records/min When I submit the job with parallelism of 4, if one task slots is on taskmanager01 and the other 3 task slots are on taskmanager02 it processes ~40,000 records/min It seems that whenever there is more than one task of the job on the same taskmanager it dramatically slows down the job. Here is the code that I'm running:
Copy code
from pyflink.common import Configuration
from pyflink.datastream.connectors import FlinkKafkaConsumer
from pyflink.datastream.connectors import kafka as kfk
from pyflink.common.serialization import SimpleStringSchema
import json
from pyflink.datastream import StreamExecutionEnvironment, RuntimeContext, MapFunction
from pyflink.common.typeinfo import Types

class EmbeddingAndClustering(MapFunction):
    def init(self):
        self.kmeans = None
        self.vectorizer = None
    def open(self, runtime_context: RuntimeContext):
        import pickle
        with open('path/model-tests/kmeans_model_ross.pkl', "rb") as f:
            self.kmeans, self.vectorizer = pickle.load(f)
    def map(self,value):
        value = json.loads(value)
        text = [value['description']]
        vectorized_text = self.vectorizer.transform(text)
        value['incident_cluster_number'] = str(self.kmeans.predict(vectorized_text)[0])
        return json.dumps(value)

config = Configuration()
env = StreamExecutionEnvironment.get_execution_environment(config)
env.set_parallelism(1)
env.add_jars("file:///path/flink-sql-connector-kafka-1.17.1.jar")
properties = {'bootstrap.servers': 'server01:9091', 'group.id': 'group-test'}
source = FlinkKafkaConsumer('ross-test', SimpleStringSchema(), properties).set_start_from_earliest()

topic_stream = env.add_source(source)
speed_stream = topic_stream.map(EmbeddingAndClustering(), output_type = Types.STRING())
speed_stream.sink_to(kafka sink omitted)

env.execute("ross-cluster-test")
A few notes: 1. This pkl model file is only 6kb for testing purposes trained from sklearn's Kmeans. Actual model much larger and slower, but even on small model see similar slowdown. 2. Have confirmed that normal data transfers benefit from increased parallelism & kafka topics partitioned properly 3. I have also tried setting source and sink to parallelism of 1, and the mapfunction to higher parallelisms and still see same slowdown, and backpressure build up in source. 4. The 'description' field is usually between 200 and 2000 words 5. The taskmanager has sufficient memory, threads, and every other observable metric. I've attached a few job config parameters. I can also provide metrics about the job manager/taskmanager/anything else Any help would be greatly appreciated.
r
How many partitions does your Kafka topic have?
With 20GB of heap, you can most likely run at least a parallelism of 10 per TaskManager
E.g. slotsPerTaskManager: 10, taskmanagers: 2, parallelism of the job: 20 Does the python api support rebalancing? E.g. can you change this line to
Copy code
topic_stream = env.add_source(source).rebalance()
so that we are not limited by the number of partitions in Kafka
r
Thank you very much for the response, Kafka has 20 partitions, both source and sink. Doing
Copy code
env.set_parallelism(10) and topic_stream = env.add_source(source).rebalance():
Still see 66% slowdown, only 10,000 records per minute. See first screenshot. Doing:
Copy code
topic_stream = env.add_source(source).set_parallelism(1).rebalance()
speed_stream = topic_stream.map(EmbeddingAndClustering(), output_type = Types.STRING()).set_parallelism(10)
speed_stream.sink_to(kafka sink omitted).set_parallelism(1)
We still see the slowdown of 66% and that it's the mapfunction part that causes the bottleneck in the 2nd screen shot. Please let me know if you'd like any more information, or have any ideas.
Subtask metrics for that last job after 10 mins:
r
Are the subtask metrics for the "map" operator?
This screenshot is super helpful. I guess you've disabled operator chaining here -- you can see that the "map" operator is causing the low throughput
@Dian Fu @Xingbo Huang Do you guys have an explanation for the performance regression when running multiple slots on the same task manger? is it possible that there is only a single python process per TaskManager instance, instead of a python process per Flink task?
d
+1 to @rmetzger’s analysis. I also suspect that the performance decrease comes from the serialization/deserialization between the source operator and map operator.
is it possible that there is only a single python process per TaskManager instance, instead of a python process per Flink task?
@rmetzger There will be one python process one task slot. So event if two subtasks are scheduled to the same TaskManager, it sill has two python process. @Ross Ellison Since there are 20 partitions for the source, could you set the parallelism of the source operator to the same as the map operator to make sure they are chained together and test again?
r
@Dian Fu Sorry for the late response, I was on vacation the past week and can respond promptly now. I greatly appreciate your reply. Setting parallelism of the source, map, and sink operator to 2 results in 66% slowdown compared to parallelism of 1. See attached screenshot. However if each task gets assigned to a different taskmanager, there is a 2x speed increase. If both tasks are on the same taskmanager I see a 66% slowdown. Seems like whenever multiple tasks are on the same taskmanager there is a huge slowdown. Can run more tests and will respond much more quickly. Thank you.