Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Sintleton group transaction support
0003  *
0004  * @file txsinglegrp.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <ndrx_config.h>
0035 #include <string.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <errno.h>
0040 #include <sys/sem.h>
0041 
0042 #include <lcfint.h>
0043 #include <atmi.h>
0044 #include <atmi_shm.h>
0045 #include <ndrstandard.h>
0046 #include <ndebug.h>
0047 #include <ndrxdcmn.h>
0048 #include <userlog.h>
0049 #include <singlegrp.h>
0050 #include <Exfields.h>
0051 #include <ubfutil.h>
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 /**
0061  * Extended lock verificaiton, if enabled on.
0062  * @param grpno group number
0063  * @return -1 on FAIL, 0 not locked, > 0 (sequence of the lock)
0064  */
0065 expublic long ndrx_tpsgislocked(int grpno, long flags, long *grp_flags)
0066 {
0067     long ret=EXSUCCEED;
0068     ndrx_sg_shm_t *p_shm, local;
0069     UBFH *p_ub=NULL;
0070 
0071     NDRX_LOG(log_debug, "Checking if group %d is locked", grpno);
0072 
0073     /*
0074      * get the flags from the sintleton group...
0075      */
0076     if (0==grpno)
0077     {
0078         grpno = G_atmi_env.procgrp_no;
0079     }
0080 
0081     NDRX_LOG(log_debug, "Checking if group %d is locked", grpno);
0082 
0083     p_shm = ndrx_sg_get(grpno);
0084 
0085     if (NULL==p_shm)
0086     {
0087         /* set error */
0088         ndrx_TPset_error_fmt(TPEINVAL,  "Process group not found in shared memory %d",
0089                 grpno);
0090         EXFAIL_OUT(ret);
0091     }
0092 
0093     ndrx_sg_load(&local, p_shm);
0094 
0095     if (NULL!=grp_flags)
0096     {
0097         /* NDRX_SG flag values matches TPPG_ flags values */
0098         *grp_flags = ((long)local.flags & NDRX_SG_SINGLETON);
0099     }
0100     
0101     /* check is group singleton? */
0102     if (local.flags & NDRX_SG_SINGLETON)
0103     {
0104         if ( (local.flags & NDRX_SG_VERIFY) && (flags & TPPG_SGVERIFY) )
0105         {
0106             long tmp, rsplen;
0107             char svcnm[MAXTIDENT+1]={EXEOS};
0108             /* call server for results (local server) */
0109             snprintf(svcnm, sizeof(svcnm), NDRX_SVC_SGLOC, tpgetnodeid(), grpno);
0110 
0111             p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0112 
0113             if (NULL==p_ub)
0114             {
0115                 NDRX_LOG(log_error, "Buffer alloc fail");
0116                 EXFAIL_OUT(ret);
0117             }
0118 
0119             tmp = grpno;
0120             if (EXSUCCEED!=Bchg(p_ub, EX_COMMAND, 0, NDRX_SGCMD_VERIFY, 0L)
0121                 || EXSUCCEED!=Bchg(p_ub, EX_PROCGRP_NO, 0, (char *)&tmp, 0L))
0122             {
0123                 NDRX_LOG(log_error, "Failed to setup request buffer: %s", Bstrerror(Berror));
0124                 ndrx_TPset_error_fmt(TPESYSTEM,  "Failed to setup request buffer: %s", Bstrerror(Berror));
0125                 EXFAIL_OUT(ret);
0126             }
0127 
0128             ndrx_debug_dump_UBF(log_info, "Request buffer:", p_ub);
0129             NDRX_LOG(log_debug, "Calling [%s]", svcnm);
0130 
0131             ret = tpcall(svcnm, (char*)p_ub, 0L, (char **)&p_ub, &rsplen, TPNOTRAN);
0132 
0133             if (EXSUCCEED!=ret)
0134             {
0135                 NDRX_LOG(log_error, "Failed to call [%s]: %s", 
0136                     svcnm, tpstrerror(tperrno));
0137 
0138                 /* if service failed for some reason...  */
0139                 if (TPESVCFAIL==tperrno)
0140                 {
0141                     char errmsg[MAX_TP_ERROR_LEN+1];
0142                     BFLDLEN errmsglen;
0143                     long err;
0144                     ndrx_debug_dump_UBF(log_error, "reply buffer:", p_ub);
0145                     /* try to read the error message/code */
0146                     if (EXSUCCEED==Bget(p_ub, EX_TPERRNO, 0, (char *)&err, 0L)
0147                         && EXSUCCEED==Bget(p_ub, EX_TPSTRERROR, 0, (char *)errmsg, &errmsglen))
0148                     {
0149                         /* set returned error code */
0150                         ndrx_TPunset_error();
0151                         /* set error */
0152                         ndrx_TPset_error_msg(err, errmsg);
0153                     }
0154                 }
0155                 /* done with error... */
0156                 EXFAIL_OUT(ret);
0157             }
0158             else
0159             {
0160                 char lock_stat;
0161 
0162                 ndrx_debug_dump_UBF(log_info, "reply buffer:", p_ub);
0163 
0164                 /* read the locked flag... */
0165                 if (EXSUCCEED!=Bget(p_ub, EX_LCKSTATUS, 0, (char *)&lock_stat, 0L))
0166                 {
0167                     NDRX_LOG(log_error, "Missing EX_LCKSTATUS: %s", Bstrerror(Berror));
0168                     ndrx_TPset_error_fmt(TPESYSTEM,  "Missing EX_LCKSTATUS: %s", Bstrerror(Berror));
0169                     EXFAIL_OUT(ret);
0170                 }
0171 
0172                 ret=lock_stat;
0173 
0174                 if (EXTRUE==ret && EXSUCCEED!=Bget(p_ub, EX_SEQUENCE, 0, (char *)&ret, 0L))
0175                 {
0176                     NDRX_LOG(log_error, "Missing EX_SEQUENCE: %s", Bstrerror(Berror));
0177                     ndrx_TPset_error_fmt(TPESYSTEM,  "Missing EX_SEQUENCE: %s", Bstrerror(Berror));
0178                     EXFAIL_OUT(ret);
0179                 }
0180             }
0181         }
0182         else
0183         {
0184             /* read directly from shm */
0185             ret=ndrx_sg_is_locked_int(grpno, p_shm, NULL, 0);
0186 
0187             if (EXFAIL==ret)
0188             {
0189                 NDRX_LOG(log_error, "Local group %d check failed", grpno);
0190                 goto out;
0191             }
0192 
0193             if (EXTRUE==ret)
0194             {
0195                 /* return current sequence */
0196                 ret=p_shm->sequence;
0197             }
0198         }
0199     }
0200     else if (flags & TPPG_NONSGSUCC)
0201     {
0202         NDRX_LOG(log_debug, "grpno=%d is not singleton", grpno);
0203         ret=EXSUCCEED;
0204     }
0205     else
0206     {
0207         /* not singleton group */
0208         ndrx_TPset_error_fmt(TPEPROTO,  "Process group %d is not singleton",
0209                 grpno);
0210         EXFAIL_OUT(ret);
0211     }
0212 
0213 out:
0214     if (NULL!=p_ub)
0215     {
0216         tpfree((char *)p_ub);
0217     }
0218     NDRX_LOG(log_info, "lock check grpno=%d ret = %ld", grpno, ret);
0219     return ret;
0220 }
0221 
0222 
0223 /* vim: set ts=4 sw=4 et smartindent: */