If you have page compression turned on and using mongodb to cache the page mongo by default will throw an exception.

After some investigation the problem turns out to be that understandably the compresses page is not utf8, and mongo will throw an exception about it not being utf8.

to get around this problem you can set in the php.ini

mongo.utf8 = 0

and the problem will be solved.

CommentFileSizeAuthor
#5 910726_non_utf8_strings.patch2.34 KBmakara

Comments

bojanz’s picture

mongo.utf8 int
If an exception should be thrown for non-UTF8 strings. Until version 1.0.4, the PHP driver would ignore non-UTF8 strings, even though you're not supposed to insert them. As of 1.0.4, the driver throws a MongoException. To ease the transition for applications that insert non-UTF8 strings, you can turn this option off to emulate the old, non-exception-throwning behavior. This option will be eliminated and exceptions always thrown for non-UTF8 strings starting with version 1.1.0.

Which means the fix is not exactly viable in the long term.

Read on mongodb.org:

You can definitely save compressed data, just use MongoBinData instead of saving the plain string.

gordon’s picture

I wonder if cache should be changed to use MongoBinData instead of plain strings.

The work around that I posted is OK, but really it should just work.

crea’s picture

Subscribing

dob_’s picture

Tested under Drupal6

131,132c131,136
< if ($cache->serialized) {
< $cache->data = unserialize($cache->data);
---
> if($cache->binary && $cache->serialized) {
> $cache->data = unserialize($cache->data->bin);
> } else if(!$cache->binary && $cache->serialized){
> $cache->data = unserialize($cache->data);
> } else {
> $cache->data = $cache->data->bin;
134c138
<
---
>
145a150
> 'binary' => true,
147c152
< 'data' => $scalar ? $data : serialize($data),
---
> 'data' => $scalar ? $data : serialize($data),
149c154
<
---
> $entry['data'] = new MongoBinData($entry['data']);
151d155
<

makara’s picture

Status: Active » Needs review
StatusFileSize
new2.34 KB

Thanks dob_.

Here's a patch improves it a bit.

Status: Needs review » Needs work

The last submitted patch, 910726_non_utf8_strings.patch, failed testing.

carlos8f’s picture

Status: Needs work » Reviewed & tested by the community

Patch looks good, I incorporated it into Cache Tags, ran the benchmarks, and it wasn't noticeably slower. Plus it fixed the crash :) I also think the try/catch wrapping is good because I like rendered pages better than crashed pages.

langworthy’s picture

This might be a newbish question but why are the $headers being removed from set()?

Also, why is there a new question mark in comments on its own line?

makara’s picture

Sorry forgot to mention that the $headers was in D6 and moved into cache data in D7. For reference, see function drupal_page_set_cache() (or function page_set_cache() for D6).

misc’s picture

Patch seems good, removed the question mark (comment #8). Patch commited to latest 7.x-1.x.

misc’s picture

Status: Reviewed & tested by the community » Closed (fixed)
misc’s picture

Status: Closed (fixed) » Fixed

Status: Fixed » Closed (fixed)

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

summit’s picture

Hi,
I got this error using latest dev:

>
MongoException: non-utf8 string:  in MongoCollection->save() (line 198 of /sites/all/modules/mongodb/mongodb_field_storage/mongodb_field_storage.module)

Is this patch may be not complete?
Greetings, Martijn

summit’s picture

Status: Closed (fixed) » Active

May be something like this on mongodb_field_storage.module:

// Use MongoBinData for non-UTF8 strings.
if (is_string($new_entity) && !drupal_validate_utf8($new_entity)) {
    $new_entity = new MongoBinData($new_entity);
}

Just above line 198?

I am not a programmer, but just trying to get rid of this error..
Greetings, Martijn

summit’s picture

Title: Drupal Crashes when writting to page cache in mongodb » Drupal Crashes when writting utf8 to page cache or someone else in mongodb

New title what I think is relevant, I add a new issue if you think this is not related?
Than I am sorry to open this closed issue.
greetings, Martijn

summit’s picture

Status: Active » Closed (fixed)

Hi,

Edit: mongo.utf8 = 0 worked, but this is not possible on a shared host always..
I made a new issue, may be they are related, but I cannot see that: http://drupal.org/node/1944188
Sorry for opening a closed issue earlier.

Greetings, Martijn