mongodb / mongo-php-library

The Official MongoDB PHP library
https://mongodb.com/docs/php-library/current/
Apache License 2.0
1.6k stars 261 forks source link

readConcern.level Error #116

Closed RaeesBhatti closed 8 years ago

RaeesBhatti commented 8 years ago

I'm trying to move from mongofill to this library. I've successfully build and added HHVM driver but keep getting this error.

Fatal error: Uncaught exception 'MongoDB\\Driver\\Exception\\RuntimeException' with message 'readConcern.level must be either 'local' or 'majority'' in /site/External/mongo-php-library/src/Operation/Find.php:179
Stack trace:
#0 /site/External/mongo-php-library/src/Operation/Find.php(179): MongoDB\\Driver\\Server->executeQuery()
#1 /site/External/mongo-php-library/src/Collection.php(439): MongoDB\\Operation\\Find->execute()
#2 /site/Service/Main.hh(7): MongoDB\\Collection->find()
#3 /site/Core/Main.hh(18): Service_Main::Packages()
#4 /site/Main.hh(18): Core_Main::initialize()
#5 {main}

I'm not sure how to set default readConcern.level. Any help would be appreciated.

jacqueswaller commented 8 years ago

You can set the default readConcern on a Client, Database or Collection by passing it in as one of the options like

$myClient->selectCollection("database", "collection", ["readConcern" => new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern.LOCAL)])

LOCAL is the default on the Client if you don't explicitly specify, as far as I know, but I've been using the straight up PHP version (not HHVM). If the Manager class has an invalid readConcern by default, it sounds like a problem for https://github.com/mongodb/mongo-hhvm-driver

jmikola commented 8 years ago

@raeesiqbal: Are you explicitly setting the read concern in your own code?

Would you be able to provide a small stand-alone script that reproduces this error? Additionally, a var_dump() of your Collection object would be helpful, as that should report the read concern being used by the library. If you weren't specifying your own, we default to Manager::getReadConcern() (feel free to dump that as well).

RaeesBhatti commented 8 years ago

When I try to set ReadConcern explicitly as suggested by @jacqueswaller. There is this error

Fatal error: Undefined constant 'MongoDB\\Driver\\ReadConcern' in /site/Core/Main.hh on line 11

Here is the output for get_declared_classes():

