Problem/Motivation

On a migration with highwater "changed" handling i experienced that some source items below highwater are imported.
Debugging showed that highwater is saved for every item, while it should only for higher-water items.

Proposed resolution

Fix.

Remaining tasks

Fix

User interface changes

None

API changes

None

Data model changes

None

Comments

axel.rutz created an issue. See original summary.

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new1011 bytes

Patch flying in that should fix this.

geek-merlin’s picture

Patch applies cleanly on a separate box and fixes the problem for me.
The code did not change for ages so patch should work as well against 8.6.x.

heddn’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Highwater is a tough nut and is a traditional area of bugs. Can we add a test, as we obviously don't have anything that is broken/fixed with this change.

mikeryan’s picture

The intent (which was documented for the D7 migrate module, but does not appear to be documented for D8) is that when using highwater marks you need to sort your source data by the field you're using for your highwater. Then, setting the highwater for every row processed guarantees that you don't remigrate stuff, and if the migration is interrupted unexpectedly you don't miss anything. Well, there is a boundary condition if more than one row has the same value in the highwater field - for safety's sake, they'll all be migrated, even if they were before.

For unsorted data, this patch will indeed prevent unnecessary remigration. However, it will cause you to miss rows that *should* be remigrated, if they've changed since the previous migration (i.e., their highwater field's value is higher than the highwater from that migration), if they come after rows with even higher highwater fields.

The general rule of thumb:

  1. track_changes is the most reliable method to detect changed rows
  2. If your data has a field which reliably increases with each change (practically speaking this is nearly always a timestamp of course) and you can sort by that field, then highwater marks are a more efficient way to detect changed rows.
quietone’s picture

@mikeryan, thanks. Always love your summaries.

Right, so plugins extending from SqlBase.php do have the orderBy clause automatically added but those extending from SourcePluginBase will not.

mikeryan’s picture

Title: Highwater malfunction in SourcePluginBase » Document need to sort source data when using highwater marks
Status: Needs work » Active
Issue tags: -Needs tests +Novice

I've updated the doc page at https://www.drupal.org/docs/8/api/migrate-api/migrate-api-overview#comme.... The high_water_property doc in SourcePluginBase should at a minimum state that sorted source data is required for highwater marks to work correctly. It might also be good to incorporate the rule of thumb above.

heddn’s picture

Status: Active » Needs work

I take it then, we need to roll a new patch for the docs, not necessarily change the code in the way we have it listed in the latest patch?

geek-merlin’s picture

Yess, thanks a lot @mikeryan for the explanation in #5, it perfectly makes sense to me (and patch #2 is obsolete).

As of #6:
> Right, so plugins extending from SqlBase.php do have the orderBy clause automatically added but those extending from SourcePluginBase will not.

Ah, in my case i use ContentEntity as source (which derives from SourcePluginBase) and use 'changed' as highwater - not too uncommon. Should we then add a feature request for ContentEntity to sort according to highwater (or add a sort key)?

mikeryan’s picture

mikeryan’s picture

I take it then, we need to roll a new patch for the docs, not necessarily change the code in the way we have it listed in the latest patch?

I was going to say yes, but the code change wouldn't hurt - it would actually be a (very slight) performance improvement in that the highwater mark wouldn't get rewritten when you have multiple rows with the same highwater value. And it would make unsorted data break less (which may not be a good thing - you'd be less likely to notice that it was indeed broken). I don't have a strong opinion on this...

geek-merlin’s picture

> And it would make unsorted data break less (which may not be a good thing - you'd be less likely to notice that it was indeed broken). I don't have a strong opinion on this...

As you explained in #5, in the case of an interrupted migration this would lead to an invalid system state, and spradic, hard-to-track malfunctions. We should not allow that. If a source plugin uses a highwater property, having it sort by that must be part of the contract. And if it does not, it should not be silently ignored.

This leads to
* change this patch so that a decrease of highwatermark leads to an exception
* fix core source plugins to respect the highwater contract to sort by the configured property

geek-merlin’s picture

Title: Document need to sort source data when using highwater marks » Enforce need to sort source data when using highwater marks

What u mean?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

narendra.rajwar27’s picture

Assigned: Unassigned » narendra.rajwar27
narendra.rajwar27’s picture

Assigned: narendra.rajwar27 » Unassigned

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

This is not a novice issue and it needs an issue summary update.

wim leers’s picture

+++ b/core/modules/migrate/src/Plugin/migrate/source/SourcePluginBase.php
@@ -383,7 +383,7 @@ public function next() {
-      if ($this->getHighWaterProperty()) {
+      if ($this->aboveHighwater($row)) {

OMG yes, I wrote about this same problem in
#2859314-21: Highwater condition with unjoined maps skips unprocessed and NEEDS_UPDATE rows:

And since later rows are processed, and it's \Drupal\migrate\Plugin\migrate\source\SourcePluginBase::next() that updates the high water mark upon accessing the source, not upon saving the mapping, then any unprocessed rows with high water property values below the high water mark are simply never even considered — they're filtered away in the source plugin's query!

But as you showed here, the problem is even bigger: not only is it saving the high water before a row is processed, it is simply overwriting whatever the previous value was, no matter if it's greater than or lesser than 😨

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.