Changes to block caching mode not caught

he_who_shall_no... - March 18, 2008 - 08:35
Project:Drupal
Version:7.x-dev
Component:block.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs work
Issue tags:DX (Developer Experience), needs backport to D6
Description

I updated one of my modules to have BLOCK_NO_CACHE activated.

After the module updated the block didn't changed its behavior. After a manual modification of the cache column from the blocks table it started to work as expected.

How can I modify the install/update script to take the new settings?

#1

he_who_shall_no... - March 18, 2008 - 09:54

Solved with:

<?php
function messenger_update_6006() {
 
$ret = array();

 
db_query('UPDATE {blocks} SET cache=-1 WHERE module=\'messenger\'');

  return
$ret;
}
?>

but it looks tricky for me.

#2

he_who_shall_no... - March 19, 2008 - 06:25
Category:support request» bug report

I look at in few contributed modules. Some of them changes the block cache from a the block's configuration screen, but no change in the {blocks} table.

Searched all the Drupal code to see where the cache column gets updated, but there is no such a thing.

Transform this to a bug report.

#3

BryanSD - May 2, 2008 - 07:10
Version:6.1» 6.2

I have observed this same behavior, while looking at some issues with the "similar entries" module: http://drupal.org/node/253299 . Though I wonder, if this is really more of an oversight by some contributed modules in not following the new Schema API in D6 ( http://drupal.org/node/114774#schema-api ) as well as new formats for hook_install(), hook_uninstall(), and hook_update_N().

Either way, when a module is first enabled the block cache setting specified though hook_block($op = 'list') appears to write to the block table just fine. It is when there is a change in a module for the 'cache' setting that appears to be the problem (such as $blocks[0]['cache'] = BLOCK_CACHE_PER_ROLE; to $blocks[0]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE; ). While a contributed module could makes this change via a db_query, shouldn't the change in the block cache setting be recognized by the hook_block? Who carries the burden, the core or the contributed module for making the change in the block table?

#4

yched - May 9, 2008 - 21:39
Title:modules update to BLOCK_NO_CACHE» modules update to a different block caching mode

More generic title - issue is not specific to BLOCK_NO_CACHE

#5

mfb - May 13, 2008 - 22:34

Any thoughts on when this syncronization between module block hooks and {blocks} cache column should/could happen? For example, when "Save blocks" or "Clear cached data" are clicked.

#6

BryanSD - May 14, 2008 - 02:30

Ideally, wouldn't you want the "sync" to take place while running an update.php?

#7

mfb - May 14, 2008 - 17:14

@BryanSD: This issue doesn't arise only during update, e.g. Views lets a user tweak their block cache settings whenever they want -- http://drupal.org/node/257267

So, should modules be responsible for directly updating the blocks table via SQL? If so then we can go back to the Views issue and add the required SQL. Although this seems less than ideal, there could be some API for modules to use or the cache settings could be automatically refreshed at certain points in the admin interface.

#8

yched - June 28, 2008 - 16:57
Title:modules update to a different block caching mode» changes to block caching mode not caught
Status:active» needs review

Attached patch lets _block_rehash() update the 'cache' column. IIRC, this was the case when the block cache patch was initially committed, but got lost in a later _block_rehash() refactoring.

As mfb points out, this is needed so that Views-defined blocks can be cached (and those likely to be the ones that most benefit from caching...)

However, _block_rehash() only acts on the current theme, so a user updating the cache setting for his views-blocks still needs to visit the 'admin blocks' page for all his themes for the change to be effective...

I'm thinking of an additional block_update_cache_mode($module, $delta) API function, that would update the cache mode for all enabled themes. This function could also be used by module updates.
Gabor, Earl, what do you think ?

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-8.patch1.89 KBIdleFailed on MySQL 5.0 ISAM, with: 14,787 pass(es), 0 fail(s), and 604 exception(es).View details | Re-test

#9

yched - June 28, 2008 - 17:02
Version:6.2» 7.x-dev

Needs to be fixed in D7 first.
Same patch applies with offset, but here it is rerolled for D7

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-9.patch1.83 KBIdleFailed on MySQL 5.0 ISAM, with: 14,781 pass(es), 0 fail(s), and 604 exception(es).View details | Re-test

#10

merlinofchaos - June 28, 2008 - 18:26

Could we fix _block_rehash so that it can accept a theme key, using the global $theme by default? That way we could loop over osmething like list_themes() to rehash blocks for all themes.

#11

yched - June 28, 2008 - 23:38
Status:needs review» needs work

Maybe - not sure that's the best way to get fast D6 acceptance :-/