Array
(
    [0] => HH\Pair
    [1] => HH\Set
    [2] => HH\Vector
    [3] => Closure
    [4] => HH\ImmVector
    [5] => HH\ImmSet
    [6] => HH\ImmMap
    [7] => HH\Map
    [8] => MongoDB\BSON\Timestamp
    [9] => HH\Asio\WrappedException
    [10] => LazyFilterIterable
    [11] => DOMNodeIterator
    [12] => LazySkipKeyedIterable
    [13] => RecursiveFilterIterator
    [14] => LazyValuesIterator
    [15] => __SystemLib\TarArchiveHandler
    [16] => LazyFilterKeyedIterator
    [17] => MongoDB\Driver\Exception\LogicException
    [18] => NoRewindIterator
    [19] => XMLReader
    [20] => __SystemLib_ChunkedInflator
    [21] => HH\AsyncFunctionWaitHandle
    [22] => LazyFilterIterator
    [23] => IntlGregorianCalendar
    [24] => LazyZipIterator
    [25] => __SystemLib\DivisionByZeroError
    [26] => HH\WaitableWaitHandle
    [27] => MongoDB\Driver\ReadPreference
    [28] => AsyncMysqlErrorResult
    [29] => FilterIterator
    [30] => AsyncMysqlConnection
    [31] => ReflectionObject
    [32] => PDOStatement
    [33] => LazySkipWhileKeyedIterable
    [34] => BadMethodCallException
    [35] => SplObjectStorage
    [36] => SoapClient
    [37] => IntlDateFormatter
    [38] => LazyMapKeyedIterable
    [39] => MongoDB\Driver\Exception\WriteException
    [40] => MessageFormatter
    [41] => HH\AsyncGeneratorWaitHandle
    [42] => LazyKeysIterable
    [43] => HH\SleepWaitHandle
    [44] => SoapFault
    [45] => ReflectionExtension
    [46] => ReflectionProperty
    [47] => DateTimeImmutable
    [48] => HH\GenMapWaitHandle
    [49] => SplDoublyLinkedList
    [50] => LazyTakeIterable
    [51] => AsyncMysqlResult
    [52] => __SystemLib\InvariantCallback
    [53] => finfo
    [54] => MemcacheSessionModule
    [55] => LazySkipWhileKeyedIterator
    [56] => __resource
    [57] => ReflectionTypeAlias
    [58] => RecursiveDirectoryIterator
    [59] => LazySkipWhileIterator
    [60] => LazyKVZipIterator
    [61] => SplMaxHeap
    [62] => HH\Asio\WrappedResult
    [63] => Phar
    [64] => LazySliceIterator
    [65] => MongoDB\Driver\CursorId
    [66] => __SystemLib\ArithmeticError
    [67] => UConverter
    [68] => LazyIterableView
    [69] => PharException
    [70] => Memcached
    [71] => MongoDB\Driver\Exception\BulkWriteException
    [72] => DOMProcessingInstruction
    [73] => __SystemLib\ConvertIconvFilter
    [74] => LazyFilterWithKeyIterable
    [75] => AsyncMysqlConnectionPool
    [76] => SplMinHeap
    [77] => LazyMapWithKeyIterable
    [78] => LazySliceIterable
    [79] => __SystemLib\DeflateStreamFilter
    [80] => InvalidOperationException
    [81] => SQLite3
    [82] => __SystemLib\ParseError
    [83] => SpoofChecker
    [84] => mysqli
    [85] => HH\GenArrayWaitHandle
    [86] => DatePeriod
    [87] => ErrorException
    [88] => LazyValuesIterable
    [89] => InvalidArgumentException
    [90] => ReflectionFunction
    [91] => HH\Client\TypecheckResult
    [92] => Memcache
    [93] => __SystemLib\StringRot13StreamFilter
    [94] => LazyConcatIterator
    [95] => MongoDB\Driver\Exception\SSLConnectionException
    [96] => SplFixedArray
    [97] => DOMNodeList
    [98] => LazyTakeWhileIterator
    [99] => MongoDB\BSON\Regex
    [100] => RuntimeException
    [101] => FilesystemIterator
    [102] => ReflectionParameter
    [103] => __SystemLib\StreamFilterBucket
    [104] => MongoDB\Driver\Manager
    [105] => DateTime
    [106] => Redis
    [107] => MongoDB\Driver\BulkWrite
    [108] => LazyTakeWhileKeyedIterable
    [109] => RedisException
    [110] => AsyncMysqlRowBlockIterator
    [111] => LazyTakeWhileIterable
    [112] => AsyncMysqlQueryErrorResult
    [113] => SessionHandler
    [114] => DateInterval
    [115] => DOMNode
    [116] => AsyncMysqlException
    [117] => LazyTakeKeyedIterator
    [118] => IntlPartsIterator
    [119] => SplFileInfo
    [120] => DOMXPath
    [121] => AsyncMysqlQueryException
    [122] => LazyZipKeyedIterable
    [123] => Normalizer
    [124] => LazySkipIterable
    [125] => SQLite3Stmt
    [126] => SQLite3Result
    [127] => ReflectionFunctionAbstract
    [128] => DOMElement
    [129] => ZipArchive
    [130] => HH\AsyncGenerator
    [131] => mysqli_stmt
    [132] => mysqli_result
    [133] => DomainException
    [134] => __SystemLib\Error
    [135] => mysqli_driver
    [136] => ArrayIterator
    [137] => RecursiveCallbackFilterIterator
    [138] => AsyncMysqlConnectResult
    [139] => MongoDB\Driver\Exception\AuthenticationException
    [140] => LazyMapKeyedIterator
    [141] => AppendIterator
    [142] => LazySliceKeyedIterator
    [143] => DOMImplementation
    [144] => DOMException
    [145] => RegexIterator
    [146] => PDOException
    [147] => SoapParam
    [148] => EncodingDetector
    [149] => HH\RescheduleWaitHandle
    [150] => SplHeap
    [151] => DOMNotation
    [152] => MongoDB\BSON\MaxKey
    [153] => SplFileObject
    [154] => LazyMapIterator
    [155] => RangeException
    [156] => MongoDB\Driver\Exception\InvalidArgumentException
    [157] => LogicException
    [158] => __SystemLib\StringToUpperStreamFilter
    [159] => AsyncMysqlQueryResult
    [160] => DOMCdataSection
    [161] => HH\InvariantException
    [162] => LazyKeyedIterableView
    [163] => IntervalTimer
    [164] => MongoDB\Driver\Exception\ConnectionException
    [165] => RecursiveArrayIterator
    [166] => DOMAttr
    [167] => Imagick
    [168] => DOMNameSpaceNode
    [169] => HH\WaitHandle
    [170] => GMP
    [171] => OverflowException
    [172] => MemcachedSessionModule
    [173] => LazyMapIterable
    [174] => MapIterator
    [175] => LazyZipIterable
    [176] => ParentIterator
    [177] => XMLWriter
    [178] => DirectoryIterator
    [179] => ImagickDrawException
    [180] => HH\TypeStructureKind
    [181] => MongoDB\Driver\WriteConcernError
    [182] => Locale
    [183] => LazySkipIterator
    [184] => MongoDB\Driver\Exception\UnexpectedValueException
    [185] => ReflectionTypeConstant
    [186] => LazyFilterWithKeyIterator
    [187] => DOMEntityReference
    [188] => LazyKeysIterator
    [189] => SoapServer
    [190] => SimpleXMLElement
    [191] => ImagickDraw
    [192] => __SystemLib\MethCallerHelper
    [193] => IntlChar
    [194] => php_user_filter
    [195] => GlobIterator
    [196] => AsyncMysqlConnectException
    [197] => DOMEntity
    [198] => DOMNamedNodeMap
    [199] => AsyncMysqlClient
    [200] => LazySkipKeyedIterator
    [201] => __pinitSentinel
    [202] => SoapVar
    [203] => LengthException
    [204] => LazySkipWhileIterable
    [205] => MongoDB\Driver\Cursor
    [206] => Directory
    [207] => RecursiveCachingIterator
    [208] => MongoDB\Driver\WriteConcern
    [209] => RecursiveTreeIterator
    [210] => SoapHeader
    [211] => OutOfRangeException
    [212] => MCRouterException
    [213] => CallbackFilterIterator
    [214] => Transliterator
    [215] => ImagickPixelException
    [216] => LazyMapWithKeyIterator
    [217] => SplPriorityQueue
    [218] => IntlDatePatternGenerator
    [219] => UnexpectedValueException
    [220] => HH\ExternalThreadEventWaitHandle
    [221] => MongoDB\Driver\Server
    [222] => OutOfBoundsException
    [223] => Exception
    [224] => RedisSessionModule
    [225] => LazyConcatIterable
    [226] => UnderflowException
    [227] => BadFunctionCallException
    [228] => MCRouterOptionException
    [229] => mysqli_warning
    [230] => SimpleXMLIterator
    [231] => DOMText
    [232] => MongoDB\Driver\Query
    [233] => MongoDB\Driver\WriteError
    [234] => __SystemLib\AssertionError
    [235] => MongoDB\Driver\Command
    [236] => MongoDB\BSON\Javascript
    [237] => ImagickPixel
    [238] => IntlCodePointBreakIterator
    [239] => __SystemLib\InflateStreamFilter
    [240] => LazyTakeWhileKeyedIterator
    [241] => EncodingMatch
    [242] => AsyncMysqlClientStats
    [243] => Reflection
    [244] => HH\GenVectorWaitHandle
    [245] => SplTempFileObject
    [246] => MongoDB\Driver\Exception\ExecutionTimeoutException
    [247] => LazyTakeKeyedIterable
    [248] => ImagickException
    [249] => HH\BuiltinEnum
    [250] => MultipleIterator
    [251] => SplQueue
    [252] => APCIterator
    [253] => EmptyIterator
    [254] => LazyZipKeyedIterator
    [255] => __SystemLib\HH\Client\CacheKeys
    [256] => DOMDocumentFragment
    [257] => MongoDB\BSON\ObjectID
    [258] => __SystemLib\ArchiveEntryStat
    [259] => XhprofFrame
    [260] => SetIterator
    [261] => RecursiveRegexIterator
    [262] => AsyncMysqlRow
    [263] => IntlTimeZone
    [264] => VectorIterator
    [265] => DOMCharacterData
    [266] => CachingIterator
    [267] => DateTimeZone
    [268] => DOMComment
    [269] => SplStack
    [270] => stdClass
    [271] => DOMDocument
    [272] => LazySliceKeyedIterable
    [273] => AsyncMysqlRowIterator
    [274] => MongoDB\Driver\ReadConcern
    [275] => Collator
    [276] => HH\Client\TypecheckStatus
    [277] => HH\Shapes
    [278] => MongoDB\Driver\Exception\RuntimeException
    [279] => HH\ConditionWaitHandle
    [280] => ImagickPixelIterator
    [281] => MongoDB\BSON\Binary
    [282] => ReflectionClass
    [283] => ReflectionType
    [284] => PharData
    [285] => LibXMLError
    [286] => IntlCalendar
    [287] => LazyTakeIterator
    [288] => _SplPriorityQueueHeap
    [289] => HH\ResumableWaitHandle
    [290] => IntlRuleBasedBreakIterator
    [291] => MongoDB\BSON\UTCDateTime
    [292] => AsyncMysqlRowBlock
    [293] => DOMDocumentType
    [294] => NumberFormatter
    [295] => __SystemLib\StringToLowerStreamFilter
    [296] => MongoDB\Driver\Exception\ConnectionTimeoutException
    [297] => _SplDoublyLinkedListNode
    [298] => MongoDB\Driver\WriteResult
    [299] => ResourceBundle
    [300] => SimpleXMLElementIterator
    [301] => MongoDB\Driver\Utils
    [302] => PDO
    [303] => IteratorIterator
    [304] => RecursiveIteratorIterator
    [305] => LazyKVZipIterable
    [306] => InfiniteIterator
    [307] => MCRouter
    [308] => __PHP_Incomplete_Class
    [309] => MongoDB\BSON\MinKey
    [310] => PharFileInfo
    [311] => XSLTProcessor
    [312] => CURLFile
    [313] => Generator
    [314] => PairIterator
    [315] => ImagickPixelIteratorException
    [316] => __SystemLib\TypeError
    [317] => HH\AwaitAllWaitHandle
    [318] => ReflectionException
    [319] => IntlBreakIterator
    [320] => LazyFilterKeyedIterable
    [321] => __SystemLib\SessionForwardingHandler
    [322] => IntlIterator
    [323] => LimitIterator
    [324] => ReflectionMethod
    [325] => HH\StaticWaitHandle
    [326] => MySSLContextProvider
    [327] => MemcachedException
    [328] => ArrayObject
    [329] => __SystemLib\ZipArchiveHandler
    [330] => __SystemLib\ArchiveHandler
    [331] => ConfigSite
    [332] => StringBuilder
    [333] => VultrApp
    [334] => Page
    [335] => XHPChildrenDeclarationType
    [336] => xhp_link
    [337] => XHPAttributeType
    [338] => Scope
    [339] => xhp_s__img
    [340] => ServiceHostingType
    [341] => Core_H
    [342] => UserStatus
    [343] => APIException
    [344] => RedisObserver
    [345] => xhp_s__link
    [346] => ReflectionXHPChildrenExpression
    [347] => StringTemplates
    [348] => APIFieldException
    [349] => xhp_page_style
    [350] => xhp_x__composable_element
    [351] => ReflectionXHPAttribute
    [352] => Debug
    [353] => APIDeniedException
    [354] => CloudFlareSecurityLevel
    [355] => Mirror
    [356] => XHPChildrenConstraintType
    [357] => xhp_string
    [358] => ServiceDomainType
    [359] => VultrPlan
    [360] => xhp_xhp__html_element
    [361] => APIMultiException
    [362] => xhp_a
    [363] => xhp_google_captcha
    [364] => xhp_xhp
    [365] => CloudFlarePlan
    [366] => xhp_meta
    [367] => Theme
    [368] => Form
    [369] => xhp_page_head
    [370] => FormFieldType
    [371] => xhp_img
    [372] => xhp_s__a
    [373] => HostingStatus
    [374] => L_U
    [375] => OrderType
    [376] => APIAuthRequired
    [377] => HG_HostingOp
    [378] => SupportTicketStatus
    [379] => DiscountType
    [380] => xhp_hg__header__cart
    [381] => Core_Main
    [382] => xhp_page_footer
    [383] => TOTP
    [384] => xhp_x__element
    [385] => D
    [386] => xhp_page_script
    [387] => VultrDC
    [388] => xhp_s__meta
    [389] => DNSRecordType
    [390] => XHPChildrenExpressionType
    [391] => HTTP
    [392] => HG_DomainOp
    [393] => xhp_x__primitive
    [394] => DiscountProduct
    [395] => CloudFlareStatus
    [396] => ConfigRedis
    [397] => InvoiceStatus
    [398] => xhp_page
    [399] => RouterException
    [400] => xhp_xhp__html_singleton
    [401] => ReflectionXHPChildrenDeclaration
    [402] => HTTPException
    [403] => DomainStatus
    [404] => xhp_hg__dropdown
    [405] => CloudFlareTTL
    [406] => xhp_riot_element
    [407] => MongoDB
)

