Skip to content

PHPC-1891, PHPC-1892, PHPC-1893: Implement TopologyDescription, SDAMSubscriber, and TopologyChangedEvent #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ if test "$PHP_MONGODB" != "no"; then
src/MongoDB/ServerApi.c \
src/MongoDB/ServerDescription.c \
src/MongoDB/Session.c \
src/MongoDB/TopologyDescription.c \
src/MongoDB/WriteConcern.c \
src/MongoDB/WriteConcernError.c \
src/MongoDB/WriteError.c \
Expand All @@ -170,7 +171,9 @@ if test "$PHP_MONGODB" != "no"; then
src/MongoDB/Monitoring/CommandStartedEvent.c \
src/MongoDB/Monitoring/CommandSubscriber.c \
src/MongoDB/Monitoring/CommandSucceededEvent.c \
src/MongoDB/Monitoring/SDAMSubscriber.c \
src/MongoDB/Monitoring/Subscriber.c \
src/MongoDB/Monitoring/TopologyChangedEvent.c \
src/MongoDB/Monitoring/functions.c \
"

Expand Down
4 changes: 2 additions & 2 deletions config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ if (PHP_MONGODB != "no") {
EXTENSION("mongodb", "php_phongo.c phongo_compat.c", null, PHP_MONGODB_CFLAGS);
MONGODB_ADD_SOURCES("/src", "bson.c bson-encode.c phongo_apm.c");
MONGODB_ADD_SOURCES("/src/BSON", "Binary.c BinaryInterface.c DBPointer.c Decimal128.c Decimal128Interface.c Int64.c Javascript.c JavascriptInterface.c MaxKey.c MaxKeyInterface.c MinKey.c MinKeyInterface.c ObjectId.c ObjectIdInterface.c Persistable.c Regex.c RegexInterface.c Serializable.c Symbol.c Timestamp.c TimestampInterface.c Type.c Undefined.c Unserializable.c UTCDateTime.c UTCDateTimeInterface.c functions.c");
MONGODB_ADD_SOURCES("/src/MongoDB", "BulkWrite.c ClientEncryption.c Command.c Cursor.c CursorId.c CursorInterface.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c ServerApi.c ServerDescription.c Session.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c");
MONGODB_ADD_SOURCES("/src/MongoDB", "BulkWrite.c ClientEncryption.c Command.c Cursor.c CursorId.c CursorInterface.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c ServerApi.c ServerDescription.c Session.c TopologyDescription.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c");
MONGODB_ADD_SOURCES("/src/MongoDB/Exception", "AuthenticationException.c BulkWriteException.c CommandException.c ConnectionException.c ConnectionTimeoutException.c EncryptionException.c Exception.c ExecutionTimeoutException.c InvalidArgumentException.c LogicException.c RuntimeException.c ServerException.c SSLConnectionException.c UnexpectedValueException.c WriteException.c");
MONGODB_ADD_SOURCES("/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c Subscriber.c functions.c");
MONGODB_ADD_SOURCES("/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c SDAMSubscriber.c Subscriber.c TopologyChangedEvent.c functions.c");
MONGODB_ADD_SOURCES("/src/libmongoc/src/common", PHP_MONGODB_COMMON_SOURCES);
MONGODB_ADD_SOURCES("/src/libmongoc/src/libbson/src/bson", PHP_MONGODB_BSON_SOURCES);
MONGODB_ADD_SOURCES("/src/libmongoc/src/libbson/src/jsonsl", PHP_MONGODB_JSONSL_SOURCES);
Expand Down
78 changes: 78 additions & 0 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ void phongo_session_init(zval* return_value, zval* manager, mongoc_client_sessio
}
/* }}} */

void phongo_topologydescription_init(zval* return_value, mongoc_topology_description_t* topology_description) /* {{{ */
{
php_phongo_topologydescription_t* intern;

object_init_ex(return_value, php_phongo_topologydescription_ce);

intern = Z_TOPOLOGYDESCRIPTION_OBJ_P(return_value);
intern->topology_description = topology_description;
}
/* }}} */

void phongo_readconcern_init(zval* return_value, const mongoc_read_concern_t* read_concern) /* {{{ */
{
php_phongo_readconcern_t* intern;
Expand Down Expand Up @@ -1159,6 +1170,35 @@ const mongoc_read_prefs_t* phongo_read_preference_from_zval(zval* zread_preferen
/* }}} */

/* {{{ phongo zval from mongoc types */
bool php_phongo_server_description_to_zval(zval* retval, mongoc_server_description_t* sd) /* {{{ */
{
mongoc_host_list_t* host = mongoc_server_description_host(sd);
const bson_t* hello_response = mongoc_server_description_hello_response(sd);

array_init(retval);

ADD_ASSOC_STRING(retval, "host", host->host);
ADD_ASSOC_LONG_EX(retval, "port", host->port);
ADD_ASSOC_LONG_EX(retval, "type", php_phongo_server_description_type(sd));

{
php_phongo_bson_state state;

PHONGO_BSON_INIT_DEBUG_STATE(state);

if (!php_phongo_bson_to_zval_ex(bson_get_data(hello_response), hello_response->len, &state)) {
zval_ptr_dtor(&state.zchild);
return false;
}

ADD_ASSOC_ZVAL_EX(retval, "hello_response", &state.zchild);
}
ADD_ASSOC_LONG_EX(retval, "last_update_time", (zend_long) mongoc_server_description_last_update_time(sd));
ADD_ASSOC_LONG_EX(retval, "round_trip_time", (zend_long) mongoc_server_description_round_trip_time(sd));

return true;
} /* }}} */

php_phongo_server_description_type_t php_phongo_server_description_type(mongoc_server_description_t* sd)
{
const char* name = mongoc_server_description_type(sd);
Expand Down Expand Up @@ -1222,6 +1262,41 @@ bool php_phongo_server_to_zval(zval* retval, mongoc_server_description_t* sd) /*
return true;
} /* }}} */

bool php_phongo_topology_description_to_zval(zval* retval, mongoc_topology_description_t* td) /* {{{ */
{
array_init(retval);

{
zval servers;
size_t i, n = 0;
mongoc_server_description_t** sds = mongoc_topology_description_get_servers(td, &n);

array_init_size(&servers, n);

for (i = 0; i < n; i++) {
zval obj;

if (!php_phongo_server_description_to_zval(&obj, sds[i])) {
/* Exception already thrown */
zval_ptr_dtor(&obj);
zval_ptr_dtor(&servers);
mongoc_server_descriptions_destroy_all(sds, n);
return false;
}

add_next_index_zval(&servers, &obj);
zval_ptr_dtor(&obj);
zval_ptr_dtor(&servers);
}
mongoc_server_descriptions_destroy_all(sds, n);

ADD_ASSOC_ZVAL_EX(retval, "servers", &servers);
}
ADD_ASSOC_STRING(retval, "type", mongoc_topology_description_type(td));

return true;
} /* }}} */

void php_phongo_read_concern_to_zval(zval* retval, const mongoc_read_concern_t* read_concern) /* {{{ */
{
const char* level = mongoc_read_concern_get_level(read_concern);
Expand Down Expand Up @@ -3750,6 +3825,7 @@ PHP_MINIT_FUNCTION(mongodb)
php_phongo_server_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_serverapi_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_serverdescription_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_topologydescription_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_session_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_writeconcernerror_init_ce(INIT_FUNC_ARGS_PASSTHRU);
Expand Down Expand Up @@ -3780,6 +3856,8 @@ PHP_MINIT_FUNCTION(mongodb)
php_phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandstartedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS_PASSTHRU);
php_phongo_topologychangedevent_init_ce(INIT_FUNC_ARGS_PASSTHRU);

REGISTER_STRING_CONSTANT("MONGODB_VERSION", (char*) PHP_MONGODB_VERSION, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("MONGODB_STABILITY", (char*) PHP_MONGODB_STABILITY, CONST_CS | CONST_PERSISTENT);
Expand Down
2 changes: 2 additions & 0 deletions php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void phongo_clientencryption_init(php_phongo_clientencryption_t* ce_obj, zval* m
void phongo_server_init(zval* return_value, zval* manager, uint32_t server_id);
void phongo_serverdescription_init(zval* return_value, mongoc_server_description_t* serverDescription);
void phongo_session_init(zval* return_value, zval* manager, mongoc_client_session_t* client_session);
void phongo_topologydescription_init(zval* return_value, mongoc_topology_description_t* topologyDescription);
void phongo_readconcern_init(zval* return_value, const mongoc_read_concern_t* read_concern);
void phongo_readpreference_init(zval* return_value, const mongoc_read_prefs_t* read_prefs);
void phongo_writeconcern_init(zval* return_value, const mongoc_write_concern_t* write_concern);
Expand All @@ -143,6 +144,7 @@ bool php_phongo_read_preference_tags_are_valid(const bson_t* tags);

bool php_phongo_server_to_zval(zval* retval, mongoc_server_description_t* sd);
bool php_phongo_server_description_to_zval(zval* retval, mongoc_server_description_t* sd);
bool php_phongo_topology_description_to_zval(zval* retval, mongoc_topology_description_t* td);
void php_phongo_read_concern_to_zval(zval* retval, const mongoc_read_concern_t* read_concern);
void php_phongo_write_concern_to_zval(zval* retval, const mongoc_write_concern_t* write_concern);

Expand Down
18 changes: 18 additions & 0 deletions php_phongo_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ static inline php_phongo_serverdescription_t* php_serverdescription_fetch_object
{
return (php_phongo_serverdescription_t*) ((char*) obj - XtOffsetOf(php_phongo_serverdescription_t, std));
}
static inline php_phongo_topologydescription_t* php_topologydescription_fetch_object(zend_object* obj)
{
return (php_phongo_topologydescription_t*) ((char*) obj - XtOffsetOf(php_phongo_topologydescription_t, std));
}
static inline php_phongo_serverapi_t* php_serverapi_fetch_object(zend_object* obj)
{
return (php_phongo_serverapi_t*) ((char*) obj - XtOffsetOf(php_phongo_serverapi_t, std));
Expand Down Expand Up @@ -155,6 +159,10 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
{
return (php_phongo_commandsucceededevent_t*) ((char*) obj - XtOffsetOf(php_phongo_commandsucceededevent_t, std));
}
static inline php_phongo_topologychangedevent_t* php_topologychangedevent_fetch_object(zend_object* obj)
{
return (php_phongo_topologychangedevent_t*) ((char*) obj - XtOffsetOf(php_phongo_topologychangedevent_t, std));
}

#define Z_CLIENTENCRYPTION_OBJ_P(zv) (php_clientencryption_fetch_object(Z_OBJ_P(zv)))
#define Z_COMMAND_OBJ_P(zv) (php_command_fetch_object(Z_OBJ_P(zv)))
Expand All @@ -168,6 +176,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
#define Z_SERVERAPI_OBJ_P(zv) (php_serverapi_fetch_object(Z_OBJ_P(zv)))
#define Z_SERVERDESCRIPTION_OBJ_P(zv) (php_serverdescription_fetch_object(Z_OBJ_P(zv)))
#define Z_SESSION_OBJ_P(zv) (php_session_fetch_object(Z_OBJ_P(zv)))
#define Z_TOPOLOGYDESCRIPTION_OBJ_P(zv) (php_topologydescription_fetch_object(Z_OBJ_P(zv)))
#define Z_BULKWRITE_OBJ_P(zv) (php_bulkwrite_fetch_object(Z_OBJ_P(zv)))
#define Z_WRITECONCERN_OBJ_P(zv) (php_writeconcern_fetch_object(Z_OBJ_P(zv)))
#define Z_WRITECONCERNERROR_OBJ_P(zv) (php_writeconcernerror_fetch_object(Z_OBJ_P(zv)))
Expand All @@ -189,6 +198,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
#define Z_COMMANDFAILEDEVENT_OBJ_P(zv) (php_commandfailedevent_fetch_object(Z_OBJ_P(zv)))
#define Z_COMMANDSTARTEDEVENT_OBJ_P(zv) (php_commandstartedevent_fetch_object(Z_OBJ_P(zv)))
#define Z_COMMANDSUCCEEDEDEVENT_OBJ_P(zv) (php_commandsucceededevent_fetch_object(Z_OBJ_P(zv)))
#define Z_TOPOLOGYCHANGEDEVENT_OBJ_P(zv) (php_topologychangedevent_fetch_object(Z_OBJ_P(zv)))

#define Z_OBJ_CLIENTENCRYPTION(zo) (php_clientencryption_fetch_object(zo))
#define Z_OBJ_COMMAND(zo) (php_command_fetch_object(zo))
Expand All @@ -202,6 +212,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
#define Z_OBJ_SERVERAPI(zo) (php_serverapi_fetch_object(zo))
#define Z_OBJ_SERVERDESCRIPTION(zo) (php_serverdescription_fetch_object(zo))
#define Z_OBJ_SESSION(zo) (php_session_fetch_object(zo))
#define Z_OBJ_TOPOLOGYDESCRIPTION(zo) (php_topologydescription_fetch_object(zo))
#define Z_OBJ_BULKWRITE(zo) (php_bulkwrite_fetch_object(zo))
#define Z_OBJ_WRITECONCERN(zo) (php_writeconcern_fetch_object(zo))
#define Z_OBJ_WRITECONCERNERROR(zo) (php_writeconcernerror_fetch_object(zo))
Expand All @@ -223,6 +234,7 @@ static inline php_phongo_commandsucceededevent_t* php_commandsucceededevent_fetc
#define Z_OBJ_COMMANDFAILEDEVENT(zo) (php_commandfailedevent_fetch_object(zo))
#define Z_OBJ_COMMANDSTARTEDEVENT(zo) (php_commandstartedevent_fetch_object(zo))
#define Z_OBJ_COMMANDSUCCEEDEDEVENT(zo) (php_commandsucceededevent_fetch_object(zo))
#define Z_OBJ_TOPOLOGYCHANGEDEVENT(zo) (php_topologychangedevent_fetch_object(zo))

extern zend_class_entry* php_phongo_clientencryption_ce;
extern zend_class_entry* php_phongo_command_ce;
Expand All @@ -236,6 +248,7 @@ extern zend_class_entry* php_phongo_server_ce;
extern zend_class_entry* php_phongo_serverapi_ce;
extern zend_class_entry* php_phongo_serverdescription_ce;
extern zend_class_entry* php_phongo_session_ce;
extern zend_class_entry* php_phongo_topologydescription_ce;
extern zend_class_entry* php_phongo_bulkwrite_ce;
extern zend_class_entry* php_phongo_writeconcern_ce;
extern zend_class_entry* php_phongo_writeconcernerror_ce;
Expand Down Expand Up @@ -292,7 +305,9 @@ extern zend_class_entry* php_phongo_commandfailedevent_ce;
extern zend_class_entry* php_phongo_commandstartedevent_ce;
extern zend_class_entry* php_phongo_commandsubscriber_ce;
extern zend_class_entry* php_phongo_commandsucceededevent_ce;
extern zend_class_entry* php_phongo_sdamsubscriber_ce;
extern zend_class_entry* php_phongo_subscriber_ce;
extern zend_class_entry* php_phongo_topologychangedevent_ce;

extern void php_phongo_binary_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_dbpointer_init_ce(INIT_FUNC_ARGS);
Expand Down Expand Up @@ -335,6 +350,7 @@ extern void php_phongo_server_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_serverapi_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_serverdescription_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_session_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_topologydescription_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_writeconcern_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_writeconcernerror_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_writeerror_init_ce(INIT_FUNC_ARGS);
Expand Down Expand Up @@ -362,7 +378,9 @@ extern void php_phongo_commandfailedevent_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_commandstartedevent_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_commandsubscriber_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_commandsucceededevent_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_subscriber_init_ce(INIT_FUNC_ARGS);
extern void php_phongo_topologychangedevent_init_ce(INIT_FUNC_ARGS);

/* Shared function entries for disabling constructors and unserialize() */
PHP_FUNCTION(MongoDB_disabled___construct);
Expand Down
13 changes: 13 additions & 0 deletions php_phongo_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ typedef struct {
zend_object std;
} php_phongo_session_t;

typedef struct {
mongoc_topology_description_t* topology_description;
HashTable* properties;
zend_object std;
} php_phongo_topologydescription_t;

typedef struct {
HashTable* properties;
mongoc_write_concern_t* write_concern;
Expand Down Expand Up @@ -278,6 +284,13 @@ typedef struct {
zend_object std;
} php_phongo_commandsucceededevent_t;

typedef struct {
bson_oid_t topology_id;
mongoc_topology_description_t* new_topology_description;
mongoc_topology_description_t* old_topology_description;
zend_object std;
} php_phongo_topologychangedevent_t;

#endif /* PHONGO_STRUCTS */

/*
Expand Down
2 changes: 1 addition & 1 deletion src/LIBMONGOC_VERSION_CURRENT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.19.0
1.11.1-20210817+gitd2838038aa
62 changes: 62 additions & 0 deletions src/MongoDB/Monitoring/SDAMSubscriber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2021-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <php.h>
#include <Zend/zend_interfaces.h>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "phongo_compat.h"
#include "php_phongo.h"

zend_class_entry* php_phongo_sdamsubscriber_ce;

/* {{{ MongoDB\Driver\Monitoring\SDAMSubscriber function entries */
ZEND_BEGIN_ARG_INFO_EX(ai_SDAMSubscriber_topologyChanged, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, event, MongoDB\\Driver\\Monitoring\\TopologyChangedEvent, 0)
ZEND_END_ARG_INFO()

static zend_function_entry php_phongo_sdamsubscriber_me[] = {
/* clang-format off */
ZEND_ABSTRACT_ME(SDAMSubscriber, topologyChanged, ai_SDAMSubscriber_topologyChanged)
PHP_FE_END
/* clang-format on */
};
/* }}} */

void php_phongo_sdamsubscriber_init_ce(INIT_FUNC_ARGS) /* {{{ */
{
zend_class_entry ce;
(void) type;
(void) module_number;

INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver\\Monitoring", "SDAMSubscriber", php_phongo_sdamsubscriber_me);
php_phongo_sdamsubscriber_ce = zend_register_internal_interface(&ce);
zend_class_implements(php_phongo_sdamsubscriber_ce, 1, php_phongo_subscriber_ce);

return;
} /* }}} */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
Loading