Skip to content
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

MDEV-30432: Refactor connect to use libcurl instead of cpprestsdk/curl #2996

Open
wants to merge 1 commit into
base: 11.4
Choose a base branch
from

Conversation

an3l
Copy link
Contributor

@an3l an3l commented Jan 11, 2024

  • Remove GetRest occurance (cpprestsdk references)
  • Drop execv(curl) references
  • Link to libcurl (Unix/Windows)
  • Use IMPORTED target, so that INCLUDE_DIRECTORIES be called implicity
  • Add libcurl feature to ConnectSE to issue HTPP requests

Check linkage

  • The libcurl is linked in dynamic library
$ ldd storage/connect/ha_connect.so |grep libcurl
	libcurl.so.4 => /lib/x86_64-linux-gnu/libcurl.so.4 (0x00007fd815d28000)

Check linkage on Windows

  • MDBF-644
>> cmake ../server -A x64 -DPLUGIN_ROCKSDB=NO -DMYSQL_MAINTAINER_MODE=ERR -Wno-dev -DCURL_LIBRARY=C:/ProgramData/chocolatey/lib/curl/tools/curl-7.85.0-win64-mingw/lib/libcurl.dll.a -DCURL_INCLUDE_DIR=C:/ProgramData/chocolatey/lib/curl/tools/curl-7.85.0-win64-mingw/include
-- Found CURL: C:/ProgramData/chocolatey/lib/curl/tools/curl-7.85.0-win64-mingw/lib/libcurl.dll.a (found version "7.85.0")
The following features have been enabled:
 * CONNECT_REST, Support for REST API in the CONNECT storage engine
 
>> # Only building connect module 
>> cmake --build . --verbose --config Debug --target connect -- -m
>> dumpbin /dependents storage\connect\Debug\ha_connect.dll
Image has the following dependencies:
    ODBC32.dll
    IPHLPAPI.DLL
    libcurl-x64.dll  # <<< here we are << 
    server.dll
    KERNEL32.dll
    USER32.dll
    ole32.dll
    OLEAUT32.dll
    ADVAPI32.dll
    WS2_32.dll
    api-ms-win-crt-string-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-convert-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-filesystem-l1-1-0.dll
    api-ms-win-crt-time-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    api-ms-win-crt-environment-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-crt-utility-l1-1-0.dll

Check without libcurl

Remove the library and check the patch
Install DB and run server locally ✔️
$ sudo apt remove libcurl4-openssl-dev
$ cmake --build . --target connect
[  0%] Built target uca-dump
[  8%] Built target mysqlservices
[  8%] Built target GenUnicodeDataSource
[ 58%] Built target mysys
[ 75%] Built target strings
[ 75%] Built target dbug
[ 75%] Built target comp_err
[ 75%] Built target GenError
make[3]: *** No rule to make target '/usr/lib/x86_64-linux-gnu/libcurl.so', needed by 'storage/connect/ha_connect.so'.  Stop.
make[3]: *** Waiting for unfinished jobs....
[ 75%] Building CXX object storage/connect/CMakeFiles/connect.dir/tabrest.cpp.o
/home/anel/GitHub/mariadb/server/src/connect-curl-11.4/storage/connect/tabrest.cpp:39:10: fatal error: curl/curl.h: No such file or directory
   39 | #include 
      |          ^~~~~~~~~~~~~
compilation terminated.

Check in container

  • This patch closes MDEV-26727 (tested with Docker):
buildbot@0013f8b295b9:/$ sudo mariadb --user root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 11.4.0-MariaDB-1:11.4.0+maria~deb11 mariadb.org binary distribution
MariaDB [test]> create table cusers3 engine=connect http='http://jsonplaceholder.typicode.com/users' table_type=json;
Query OK, 0 rows affected, 1 warning (0.179 sec)

