View Single Post
GuruQA
Member
Join Date: Oct 2014
Old 02-20-2015 , 04:35   Re: SQL_ThreadQuery stop server in restart or amx_map if mysql server goes offline
Reply With Quote #29

I analyzed your test

Have you tested with the local IP 127.0.0.1

Makes a test with this IP:

Code:
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
//     https://alliedmods.net/amxmodx-license
 
#include amxmodx
#include sqlx
 
// SQL credentials
#define HOST "200.149.119.103"
#define USER "root"
#define PASSWORD "<???>"
#define DATABASE "hattrick"
 
// Should never reach here
#define QUEUE_TIME_FOR_WARNING 5.0
 
new Handle:g_Tuple;
new Array:g_startedThreads;
new g_finishedThreads = 0;
 
public plugin_init()
{
        register_plugin("SQL Stress Test", "1.0", "Hattrick (HttrckCldHKS)");
 
        // This array will be filled in with threads that have been started
        g_startedThreads = ArrayCreate();
 
        // Stress SQL natives 5 times a second
        set_task(0.2, "stressSQL", .flags = "b");
}
 
public stressSQL()
{
        // Stress MakeDbTuple
        g_Tuple = SQL_MakeDbTuple(HOST, USER, PASSWORD, DATABASE);
 
        if (g_Tuple == Empty_Handle)
        {
                // Less probable you reach here...
                log_amx("SQL_MakeDbTuple Failed!");
                return;
        }
 
        // Stress Connect
        new errorId, Error[512], \
                Handle:Connection = SQL_Connect(g_Tuple, errorId, Error, charsmax(Error));
 
        if (Connection == Empty_Handle || errorId || Error[0])
        {
                // Only reach here if MySQL server is down
                log_amx("SQL_Connect Failed! errorId = %d | Error = %s", errorId, Error);
                goto doThreadQuery;
        }
 
        // Stress PrepareQuery
        new Handle:Query = SQL_PrepareQuery(Connection, "CREATE TABLE IF NOT EXISTS Developers (Name TEXT);");
        SQL_Execute(Query);
        SQL_FreeHandle(Query);
 
        Query = SQL_PrepareQuery(Connection, "CREATE TABLE IF NOT EXISTS Developers (Name TEXT);");
        SQL_Execute(Query);
        SQL_FreeHandle(Query);
 
        Query = SQL_PrepareQuery(Connection, "INSERT INTO Developers VALUES ('Arkshine');");
        SQL_Execute(Query);
        SQL_FreeHandle(Query);
 
        Query = SQL_PrepareQuery(Connection, "INSERT INTO Developers VALUES ('Hattrick');");
        SQL_Execute(Query);
        SQL_FreeHandle(Query);
 
        SQL_FreeHandle(Connection);
 
        // Stress ThreadQuery
doThreadQuery:
        SQL_ThreadQuery(g_Tuple, "displayFunction", "SELECT * FROM Developers ORDER BY Name DESC LIMIT 10;");
 
        // Mark this threaded query as started
        ArrayPushCell(g_startedThreads, 1);
}
 
// Display some data, as a test
public displayFunction(failState, Handle:Query, Error[], errorId, Data[], dataSize, Float:queueTime)
{
        // Only reach here if there is no connection or invalid query
        if (failState || errorId || Error[0])
                log_amx("SQL_ThreadQuery Failed! FailState = %d | Error = %s | errorId = %d", failState, Error, errorId);
 
        else
        {
                if (SQL_NumRows(Query))
                {
                        // Stress results
                        while (SQL_MoreResults(Query))
                        {
                                new Name[512];
                                SQL_ReadResult(Query, 0, Name, charsmax(Name));
 
                                SQL_NextRow(Query);
                        }
 
                        // Rewind from zero
                        SQL_Rewind(Query);
 
                        // Stress again
                        while (SQL_MoreResults(Query))
                        {
                                new Name[512];
                                SQL_ReadResult(Query, 0, Name, charsmax(Name));
 
                                SQL_NextRow(Query);
 
                                // Print
                                server_print(Name);
                        }
                }
 
                // Get field's name
                new fieldName[512];
                SQL_FieldNumToName(Query, 0, fieldName, charsmax(fieldName));
 
                server_print(fieldName);
        }
 
        // Should never reach here
        if (queueTime > QUEUE_TIME_FOR_WARNING)
                server_print("Attention! QueueTime == %f", queueTime);
 
        // This threaded query was finished
        g_finishedThreads++;
}
 
public plugin_end()
{
        if (g_Tuple != Empty_Handle)
                SQL_FreeHandle(g_Tuple);
 
        // Compare to see if all threads succeeded
        new startedThreads = ArraySize(g_startedThreads);
        if (g_finishedThreads != startedThreads)
        {
                // Now... you will probably reach here if there is no Internet connection...
                // Of course, this message will be displayed, but dont worry :
                // X = startedThreads - g_finishedThreads
                // So, after this message is displayed, you will see X new messages (while OnPluginsUnloading()), being displayed
                // They will be SQL_ThreadQuery's Handlers being executed after this plug-in's plugin_end() forward.
                // They'll more than probably say :  No internet connection or TQuery failed.
                server_print("Problem at PluginEnd() :  %d threads have been started but only %d were executed...", \
                        startedThreads, g_finishedThreads);
                // So, everything is going to be executed, and much more faster than before.
        }
}

Last edited by GuruQA; 02-20-2015 at 04:38.
GuruQA is offline