LCOV - code coverage report
Current view: top level - monetdb5/modules/mal - oltp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 111 1.8 %
Date: 2021-01-13 20:07:21 Functions: 1 9 11.1 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * (c) Martin Kersten
      11             :  * This module provides an advisary lock manager for SQL transactions
      12             :  * that prefer waiting over transaction failures due to OCC
      13             :  * The table may only grow with lockable items
      14             :  * It could be extended with a semaphore for queue management
      15             :  */
      16             : #include "monetdb_config.h"
      17             : #include "oltp.h"
      18             : #include "gdk_time.h"
      19             : 
      20             : #define LOCKTIMEOUT (20 * 1000)
      21             : #define LOCKDELAY 20
      22             : 
      23             : typedef struct{
      24             :         Client cntxt;   // user holding the write lock
      25             :         lng start;              // time when it started
      26             :         lng retention;  // time when the lock is released
      27             :         lng total;              // accumulated lock time
      28             :         int used;               // how often it used, for balancing
      29             :         int locked;             // writelock set or not
      30             : } OLTPlockRecord;
      31             : 
      32             : static OLTPlockRecord oltp_locks[MAXOLTPLOCKS];
      33             : static int oltp_delay;
      34             : 
      35             : /*
      36             : static void
      37             : OLTPdump_(Client cntxt, str msg)
      38             : {
      39             :         int i;
      40             : 
      41             :         mnstr_printf(cntxt->fdout,"%s",msg);
      42             :         for(i=0; i< MAXOLTPLOCKS; i++)
      43             :         if( oltp_locks[i].locked)
      44             :                 mnstr_printf(cntxt->fdout,"#[%i] %3d\n",i, (oltp_locks[i].cntxt ? oltp_locks[i].cntxt->idxx: -1));
      45             : }
      46             : */
      47             : 
      48             : str
      49           0 : OLTPreset(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      50             : {       int i;
      51             :         (void) cntxt;
      52             :         (void) mb;
      53             :         (void) stk;
      54             :         (void) pci;
      55             : 
      56           0 :         MT_lock_set(&mal_oltpLock);
      57             : 
      58           0 :         for( i=0; i<MAXOLTPLOCKS; i++){
      59           0 :                 oltp_locks[i].locked = 0;
      60           0 :                 oltp_locks[i].cntxt = 0;
      61           0 :                 oltp_locks[i].start = 0;
      62           0 :                 oltp_locks[i].used = 0;
      63           0 :                 oltp_locks[i].retention = 0;
      64             :         }
      65           0 :         MT_lock_unset(&mal_oltpLock);
      66           0 :         return MAL_SUCCEED;
      67             : }
      68             : 
      69             : str
      70           0 : OLTPenable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      71             : {
      72             :         (void) mb;
      73             :         (void) stk;
      74             :         (void) pci;
      75             :         (void) cntxt;
      76             : 
      77           0 :         oltp_delay = TRUE;
      78           0 :         return MAL_SUCCEED;
      79             : }
      80             : 
      81             : str
      82           0 : OLTPdisable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      83             : {
      84           0 :         OLTPreset(cntxt, mb, stk,pci);
      85           0 :         oltp_delay = FALSE;
      86             :         (void) cntxt;
      87           0 :         return MAL_SUCCEED;
      88             : }
      89             : 
      90             : str
      91           0 : OLTPinit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      92             : {
      93             :         // nothing to be done right now
      94           0 :         return OLTPreset(cntxt,mb,stk,pci);
      95             : }
      96             : 
      97             : // The locking is based in the hash-table.
      98             : // It contains all write locks outstanding
      99             : // A transaction may proceed if no element in its read set is locked
     100             : 
     101             : str
     102           0 : OLTPlock(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     103             : {
     104             :         int i,lck;
     105           0 :         int clk, wait= GDKms();
     106             :         lng now;
     107             :         str sql,cpy;
     108             : 
     109             :         (void) stk;
     110           0 :         if ( oltp_delay == FALSE )
     111             :                 return MAL_SUCCEED;
     112             : 
     113           0 :         TRC_DEBUG(MAL_SERVER, "%6d lock request for client: %d, pc %d", GDKms(), cntxt->idx, pci->pc);
     114             : 
     115             :         do{
     116           0 :                 MT_lock_set(&mal_oltpLock);
     117           0 :                 clk = GDKms();
     118           0 :                 now = GDKusec();
     119             :                 // check if all write locks are available
     120           0 :                 for( i=1; i< pci->argc; i++){
     121           0 :                         lck= getVarConstant(mb, getArg(pci,i)).val.ival;
     122           0 :                         if ( lck > 0 && (oltp_locks[lck].locked || oltp_locks[lck].retention > now ))
     123             :                                 break;
     124             :                 }
     125             : 
     126           0 :                 if( i  == pci->argc ){
     127           0 :                         TRC_DEBUG(MAL_SERVER, "OLTP '%6d' set lock for client: %d\n", GDKms(), cntxt->idx);
     128             : 
     129           0 :                         for( i=1; i< pci->argc; i++){
     130           0 :                                 lck= getVarConstant(mb, getArg(pci,i)).val.ival;
     131             :                                 // only set the write locks
     132           0 :                                 if( lck > 0){
     133           0 :                                         oltp_locks[lck].cntxt = cntxt;
     134           0 :                                         oltp_locks[lck].start = now;
     135           0 :                                         oltp_locks[lck].locked = 1;
     136           0 :                                         oltp_locks[lck].retention = 0;
     137             :                                 }
     138             :                         }
     139           0 :                         MT_lock_unset(&mal_oltpLock);
     140           0 :                         return MAL_SUCCEED;
     141             :                 } else {
     142           0 :                         MT_lock_unset(&mal_oltpLock);
     143           0 :                         TRC_DEBUG(MAL_SERVER, "%d delay imposed for client: %d\n", GDKms(), cntxt->idx);
     144           0 :                         MT_sleep_ms(LOCKDELAY);
     145             :                 }
     146           0 :         } while( clk - wait < LOCKTIMEOUT);
     147             : 
     148           0 :         TRC_DEBUG(MAL_SERVER, "%6d proceed query for client: %d\n", GDKms(), cntxt->idx);
     149             : 
     150             :         // if the time out is related to a copy_from query, we should not start it either.
     151           0 :         sql = getName("sql");
     152           0 :         cpy = getName("copy_from");
     153             : 
     154           0 :         for( i = 0; i < mb->stop; i++)
     155           0 :                 if( getFunctionId(getInstrPtr(mb,i)) == cpy && getModuleId(getInstrPtr(mb,i)) == sql ){
     156           0 :                         TRC_DEBUG(MAL_SERVER, "%6d bail out a concurrent copy into: %d\n", GDKms(), cntxt->idx);
     157           0 :                         throw(SQL,"oltp.lock","Conflicts with other write operations\n");
     158             :                 }
     159             :         return MAL_SUCCEED;
     160             : }
     161             : 
     162             : str
     163           0 : OLTPrelease(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     164             : {
     165             :         int i,lck;
     166             :         lng delay,clk;
     167             : 
     168             :         (void) cntxt;
     169             :         (void) stk;
     170           0 :         if ( oltp_delay == FALSE )
     171             :                 return MAL_SUCCEED;
     172             : 
     173           0 :         MT_lock_set(&mal_oltpLock);
     174           0 :         clk = GDKusec();
     175             : 
     176           0 :         TRC_DEBUG(MAL_SERVER, "%6d release the locks: %d", GDKms(), cntxt->idx);
     177             : 
     178           0 :         for( i=1; i< pci->argc; i++){
     179           0 :                 lck= getVarConstant(mb, getArg(pci,i)).val.ival;
     180           0 :                 if( lck > 0){
     181           0 :                                 oltp_locks[lck].total += clk - oltp_locks[lck].start;
     182           0 :                                 oltp_locks[lck].used ++;
     183           0 :                                 oltp_locks[lck].cntxt = 0;
     184           0 :                                 oltp_locks[lck].start = 0;
     185           0 :                                 oltp_locks[lck].locked = 0;
     186           0 :                                 delay = oltp_locks[lck].total/ oltp_locks[lck].used;
     187           0 :                                 if( delay > LOCKDELAY || delay < LOCKDELAY/10)
     188             :                                         delay = LOCKDELAY;
     189           0 :                                 oltp_locks[lck].retention = clk + delay;
     190           0 :                                 TRC_DEBUG(MAL_SERVER, "Retention period for lock: %d " LLFMT"\n", lck, delay);
     191             :                         }
     192             :         }
     193           0 :         MT_lock_unset(&mal_oltpLock);
     194           0 :         return MAL_SUCCEED;
     195             : }
     196             : 
     197             : str
     198           0 : OLTPtable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     199             : {
     200             :         BAT *bs= NULL, *bu= NULL, *bl= NULL, *bq= NULL, *bc = NULL;
     201           0 :         bat *started = getArgReference_bat(stk,pci,0);
     202           0 :         bat *userid = getArgReference_bat(stk,pci,1);
     203           0 :         bat *lockid = getArgReference_bat(stk,pci,2);
     204           0 :         bat *used = getArgReference_bat(stk,pci,3);
     205             :         int i;
     206             :         timestamp tsn;
     207             : 
     208             :         (void) cntxt;
     209             :         (void) mb;
     210             : 
     211           0 :         bs = COLnew(0, TYPE_timestamp, 0, TRANSIENT);
     212           0 :         bu = COLnew(0, TYPE_str, 0, TRANSIENT);
     213           0 :         bl = COLnew(0, TYPE_int, 0, TRANSIENT);
     214           0 :         bc = COLnew(0, TYPE_int, 0, TRANSIENT);
     215           0 :         bq = COLnew(0, TYPE_str, 0, TRANSIENT);
     216             : 
     217           0 :         if( bs == NULL || bu == NULL || bl == NULL  || bq == NULL || bc == NULL){
     218           0 :                 if( bs) BBPunfix(bs->batCacheid);
     219           0 :                 if( bl) BBPunfix(bl->batCacheid);
     220           0 :                 if( bu) BBPunfix(bu->batCacheid);
     221           0 :                 if( bc) BBPunfix(bc->batCacheid);
     222           0 :                 if( bq) BBPunfix(bq->batCacheid);
     223           0 :                 throw(MAL,"oltp.table", GDK_EXCEPTION);
     224             :         }
     225           0 :         for( i = 0; i < MAXOLTPLOCKS; i++) {
     226           0 :                 if (oltp_locks[i].used ){
     227           0 :                         tsn = oltp_locks[i].start ? timestamp_fromusec(oltp_locks[i].start) : timestamp_nil;
     228           0 :                         if (BUNappend(bs, &tsn, false) != GDK_SUCCEED ||
     229           0 :                                 BUNappend(bu, oltp_locks[i].cntxt ? oltp_locks[i].cntxt->username : str_nil, false) != GDK_SUCCEED ||
     230           0 :                                 BUNappend(bl, &i, false) != GDK_SUCCEED ||
     231           0 :                                 BUNappend(bc, &oltp_locks[i].used, false) != GDK_SUCCEED)
     232           0 :                                 goto bailout;
     233             :                 }
     234             :         }
     235             :         //OLTPdump_(cntxt,"#lock table\n");
     236           0 :         BBPkeepref(*started = bs->batCacheid);
     237           0 :         BBPkeepref(*userid = bu->batCacheid);
     238           0 :         BBPkeepref(*lockid = bl->batCacheid);
     239           0 :         BBPkeepref(*used = bc->batCacheid);
     240           0 :         return MAL_SUCCEED;
     241             :   bailout:
     242           0 :         BBPunfix(bs->batCacheid);
     243           0 :         BBPunfix(bl->batCacheid);
     244           0 :         BBPunfix(bu->batCacheid);
     245           0 :         BBPunfix(bc->batCacheid);
     246           0 :         BBPunfix(bq->batCacheid);
     247           0 :         throw(MAL, "oltp.table", GDK_EXCEPTION);
     248             : }
     249             : 
     250             : str
     251           0 : OLTPis_enabled(int *ret) {
     252           0 :   *ret = oltp_delay;
     253           0 :   return MAL_SUCCEED;
     254             : }
     255             : 
     256             : #include "mel.h"
     257             : mel_func oltp_init_funcs[] = {
     258             :  pattern("oltp", "init", OLTPinit, true, "Initialize the lock table", noargs),
     259             :  pattern("oltp", "enable", OLTPenable, true, "Enable the OLTP delay monitor", noargs),
     260             :  pattern("oltp", "disable", OLTPdisable, true, "Disable the OLTP delay monitor", noargs),
     261             :  pattern("oltp", "reset", OLTPreset, true, "Reset the OLTP lock table", noargs),
     262             :  pattern("oltp", "lock", OLTPlock, true, "Wait for all write locks needed", args(1,2, arg("",void),vararg("lck",int))),
     263             :  pattern("oltp", "release", OLTPrelease, true, "Release for all write locks needed", args(1,2, arg("",void),vararg("lck",int))),
     264             :  pattern("oltp", "table", OLTPtable, true, "Show status of lock table", args(4,4, batarg("start",timestamp),batarg("usr",str),batarg("unit",int),batarg("cnt",int))),
     265             :  command("oltp", "isenabled", OLTPis_enabled, true, "Query the OLTP state", args(1,1, arg("",int))),
     266             :  { .imp=NULL }
     267             : };
     268             : #include "mal_import.h"
     269             : #ifdef _MSC_VER
     270             : #undef read
     271             : #pragma section(".CRT$XCU",read)
     272             : #endif
     273         255 : LIB_STARTUP_FUNC(init_oltp_mal)
     274         255 : { mal_module("oltp", NULL, oltp_init_funcs); }

Generated by: LCOV version 1.14