get_class_methods('MongoDB'):


Array
(
    [0] => __construct
    [1] => __get
    [2] => _getClient
    [3] => selectCollection
    [4] => _getFullCollectionName
    [5] => drop
    [6] => command
    [7] => getCollectionNames
    [8] => listCollections
    [9] => getIndexesCollection
    [10] => getGridFS
    [11] => authenticate
    [12] => createCollection
    [13] => createDBRef
    [14] => dropCollection
    [15] => execute
    [16] => forceError
    [17] => getDBRef
    [18] => getProfilingLevel
    [19] => getReadPreference
    [20] => getSlaveOkay
    [21] => lastError
    [22] => prevError
    [23] => repair
    [24] => resetError
    [25] => setProfilingLevel
    [26] => setReadPreference
    [27] => setSgetGridFSlaveOkay
    [28] => __toString
)

get_class_methods('MongoDB\Driver\ReadConcern'):

Array
(
    [0] => __construct
    [1] => getLevel
    [2] => __debugInfo
)

If I do

$client = new MongoDB\Client();
var_dump(MongoDB\Driver\Manager\getReadConcern());

It says, Fatal error: Call to undefined function MongoDB\\Driver\\Manager\\getReadConcern() in /site/Core/Main.hh on line 16

RaeesBhatti commented 8 years ago

