pharo-nosql / mongotalk

A Pharo driver for MongoDB
MIT License
19 stars 13 forks source link

SDAM client #68

Closed tinchodias closed 4 years ago

tinchodias commented 4 years ago

These changes add a partial implementation of a SDAM MongoClient (explained in issue #54 but also give an introduction in this text).

Intro

As you know, we can open a connection and perform a write operation simply as follows:

mongo := Mongo host: 'localhost' port: 27017.
mongo open.
((mongo 
    databaseNamed: 'test')
    getCollection: 'pilots')
    add: { 'name' -> 'Martin' } asDictionary.

This is fine in a mongodb single server configuration, but it is not as simple when we are not sure to which server perform the operation. This is the case when the server belongs to a Replica Set, because write operations can only be performed to the current primary server and it may change along the time due to network issues or other inconveniences.

Additionally, Replica Sets support read operations to be performed on non-primary servers so caller could choose between multiple servers (see mongodb's ReadPreferences).

SDAM client

To help in this kind of scenarios, the SDAM MongoClient monitors the Replica Set status to provide the instance of Mongo that the caller requires to perform the operation.

The client can be created and make it start monitoring as follows:

client := MongoClient withUrls: urlsOfSomeReplicaSetMembers.
client start.

and perform write operations with code such as:

client primaryMongoDo: [ :mongo |
    ((mongo 
        databaseNamed: 'test')
        getCollection: 'pilots')
        add: { 'name' -> 'Martin' } asDictionary ].

Note

This is only a partial implementation of the official mongodb's SDAM specification. Why? One reason is that the MongoClient doesn't provide any db or collection operations (e.g. CRUD operations) like the specification states. Instead, such operations are supported by first obtaining a Mongo instance (the one provided by current MongoTalk) and then obtaining the db/collection to perform the operations.

Metacello configuration

Reviewers can load this change with:

Metacello new 
    baseline: 'MongoTalk'; 
    repository: 'github://tinchodias/mongotalk:features-sdam2/mc'; 
    load: #('SDAM').

Please note that SDAM requires taskit as dependency (the client monitors are taskit services).


This change is Reviewable

tinchodias commented 4 years ago

Hello all, it is finally ready for merging, I can just do it as it doesn't alter the "default" group of the Metacello baseline.

But would be great if you can give your opinion/review. @noha ? @estebanlm ? :) :)

tinchodias commented 4 years ago

Some possible questions:

tinchodias commented 4 years ago

mc/Mongo-SDAM.package/MongoClient.class/instance/mongoDo.serverSelection..st, line 7 at r1 (raw file):

Previously, zecket495 wrote…
This doesn't honor a socket timeout and might block for a long time.

Oh, this is a pending-to-fix comment.

tinchodias commented 4 years ago

The period to review it has extended too long and the changes doesn't impact users unless they load the SDAM group specifically. So I wrote some documentation for README and I'm merging it.