In my case the function nodeaccess_nodereference_get_referenced is called so often, that it overloads my server.

I tracked the queries for about two hours.
It has been called 36527 times and used 261951.145(ms)

Query excerpt:

/* Anonymous : nodeaccess_nodereference_get_referenced */ SELECT DISTINCT field_data.field_section_nid AS reference FROM content_field_section field_data INNER JOIN node nD

I had to deactivate the module as it brakes the site for anonymous users.
Any Idea on this?

Comments

danielb’s picture

Status: Active » Closed (works as designed)

I fail to see how this is a bug.

It has been called 36527 times and used 261951.145(ms)

So 0.14ms per page load?

Seems pretty reasonable.

If you've got a lot of node references, and a lot of visitors (300 hits per minute??), but limited resources on the server, then this might not be the module for you.

stefan81’s picture

Thank you for your feedback.
Actually, I copied the site to my local machine to debug it. So I was the only user.
I am not sure if my node references are producing some kind of a loop. Will investigate it.

Idea:
Now we can do the following settings on node references:
- always
- only if in use

Would it help to explicitly set to
- never?

I have many (4) node references on nodes.
But only one field passes on acess settings.
Are fields without any settings activated ignored?

johnv’s picture

Title: nodeaccess_nodereference_get_referenced overloads site » nodeaccess_nodereference_get_referenced overloads site for anonymous users

I second this request (on D7).
In my case the function is called 500 times in 1 page request, because I have 600 possible referenced nodes.

IMO the culprit is:

function nodeaccess_nodereference_node_access_records($node) {
  $grants = array();
  $field_data = nodeaccess_nodereference_field_settings($node->type);
  if (!empty($field_data)) {
    ...
  }
  // Flag that the cache is not to be relied upon.
  variable_set('nodeaccess_nodereference_cache_needs_clear', TRUE);  // <---- This line
  return $grants;
}

which is called every time, when a non-relevant node is viewed. That needs two repairs:
#3-1: no cacheflushing (via 'This line') when viewing content, only on change/delete
#3-2: no cacheflushing (via 'This line') (and no expensive calls) when a entitytype has no settings for NANR.

EDIT: this is only when you have one of the blocks devel_node_access/dna_node or devel_node_access/dna_node enabled. So, this module does not cause that problem.

stefan81's idea seems like a good solution.
- If always/only_in_use is selected, you can do get_referenced()
- If 'never' is used, $field_data would be empty, so not relevant, AND the variable_set should not be called.

Somehow, the variable_set should only be done when saving/deleting a node, that is on node_page_edit();
There is some problem with the recursive calling, and the cache_clear should only be done once per page-request.

I was trying to create a patch on the [unused] field, but I am unsure when the variable_set() is still valid.
For now, I move above line inside the brackets.

johnv’s picture

Version: 6.x-1.7 » 7.x-1.x-dev
Status: Closed (works as designed) » Active

I did a dpm(debug_backtrace()); on one of the hook_node_access() calls. Below the most important lines.

0 =
function (String, 15 characters ) abt_node_access | (Callback) abt_node_access(); args (Array, 3 elements)
3 =
file \nodeaccess_nodereference\nodeaccess_nodereference.moduleline
function (String, 11 characters ) node_access | (Callback) node_access();
4 =
file \nodeaccess_nodereference\nodeaccess_nodereference.moduleline (Integer) 44
function nodeaccess_nodereference_calculate_grants | (Callback) nodeaccess_nodereference_calculate_grants();
5 =
function nodeaccess_nodereference_node_grants | (Callback) nodeaccess_nodereference_node_grants();
8 =
file \modules\node\node.moduleline (Integer) 2914
function node_access_grants | (Callback) node_access_grants();
9 =
function node_access | (Callback) node_access();
11 =
file \includes\menu.inc line (Integer) 775
function (String, 18 characters ) _menu_check_access | (Callback) _menu_check_access();
13 =
file \includes\menu.inc ;
function menu_get_item | (Callback) menu_get_item();
17 =
file \index.php ;
function drupal_bootstrap | (Callback) drupal_bootstrap();

johnv’s picture

Title: nodeaccess_nodereference_get_referenced overloads site for anonymous users » nodeaccess_nodereference_get_referenced overloads site

Btw, this is also for authenticated users

danielb’s picture

I'm not following this at all. I get that nodeaccess_nodereference_get_referenced() is being called a lot - but.. you have a lot of nodes and node references... I don't see a way around that at this stage. It is the unfortunate design of the Drupal node access system that at the time of viewing/updating/deleting the node and deciding whether to allow the user to do it: we don't know what the node is, so we have to build a picture of the whole site and that means all relevant references are looked at. Obviously once you have a reasonable quantity of nodes, this solution is ridiculous.
The queries in nodeaccess_nodereference_get_referenced() will not be called if the field does not have settings enabled from this module, so the 'never' setting seems irrelevant. At least this is the intention of the code that is in dev.
The cache is only cleared after a relevant node has it's access rebuilt, as updated field information can break chains of node access. It does not clear on every page load as you say. The statement you've moved should probably remain where it is, as it should mark the cache as bad when the node has NANR fields, but also importantly: when it does not have NANR fields - as it could previously have had them.

danielb’s picture

OK how about this,
rewrite nodeaccess_nodereference_get_referenced() to also select entity_id
make only a single call to it once, without supplying a $nid, and store all the returned data in a static cache (maybe even a persistent cache), keyed by entity_id.
Then subsequent calls to nodeaccess_nodereference_get_referenced($nid) would simply lookup the cache.

danielb’s picture

along the lines of what I was talking about.