MariaDB [test]> select * from cusers3;
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+
| id | name                     | username         | email                     | address_street    | address_suite | address_city   | address_zipcode | address_geo_lat | address_geo_lng | phone                 | website       | company_name       | company_catchPhrase                      | company_bs                           |
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+
|  1 | Leanne Graham            | Bret             | Sincere@april.biz         | Kulas Light       | Apt. 556      | Gwenborough    | 92998-3874      | -37.3159        | 81.1496         | 1-770-736-8031 x56442 | hildegard.org | Romaguera-Crona    | Multi-layered client-server neural-net   | harness real-time e-markets          |
|  2 | Ervin Howell             | Antonette        | Shanna@melissa.tv         | Victor Plains     | Suite 879     | Wisokyburgh    | 90566-7771      | -43.9509        | -34.4618        | 010-692-6593 x09125   | anastasia.net | Deckow-Crist       | Proactive didactic contingency           | synergize scalable supply-chains     |
|  3 | Clementine Bauch         | Samantha         | Nathan@yesenia.net        | Douglas Extension | Suite 847     | McKenziehaven  | 59590-4157      | -68.6102        | -47.0653        | 1-463-123-4447        | ramiro.info   | Romaguera-Jacobson | Face to face bifurcated interface        | e-enable strategic applications      |
|  4 | Patricia Lebsack         | Karianne         | Julianne.OConner@kory.org | Hoeger Mall       | Apt. 692      | South Elvis    | 53919-4257      | 29.4572         | -164.2990       | 493-170-9623 x156     | kale.biz      | Robel-Corkery      | Multi-tiered zero tolerance productivity | transition cutting-edge web services |
|  5 | Chelsey Dietrich         | Kamren           | Lucio_Hettinger@annie.ca  | Skiles Walks      | Suite 351     | Roscoeview     | 33263           | -31.8129        | 62.5342         | (254)954-1289         | demarco.info  | Keebler LLC        | User-centric fault-tolerant solution     | revolutionize end-to-end systems     |
|  6 | Mrs. Dennis Schulist     | Leopoldo_Corkery | Karley_Dach@jasper.info   | Norberto Crossing | Apt. 950      | South Christy  | 23505-1337      | -71.4197        | 71.7478         | 1-477-935-8478 x6430  | ola.org       | Considine-Lockman  | Synchronised bottom-line interface       | e-enable innovative applications     |
|  7 | Kurtis Weissnat          | Elwyn.Skiles     | Telly.Hoeger@billy.biz    | Rex Trail         | Suite 280     | Howemouth      | 58804-1099      | 24.8918         | 21.8984         | 210.067.6132          | elvis.io      | Johns Group        | Configurable multimedia task-force       | generate enterprise e-tailers        |
|  8 | Nicholas Runolfsdottir V | Maxime_Nienow    | Sherwood@rosamond.me      | Ellsworth Summit  | Suite 729     | Aliyaview      | 45169           | -14.3990        | -120.7677       | 586.493.6943 x140     | jacynthe.com  | Abernathy Group    | Implemented secondary concept            | e-enable extensible e-tailers        |
|  9 | Glenna Reichert          | Delphine         | Chaim_McDermott@dana.io   | Dayna Park        | Suite 449     | Bartholomebury | 76495-3109      | 24.6463         | -168.8889       | (775)976-6794 x41206  | conrad.com    | Yost and Sons      | Switchable contextually-based project    | aggregate real-time technologies     |
| 10 | Clementina DuBuque       | Moriah.Stanton   | Rey.Padberg@karina.biz    | Kattie Turnpike   | Suite 198     | Lebsackbury    | 31428-2261      | -38.2386        | 57.2232         | 024-648-3804          | ambrose.net   | Hoeger LLC         | Centralized empowering task-force        | target end-to-end models             |
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+
10 rows in set (0.126 sec)

Check container from CLI

