## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.importjsonfromsqlalchemyimportBigInteger,Column,Index,Integer,String,Textfromairflow.configurationimportconffromairflow.exceptionsimportAirflowExceptionfromairflow.models.baseimportID_LEN,Basefromairflow.utilsimporttimezonefromairflow.utils.sessionimportprovide_sessionfromairflow.utils.sqlalchemyimportUtcDateTimefromairflow.utils.stateimportState
[docs]classSensorInstance(Base):""" SensorInstance support the smart sensor service. It stores the sensor task states and context that required for poking include poke context and execution context. In sensor_instance table we also save the sensor operator classpath so that inside smart sensor there is no need to import the dagbag and create task object for each sensor task. SensorInstance include another set of columns to support the smart sensor shard on large number of sensor instance. The key idea is to generate the hash code from the poke context and use it to map to a shorter shard code which can be used as an index. Every smart sensor process takes care of tasks whose `shardcode` are in a certain range. """
[docs]defget_classpath(obj):""" Get the object dotted class path. Used for getting operator classpath. :param obj: :return: The class path of input object :rtype: str """module_name,class_name=obj.__module__,obj.__class__.__name__returnmodule_name+"."+class_name
@classmethod@provide_session
[docs]defregister(cls,ti,poke_context,execution_context,session=None):""" Register task instance ti for a sensor in sensor_instance table. Persist the context used for a sensor and set the sensor_instance table state to sensing. :param ti: The task instance for the sensor to be registered. :param poke_context: Context used for sensor poke function. :param execution_context: Context used for execute sensor such as timeout setting and email configuration. :param session: SQLAlchemy ORM Session :return: True if the ti was registered successfully. :rtype: Boolean """ifpoke_contextisNone:raiseAirflowException('poke_context should not be None')encoded_poke=json.dumps(poke_context)encoded_execution_context=json.dumps(execution_context)sensor=(session.query(SensorInstance).filter(SensorInstance.dag_id==ti.dag_id,SensorInstance.task_id==ti.task_id,SensorInstance.execution_date==ti.execution_date,).with_for_update().first())ifsensorisNone:sensor=SensorInstance(ti=ti)sensor.operator=ti.operatorsensor.op_classpath=SensorInstance.get_classpath(ti.task)sensor.poke_context=encoded_pokesensor.execution_context=encoded_execution_contextsensor.hashcode=hash(encoded_poke)sensor.shardcode=sensor.hashcode%conf.getint('smart_sensor','shard_code_upper_limit')sensor.try_number=ti.try_numbersensor.state=State.SENSINGsensor.start_date=timezone.utcnow()session.add(sensor)session.commit()returnTrue
@property
[docs]deftry_number(self):""" Return the try number that this task number will be when it is actually run. If the TI is currently running, this will match the column in the database, in all other cases this will be incremented. """# This is designed so that task logs end up in the right file.ifself.stateinState.running:returnself._try_numberreturnself._try_number+1