Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Cache Dump - dump cache data to console
0003  *
0004  * @file cmd_cd.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 <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <sys/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 #include <nstdutil.h>
0043 
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <utlist.h>
0048 #include <Exfields.h>
0049 
0050 #include "xa_cmn.h"
0051 #include "nclopt.h"
0052 #include <ndrx.h>
0053 #include <qcommon.h>
0054 #include <atmi_cache.h>
0055 #include <typed_buf.h>
0056 #include <ubfutil.h>
0057 /*---------------------------Externs------------------------------------*/
0058 /*---------------------------Macros-------------------------------------*/
0059 /*---------------------------Enums--------------------------------------*/
0060 /*---------------------------Typedefs-----------------------------------*/
0061 /*---------------------------Globals------------------------------------*/
0062 /*---------------------------Statics------------------------------------*/
0063 /*---------------------------Prototypes---------------------------------*/
0064 
0065 /**
0066  * Call cache server
0067  * @return
0068  */
0069 exprivate int call_cache(char *dbname, char *key, int interpret)
0070 {
0071     UBFH *p_ub = (UBFH *)tpalloc("UBF", "", 1024);
0072     int ret=EXSUCCEED;
0073     long rcvlen;
0074     char *svcnm;
0075     char cmd = NDRX_CACHE_SVCMD_CLCDUMP;
0076     ndrx_tpcache_data_t cdata;
0077     char *keydata = NULL;
0078     char *data = NULL;
0079     char *odata = NULL;
0080     long olen;
0081     
0082     svcnm = ndrx_cache_mgt_getsvc();
0083             
0084     /* Setup the call buffer... */
0085     if (NULL==p_ub)
0086     {
0087         NDRX_LOG(log_error, "Failed to alloc FB!");        
0088         EXFAIL_OUT(ret);
0089     }
0090     
0091     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_CMD, 0, &cmd, 0L))
0092     {
0093         NDRX_LOG(log_error, "Failed to install command code: %s", 
0094                 Bstrerror(Berror));
0095         EXFAIL_OUT(ret);
0096     }
0097     
0098     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_DBNAME, 0, dbname, 0L))
0099     {
0100         NDRX_LOG(log_error, "Failed to install db name to buffer: %s",
0101                 Bstrerror(Berror));
0102         EXFAIL_OUT(ret);
0103     }
0104     
0105     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_OPEXPR, 0, key, 0L))
0106     {
0107         NDRX_LOG(log_error, "Failed to install key to buffer: %s",
0108                 Bstrerror(Berror));
0109         EXFAIL_OUT(ret);
0110     }
0111     
0112     /* Call cache server! */
0113     if (EXSUCCEED!=tpcall(svcnm, (char *)p_ub, 0L, (char **)&p_ub, &rcvlen, 0L))
0114     {
0115         if (Bpres(p_ub, EX_TPSTRERROR, 0))
0116         {
0117             fprintf(stderr, "%s\n", Bfind(p_ub, EX_TPSTRERROR, 0, 0L));
0118         }
0119         else
0120         {
0121             NDRX_LOG(log_error, "Failed to call [%s]: %s", svcnm, tpstrerror(tperrno));
0122             fprintf(stderr, "Failed to call cache server [%s]: %s\n", 
0123                 svcnm, tpstrerror(tperrno));
0124         }
0125         
0126         EXFAIL_OUT(ret);
0127     }
0128     
0129     /* Dump results to stdout (currently hex dump of buffer) 
0130      * If it is UBF buffer, then we might want to interpret it as UBF...
0131      */
0132     if (EXSUCCEED!=ndrx_cache_mgt_ubf2data(p_ub, &cdata, &data, &keydata, 
0133             &odata, &olen))
0134     {
0135         NDRX_LOG(log_error, "Failed to get mandatory UBF data!");
0136         EXFAIL_OUT(ret);
0137     }
0138     
0139     ndrx_debug_dump_UBF(log_debug, "Got reply buffer", p_ub);
0140     
0141     /* Print the results */
0142     printf("nodeid: %hd\n", cdata.nodeid);
0143     printf("saved_tperrno: %d\n", cdata.saved_tperrno);
0144     printf("saved_tpurcode: %ld\n", cdata.saved_tpurcode);
0145     printf("time_added: %ld (%s)\n", cdata.t, ndrx_get_strtstamp_from_sec(0, cdata.t));
0146     printf("time_added_usec: %ld\n", cdata.tusec);
0147     printf("time_hit: %ld (%s)\n", cdata.hit_t, ndrx_get_strtstamp_from_sec(0, cdata.hit_t));
0148     printf("time_hit_usec: %ld\n", cdata.hit_tusec);
0149     printf("hits: %ld\n", cdata.hits);
0150     printf("atmi_type_id: %hd\n", cdata.atmi_type_id);
0151     printf("atmi_buf_len: %ld\n", cdata.atmi_buf_len);
0152     
0153     
0154     printf("---------------------------------- HEX DUMP ------------------------------------\n");
0155     STDOUT_DUMP(log_info, "Massage dump", data, cdata.atmi_buf_len);
0156     printf("--------------------------------------------------------------------------------\n\n");
0157     
0158     if (interpret)
0159     {
0160         UBFH *p_ubf_h = (UBFH *)odata;
0161         /* interpret the results, currently we support UBF buffer only */
0162         
0163         if (BUF_TYPE_UBF==cdata.atmi_type_id)
0164         {
0165             printf("---------------------------------- UBF BUFFER ----------------------------------\n");
0166             Bprint(p_ubf_h);
0167             printf("--------------------------------------------------------------------------------\n");
0168         }
0169     }
0170 
0171 out:
0172 
0173     if (NULL!=p_ub)
0174     {
0175         tpfree((char *)p_ub);
0176     }
0177 
0178     if (NULL!=keydata)
0179     {
0180         NDRX_FREE(keydata);
0181     }
0182 
0183     if (NULL!=odata)
0184     {
0185         tpfree(odata);
0186     }
0187     
0188     if (NULL!=data)
0189     {
0190         NDRX_FREE(data);
0191     }
0192 
0193     return ret;
0194 }
0195 
0196 /**
0197  * Cache Dump command
0198  * @param p_cmd_map
0199  * @param argc
0200  * @param argv
0201  * @return SUCCEED
0202  */
0203 expublic int cmd_cd(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0204 {
0205     int ret=EXSUCCEED;
0206     char dbname[NDRX_CCTAG_MAX+1]={EXEOS};
0207     char key[PATH_MAX+1]={EXEOS};
0208     int interpret = EXFALSE;
0209     ncloptmap_t clopt[] =
0210     {
0211         {'d', BFLD_STRING, (void *)dbname, sizeof(dbname), 
0212                                 NCLOPT_MAND|NCLOPT_HAVE_VALUE, "Database name"},
0213     {'k', BFLD_STRING, (void *)key, sizeof(key), 
0214                                 NCLOPT_MAND|NCLOPT_HAVE_VALUE, "Key string"},
0215                 
0216     /* should we interpret the result? For example UBF
0217      * Boolean flag
0218      */
0219     {'i', BFLD_SHORT, (void *)&interpret, sizeof(interpret), 
0220                 NCLOPT_OPT|NCLOPT_TRUEBOOL, "Interpret?"},
0221         {0}
0222     };
0223         
0224     /* parse command line */
0225     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0226     {
0227         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0228         EXFAIL_OUT(ret);
0229     }
0230     
0231     /* we need to init TP subsystem... */
0232     if (EXSUCCEED!=tpinit(NULL))
0233     {
0234         fprintf(stderr, "Failed to tpinit(): %s\n", tpstrerror(tperrno));
0235         EXFAIL_OUT(ret);
0236     }
0237     
0238     if (EXSUCCEED!=call_cache(dbname, key, interpret))
0239     {
0240         NDRX_LOG(log_debug, "Failed to call cache server for db [%s]", dbname);
0241         fprintf(stderr, "Failed to call cache server!\n");
0242         EXFAIL_OUT(ret);
0243     }
0244         
0245 out:
0246     return ret;
0247 }
0248 
0249 /* vim: set ts=4 sw=4 et smartindent: */