Ross Ellison
03/26/2024, 7:44 PMfrom 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.rmetzger
03/27/2024, 3:47 PMrmetzger
03/27/2024, 3:48 PMrmetzger
03/27/2024, 3:50 PMtopic_stream = env.add_source(source).rebalance()
so that we are not limited by the number of partitions in KafkaRoss Ellison
03/27/2024, 5:32 PMenv.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:
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.Ross Ellison
03/27/2024, 5:43 PMrmetzger
04/02/2024, 8:18 AMrmetzger
04/02/2024, 8:19 AMrmetzger
04/02/2024, 8:21 AMDian Fu
04/03/2024, 1:46 AMis 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?
Ross Ellison
04/09/2024, 3:44 PM