ENGINE-282 - non-compiling code, interrupted because we may have found another solution to the problem - what's here is simply scraps for logging in a busy handler ENGINE-282
authorKrista Bennett <krista@pep-project.org>
Fri, 20 Oct 2017 11:31:40 +0200
branchENGINE-282
changeset 2175c088dc17ce17
parent 2174 e2f29960e2f6
ENGINE-282 - non-compiling code, interrupted because we may have found another solution to the problem - what's here is simply scraps for logging in a busy handler
src/pEpEngine.c
     1.1 --- a/src/pEpEngine.c	Thu Oct 19 11:03:28 2017 +0200
     1.2 +++ b/src/pEpEngine.c	Fri Oct 20 11:31:40 2017 +0200
     1.3 @@ -35,6 +35,58 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +static char* get_my_pthread_id_as_string() {
     1.8 +    pthread_t my_tid = pthread_self();
     1.9 +    size_t pthread_t_size = sizeof(pthread_t);
    1.10 +
    1.11 +    int MAX_BYTES = (pthread_t_size > 25 ? 25 : pthread_t_size);
    1.12 +    
    1.13 +    int i = 0;
    1.14 +    
    1.15 +    char* retval = calloc((2*MAX_BYTES) + 1);
    1.16 +    char* my_tid_bytes = (char*)(&my_tid);
    1.17 +    
    1.18 +    for (; i < MAX_BYTES; i++) {
    1.19 +        sprintf(retval + i, "%02x", my_tid_bytes[i]);
    1.20 +    }
    1.21 +
    1.22 +    return retval;
    1.23 +}
    1.24 +
    1.25 +/* KG: from https://github.com/jterrace/craq/blob/master/db-5.0.21/sql/examples/c/ex_sql_multi_thread.c
    1.26 + *     for testing
    1.27 + * Busy callback handler for all operations. If the busy callback handler is 
    1.28 + * NULL, then SQLITE_BUSY or SQLITE_IOERR_BLOCKED is returned immediately upon 
    1.29 + * encountering the lock. If the busy callback is not NULL, then the callback 
    1.30 + * might be invoked.
    1.31 + *
    1.32 + * This callback will be registered by SQLite's API:
    1.33 + *    int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
    1.34 + * That's very useful to deal with SQLITE_BUSY event automatically. Otherwise,
    1.35 + * you have to check the return code, reset statement and do retry manually.
    1.36 + */
    1.37 +static int
    1.38 +busy_handler(void *data, int retry)
    1.39 +{
    1.40 +	const int max_retries = 10;
    1.41 +
    1.42 +    char* my_thread_id = get_my_pthread_id_as_string();
    1.43 +        
    1.44 +	if (retry < max_retries) {
    1.45 +		/* Sleep a while and retry again. */
    1.46 +		printf("Thread %s hits SQLITE_BUSY %d times, retry again.\n",
    1.47 +			my_thread_id, retry);
    1.48 +		sqlite3_sleep(attr->sleep_ms);
    1.49 +		/* Return non-zero to let caller retry again. */
    1.50 +		return 1;
    1.51 +	}
    1.52 +	/* Return zero to let caller return SQLITE_BUSY immediately. */
    1.53 +	printf("Error: Thread %d had retried %d times, exit.\n",
    1.54 +		my_thread_id, retry);
    1.55 +	return 0;
    1.56 +}
    1.57 +
    1.58 +
    1.59  
    1.60  // sql manipulation statements
    1.61  static const char *sql_log =