-
Notifications
You must be signed in to change notification settings - Fork 13.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(reports): Clear last value when state is WORKING #19941
Merged
john-bodley
merged 2 commits into
apache:master
from
john-bodley:john-bodley--fix-report-last-value
May 13, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
superset/migrations/versions/cbe71abde154_fix_report_schedule_and_log.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# 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. | ||
"""fix report schedule and execution log | ||
|
||
Revision ID: cbe71abde154 | ||
Revises: a9422eeaae74 | ||
Create Date: 2022-05-03 19:39:32.074608 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "cbe71abde154" | ||
down_revision = "a9422eeaae74" | ||
|
||
from alembic import op | ||
from sqlalchemy import Column, Float, Integer, String, Text | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
from superset import db | ||
from superset.models.reports import ReportState | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class ReportExecutionLog(Base): | ||
__tablename__ = "report_execution_log" | ||
|
||
id = Column(Integer, primary_key=True) | ||
state = Column(String(50), nullable=False) | ||
value = Column(Float) | ||
value_row_json = Column(Text) | ||
|
||
|
||
class ReportSchedule(Base): | ||
__tablename__ = "report_schedule" | ||
|
||
id = Column(Integer, primary_key=True) | ||
last_state = Column(String(50)) | ||
last_value = Column(Float) | ||
last_value_row_json = Column(Text) | ||
|
||
|
||
def upgrade(): | ||
bind = op.get_bind() | ||
session = db.Session(bind=bind) | ||
|
||
for schedule in ( | ||
session.query(ReportSchedule) | ||
.filter(ReportSchedule.last_state == ReportState.WORKING) | ||
.all() | ||
): | ||
schedule.last_value = None | ||
schedule.last_value_row_json = None | ||
|
||
session.commit() | ||
|
||
for execution_log in ( | ||
session.query(ReportExecutionLog) | ||
.filter(ReportExecutionLog.state == ReportState.WORKING) | ||
.all() | ||
): | ||
execution_log.value = None | ||
execution_log.value_row_json = None | ||
|
||
session.commit() | ||
session.close() | ||
|
||
|
||
def downgrade(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,35 +94,39 @@ def __init__( | |
self._start_dttm = datetime.utcnow() | ||
self._execution_id = execution_id | ||
|
||
def set_state_and_log( | ||
def update_report_schedule_and_log( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I felt that |
||
self, | ||
state: ReportState, | ||
error_message: Optional[str] = None, | ||
) -> None: | ||
""" | ||
Updates current ReportSchedule state and TS. If on final state writes the log | ||
for this execution | ||
Update the report schedule state et al. and reflect the change in the execution | ||
log. | ||
""" | ||
now_dttm = datetime.utcnow() | ||
self.set_state(state, now_dttm) | ||
self.create_log( | ||
state, | ||
error_message=error_message, | ||
) | ||
|
||
def set_state(self, state: ReportState, dttm: datetime) -> None: | ||
self.update_report_schedule(state) | ||
self.create_log(error_message) | ||
|
||
def update_report_schedule(self, state: ReportState) -> None: | ||
""" | ||
Set the current report schedule state, on this case we want to | ||
commit immediately | ||
Update the report schedule state et al. | ||
|
||
When the report state is WORKING we must ensure that the values from the last | ||
execution run are cleared to ensure that they are not propagated to the | ||
execution log. | ||
""" | ||
|
||
if state == ReportState.WORKING: | ||
self._report_schedule.last_value = None | ||
self._report_schedule.last_value_row_json = None | ||
|
||
self._report_schedule.last_state = state | ||
self._report_schedule.last_eval_dttm = dttm | ||
self._report_schedule.last_eval_dttm = datetime.utcnow() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to pass in the timestamp which isn't used elsewhere. |
||
|
||
self._session.merge(self._report_schedule) | ||
self._session.commit() | ||
|
||
def create_log( | ||
self, state: ReportState, error_message: Optional[str] = None | ||
) -> None: | ||
def create_log(self, error_message: Optional[str] = None) -> None: | ||
""" | ||
Creates a Report execution log, uses the current computed last_value for Alerts | ||
""" | ||
|
@@ -132,7 +136,7 @@ def create_log( | |
end_dttm=datetime.utcnow(), | ||
value=self._report_schedule.last_value, | ||
value_row_json=self._report_schedule.last_value_row_json, | ||
state=state, | ||
state=self._report_schedule.last_state, | ||
error_message=error_message, | ||
report_schedule=self._report_schedule, | ||
uuid=self._execution_id, | ||
|
@@ -490,17 +494,19 @@ class ReportNotTriggeredErrorState(BaseReportState): | |
initial = True | ||
|
||
def next(self) -> None: | ||
self.set_state_and_log(ReportState.WORKING) | ||
self.update_report_schedule_and_log(ReportState.WORKING) | ||
try: | ||
# If it's an alert check if the alert is triggered | ||
if self._report_schedule.type == ReportScheduleType.ALERT: | ||
if not AlertCommand(self._report_schedule).run(): | ||
self.set_state_and_log(ReportState.NOOP) | ||
self.update_report_schedule_and_log(ReportState.NOOP) | ||
return | ||
self.send() | ||
self.set_state_and_log(ReportState.SUCCESS) | ||
self.update_report_schedule_and_log(ReportState.SUCCESS) | ||
except CommandException as first_ex: | ||
self.set_state_and_log(ReportState.ERROR, error_message=str(first_ex)) | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, error_message=str(first_ex) | ||
) | ||
# TODO (dpgaspar) convert this logic to a new state eg: ERROR_ON_GRACE | ||
if not self.is_in_error_grace_period(): | ||
try: | ||
|
@@ -509,12 +515,12 @@ def next(self) -> None: | |
f" {self._report_schedule.name}", | ||
str(first_ex), | ||
) | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, | ||
error_message=REPORT_SCHEDULE_ERROR_NOTIFICATION_MARKER, | ||
) | ||
except CommandException as second_ex: | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, error_message=str(second_ex) | ||
) | ||
raise first_ex | ||
|
@@ -533,13 +539,13 @@ class ReportWorkingState(BaseReportState): | |
def next(self) -> None: | ||
if self.is_on_working_timeout(): | ||
exception_timeout = ReportScheduleWorkingTimeoutError() | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, | ||
error_message=str(exception_timeout), | ||
) | ||
raise exception_timeout | ||
exception_working = ReportSchedulePreviousWorkingError() | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.WORKING, | ||
error_message=str(exception_working), | ||
) | ||
|
@@ -560,33 +566,35 @@ class ReportSuccessState(BaseReportState): | |
def next(self) -> None: | ||
if self._report_schedule.type == ReportScheduleType.ALERT: | ||
if self.is_in_grace_period(): | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.GRACE, | ||
error_message=str(ReportScheduleAlertGracePeriodError()), | ||
) | ||
return | ||
self.set_state_and_log(ReportState.WORKING) | ||
self.update_report_schedule_and_log(ReportState.WORKING) | ||
try: | ||
if not AlertCommand(self._report_schedule).run(): | ||
self.set_state_and_log(ReportState.NOOP) | ||
self.update_report_schedule_and_log(ReportState.NOOP) | ||
return | ||
except CommandException as ex: | ||
self.send_error( | ||
f"Error occurred for {self._report_schedule.type}:" | ||
f" {self._report_schedule.name}", | ||
str(ex), | ||
) | ||
self.set_state_and_log( | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, | ||
error_message=REPORT_SCHEDULE_ERROR_NOTIFICATION_MARKER, | ||
) | ||
raise ex | ||
|
||
try: | ||
self.send() | ||
self.set_state_and_log(ReportState.SUCCESS) | ||
self.update_report_schedule_and_log(ReportState.SUCCESS) | ||
except CommandException as ex: | ||
self.set_state_and_log(ReportState.ERROR, error_message=str(ex)) | ||
self.update_report_schedule_and_log( | ||
ReportState.ERROR, error_message=str(ex) | ||
) | ||
|
||
|
||
class ReportScheduleStateMachine: # pylint: disable=too-few-public-methods | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@john-bodley do you think there's a chance that if there is a report that's in a working state, that we could potentially wind up altering the value after the report has completed? Just based off your migration timing of around a minute and most of these reports take under a minute to run, we may wind up with some reports that wind up with a completed state and no value, i.e., the commit happens here after the state has changed on the report side. Just wondering if it's safer not to change reports that are in progress.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eschutho I guess with any migration there's a risk of a race condition happening whilst the migration is running. I've replicated the
session.commit()
to ensure that there's also a commit directly after theReportSchedule
which likely only has a handful of reports inWORKING
state.