Problem statement

The module adds event subscribers so that other modules can interact with data before sending them to akamai. Module also uses CacheTagFormatter to applying formatting to the cache tags before sending to akamai so that tags can work correctly with akamai.

Both these features are good but there is an inconsistency in AkamaiTagPurger::invalidate() method where it applies formatting first to the cache tags and then dispatch the events for changing them:

This causes following problems:

  1. Event subscribers can be expecting tag in the format of node:1 but instead they will get node_1 and their manipulation could fail.
  2. Event subscribers could add extra tags which will not be in proper format to work with akamai and can cause issues.

Proposed Resolution

Apply the formatting to cache tags after they have been altered with event subscribers.

Issue fork akamai-3160999

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

guptahemant created an issue. See original summary.

guptahemant’s picture

Issue summary: View changes
guptahemant’s picture

Assigned: guptahemant » Unassigned
Category: Feature request » Bug report
Status: Active » Needs review
StatusFileSize
new1.39 KB

Here is a patch which fixes this issue.

Please review

Status: Needs review » Needs work

The last submitted patch, 3: fix_formatting-3160999-3.patch, failed testing. View results

Anonymous’s picture

Looks good to me!

Maybe one very small remark

  • Rename $formatted_tags to $formatted_tags_to_clear
  • Do we need to alter the test? Failing now.
guptahemant’s picture

StatusFileSize
new1.99 KB

Thanks for review @wouter.adem,
Attaching an updated patch containing fix for the failing test case and changing the variable name.

Please review

guptahemant’s picture

Status: Needs work » Needs review
Anonymous’s picture

The fix is ok.

The test has some typos, I think. Should + ['foo', 'bar', 123, '234', 456, true, 'on_purge_creation'], not be + ['foo', 'bar', '123', '234', '456', true, 'on_purge_creation'],. Also, why was true added and other tags left out?

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community
pobster’s picture

This is a great catch, thank you for the patch! I'd really like a test for something being altered/ added though?

pobster’s picture

Status: Reviewed & tested by the community » Needs work

Okay, if you can't get round to making a test - I'll see if I can find time over the holidays.

guptahemant’s picture

HI @pobster

Until now i have not been able to find time to implement a test for the same, Also i am in the process of learning for writing tests,
In upcoming weeks will try to figure out how we can include an automated test functionality for this, Since we won't be directly able to use event subscribers(current test implementation) to test it completely.

WidgetsBurritos’s picture

Version: 8.x-3.x-dev » 4.x-dev
WidgetsBurritos’s picture

In addition the feedback from @pobster above, I've noticed one other issue with this solution:

     $formatter = \Drupal::service('akamai.helper.cachetagformatter');
     foreach ($invalidations as $invalidation) {
       $invalidation->setState(InvalidationInterface::PROCESSING);
-      $tag = $formatter->format($invalidation->getExpression());
+      $tag = $invalidation->getExpression();
       if (mb_strlen($tag) > 128) {
         $this->logger->warning('Cache Tag %tag has exceeded the Akamai 128 character tag maximum length.', ['%tag' => $tag]);
       }

Since the formatted version is what is getting sent to Akamai, the mb_strlen($tag) check needs to occur after the formatting, not before.

In its present state CacheTagFormatter::format() shouldn't modify the length of the cache tag at all, but we can't guarantee that will always be the case, especially if #3154613: Add cache-tag prefix is introduced.

So we need to move the mb_strlen($tag) check inside this loop instead:

+    // Apply the formatting after the tags have been altered.
+    $formatted_tags_to_clear = [];
+    foreach ($tags_to_clear as $tag) {
+      $formatted_tags_to_clear[] = $formatter->format($tag);
+    }
smitghelani’s picture

StatusFileSize
new1.92 KB

This patch #6 is not working for the latest version 5.x, so created new patch with same code changes mentioned inside the #6. This is working for me.

smitghelani’s picture

Version: 4.x-dev » 5.0.0-rc2
smitghelani’s picture

StatusFileSize
new1.99 KB

inside comment #15 i had missed 1 line to remove so adding new patch for the same.

pobster’s picture

I'm afraid it's still going to need a specific test though.

Plus, see this previous comment:

Since the formatted version is what is getting sent to Akamai, the mb_strlen($tag) check needs to occur after the formatting, not before.

smustgrave’s picture

Version: 5.0.0-rc2 » 6.0.x-dev

smustgrave’s picture

Status: Needs work » Needs review

Disclosure used AI to help right the unit test.

But during review found that typecasting to (string) means we don't need to update the one test

pobster’s picture

Assigned: Unassigned » smustgrave
Status: Needs review » Reviewed & tested by the community

Sure, LGTM

  • smustgrave committed db4e0960 on 6.0.x
    fix: #3160999 In Akamai tag purger apply formatting after the tags has...
smustgrave’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.