Well, previous patch precisely had a bug making _block_rehash() update {blocks}.cache for all themes (that's what we want to do, but not the way we want it...). Fixed in attached patch.

Switching to CNW until we have an acceptable API solution for 'update all themes'. I'll work on the suggestion in #10, feedback welcome meanwhile (for instance from Gabor :-) )

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-11.patch1.86 KBIdleFailed on MySQL 5.0 ISAM, with: 14,743 pass(es), 0 fail(s), and 604 exception(es).View details | Re-test

#12

yched - July 1, 2008 - 01:03
Status:needs work» needs review

Attached patch builds on Earl's suggestion in #10 :
_block_rehash($theme) acts on one theme
_block_rehash('**ALL**') acts on all themes.

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-12.patch4.64 KBIdleFailed: Failed to apply patch.View details | Re-test

#13

merlinofchaos - July 1, 2008 - 02:53

Since theme keys must be strings, I would recommend the use of TRUE (using === TRUE) as an easy flag to do all themes rather than the all string used here.

#14

yched - July 1, 2008 - 12:52

Fair enough. Updated patch does :
- _block_rehash('garland') rehashes for garland
- _block_rehash() rehashes for the current theme
- _block_rehash(TRUE) rehashes for all themes.

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-14.patch5.12 KBIdleFailed: 7107 passes, 61 fails, 5 exceptionsView details | Re-test

#15

Damien Tournoud - July 1, 2008 - 13:05

hum. There is still a dsm() call in there.

Moreover, what's the use of _block_rehash(TRUE)? It doesn't seem to be used anywhere in the patch...

On that matter, I don't even see the use of the TRUE parameter, if we can simply do:

<?php
 
foreach (list_themes() as $theme) {
    if (
$theme->status || $theme->name == variable_get('admin_theme', '0')) {
     
_block_rehash($theme->name);
    }
  }
?>

#16

yched - July 1, 2008 - 18:01

D'oh for the leftover dsm(). Fixed.

As mentioned in the discussion above, the 'TRUE' param has two use cases :
- called by Views.module, whenever an admin changes the 'cache mode' for its views-defined blocks through Views UI. Currently, this has no effect, and Earl removed block caching for Views blocks until this is fixed.
Views is not the only use case, there are other modules out there with a dynamic set of blocks. Views is the most prominent one, and probably the one wher block caching makes most difference in terms of overall performance boost.
- in an update function when a module developer releases a new version of his module and has changed the cache_mode for one of his blocks.

The foreach(themes) loop could indeed be left to the caller, but there is a little logic in there (no need to run it for disabled themes, but don't forget the admin theme) that we're better off doing ourselves.

The patch does raise the question of promoting _block_rehash() to an API function, though. At least in D6 this would probably be done by adding a block_rehash() wrapper that just calls the existing _block_rehash() - there *are* contrib modules that call _block_rehash() currently). This is true whether we support this TRUE param or not, btw.

AttachmentSizeStatusTest resultOperations
update_block_cache_mode-235673-16.patch5.1 KBIdleFailed: Failed to apply patch.View details | Re-test

#17

Damien Tournoud - July 2, 2008 - 02:48
Title:changes to block caching mode not caught» Changes to block caching mode not caught

Ok, here is a counter patch.

  • _block_rehash() without argument now triggers the rehash of blocks of all themes, I don't think that contrib modules that could call this private core function would mind, because this is probably what they want to do, anyway,
  • Simplified the same function somehow (more can be probably done, this code is awful...),
  • Implemented a test to confirm that this actually works.
AttachmentSizeStatusTest resultOperations
235673-block-caching-fix.patch8.25 KBIdleFailed: Failed to apply patch.View details | Re-test
235673-block-caching-tests.patch4.82 KBIdleFailed: Failed to apply patch.View details | Re-test

#18

yched - July 2, 2008 - 03:20

Damien, the important thing is to get this accepted in D6 asap, which is why I think it's important to keep API changes (and code refactoring) minimal. Patch in #16 is no API change at all.
More cleanup of block_rehash() can be kept for a later D7-only patch IMO.

Besides, I'm not sure I got the concerns you have with the approach in #17 that makes a counter-patch needed :-)

#19

Damien Tournoud - July 2, 2008 - 08:10

@yched: It's either and API change or not, so:

  • Either we consider that we are only changing a private code function: in that case we don't care about changing its behavior but we shouldn't have to take into account contribution modules use cases (ie. Views);
  • Or we consider that this is indeed a small API change and we have to make it right.

Moreover, this has to be accepted in D7 first, and D7 requires tests.

#20

Damien Tournoud - July 2, 2008 - 09:02

Note that for the tests to succeed you may or may not require to apply #277621: drupalGet not working correctly first.

#21

Damien Tournoud - July 4, 2008 - 14:49

Any review on that?

#22

Dries - July 5, 2008 - 07:12

I'm thinking we should always flush all themes' cache. I think we should simplify this patch some more.

#23

catch - July 5, 2008 - 11:14
Status:needs review» needs work

The tests from #17 don't apply.

#24

Damien Tournoud - July 5, 2008 - 18:30

@catch: Both patches apply cleanly here. What's the issue?

#25

chx - July 5, 2008 - 19:13

#22 most definitely should not be! If developer Alice is setting up some blocks while developer Charlie edits the code of the theme then Drupal will take away the block region settings of Charlie just because at one minute or another the region was not there? What's this, Drupal Bob? Are we adding Clippy too? /me runs screaming

Edit: Alice and Charlie are working on two themes.

#26

Damien Tournoud - August 28, 2008 - 23:22
Status:needs work» needs review

The test and patch from #17 still applies cleanly to HEAD. Given chx' remark in #25, I see no reason for this to be in CNW.

#27

Anonymous (not verified) - November 11, 2008 - 10:05
Status:needs review» needs work

The last submitted patch failed testing.

#28

swentel - November 30, 2008 - 18:04
Status:needs work» needs review

Chasing HEAD. I merged the tests & the patch into one file.
Changes in the patch:
- blocks is now block (you don't want to now how much time I lost with that change)
- moved block_test module to modules/simpletest/tests

Let's get this in and backport asap so we can make views 2 happy :)

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-D7.patch13.21 KBIdleFailed: Failed to apply patch.View details | Re-test

#29

mfer - December 1, 2008 - 00:19

subscribe.... need to test this one.

#30

pwolanin - December 6, 2008 - 01:08

this just bit me - ugh.

#31

System Message - December 18, 2008 - 13:45
Status:needs review» needs work

The last submitted patch failed testing.

#32

swentel - January 5, 2009 - 13:54
Status:needs work» needs review

Chasing HEAD - Can't run tests on my local machine for some reason, so not sure if everything is still ok.

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-D7.patch13.12 KBIdleFailed: Failed to install HEAD.View details | Re-test

#33

swentel - January 5, 2009 - 17:38
Status:needs review» needs work

Ok it seems that the test fails miserably now, will have to look into this further.

#34

swentel - January 5, 2009 - 20:57
Status:needs work» needs review

$op was killed in block module, so block_test module needed an update, tests pass now.

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-D7.patch14.21 KBIdleUnable to apply patch 235673-block-caching-fix-D7_1.patchView details | Re-test

#35

System Message - January 8, 2009 - 01:05
Status:needs review» needs work

The last submitted patch failed testing.

#36

catch - January 8, 2009 - 01:10
Status:needs work» needs review

resetting.

#37

catch - January 17, 2009 - 21:51
Priority:normal» critical
Status:needs review» needs work
Issue tags:+needs backport to D6

We no longer have "Implementation of" for setUp() and getInfo(). This probably ought to be marked critical.

#38

swentel - April 4, 2009 - 15:42
Status:needs work» needs review

Reroll + removal of the implementation texts.

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-D7.patch14.24 KBIdleUnable to apply patch 235673-block-caching-fix-D7_2.patchView details | Re-test

#39

Berdir - April 25, 2009 - 20:34
Status:needs review» needs work

All new patches should follow the DBTNG syntax and coding style for code that they create or touch..

+    $result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $only_theme);

- Use named parameter, like :theme. see : http://drupal.org/node/310072

+        db_query("DELETE FROM {block} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme);

use db_delete() instead, see : http://drupal.org/node/310081

#40

pwolanin - April 26, 2009 - 19:10
Status:needs work» needs review
AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-40-D7.patch12.78 KBIdleFailed: 10965 passes, 8 fails, 3 exceptionsView details | Re-test

#41

System Message - April 26, 2009 - 19:20
Status:needs review» needs work

The last submitted patch failed testing.

#42

swentel - April 26, 2009 - 20:35
Status:needs work» needs review

block_test.module wasn't included in the patch

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-40-D7.patch14.58 KBIdleUnable to apply patch 235673-block-caching-fix-40-D7_0.patchView details | Re-test

#43

sun - May 3, 2009 - 04:15
Status:needs review» needs work


Blank lines should be blank, no trailing white-space, please.

+ * @param $only_theme

Rename to $theme, please.

+function _block_rehash($only_theme = NULL) {

Block module is optional now. Let's rename that function to block_rebuild(), so module_invoke('block', 'rebuild') works as a public function.

+  foreach ($old_blocks as $theme => $old_theme_blocks) {
+    foreach ($old_theme_blocks as $module => $old_module_blocks) {
+      foreach ($old_module_blocks as $delta => $block) {

Ugh... That snippet highlights the complexity that is going on in this single function. We should try to avoid such stuff. Possibilities:

a) Helper functions.

b) Different structure.

c) OOP? - dunno.

+version = VERSION

To remove.

+// $Id $

No spaces, just $Id$.

#44

kbahey - June 9, 2009 - 02:55

Subscribe

#45

killes@www.drop.org - June 28, 2009 - 16:16
Priority:critical» normal
Issue tags:-Performance+DX (Developer Experience)

removing performance tag, adding DX tag. This is an ordinary bugfix which has bitten several of my coworkers. Lowering status.

#46

redndahead - September 21, 2009 - 07:26
Status:needs work» needs review

Reroll with most of sun's changes. Instead of changing $only_theme to $theme I changed it to $theme_name. $theme was used more appropriately elsewhere in that function.

Didn't change the foreach loops. I think this patch is important enough that it shouldn't wait for a refactor of that section.

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-40-D7_1.patch14.25 KBIdleFailed: 13117 passes, 215 fails, 17 exceptionsView details | Re-test

#47

System Message - September 21, 2009 - 07:45
Status:needs review» needs work

The last submitted patch failed testing.

#48

sun - September 21, 2009 - 09:26

+++ modules/block/block.admin.inc 21 Sep 2009 07:22:46 -0000
@@ -15,8 +15,10 @@ function block_admin_display($theme = NU
+  init_theme();
+++ modules/block/block.module 21 Sep 2009 07:22:46 -0000
@@ -245,83 +245,107 @@ function block_get_blocks_by_region($reg
-  drupal_theme_initialize();

init_theme() has been renamed.

+++ modules/block/block.admin.inc 21 Sep 2009 07:22:46 -0000
@@ -15,8 +15,10 @@ function block_admin_display($theme = NU

(and elsewhere) Trailing white-space here.

+++ modules/block/block.module 21 Sep 2009 07:22:46 -0000
@@ -245,83 +245,107 @@ function block_get_blocks_by_region($reg
+ *   The name of the theme whose blocks should be updated.
+ *   If NULL, updates blocks for all enabled themes.

Wraps too early.

+++ modules/block/block.module 21 Sep 2009 07:22:46 -0000
@@ -245,83 +245,107 @@ function block_get_blocks_by_region($reg

+  if (is_null($theme_name)) {
+    foreach(list_themes() as $theme) {
+      if ($theme->status || $theme->name == variable_get('admin_theme', '0')) {
+        $themes[] = $theme->name;
+      }
+    }
+    $result = db_query("SELECT * FROM {block}");
+  }
+  else {
+    $themes = array($theme_name);
+    $result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme_name));
+  }

This block needs some more comments.

No leading white-space in function bodies, please.

$themes needs to be properly initialized as an array before both conditions.

We now use single quotes for database queries.

+++ modules/block/block.module 21 Sep 2009 07:22:46 -0000
@@ -245,83 +245,107 @@ function block_get_blocks_by_region($reg
+        $regions = system_region_list($theme);       

(and elsewhere) Trailing white-space here.

+++ modules/block/block.test 21 Sep 2009 07:22:46 -0000
@@ -214,6 +214,73 @@ class BlockTestCase extends DrupalWebTes
+class BlockCachingTestCase extends DrupalWebTestCase {

Missing PHPDoc for test case class.

+++ modules/block/block.test 21 Sep 2009 07:22:46 -0000
@@ -214,6 +214,73 @@ class BlockTestCase extends DrupalWebTes
+      'name' => t('Block caching'),
+      'description' => t('Test various aspects of the block caching system.'),
+      'group' => t('Block'),

These are no longer wrapped in t().

+++ modules/block/block.test 21 Sep 2009 07:22:46 -0000
@@ -214,6 +214,73 @@ class BlockTestCase extends DrupalWebTes
+    parent::setUp("block_test");

Single quotes, please. :)

+++ modules/simpletest/tests/block_test.module 21 Sep 2009 07:22:47 -0000
@@ -0,0 +1,32 @@
+ * Implementation of hook_block_list().
...
+ * Implementation of hook_block_view().

"Implementation of" is "Implement" now. (Don't blame me...)

+++ modules/simpletest/tests/block_test.module 21 Sep 2009 07:22:47 -0000
@@ -0,0 +1,32 @@
+function block_test_block_list() {
+  $blocks = array();
+
+  $blocks['test_block'] = array(

$blocks doesn't need to be initialized here.

This review is powered by Dreditor.

#49

redndahead - September 21, 2009 - 18:26
Status:needs work» needs review

With sun's changes applied.

AttachmentSizeStatusTest resultOperations
235673-block-caching-fix-40-D7_2.patch14.71 KBIdleFailed: 13268 passes, 13 fails, 600 exceptionsView details | Re-test

#50

System Message - September 21, 2009 - 18:40
Status:needs review» needs work

The last submitted patch failed testing.

 
 

Drupal is a registered trademark of Dries Buytaert.