Informazioni sulla libreria client non-bloccante

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

Note: This page describes features in the source repository for MariaDB 5.5. There are currently no official packages or binaries available for download which contain the features. If you want to try out any of the new features described here you will need to get and compile the code yourself.

MariaDB, a partire dalla versione 5.5.21, supporta le operazioni non-bloccanti nella libreria client. Questo permette a un'applicazione di iniziare una query o altre azioni su un database, e continuare a eseguire altri compiti (nello stesso thread) mentre la richiesta viene inviata attraverso la rete, la query viene elaborata dal server e il risultato viene rispedito al client. Man mano che porzioni del risultato sono pronte, l'applicazione può a suo piacere richiamare la ribreria, ripetendo questa azione finchè l'operazione non è completata.

Le operazioni non-bloccanti sono implementate interamente nella libreria client. Questo significa che non è necessario alcun supporto da parte del server e tali operazioni funzionano con qualsiasi versioni del server MariaDB o MySQL, esattamente come la normale API bloccante. Significa anche che non è possibile avere due query eseguite allo stesso tempo e nella stessa connessione (a causa di un limite del protocollo). Ma un singolo thread potrebbe lanciare un qualsiasi numero di query non bloccanti allo stesso tempo, ognuna delle quali utilizza un diverso oggetto connessione MYSQL.

Le operazioni non-bloccanti sono utili per le applicazioni che devono eseguire un certo numero di query indipendenti in parallelo allo stesso tempo, per eseguire certe operazioni più rapidamente di quanto potrebbero fare eseguendole in sequenza. Può trattarsi di diverse query eseguite su un solo server (per utilizzare meglio le core della CPU e/o un sistema di I/O ad alta capacità del server), o potrebbero essere query lanciate su server diversi (per esempio SHOW STATUS su tutti i server per monitorarne il funzionamento, o un'operazione di mappamento su grandi database frammentati).

Non-blocking operation is also very useful in applications that are already written in a non-blocking style, for example using a framework like libevent, or, for example, a GUI-application using an event loop. Using the non-blocking client library allows the integrations of database queries into such applications, without the risk of long-running queries "hanging" the user interface or stalling the event loop, and without having to manually spawn separate threads to run the queries and re-synchronize with the threads to get the results back.

In this context, "blocking" means the situation where communication on the network socket to the server has to wait while processing the query. Waiting can be necessary because the server has not yet had time to process the query, or because the data needs to travel over the network from the server, or even because the first part of a large request needs to be sent out on the network before local socket buffers can accept the last part. Whenever such a wait is necessary, control returns to the application. The application will then run select() or poll() (or something similar) to detect when any wait condition is satisfied, and then call back into the library to continue processing.

An example program is available in the MariaDB source tree:

tests/async_queries.c

It uses libevent to run a set of queries in parallel from within a single thread / event loop. This is a good example of how to integrate non-blocking query processing into an event-based framework.

The non-blocking API in the client library is entirely optional. The new library is completely ABI- and source-compatible with existing applications. Also, applications not using non-blocking operations are not affected, nor is there any significant performance penalty for having support for non-blocking operations in the library for applications which do not use them.

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.