Here is the dump for

$reflection = new ReflectionClass('MongoDB\Driver\Manager')
var_dump($reflection->getMethods());
array(11) {
  [0]=>
  object(ReflectionMethod)#24 (2) {
    ["name"]=>
    string(11) "__construct"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [1]=>
  object(ReflectionMethod)#25 (2) {
    ["name"]=>
    string(11) "__debugInfo"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [2]=>
  object(ReflectionMethod)#26 (2) {
    ["name"]=>
    string(14) "executeCommand"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [3]=>
  object(ReflectionMethod)#27 (2) {
    ["name"]=>
    string(12) "executeQuery"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [4]=>
  object(ReflectionMethod)#28 (2) {
    ["name"]=>
    string(16) "executeBulkWrite"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [5]=>
  object(ReflectionMethod)#29 (2) {
    ["name"]=>
    string(10) "getServers"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [6]=>
  object(ReflectionMethod)#30 (2) {
    ["name"]=>
    string(14) "getReadConcern"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [7]=>
  object(ReflectionMethod)#31 (2) {
    ["name"]=>
    string(17) "getReadPreference"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [8]=>
  object(ReflectionMethod)#32 (2) {
    ["name"]=>
    string(15) "getWriteConcern"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [9]=>
  object(ReflectionMethod)#33 (2) {
    ["name"]=>
    string(8) "__wakeUp"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
  [10]=>
  object(ReflectionMethod)#34 (2) {
    ["name"]=>
    string(12) "selectServer"
    ["class"]=>
    string(22) "MongoDB\Driver\Manager"
  }
}
RaeesBhatti commented 8 years ago