$ docker exec -it connect-patch sudo mariadb -uroot -e "use test; create table cusers4 engine=connect http='http://jsonplaceholder.typicode.com/users' table_type=json; select * from cusers4;"
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+
| id | name                     | username         | email                     | address_street    | address_suite | address_city   | address_zipcode | address_geo_lat | address_geo_lng | phone                 | website       | company_name       | company_catchPhrase                      | company_bs                           |
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+
|  1 | Leanne Graham            | Bret             | Sincere@april.biz         | Kulas Light       | Apt. 556      | Gwenborough    | 92998-3874      | -37.3159        | 81.1496         | 1-770-736-8031 x56442 | hildegard.org | Romaguera-Crona    | Multi-layered client-server neural-net   | harness real-time e-markets          |
|  2 | Ervin Howell             | Antonette        | Shanna@melissa.tv         | Victor Plains     | Suite 879     | Wisokyburgh    | 90566-7771      | -43.9509        | -34.4618        | 010-692-6593 x09125   | anastasia.net | Deckow-Crist       | Proactive didactic contingency           | synergize scalable supply-chains     |
|  3 | Clementine Bauch         | Samantha         | Nathan@yesenia.net        | Douglas Extension | Suite 847     | McKenziehaven  | 59590-4157      | -68.6102        | -47.0653        | 1-463-123-4447        | ramiro.info   | Romaguera-Jacobson | Face to face bifurcated interface        | e-enable strategic applications      |
|  4 | Patricia Lebsack         | Karianne         | Julianne.OConner@kory.org | Hoeger Mall       | Apt. 692      | South Elvis    | 53919-4257      | 29.4572         | -164.2990       | 493-170-9623 x156     | kale.biz      | Robel-Corkery      | Multi-tiered zero tolerance productivity | transition cutting-edge web services |
|  5 | Chelsey Dietrich         | Kamren           | Lucio_Hettinger@annie.ca  | Skiles Walks      | Suite 351     | Roscoeview     | 33263           | -31.8129        | 62.5342         | (254)954-1289         | demarco.info  | Keebler LLC        | User-centric fault-tolerant solution     | revolutionize end-to-end systems     |
|  6 | Mrs. Dennis Schulist     | Leopoldo_Corkery | Karley_Dach@jasper.info   | Norberto Crossing | Apt. 950      | South Christy  | 23505-1337      | -71.4197        | 71.7478         | 1-477-935-8478 x6430  | ola.org       | Considine-Lockman  | Synchronised bottom-line interface       | e-enable innovative applications     |
|  7 | Kurtis Weissnat          | Elwyn.Skiles     | Telly.Hoeger@billy.biz    | Rex Trail         | Suite 280     | Howemouth      | 58804-1099      | 24.8918         | 21.8984         | 210.067.6132          | elvis.io      | Johns Group        | Configurable multimedia task-force       | generate enterprise e-tailers        |
|  8 | Nicholas Runolfsdottir V | Maxime_Nienow    | Sherwood@rosamond.me      | Ellsworth Summit  | Suite 729     | Aliyaview      | 45169           | -14.3990        | -120.7677       | 586.493.6943 x140     | jacynthe.com  | Abernathy Group    | Implemented secondary concept            | e-enable extensible e-tailers        |
|  9 | Glenna Reichert          | Delphine         | Chaim_McDermott@dana.io   | Dayna Park        | Suite 449     | Bartholomebury | 76495-3109      | 24.6463         | -168.8889       | (775)976-6794 x41206  | conrad.com    | Yost and Sons      | Switchable contextually-based project    | aggregate real-time technologies     |
| 10 | Clementina DuBuque       | Moriah.Stanton   | Rey.Padberg@karina.biz    | Kattie Turnpike   | Suite 198     | Lebsackbury    | 31428-2261      | -38.2386        | 57.2232         | 024-648-3804          | ambrose.net   | Hoeger LLC         | Centralized empowering task-force        | target end-to-end models             |
+----+--------------------------+------------------+---------------------------+-------------------+---------------+----------------+-----------------+-----------------+-----------------+-----------------------+---------------+--------------------+------------------------------------------+--------------------------------------+

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.
  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@an3l an3l added the MariaDB Foundation Pull requests created by MariaDB Foundation label Jan 11, 2024