danielb’s picture

this approach will cache the map of references across different page loads and for all users....

stefan81’s picture

Hello danielb

Thank you for your efforts and time you invested in the patch.
It is very much appreciated!

As it is a drupal 7 specific patch due the drupal_static() function,
I will try to find a workaround to my project.

johnv’s picture

Title: nodeaccess_nodereference_get_referenced overloads site for anonymous users » nodeaccess_nodereference_get_referenced overloads site

I have corrected my comment #3 about variable_set('nodeaccess_nodereference_cache_needs_clear', TRUE)
Sorry for being so annoying.

johnv’s picture

Thanks for your patch. I am testing it. These are the results:

1. I sometimes get the error
Fatal error: Cannot use object of type stdClass as array in nodeaccess_nodereference.module on the first part of this line:
return !empty($references['refs'][$nid]) ? $references['refs'][$nid] : array();
in that case, the content is not an array, but:

... (Object) stdClass

    cid (String, 39 characters ) nodeaccess_nodereference_get_referenced | (Callback) nodeaccess_nodereference_get_referenced();
    data (Array, 1 element)
    created (String, 10 characters ) 1342467559
    expire (String, 1 characters ) 0
    serialized (String, 1 characters ) 1

I am not sure when it happens,: not on a node page, but on a book page.

2. I have 18.000 nodes of 7 node types, of which 40 nodes (each referring to exactly 1 other node) of 1 node type need this module. ATM this query fetches 15000 records, which will be evaluated puposely.
This query should be more specific, and include the node_type:

            // Assumed node_reference.  This assumption may change if more field
            // types supported.
            $sql = 'SELECT DISTINCT entity_id, ' . $field . '_nid AS reference '
               . 'FROM {field_data_' . $field . '} '
               . 'WHERE ' . $field . '_nid IS NOT NULL '
               . "AND entity_type='node' "         // this added
               . "AND bundle='" . $type . "' "      // and this added
               ;

This requires a substructure $references['refs'][][], I presume?

danielb’s picture

I've revised it a bit based on these comments.

As it is a drupal 7 specific patch due the drupal_static() function,
I will try to find a workaround to my project.

instead of $references = &drupal_static(__FUNCTION__); Drupal 6 would use static $references = array(); or something like that.

danielb’s picture

we can probably backport these changes easily

johnv’s picture

Status: Active » Needs review

Patch #13 work nicely, thanks.

I thought you could read data per node_type (pasing $node instead of $nid), but you need to load all data at once, since hook_node_grants($account, $op) is not per $node.

danielb’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev
StatusFileSize
new3.86 KB

I've committed that one.

Here is a potential backport.

danielb’s picture

Status: Needs review » Fixed

well i've committed it

morbiD’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Fixed » Needs work

I don't believe this is working properly.

I just loaded up a fresh D7 install with dev releases of the following modules:

  • entityreference
  • content_access
  • nodeaccess_userreference
  • nodeaccess_nodereference

I configured the test site like so:

  1. I created three content types. Let's call them A, B and C for simplicity.
  2. I set content_access to allow view access on all content types.
  3. I added a user reference on A which provided update/delete permission.
  4. I added a node reference on B to infer update/delete permission from A.
  5. I added a node reference on C to infer update/delete permission from B.

However, when a user was referenced in A, they only gained update/delete on A; not on B or C.

I tried reverting to the cbeb16d snapshot of nodeaccess_nodereference from git, and the user then gained the correct update/delete permissions on B and C.

Stepping up to the 40c1aba snapshot removed the update/delete permissions on B and C again, so it seems something isn't quite right in the committed patch from this issue.

johnv’s picture

I have a problem, too
2 identical users should have access to the same node, however, only the Author does (set set in Author-settings). The other one not.
I was still testing my current settings, but perhaps the same sympton as #18.

danielb’s picture

If you happen to be testing this the first thing I would do is kill the following line:

cache_set('nodeaccess_nodereference_get_referenced', $references);

To remove caching.

If that doesn't change anything kill the static caching by changing this:

$references = &drupal_static(__FUNCTION__);
to this:
$references = array();

If it's still a problem then at least we know it wasn't the caching.

morbiD’s picture

It's not the caching. I changed both of the above and the problem persisted.

The cause is actually the new return condition:

return !empty($references['refs'][$nid]) ? $references['refs'][$nid] : array();

In $references = nodeaccess_nodereference_calculate_grants($parent_reference); if $parent_reference is NULL, nodeaccess_nodereference_get_referenced() returns an empty array because of that new condition.

Something like this seems to get it working again:

$all_refs = array();
if (!isset($nid)) {
  foreach ($references['refs'] as $reference) {
    foreach ($reference as $key => $value) {
      $all_refs[$key] = $value;
    }
  }
}
return !empty($references['refs'][$nid]) ? $references['refs'][$nid] : $all_refs;
danielb’s picture

i might go with something like this?

<?php

  if ($nid && !empty($references['refs'][$nid])) {
    return $references['refs'][$nid];
  }
  elseif (!$nid && !empty($references['refs'])) {
    return call_user_func_array('array_merge', $references['refs']);
  }
  else {
    return array();
  }

?>
danielb’s picture

Status: Needs work » Needs review

committed, if this is ok I will release

johnv’s picture

I am on holidays. Cant test these three weeks. Cheers!

morbiD’s picture

No problems encountered in my brief testing. Seems good.

danielb’s picture

Status: Needs review » Fixed

Thanks guys, I'll mark this issue as fixed then. Any new problems arising from the rewrite of these functions should be reported as a new issue. Cheers!

Status: Fixed » Closed (fixed)

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