I'll try to reproduce this error in a bare minimum environment soon.

jmikola commented 8 years ago

@raeesiqbal: I'm not sure why you dumped get_class_methods('MongoDB') above, as that's a class from the legacy driver (also present in mongofill) and not related to this library or the current HHVM extension.

If I do

$client = new MongoDB\Client();
var_dump(MongoDB\Driver\Manager\getReadConcern());

It says, Fatal error: Call to undefined function MongoDB\Driver\Manager\getReadConcern() in /site/Core/Main.hh on line 16

An error makes sense in this case, as MongoDB\Driver\Manager\getReadConcern() is syntax for a getReadConcern() function within the MongoDB\Driver\Manager namespace. My suggestion was the poke inside the Collection object (or capture the array returned by __debugInfo()) and dump the manager's read concern with something like:

$debug = $collection->__debugInfo();
var_dump($debug['manager']->getReadConcern());
RaeesBhatti commented 8 years ago

Okay, here is my minimum repro environment.

/var/www/mongo-test
                             main.hh
                             External/
                                            mongo-php-library/
                                            app.hdf
                                            mongodb.so
                                            require.php                             

main.hh

<?php
spl_autoload_register(function($Name){
  // MongoDB library
  if(substr($Name, 0, 7) !== 'MongoDB') return;
  $Path = __DIR__.'/mongo-php-library/src/'.str_replace("\\", '/', substr($Name, 8)).'.php';
  if(file_exists($Path)){
    require($Path);
  }
});

