memcachedplugin.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "memcachedplugin.hpp"
  15. #include "eclrtl.hpp"
  16. #include "jexcept.hpp"
  17. #include "jstring.hpp"
  18. #include "jthread.hpp"
  19. #include <libmemcached/memcached.hpp>
  20. #include <libmemcached/util.h>
  21. #define MEMCACHED_VERSION "memcached plugin 1.0.0"
  22. ECL_MEMCACHED_API bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  23. {
  24. if (pb->size != sizeof(ECLPluginDefinitionBlock))
  25. return false;
  26. pb->magicVersion = PLUGIN_VERSION;
  27. pb->version = MEMCACHED_VERSION;
  28. pb->moduleName = "lib_memcached";
  29. pb->ECL = NULL;
  30. pb->flags = PLUGIN_IMPLICIT_MODULE;
  31. pb->description = "ECL plugin library for the C/C++ API libmemcached (http://libmemcached.org/)\n";
  32. return true;
  33. }
  34. namespace MemCachedPlugin {
  35. static const unsigned unitExpire = 86400;//1 day (secs)
  36. enum eclDataType {
  37. ECL_BOOLEAN,
  38. ECL_DATA,
  39. ECL_INTEGER,
  40. ECL_REAL,
  41. ECL_STRING,
  42. ECL_UTF8,
  43. ECL_UNICODE,
  44. ECL_UNSIGNED,
  45. ECL_NONE
  46. };
  47. const char * enumToStr(eclDataType type)
  48. {
  49. switch(type)
  50. {
  51. case ECL_BOOLEAN:
  52. return "BOOLEAN";
  53. case ECL_INTEGER:
  54. return "INTEGER";
  55. case ECL_UNSIGNED:
  56. return "UNSIGNED";
  57. case ECL_REAL:
  58. return "REAL";
  59. case ECL_STRING:
  60. return "STRING";
  61. case ECL_UTF8:
  62. return "UTF8";
  63. case ECL_UNICODE:
  64. return "UNICODE";
  65. case ECL_DATA:
  66. return "DATA";
  67. case ECL_NONE:
  68. return "Nonexistent";
  69. default:
  70. return "UNKNOWN";
  71. }
  72. }
  73. class MCached : public CInterface
  74. {
  75. public :
  76. MCached(ICodeContext * ctx, const char * _options);
  77. ~MCached();
  78. //set
  79. template <class type> void set(ICodeContext * ctx, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType);
  80. template <class type> void set(ICodeContext * ctx, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType);
  81. //get
  82. template <class type> void get(ICodeContext * ctx, const char * partitionKey, const char * key, type & value, eclDataType eclType);
  83. template <class type> void get(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & valueLength, type * & value, eclDataType eclType);
  84. void getVoidPtrLenPair(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & valueLength, void * & value, eclDataType eclType);
  85. void clear(ICodeContext * ctx, unsigned when);
  86. bool exists(ICodeContext * ctx, const char * key, const char * partitionKey);
  87. eclDataType getKeyType(const char * key, const char * partitionKey);
  88. bool isSameConnection(const char * _options) const;
  89. private :
  90. void checkServersUp(ICodeContext * ctx);
  91. void assertOnError(memcached_return_t rc, const char * _msg);
  92. const char * appendIfKeyNotFoundMsg(memcached_return_t rc, const char * key, StringBuffer & target) const;
  93. void connect(ICodeContext * ctx);
  94. bool logErrorOnFail(ICodeContext * ctx, memcached_return_t rc, const char * _msg);
  95. void reportKeyTypeMismatch(ICodeContext * ctx, const char * key, uint32_t flag, eclDataType eclType);
  96. void * cpy(const char * src, size_t length);
  97. void logServerStats(ICodeContext * ctx);
  98. void init(ICodeContext * ctx);
  99. void invokePoolSecurity(ICodeContext * ctx);
  100. void invokeConnectionSecurity(ICodeContext * ctx);
  101. void setPoolSettings();
  102. void assertPool();//For internal purposes to insure correct order of the above processes and instantiation.
  103. private :
  104. memcached_st * connection;
  105. memcached_pool_st * pool;
  106. StringAttr options;
  107. bool alreadyInitialized;
  108. unsigned typeMismatchCount;
  109. };
  110. typedef Owned<MCached> OwnedMCached;
  111. static const unsigned MAX_TYPEMISMATCHCOUNT = 10;
  112. static __thread MCached * cachedConnection;
  113. static __thread ThreadTermFunc threadHookChain;
  114. //The following class is here to ensure destruction of the cachedConnection within the main thread
  115. //as this is not handled by the thread hook mechanism.
  116. static class mainThreadCachedConnection
  117. {
  118. public :
  119. mainThreadCachedConnection() { }
  120. ~mainThreadCachedConnection()
  121. {
  122. if (cachedConnection)
  123. cachedConnection->Release();
  124. }
  125. } mainThread;
  126. static void releaseContext()
  127. {
  128. if (cachedConnection)
  129. cachedConnection->Release();
  130. if (threadHookChain)
  131. {
  132. (*threadHookChain)();
  133. threadHookChain = NULL;
  134. }
  135. }
  136. MCached * createConnection(ICodeContext * ctx, const char * options)
  137. {
  138. if (!cachedConnection)
  139. {
  140. cachedConnection = new MemCachedPlugin::MCached(ctx, options);
  141. threadHookChain = addThreadTermFunc(releaseContext);
  142. return LINK(cachedConnection);
  143. }
  144. if (cachedConnection->isSameConnection(options))
  145. return LINK(cachedConnection);
  146. cachedConnection->Release();
  147. cachedConnection = new MemCachedPlugin::MCached(ctx, options);
  148. return LINK(cachedConnection);
  149. }
  150. //-------------------------------------------SET-----------------------------------------
  151. template<class type> void MSet(ICodeContext * ctx, const char * _options, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType)
  152. {
  153. OwnedMCached serverPool = createConnection(ctx, _options);
  154. serverPool->set(ctx, partitionKey, key, value, expire, eclType);
  155. }
  156. //Set pointer types
  157. template<class type> void MSet(ICodeContext * ctx, const char * _options, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType)
  158. {
  159. OwnedMCached serverPool = createConnection(ctx, _options);
  160. serverPool->set(ctx, partitionKey, key, valueLength, value, expire, eclType);
  161. }
  162. //-------------------------------------------GET-----------------------------------------
  163. template<class type> void MGet(ICodeContext * ctx, const char * options, const char * partitionKey, const char * key, type & returnValue, eclDataType eclType)
  164. {
  165. OwnedMCached serverPool = createConnection(ctx, options);
  166. serverPool->get(ctx, partitionKey, key, returnValue, eclType);
  167. }
  168. template<class type> void MGet(ICodeContext * ctx, const char * options, const char * partitionKey, const char * key, size_t & returnLength, type * & returnValue, eclDataType eclType)
  169. {
  170. OwnedMCached serverPool = createConnection(ctx, options);
  171. serverPool->get(ctx, partitionKey, key, returnLength, returnValue, eclType);
  172. }
  173. void MGetVoidPtrLenPair(ICodeContext * ctx, const char * options, const char * partitionKey, const char * key, size_t & returnLength, void * & returnValue, eclDataType eclType)
  174. {
  175. OwnedMCached serverPool = createConnection(ctx, options);
  176. serverPool->getVoidPtrLenPair(ctx, partitionKey, key, returnLength, returnValue, eclType);
  177. }
  178. //----------------------------------SET----------------------------------------
  179. template<class type> void MemCachedPlugin::MCached::set(ICodeContext * ctx, const char * partitionKey, const char * key, type value, unsigned expire, eclDataType eclType)
  180. {
  181. const char * _value = reinterpret_cast<const char *>(&value);//Do this even for char * to prevent compiler complaining
  182. size_t partitionKeyLength = strlen(partitionKey);
  183. const char * msg = "'Set' request failed - ";
  184. if (partitionKeyLength)
  185. assertOnError(memcached_set_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), _value, sizeof(value), (time_t)(expire*unitExpire), (uint32_t)eclType), msg);
  186. else
  187. assertOnError(memcached_set(connection, key, strlen(key), _value, sizeof(value), (time_t)(expire*unitExpire), (uint32_t)eclType), msg);
  188. }
  189. template<class type> void MemCachedPlugin::MCached::set(ICodeContext * ctx, const char * partitionKey, const char * key, size32_t valueLength, const type * value, unsigned expire, eclDataType eclType)
  190. {
  191. const char * _value = reinterpret_cast<const char *>(value);//Do this even for char * to prevent compiler complaining
  192. size_t partitionKeyLength = strlen(partitionKey);
  193. const char * msg = "'Set' request failed - ";
  194. if (partitionKeyLength)
  195. assertOnError(memcached_set_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), _value, (size_t)(valueLength), (time_t)(expire*unitExpire), (uint32_t)eclType), msg);
  196. else
  197. assertOnError(memcached_set(connection, key, strlen(key), _value, (size_t)(valueLength), (time_t)(expire*unitExpire), (uint32_t)eclType), msg);
  198. }
  199. //----------------------------------GET----------------------------------------
  200. template<class type> void MemCachedPlugin::MCached::get(ICodeContext * ctx, const char * partitionKey, const char * key, type & returnValue, eclDataType eclType)
  201. {
  202. uint32_t flag = 0;
  203. size_t returnLength = 0;
  204. memcached_return_t rc;
  205. OwnedMalloc<char> value;
  206. size_t partitionKeyLength = strlen(partitionKey);
  207. if (partitionKeyLength)
  208. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnLength, &flag, &rc));
  209. else
  210. value.setown(memcached_get(connection, key, strlen(key), &returnLength, &flag, &rc));
  211. StringBuffer keyMsg = "'Get<type>' request failed - ";
  212. assertOnError(rc, appendIfKeyNotFoundMsg(rc, key, keyMsg));
  213. reportKeyTypeMismatch(ctx, key, flag, eclType);
  214. if (sizeof(type)!=returnLength)
  215. {
  216. VStringBuffer msg("MemCachedPlugin: ERROR - Requested type of different size (%uB) from that stored (%uB). Check logs for more information.", (unsigned)sizeof(type), (unsigned)returnLength);
  217. rtlFail(0, msg.str());
  218. }
  219. memcpy(&returnValue, value, returnLength);
  220. }
  221. template<class type> void MemCachedPlugin::MCached::get(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & returnLength, type * & returnValue, eclDataType eclType)
  222. {
  223. uint32_t flag = 0;
  224. memcached_return_t rc;
  225. OwnedMalloc<char> value;
  226. size_t partitionKeyLength = strlen(partitionKey);
  227. if (partitionKeyLength)
  228. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnLength, &flag, &rc));
  229. else
  230. value.setown(memcached_get(connection, key, strlen(key), &returnLength, &flag, &rc));
  231. StringBuffer keyMsg = "'Get<type>' request failed - ";
  232. assertOnError(rc, appendIfKeyNotFoundMsg(rc, key, keyMsg));
  233. reportKeyTypeMismatch(ctx, key, flag, eclType);
  234. returnValue = reinterpret_cast<type*>(cpy(value, returnLength));
  235. }
  236. void MemCachedPlugin::MCached::getVoidPtrLenPair(ICodeContext * ctx, const char * partitionKey, const char * key, size_t & returnLength, void * & returnValue, eclDataType eclType)
  237. {
  238. uint32_t flag = 0;
  239. size_t returnValueLength = 0;
  240. memcached_return_t rc;
  241. OwnedMalloc<char> value;
  242. size_t partitionKeyLength = strlen(partitionKey);
  243. if (partitionKeyLength)
  244. value.setown(memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnValueLength, &flag, &rc));
  245. else
  246. value.setown(memcached_get(connection, key, strlen(key), &returnValueLength, &flag, &rc));
  247. StringBuffer keyMsg = "'Get<type>' request failed - ";
  248. assertOnError(rc, appendIfKeyNotFoundMsg(rc, key, keyMsg));
  249. reportKeyTypeMismatch(ctx, key, flag, eclType);
  250. returnLength = (size32_t)(returnValueLength);
  251. returnValue = reinterpret_cast<void*>(cpy(value, returnLength));
  252. }
  253. MemCachedPlugin::MCached::MCached(ICodeContext * ctx, const char * _options)
  254. {
  255. alreadyInitialized = false;
  256. connection = NULL;
  257. pool = NULL;
  258. options.set(_options);
  259. typeMismatchCount = 0;
  260. #if (LIBMEMCACHED_VERSION_HEX<0x53000)
  261. memcached_st *memc = memcached_create(NULL);
  262. memcached_return_t rc;
  263. memcached_server_st *servers = NULL;
  264. try
  265. {
  266. unsigned pool_min = 1;
  267. unsigned pool_max = 1;
  268. StringArray optionStrings;
  269. optionStrings.appendList(_options, " ");
  270. ForEachItemIn(idx, optionStrings)
  271. {
  272. const char *opt = optionStrings.item(idx);
  273. if (strncmp(opt, "--SERVER=", 9) ==0)
  274. {
  275. opt += 9;
  276. StringArray splitPort;
  277. splitPort.appendList(opt, ":");
  278. unsigned port;
  279. if (splitPort.ordinality()==2)
  280. port = atoi(splitPort.item(1));
  281. else
  282. port = 11211;
  283. servers = memcached_server_list_append(NULL, splitPort.item(0), port, &rc);
  284. assertOnError(rc, "memcached_server_list_append failed - ");
  285. }
  286. else if (strncmp(opt, "--POOL-MIN=", 11) ==0)
  287. pool_min = atoi(opt+11);
  288. else if (strncmp(opt, "--POOL-MAX=", 11) ==0)
  289. pool_max = atoi(opt+11);
  290. else
  291. {
  292. VStringBuffer err("MemCachedPlugin: unsupported option string %s", opt);
  293. rtlFail(0, err.str());
  294. }
  295. }
  296. if (!servers)
  297. rtlFail(0, "No servers specified");
  298. rc = memcached_server_push(memc, servers);
  299. memcached_server_list_free(servers);
  300. assertOnError(rc, "memcached_server_push failed - ");
  301. pool = memcached_pool_create(memc, pool_min, pool_max); // takes ownership of memc
  302. }
  303. catch (...)
  304. {
  305. if (servers)
  306. memcached_server_list_free(servers);
  307. if (memc)
  308. memcached_free(memc);
  309. throw;
  310. }
  311. #else
  312. pool = memcached_pool(_options, strlen(_options));
  313. #endif
  314. assertPool();
  315. setPoolSettings();
  316. invokePoolSecurity(ctx);
  317. connect(ctx);
  318. checkServersUp(ctx);
  319. }
  320. //-----------------------------------------------------------------------------
  321. MemCachedPlugin::MCached::~MCached()
  322. {
  323. if (pool)
  324. {
  325. #if (LIBMEMCACHED_VERSION_HEX<0x53000)
  326. memcached_pool_push(pool, connection);
  327. #else
  328. memcached_pool_release(pool, connection);
  329. #endif
  330. connection = NULL;//For safety (from changing this destructor) as not implicit in either the above or below.
  331. memcached_st *memc = memcached_pool_destroy(pool);
  332. if (memc)
  333. memcached_free(memc);
  334. }
  335. else if (connection)//This should never be needed but just in case.
  336. {
  337. memcached_free(connection);
  338. }
  339. }
  340. bool MemCachedPlugin::MCached::isSameConnection(const char * _options) const
  341. {
  342. if (!_options)
  343. return false;
  344. return stricmp(options.get(), _options) == 0;
  345. }
  346. void MemCachedPlugin::MCached::assertPool()
  347. {
  348. if (!pool)
  349. {
  350. StringBuffer msg = "Memcached Plugin: Failed to instantiate server pool with:";
  351. msg.newline().append(options);
  352. rtlFail(0, msg.str());
  353. }
  354. }
  355. void * MemCachedPlugin::MCached::cpy(const char * src, size_t length)
  356. {
  357. void * value = rtlMalloc(length);
  358. return memcpy(value, src, length);
  359. }
  360. void MemCachedPlugin::MCached::checkServersUp(ICodeContext * ctx)
  361. {
  362. memcached_return_t rc;
  363. char * args = NULL;
  364. OwnedMalloc<memcached_stat_st> stats;
  365. stats.setown(memcached_stat(connection, args, &rc));
  366. assertex(stats);
  367. unsigned int numberOfServers = memcached_server_count(connection);
  368. unsigned int numberOfServersDown = 0;
  369. for (unsigned i = 0; i < numberOfServers; ++i)
  370. {
  371. if (stats[i].pid == -1)//perhaps not the best test?
  372. {
  373. numberOfServersDown++;
  374. VStringBuffer msg("Memcached Plugin: Failed connecting to entry %u\nwithin the server list: %s", i+1, options.str());
  375. ctx->logString(msg.str());
  376. }
  377. }
  378. if (numberOfServersDown == numberOfServers)
  379. rtlFail(0,"Memcached Plugin: Failed connecting to ALL servers. Check memcached on all servers and \"memcached -B ascii\" not used.");
  380. //check memcached version homogeneity
  381. for (unsigned i = 0; i < numberOfServers-1; ++i)
  382. {
  383. if (strcmp(stats[i].version, stats[i+1].version) != 0)
  384. ctx->logString("Memcached Plugin: Inhomogeneous versions of memcached across servers.");
  385. }
  386. }
  387. bool MemCachedPlugin::MCached::logErrorOnFail(ICodeContext * ctx, memcached_return_t rc, const char * _msg)
  388. {
  389. if (rc == MEMCACHED_SUCCESS)
  390. return false;
  391. VStringBuffer msg("Memcached Plugin: %s%s", _msg, memcached_strerror(connection, rc));
  392. ctx->logString(msg.str());
  393. return true;
  394. }
  395. void MemCachedPlugin::MCached::assertOnError(memcached_return_t rc, const char * _msg)
  396. {
  397. if (rc != MEMCACHED_SUCCESS)
  398. {
  399. VStringBuffer msg("Memcached Plugin: %s%s", _msg, memcached_strerror(connection, rc));
  400. rtlFail(0, msg.str());
  401. }
  402. }
  403. const char * MemCachedPlugin::MCached::appendIfKeyNotFoundMsg(memcached_return_t rc, const char * key, StringBuffer & target) const
  404. {
  405. if (rc == MEMCACHED_NOTFOUND)
  406. target.append("(key: '").append(key).append("') ");
  407. return target.str();
  408. }
  409. void MemCachedPlugin::MCached::clear(ICodeContext * ctx, unsigned when)
  410. {
  411. //NOTE: memcached_flush is the actual cache flush/clear/delete and not an io buffer flush.
  412. assertOnError(memcached_flush(connection, (time_t)(when)), "'Clear' request failed - ");
  413. }
  414. bool MemCachedPlugin::MCached::exists(ICodeContext * ctx, const char * key, const char * partitionKey)
  415. {
  416. #if (LIBMEMCACHED_VERSION_HEX<0x53000)
  417. throw makeStringException(0, "memcached_exist not supported in this version of libmemcached");
  418. #else
  419. memcached_return_t rc;
  420. size_t partitionKeyLength = strlen(partitionKey);
  421. if (partitionKeyLength)
  422. rc = memcached_exist_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key));
  423. else
  424. rc = memcached_exist(connection, key, strlen(key));
  425. if (rc == MEMCACHED_NOTFOUND)
  426. return false;
  427. else
  428. {
  429. assertOnError(rc, "'Exists' request failed - ");
  430. return true;
  431. }
  432. #endif
  433. }
  434. MemCachedPlugin::eclDataType MemCachedPlugin::MCached::getKeyType(const char * key, const char * partitionKey)
  435. {
  436. size_t returnValueLength;
  437. uint32_t flag;
  438. memcached_return_t rc;
  439. size_t partitionKeyLength = strlen(partitionKey);
  440. if (partitionKeyLength)
  441. memcached_get_by_key(connection, partitionKey, partitionKeyLength, key, strlen(key), &returnValueLength, &flag, &rc);
  442. else
  443. memcached_get(connection, key, strlen(key), &returnValueLength, &flag, &rc);
  444. if (rc == MEMCACHED_SUCCESS)
  445. return (MemCachedPlugin::eclDataType)(flag);
  446. else if (rc == MEMCACHED_NOTFOUND)
  447. return ECL_NONE;
  448. else
  449. {
  450. VStringBuffer msg("Memcached Plugin: 'KeyType' request failed - %s", memcached_strerror(connection, rc));
  451. rtlFail(0, msg.str());
  452. }
  453. }
  454. void MemCachedPlugin::MCached::reportKeyTypeMismatch(ICodeContext * ctx, const char * key, uint32_t flag, eclDataType eclType)
  455. {
  456. if (flag && eclType != ECL_DATA && flag != eclType)
  457. {
  458. VStringBuffer msg("Memcached Plugin: The requested key '%s' is of type %s, not %s as requested.", key, enumToStr((eclDataType)(flag)), enumToStr(eclType));
  459. if (++typeMismatchCount <= MAX_TYPEMISMATCHCOUNT)
  460. ctx->logString(msg.str());
  461. }
  462. }
  463. void MemCachedPlugin::MCached::logServerStats(ICodeContext * ctx)
  464. {
  465. //NOTE: errors are ignored here so that at least some info is reported, such as non-connection related libmemcached version numbers
  466. memcached_return_t rc;
  467. char * args = NULL;
  468. OwnedMalloc<memcached_stat_st> stats;
  469. stats.setown(memcached_stat(connection, args, &rc));
  470. OwnedMalloc<char*> keys;
  471. keys.setown(memcached_stat_get_keys(connection, stats, &rc));
  472. unsigned int numberOfServers = memcached_server_count(connection);
  473. for (unsigned int i = 0; i < numberOfServers; ++i)
  474. {
  475. StringBuffer statsStr;
  476. unsigned j = 0;
  477. do
  478. {
  479. OwnedMalloc<char> value;
  480. value.setown(memcached_stat_get_value(connection, &stats[i], keys[j], &rc));
  481. statsStr.newline().append("libmemcached server stat - ").append(keys[j]).append(":").append(value);
  482. } while (keys[++j]);
  483. statsStr.newline().append("libmemcached client stat - libmemcached version:").append(memcached_lib_version());
  484. ctx->logString(statsStr.str());
  485. }
  486. }
  487. void MemCachedPlugin::MCached::init(ICodeContext * ctx)
  488. {
  489. logServerStats(ctx);
  490. }
  491. void MemCachedPlugin::MCached::setPoolSettings()
  492. {
  493. assertPool();
  494. const char * msg = "memcached_pool_behavior_set failed - ";
  495. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY, 1), msg);//key set in invokeConnectionSecurity. Only hashed with keys and not partitionKeys
  496. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_KETAMA, 1), msg);//NOTE: alias of MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA amongst others.
  497. memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_USE_UDP, 0); // Note that this fails on early versions of libmemcached, so ignore result
  498. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1), msg);
  499. #if (LIBMEMCACHED_VERSION_HEX>=0x50000)
  500. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, 1), msg);
  501. #endif
  502. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_NO_BLOCK, 0), msg);
  503. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 100), msg);//units of ms MORE: What should I set this to or get from?
  504. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0), msg);// Buffering does not work with the ecl runtime paradigm
  505. }
  506. void MemCachedPlugin::MCached::invokePoolSecurity(ICodeContext * ctx)
  507. {
  508. assertPool();
  509. assertOnError(memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1), "memcached_pool_behavior_set failed - ");
  510. }
  511. void MemCachedPlugin::MCached::invokeConnectionSecurity(ICodeContext * ctx)
  512. {
  513. //NOTE: Whether to assert or just report? This depends on when this is called. If before checkServersUp() and
  514. //a server is down, it will cause the following to fail if asserted with only a 'poor' libmemcached error message.
  515. //Reporting means that these 'security' measures may not be carried out. Moving checkServersUp() to here is probably the best
  516. //soln. however, this comes with extra overhead.
  517. logErrorOnFail(ctx, memcached_verbosity(connection, (uint32_t)(0)), "memcached_verbosity=0 failed - ");
  518. }
  519. void MemCachedPlugin::MCached::connect(ICodeContext * ctx)
  520. {
  521. assertPool();
  522. if (connection)
  523. #if (LIBMEMCACHED_VERSION_HEX<0x53000)
  524. memcached_pool_push(pool, connection);
  525. #else
  526. memcached_pool_release(pool, connection);
  527. #endif
  528. memcached_return_t rc;
  529. #if (LIBMEMCACHED_VERSION_HEX<0x53000)
  530. connection = memcached_pool_pop(pool, (struct timespec *)0 , &rc);
  531. #else
  532. connection = memcached_pool_fetch(pool, (struct timespec *)0 , &rc);
  533. #endif
  534. invokeConnectionSecurity(ctx);
  535. if (!alreadyInitialized)//Do this now rather than after assert. Better to have something even if it could be jiberish.
  536. {
  537. init(ctx);//doesn't necessarily initialize anything, instead outputs specs etc for debugging
  538. alreadyInitialized = true;
  539. }
  540. assertOnError(rc, "memcached_pool_pop failed - ");
  541. }
  542. //--------------------------------------------------------------------------------
  543. // ECL SERVICE ENTRYPOINTS
  544. //--------------------------------------------------------------------------------
  545. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MClear(ICodeContext * ctx, const char * options)
  546. {
  547. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, options);
  548. serverPool->clear(ctx, 0);
  549. }
  550. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MExists(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  551. {
  552. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, options);
  553. return serverPool->exists(ctx, key, partitionKey);
  554. }
  555. ECL_MEMCACHED_API const char * ECL_MEMCACHED_CALL MKeyType(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  556. {
  557. OwnedMCached serverPool = MemCachedPlugin::createConnection(ctx, options);
  558. const char * keyType = enumToStr(serverPool->getKeyType(key, partitionKey));
  559. return keyType;
  560. }
  561. //-----------------------------------SET------------------------------------------
  562. //NOTE: These were all overloaded by 'value' type, however; this caused problems since ecl implicitly casts and doesn't type check.
  563. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, size32_t valueLength, const char * value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  564. {
  565. MemCachedPlugin::MSet(ctx, options, partitionKey, key, valueLength, value, expire, MemCachedPlugin::ECL_STRING);
  566. }
  567. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, size32_t valueLength, const UChar * value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  568. {
  569. MemCachedPlugin::MSet(ctx, options, partitionKey, key, (valueLength)*sizeof(UChar), value, expire, MemCachedPlugin::ECL_UNICODE);
  570. }
  571. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, signed __int64 value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  572. {
  573. MemCachedPlugin::MSet(ctx, options, partitionKey, key, value, expire, MemCachedPlugin::ECL_INTEGER);
  574. }
  575. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, unsigned __int64 value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  576. {
  577. MemCachedPlugin::MSet(ctx, options, partitionKey, key, value, expire, MemCachedPlugin::ECL_UNSIGNED);
  578. }
  579. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, double value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  580. {
  581. MemCachedPlugin::MSet(ctx, options, partitionKey, key, value, expire, MemCachedPlugin::ECL_REAL);
  582. }
  583. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSet(ICodeContext * ctx, const char * key, bool value, const char * options, const char * partitionKey, unsigned expire)
  584. {
  585. MemCachedPlugin::MSet(ctx, options, partitionKey, key, value, expire, MemCachedPlugin::ECL_BOOLEAN);
  586. }
  587. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSetData(ICodeContext * ctx, const char * key, size32_t valueLength, const void * value, const char * options, const char * partitionKey, unsigned expire)
  588. {
  589. MemCachedPlugin::MSet(ctx, options, partitionKey, key, valueLength, value, expire, MemCachedPlugin::ECL_DATA);
  590. }
  591. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MSetUtf8(ICodeContext * ctx, const char * key, size32_t valueLength, const char * value, const char * options, const char * partitionKey, unsigned expire /* = 0 (ECL default)*/)
  592. {
  593. MemCachedPlugin::MSet(ctx, options, partitionKey, key, rtlUtf8Size(valueLength, value), value, expire, MemCachedPlugin::ECL_UTF8);
  594. }
  595. //-------------------------------------GET----------------------------------------
  596. ECL_MEMCACHED_API bool ECL_MEMCACHED_CALL MGetBool(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  597. {
  598. bool value;
  599. MemCachedPlugin::MGet(ctx, options, partitionKey, key, value, MemCachedPlugin::ECL_BOOLEAN);
  600. return value;
  601. }
  602. ECL_MEMCACHED_API double ECL_MEMCACHED_CALL MGetDouble(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  603. {
  604. double value;
  605. MemCachedPlugin::MGet(ctx, options, partitionKey, key, value, MemCachedPlugin::ECL_REAL);
  606. return value;
  607. }
  608. ECL_MEMCACHED_API signed __int64 ECL_MEMCACHED_CALL MGetInt8(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  609. {
  610. signed __int64 value;
  611. MemCachedPlugin::MGet(ctx, options, partitionKey, key, value, MemCachedPlugin::ECL_INTEGER);
  612. return value;
  613. }
  614. ECL_MEMCACHED_API unsigned __int64 ECL_MEMCACHED_CALL MGetUint8(ICodeContext * ctx, const char * key, const char * options, const char * partitionKey)
  615. {
  616. unsigned __int64 value;
  617. MemCachedPlugin::MGet(ctx, options, partitionKey, key, value, MemCachedPlugin::ECL_UNSIGNED);
  618. return value;
  619. }
  620. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetStr(ICodeContext * ctx, size32_t & returnLength, char * & returnValue, const char * key, const char * options, const char * partitionKey)
  621. {
  622. size_t _returnLength;
  623. MemCachedPlugin::MGet(ctx, options, partitionKey, key, _returnLength, returnValue, MemCachedPlugin::ECL_STRING);
  624. returnLength = static_cast<size32_t>(_returnLength);
  625. }
  626. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetUChar(ICodeContext * ctx, size32_t & returnLength, UChar * & returnValue, const char * key, const char * options, const char * partitionKey)
  627. {
  628. size_t _returnSize;
  629. MemCachedPlugin::MGet(ctx, options, partitionKey, key, _returnSize, returnValue, MemCachedPlugin::ECL_UNICODE);
  630. returnLength = static_cast<size32_t>(_returnSize/sizeof(UChar));
  631. }
  632. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetUtf8(ICodeContext * ctx, size32_t & returnLength, char * & returnValue, const char * key, const char * options, const char * partitionKey)
  633. {
  634. size_t returnSize;
  635. MemCachedPlugin::MGet(ctx, options, partitionKey, key, returnSize, returnValue, MemCachedPlugin::ECL_UTF8);
  636. returnLength = static_cast<size32_t>(rtlUtf8Length(returnSize, returnValue));
  637. }
  638. ECL_MEMCACHED_API void ECL_MEMCACHED_CALL MGetData(ICodeContext * ctx, size32_t & returnLength, void * & returnValue, const char * key, const char * options, const char * partitionKey)
  639. {
  640. size_t _returnLength;
  641. MemCachedPlugin::MGetVoidPtrLenPair(ctx, options, partitionKey, key, _returnLength, returnValue, MemCachedPlugin::ECL_DATA);
  642. returnLength = static_cast<size32_t>(_returnLength);
  643. }
  644. }//close namespace