@an3l an3l added this to the 11.4 milestone Jan 11, 2024
@an3l an3l requested a review from LinuxJedi January 11, 2024 14:31
@an3l an3l self-assigned this Jan 11, 2024
storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
/***********************************************************************/
/* WriteMemoryCallback: Curl callback function */
/***********************************************************************/
static size_t WriteMemoryCallback(void *contents,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default function writes to a file. Couldn't CURLOPT_WRITEDATA be used to set the FILE* pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried this as the first approach in first testing, but the data returned in file were cut, since size cannot be known in advance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds odd. Most web requests that curl would be programmed to do won't know the file size in advance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worked with this:

diff --git a/storage/connect/tabrest.cpp b/storage/connect/tabrest.cpp
index 7413f6029b9..f976773f9a0 100644
--- a/storage/connect/tabrest.cpp
+++ b/storage/connect/tabrest.cpp
@@ -277,14 +277,11 @@ int RESTDEF::curl_run(PGLOBAL g)
   struct MemoryStruct chunk;
   chunk.memory = (char *)malloc(1);
   chunk.size = 0;
+  FILE *f= fopen(Fn, "wb");
 
   if ((curl_res= curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf)) !=
         CURLE_OK ||
-      (curl_res= curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
-                                  WriteMemoryCallback)) !=
-        CURLE_OK ||
-      (curl_res= curl_easy_setopt(curl, CURLOPT_WRITEDATA,
-                                 (void *)&chunk)) !=
+      (curl_res= curl_easy_setopt(curl, CURLOPT_WRITEDATA, f)) !=
         CURLE_OK ||
       (curl_res = curl_easy_setopt(curl, CURLOPT_URL, buf)) != CURLE_OK ||
       (curl_res = curl_easy_perform(curl)) != CURLE_OK ||
@@ -303,8 +300,6 @@ int RESTDEF::curl_run(PGLOBAL g)
     }
   }
   curl_easy_cleanup(curl);
-  FILE *f= fopen(Fn, "wb");
-  fprintf(f, "%s", chunk.memory);
   fclose(f);
   free(chunk.memory);
   bool is_error = http_code < 200 || http_code >= 300;
  • https://curl.se/libcurl/c/CURLOPT_WRITEDATA.html says libcurl DLL must have a WRITEFUNCTION.
  • BB currently doesn't have/detect libcurl on amd-windows. MDBF-644
  • Internally in curl, fwrite is used directly as the CURLOPT_WRITEFUNCTION. Unsure if that will directly work on Windows.

curl_errbuf[0] = '\0';
if (Uri)
{
if (*Uri == '/' || Http[strlen(Http) - 1] == '/')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really odd. What's it doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inherited from old code, about parsing the argument from table options.

storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
storage/connect/tabrest.cpp Outdated Show resolved Hide resolved
@an3l an3l force-pushed the bb-11.4-anel-connect-curl-MDEV-30432-v1 branch from cac2d30 to bed924a Compare January 30, 2024 08:47
@grooverdan
Copy link
Member

before I forget - need to revert #2466 as part of this.

@an3l an3l force-pushed the bb-11.4-anel-connect-curl-MDEV-30432-v1 branch from bed924a to afd313a Compare February 7, 2024 08:45
- Remove GetRest occurance (cpprestsdk references)
- Drop execv(curl) references
- Link to libcurl (Unix/Windows)
  - Use IMPORTED target, so that INCLUDE_DIRECTORIES be called implicity
- Add libcurl feature to ConnectSE to issue HTPP requests

- This patch closes MDEV-26727 (tested with Docker)
- Reviewer: <>
@an3l an3l force-pushed the bb-11.4-anel-connect-curl-MDEV-30432-v1 branch from afd313a to 3ed2341 Compare February 7, 2024 14:04
@an3l an3l changed the title MDEV-30432: Remove cpprestsdk from Connect MDEV-30432: Refactor connect to use libcurl instead of cpprestsdk/curl Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MariaDB Foundation Pull requests created by MariaDB Foundation
2 participants