app.hdf

Server {
    Host = mongo-test
    IP = 0.0.0.0
    Port = 80

    SourceRoot = /var/www/mongo-test

    RequestTimeoutSeconds = 512
    RequestMemoryMaxBytes = 16777216

    DefaultDocument = main.hh
    ErrorDocument404 = main.hh

    FatalErrorMessage = There was a problem in website loading. Please wait a few moments and refresh.

}
Hack {
  Lang {
    LookForTypechecker = false
  }
}
DynamicExtensions {
  mongodb = /var/www/mongo-test/External/mongodb.so
}

It produces this error.

Fatal error: Call to undefined method MongoDB\\Driver\\Manager::getReadConcern() in /var/www/mongo-test/External/mongo-php-library/src/Database.php on line 87

and when I make it

$DB = (new MongoDB\Client())->selectDatabase('mydb', ["readConcern" => new MongoDB\Driver\ReadConcern(MongoDB\Driver\ReadConcern.LOCAL)]);

It says,

Fatal error: Class undefined: MongoDB\\Driver\\ReadConcern in /var/www/mongo-test/main.hh on line 5
RaeesBhatti commented 8 years ago

As funny as it is, I tried this and it worked.

$DB = (new MongoDB\Client())->selectDatabase('mydb', ["readConcern" => new MongoDB\Driver\ReadConcern('local')]);

The problem was that witht the new mongo-hhvm-driver ReadConcern has to be defined explicitly and MongoDB\Driver\ReadConcern.LOCAL was undefined. So, I explored the the mongo-hhvm-driver's source and found that MongoDB\\Driver\\ReadConcern converts the parameter to string. So, I thought that why shouldn't I provide it with a string. And I did and it worked.

jmikola commented 8 years ago

This may have been related to HHVM-175, which should be included in the 1.1.0 release of that driver.