cl5_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* cl5_test.c - changelog test cases */
  42. #include "cl5_test.h"
  43. #include "slapi-plugin.h"
  44. #include "cl5.h"
  45. #define REPLICA_ROOT "dc=example,dc=com" /* replica root */
  46. #define OP_COUNT 4 /* number of ops generated at a time */
  47. #define MOD_COUNT 5
  48. #define VALUE_COUNT 5
  49. #define ENTRY_COUNT 50
  50. #define CL_DN "cn=changelog5,cn=config"
  51. #define INSTANCE_ATTR "nsslapd-instancedir"
  52. #define REPLICA_OC "nsds5Replica"
  53. #define REPLICA_RDN "cn=replica"
  54. static void testBasic ();
  55. static void testBackupRestore ();
  56. static void testIteration ();
  57. static void testTrimming ();
  58. static void testPerformance ();
  59. static void testPerformanceMT ();
  60. static void testLDIF ();
  61. static void testAll ();
  62. static int configureChangelog ();
  63. static int configureReplica ();
  64. static int populateChangelogOp ();
  65. static int populateChangelog (int entryCount, CSN ***csnList);
  66. static int processEntries (int entryCount, CSN **csnList);
  67. static void clearCSNList (CSN ***csnList, int count);
  68. static void threadMain (void *data);
  69. static char* getBaseDir (const char *dir);
  70. static LDAPMod **buildMods ();
  71. void testChangelog (TestType type)
  72. {
  73. switch (type)
  74. {
  75. case TEST_BASIC: testBasic ();
  76. break;
  77. case TEST_BACKUP_RESTORE: testBackupRestore ();
  78. break;
  79. case TEST_ITERATION: testIteration ();
  80. break;
  81. case TEST_TRIMMING: testTrimming ();
  82. break;
  83. case TEST_PERFORMANCE: testPerformance ();
  84. break;
  85. case TEST_PERFORMANCE_MT: testPerformanceMT ();
  86. break;
  87. case TEST_LDIF: testLDIF ();
  88. break;
  89. case TEST_ALL: testAll ();
  90. break;
  91. default: printf ("Taste case %d is not supported\n", type);
  92. }
  93. }
  94. /* tests Open/Close, normal recovery, read/write/remove
  95. of an entry */
  96. static void testBasic ()
  97. {
  98. int rc = 0;
  99. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting basic test ...\n");
  100. /* ONREPL - we can't run the tests from the startup code because
  101. operations can't be issued until all plugins are started. So,
  102. instead, we do it when changelog is created
  103. rc = configureChangelog (); */
  104. if (rc == 0)
  105. {
  106. rc = configureReplica ();
  107. if (rc == 0)
  108. {
  109. rc = populateChangelogOp ();
  110. }
  111. }
  112. if (rc == 0)
  113. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  114. "Basic test completed successfully\n");
  115. else
  116. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  117. "Basic test failed\n");
  118. }
  119. static void testBackupRestore ()
  120. {
  121. char *dir;
  122. int rc = -1;
  123. char *baseDir;
  124. char bkDir [MAXPATHLEN];
  125. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting backup and recovery test ...\n");
  126. dir = cl5GetDir ();
  127. if (dir)
  128. {
  129. baseDir = getBaseDir (dir);
  130. PR_snprintf (bkDir, sizeof(bkDir), "%s/clbackup", baseDir);
  131. slapi_ch_free ((void**)&baseDir);
  132. rc = cl5Backup (bkDir, NULL);
  133. if (rc == CL5_SUCCESS)
  134. {
  135. cl5Close ();
  136. rc = cl5Restore (dir, bkDir, NULL);
  137. if (rc == CL5_SUCCESS)
  138. rc = cl5Open (dir, NULL);
  139. /* PR_RmDir (bkDir);*/
  140. }
  141. }
  142. if (rc == CL5_SUCCESS)
  143. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  144. "Backup and Restore test completed successfully\n");
  145. else
  146. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  147. "Backup and Restore test failed\n");
  148. }
  149. static void testIteration ()
  150. {
  151. Object *r_obj;
  152. Slapi_DN *r_root;
  153. Replica *r;
  154. char *replGen;
  155. RUV *ruv;
  156. CL5ReplayIterator *it = NULL;
  157. slapi_operation_parameters op;
  158. int rc;
  159. int i;
  160. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting iteration test ...\n");
  161. /* get replica object */
  162. r_root = slapi_sdn_new_dn_byval(REPLICA_ROOT);
  163. r_obj = replica_get_replica_from_dn (r_root);
  164. slapi_sdn_free (&r_root);
  165. if (r_obj == NULL)
  166. {
  167. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "replica is not configured for (%s)\n",
  168. REPLICA_ROOT);
  169. return;
  170. }
  171. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting first iteration pass ...\n");
  172. /* configure empty consumer ruv */
  173. r = (Replica*)object_get_data (r_obj);
  174. PR_ASSERT (r);
  175. replGen = replica_get_generation (r);
  176. ruv_init_new (replGen, 0, NULL, &ruv);
  177. /* create replay iterator */
  178. rc = cl5CreateReplayIterator (r_obj, ruv, &it);
  179. if (it)
  180. {
  181. i = 0;
  182. while ((rc = cl5GetNextOperationToReplay (it, &op)) == CL5_SUCCESS)
  183. {
  184. ruv_set_csns (ruv, op.csn, NULL);
  185. operation_parameters_done (&op);
  186. i ++;
  187. }
  188. }
  189. if (it)
  190. cl5DestroyReplayIterator (&it);
  191. if (rc == CL5_NOTFOUND)
  192. {
  193. if (i == 0) /* success */
  194. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "First iteration pass completed "
  195. "successfully: no changes to replay\n");
  196. else /* incorrect number of entries traversed */
  197. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "First iteration pass failed: "
  198. "traversed %d entries; expected none\n", i);
  199. }
  200. else /* general error */
  201. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "First iteration pass failed\n");
  202. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting second iteration pass ...\n");
  203. /* add some entries */
  204. populateChangelogOp ();
  205. /* create replay iterator */
  206. rc = cl5CreateReplayIterator (r_obj, ruv, &it);
  207. if (it)
  208. {
  209. i = 0;
  210. while ((rc = cl5GetNextOperationToReplay (it, &op)) == CL5_SUCCESS)
  211. {
  212. ruv_set_csns (ruv, op.csn, NULL);
  213. operation_parameters_done (&op);
  214. i ++;
  215. }
  216. }
  217. if (it)
  218. cl5DestroyReplayIterator (&it);
  219. if (rc == CL5_NOTFOUND)
  220. {
  221. if (i == OP_COUNT) /* success */
  222. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Second iteration pass completed "
  223. "successfully: %d entries traversed\n", i);
  224. else /* incorrect number of entries traversed */
  225. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Second iteration pass failed: "
  226. "traversed %d entries; expected %d\n", i, OP_COUNT);
  227. }
  228. else /* general error */
  229. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Second iteration pass failed\n");
  230. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting third iteration pass ...\n");
  231. /* add more entries */
  232. populateChangelogOp ();
  233. /* create replay iterator */
  234. rc = cl5CreateReplayIterator (r_obj, ruv, &it);
  235. if (it)
  236. {
  237. i = 0;
  238. while ((rc = cl5GetNextOperationToReplay (it, &op)) == CL5_SUCCESS)
  239. {
  240. ruv_set_csns (ruv, op.csn, NULL);
  241. operation_parameters_done (&op);
  242. i ++;
  243. }
  244. }
  245. if (it)
  246. cl5DestroyReplayIterator (&it);
  247. if (rc == CL5_NOTFOUND)
  248. {
  249. if (i == OP_COUNT) /* success */
  250. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Third iteration pass completed "
  251. "successfully: %d entries traversed\n", i);
  252. else /* incorrect number of entries traversed */
  253. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Third iteration pass failed: "
  254. "traversed %d entries; expected %d\n", i, OP_COUNT);
  255. }
  256. else /* general error */
  257. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Second iteration pass failed\n");
  258. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Iteration test is complete\n");
  259. ruv_destroy (&ruv);
  260. object_release (r_obj);
  261. slapi_ch_free ((void**)&replGen);
  262. }
  263. static void testTrimming ()
  264. {
  265. PRIntervalTime interval;
  266. int count;
  267. int rc;
  268. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting trimming test ...\n");
  269. rc = populateChangelog (200, NULL);
  270. if (rc == 0)
  271. {
  272. interval = PR_SecondsToInterval(2);
  273. DS_Sleep (interval);
  274. rc = populateChangelog (300, NULL);
  275. if (rc == 0)
  276. rc = cl5ConfigTrimming (300, "1d");
  277. interval = PR_SecondsToInterval(300); /* 5 min is default trimming interval */
  278. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  279. "Trimming test: sleeping for 5 minutes until trimming kicks in\n");
  280. DS_Sleep (interval);
  281. count = cl5GetOperationCount (NULL);
  282. }
  283. if (rc == 0 && count == 300)
  284. {
  285. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  286. "Trimming test completed successfully: changelog contains 300 entries\n");
  287. }
  288. else if (rc == 0)
  289. {
  290. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  291. "Trimming test failed: changelog contains %d entries; expected - 300\n",
  292. count);
  293. }
  294. else /* general failure */
  295. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Trimming test failed\n");
  296. }
  297. static void testPerformance ()
  298. {
  299. PRTime starttime, endtime, totaltime;
  300. int entryCount = 5000;
  301. CSN **csnList = NULL;
  302. int rc;
  303. starttime = PR_Now();
  304. rc = populateChangelog (entryCount, &csnList);
  305. endtime = PR_Now();
  306. totaltime = (endtime - starttime) / 1000; /* ms */
  307. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Write performance:\n"
  308. "entry count - %d, total time - %ldms\n"
  309. "latency = %d msec\nthroughput = %d entry/sec\n",
  310. entryCount, totaltime,
  311. totaltime / entryCount, entryCount * 1000 / totaltime);
  312. starttime = endtime;
  313. rc = processEntries (entryCount, csnList);
  314. endtime = PR_Now();
  315. totaltime = (endtime - starttime) / 1000; /* ms */
  316. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Read performance:\n"
  317. "entry count - %d, total time - %ld\n"
  318. "latency = %d msec\nthroughput = %d entry/sec\n",
  319. entryCount, totaltime,
  320. totaltime / entryCount, entryCount * 1000 / totaltime);
  321. clearCSNList (&csnList, entryCount);
  322. }
  323. static int threadsLeft;
  324. static void testPerformanceMT ()
  325. {
  326. PRTime starttime, endtime, totaltime;
  327. int entryCount = 200;
  328. int threadCount = 10;
  329. int entryTotal;
  330. int i;
  331. PRIntervalTime interval;
  332. interval = PR_MillisecondsToInterval(100);
  333. threadsLeft = threadCount * 2;
  334. entryTotal = threadCount * entryCount;
  335. starttime = PR_Now();
  336. for (i = 0; i < threadCount; i++)
  337. {
  338. PR_CreateThread(PR_USER_THREAD, threadMain, (void*)&entryCount,
  339. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  340. PR_UNJOINABLE_THREAD, 0);
  341. }
  342. while (threadsLeft > 5)
  343. DS_Sleep (interval);
  344. endtime = PR_Now();
  345. totaltime = (endtime - starttime) / 1000; /* ms */
  346. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Write performance:\n"
  347. "entry count - %d, total time - %ld\n"
  348. "latency = %d msec per entry\nthroughput = %d entry/sec\n",
  349. entryCount, totaltime,
  350. totaltime / entryTotal, entryTotal * 1000 / totaltime);
  351. starttime = endtime;
  352. while (threadsLeft != 0)
  353. DS_Sleep (interval);
  354. endtime = PR_Now();
  355. totaltime = (endtime - starttime) / 1000; /* ms */
  356. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Read performance:\n"
  357. "entry count - %d, total time - %ld\n"
  358. "latency = %d msec per entry\nthroughput = %d entry/sec\n",
  359. entryCount, totaltime,
  360. totaltime / entryTotal, entryTotal * 1000 / totaltime);
  361. }
  362. static void testLDIF ()
  363. {
  364. char *clDir = cl5GetDir ();
  365. int rc;
  366. char *baseDir;
  367. char ldifFile [MAXPATHLEN];
  368. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "Starting LDIF test ...\n");
  369. baseDir = getBaseDir (clDir);
  370. PR_snprintf (ldifFile, sizeof(ldifFile), "%s/cl5.ldif", baseDir);
  371. slapi_ch_free ((void**)&baseDir);
  372. rc = populateChangelog (ENTRY_COUNT, NULL);
  373. if (rc == CL5_SUCCESS)
  374. {
  375. rc = cl5ExportLDIF (ldifFile, NULL);
  376. if (rc == CL5_SUCCESS)
  377. {
  378. cl5Close();
  379. rc = cl5ImportLDIF (clDir, ldifFile, NULL);
  380. if (rc == CL5_SUCCESS)
  381. cl5Open(clDir, NULL);
  382. }
  383. }
  384. PR_Delete (ldifFile);
  385. if (rc == CL5_SUCCESS)
  386. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  387. "LDIF test completed successfully\n");
  388. else
  389. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "LDIF test failed\n");
  390. }
  391. static void testAll ()
  392. {
  393. testBasic ();
  394. testIteration ();
  395. testBackupRestore ();
  396. testLDIF ();
  397. /* testTrimming ();*/
  398. #if 0
  399. /* xxxPINAKI */
  400. /* these tests are not working correctly...the call to db->put() */
  401. /* just hangs forever */
  402. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  403. "Starting single threaded performance measurement ...\n");
  404. testPerformance ();
  405. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  406. "Starting multi threaded performance measurement ...\n");
  407. testPerformanceMT ();
  408. #endif
  409. }
  410. static int populateChangelog (int entryCount, CSN ***csnList)
  411. {
  412. CSN *csn;
  413. int i;
  414. slapi_operation_parameters op;
  415. int rc;
  416. char *uniqueid;
  417. if (csnList)
  418. {
  419. (*csnList) = (CSN**)slapi_ch_calloc (entryCount, sizeof (CSN*));
  420. }
  421. /* generate entries */
  422. for (i = 0; i < entryCount; i++)
  423. {
  424. /* ONREPL need to get replica object
  425. rc = csnGetNewCSNForRepl (&csn);
  426. if (rc != CL5_SUCCESS) */
  427. return -1;
  428. if (csnList)
  429. (*csnList) [i] = csn_dup (csn);
  430. memset (&op, 0, sizeof (op));
  431. op.csn = csn;
  432. slapi_uniqueIDGenerateString(&uniqueid);
  433. op.target_address.uniqueid = uniqueid;
  434. op.target_address.dn = slapi_ch_strdup ("cn=entry,dc=example,dc=com");
  435. if (i % 5 == 0)
  436. {
  437. op.operation_type = SLAPI_OPERATION_MODRDN;
  438. op.p.p_modrdn.modrdn_deloldrdn = 1;
  439. op.p.p_modrdn.modrdn_newrdn = slapi_ch_strdup("cn=entry2,dc=example,dc=com");
  440. op.p.p_modrdn.modrdn_newsuperior_address.dn = NULL;
  441. op.p.p_modrdn.modrdn_newsuperior_address.uniqueid = NULL;
  442. op.p.p_modrdn.modrdn_mods = buildMods ();
  443. }
  444. else if (i % 4 == 0)
  445. {
  446. op.operation_type = SLAPI_OPERATION_DELETE;
  447. }
  448. else if (i % 3 == 0)
  449. {
  450. op.operation_type = SLAPI_OPERATION_ADD;
  451. op.p.p_add.target_entry = slapi_entry_alloc ();
  452. slapi_entry_set_dn (op.p.p_add.target_entry, slapi_ch_strdup(op.target_address.dn));
  453. slapi_entry_set_uniqueid (op.p.p_add.target_entry, slapi_ch_strdup(op.target_address.uniqueid));
  454. slapi_entry_attr_set_charptr(op.p.p_add.target_entry, "objectclass", "top");
  455. slapi_entry_attr_set_charptr(op.p.p_add.target_entry, "cn", "entry");
  456. }
  457. else
  458. {
  459. op.operation_type = SLAPI_OPERATION_MODIFY;
  460. op.p.p_modify.modify_mods = buildMods ();
  461. }
  462. /* ONREPL rc = cl5WriteOperation (&op, 1);*/
  463. operation_parameters_done (&op);
  464. if (rc != CL5_SUCCESS)
  465. return -1;
  466. }
  467. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  468. "Successfully populated changelog with %d entries\n", entryCount);
  469. return 0;
  470. }
  471. static int processEntries (int entryCount, CSN **csnList)
  472. {
  473. int i;
  474. int rc = 0;
  475. slapi_operation_parameters op;
  476. for (i = 0; i < entryCount; i++)
  477. {
  478. memset (&op, 0, sizeof (op));
  479. op.csn = csn_dup (csnList [i]);
  480. /* rc = cl5GetOperation (&op);*/
  481. if (rc != CL5_SUCCESS)
  482. return -1;
  483. operation_parameters_done (&op);
  484. }
  485. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl,
  486. "Successfully read %d entries from the changelog\n", entryCount);
  487. return 0;
  488. }
  489. void clearCSNList (CSN ***csnList, int count)
  490. {
  491. int i;
  492. for (i = 0; i < count; i++)
  493. {
  494. csn_free (&((*csnList)[i]));
  495. }
  496. slapi_ch_free ((void**)csnList);
  497. }
  498. static void threadMain (void *data)
  499. {
  500. int entryCount = *(int*)data;
  501. CSN **csnList;
  502. populateChangelog (entryCount, &csnList);
  503. PR_AtomicDecrement (&threadsLeft);
  504. processEntries (entryCount, csnList);
  505. PR_AtomicDecrement (&threadsLeft);
  506. clearCSNList (&csnList, entryCount);
  507. }
  508. static char* getBaseDir (const char *dir)
  509. {
  510. char *baseDir = slapi_ch_strdup (dir);
  511. char *ch;
  512. ch = &(baseDir [strlen (dir) - 2]);
  513. while (ch >= baseDir && *ch != '\\' && *ch != '/')
  514. ch --;
  515. if (ch >= baseDir)
  516. {
  517. *ch = '\0';
  518. }
  519. return baseDir;
  520. }
  521. static LDAPMod **buildMods ()
  522. {
  523. Slapi_Mods smods;
  524. Slapi_Mod smod;
  525. LDAPMod **mods;
  526. struct berval bv;
  527. int j, k;
  528. slapi_mods_init (&smods, MOD_COUNT);
  529. for (j = 0; j < MOD_COUNT; j++)
  530. {
  531. slapi_mod_init (&smod, VALUE_COUNT);
  532. slapi_mod_set_operation (&smod, LDAP_MOD_ADD | LDAP_MOD_BVALUES);
  533. slapi_mod_set_type (&smod, "attr");
  534. for (k = 0; k < VALUE_COUNT; k++)
  535. {
  536. bv.bv_val = "bvalue";
  537. bv.bv_len = strlen (bv.bv_val) + 1;
  538. slapi_mod_add_value (&smod, &bv);
  539. }
  540. slapi_mods_add_smod (&smods, &smod);
  541. /* ONREPL slapi_mod_done (&smod); */
  542. }
  543. mods = slapi_mods_get_ldapmods_passout (&smods);
  544. slapi_mods_done (&smods);
  545. return mods;
  546. }
  547. /* Format:
  548. dn: cn=changelog5,cn=config
  549. objectclass: top
  550. objectclass: extensibleObject
  551. cn: changelog5
  552. nsslapd-changelogDir: d:/netscape/server4/slapd-elf/cl5 */
  553. static int configureChangelog ()
  554. {
  555. Slapi_PBlock *pb = slapi_pblock_new ();
  556. Slapi_Entry *e = slapi_entry_alloc ();
  557. int rc;
  558. char *attrs[] = {INSTANCE_ATTR, NULL};
  559. Slapi_Entry **entries;
  560. char cl_dir [256];
  561. char *str = NULL;
  562. /* set changelog dn */
  563. slapi_entry_set_dn (e, slapi_ch_strdup (CL_DN));
  564. /* set object classes */
  565. slapi_entry_add_string(e, "objectclass", "top");
  566. slapi_entry_add_string(e, "objectclass", "extensibleObject");
  567. /* get directory instance dir */
  568. slapi_search_internal_set_pb (pb, "cn=config", LDAP_SCOPE_BASE, "objectclass=*",
  569. attrs, 0, NULL, NULL,
  570. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  571. slapi_search_internal_pb (pb);
  572. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  573. if (rc != LDAP_SUCCESS)
  574. {
  575. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to get server instance "
  576. "directory; LDAP error - %d\n", rc);
  577. rc = -1;
  578. goto done;
  579. }
  580. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  581. str = slapi_entry_attr_get_charptr(entries[0], INSTANCE_ATTR);
  582. PR_snprintf (cl_dir, sizeof(cl_dir), "%s/%s", str, "cl5db");
  583. slapi_ch_free((void **)&str);
  584. slapi_entry_add_string (e, CONFIG_CHANGELOG_DIR_ATTRIBUTE, cl_dir);
  585. slapi_free_search_results_internal(pb);
  586. slapi_pblock_destroy (pb);
  587. pb = slapi_pblock_new ();
  588. slapi_add_entry_internal_set_pb (pb, e, NULL,
  589. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  590. slapi_add_internal_pb (pb);
  591. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  592. if (rc != LDAP_SUCCESS)
  593. {
  594. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to add changelog "
  595. "configuration entry; LDAP error - %d\n", rc);
  596. rc = -1;
  597. }
  598. else
  599. rc = 0;
  600. done:
  601. slapi_pblock_destroy (pb);
  602. return rc;
  603. }
  604. #define DN_SIZE 1024
  605. /* Format:
  606. dn: cn=replica,cn="o=NetscapeRoot",cn= mapping tree,cn=config
  607. objectclass: top
  608. objectclass: nsds5Replica
  609. objectclass: extensibleObject
  610. nsds5ReplicaRoot: o=NetscapeRoot
  611. nsds5ReplicaId: 2
  612. nsds5flags: 1
  613. cn: replica
  614. */
  615. static int configureReplica ()
  616. {
  617. Slapi_PBlock *pb = slapi_pblock_new ();
  618. Slapi_Entry *e = slapi_entry_alloc ();
  619. int rc;
  620. char dn [DN_SIZE];
  621. /* set changelog dn */
  622. PR_snprintf (dn, sizeof(dn), "%s,cn=\"%s\",%s", REPLICA_RDN, REPLICA_ROOT,
  623. slapi_get_mapping_tree_config_root ());
  624. slapi_entry_set_dn (e, slapi_ch_strdup (dn));
  625. /* set object classes */
  626. slapi_entry_add_string(e, "objectclass", "top");
  627. slapi_entry_add_string(e, "objectclass", REPLICA_OC);
  628. slapi_entry_add_string(e, "objectclass", "extensibleObject");
  629. /* set other attributes */
  630. slapi_entry_add_string (e, attr_replicaRoot, REPLICA_ROOT);
  631. slapi_entry_add_string (e, attr_replicaId, "1");
  632. slapi_entry_add_string (e, attr_flags, "1");
  633. slapi_add_entry_internal_set_pb (pb, e, NULL,
  634. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  635. slapi_add_internal_pb (pb);
  636. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  637. if (rc != LDAP_SUCCESS)
  638. {
  639. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to add replica for (%s) "
  640. "configuration entry; LDAP error - %d\n", REPLICA_ROOT, rc);
  641. rc = -1;
  642. }
  643. else
  644. rc = 0;
  645. slapi_pblock_destroy (pb);
  646. return rc;
  647. }
  648. /* generates one of each ldap operations */
  649. static int populateChangelogOp ()
  650. {
  651. Slapi_PBlock *pb = slapi_pblock_new ();
  652. Slapi_Entry *e = slapi_entry_alloc ();
  653. int rc;
  654. char dn [DN_SIZE], newrdn [64];
  655. LDAPMod *mods[2];
  656. Slapi_Mod smod;
  657. struct berval bv;
  658. time_t cur_time;
  659. /* add entry */
  660. cur_time = time(NULL);
  661. PR_snprintf (dn, sizeof(dn), "cn=%s,%s", ctime(&cur_time), REPLICA_ROOT);
  662. slapi_entry_set_dn (e, slapi_ch_strdup (dn));
  663. slapi_entry_add_string(e, "objectclass", "top");
  664. slapi_entry_add_string(e, "objectclass", "extensibleObject");
  665. slapi_entry_add_string (e, "mail", "[email protected]");
  666. slapi_add_entry_internal_set_pb (pb, e, NULL,
  667. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  668. slapi_add_internal_pb (pb);
  669. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  670. slapi_pblock_destroy (pb);
  671. if (rc != LDAP_SUCCESS)
  672. {
  673. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to add entry (%s); "
  674. "LDAP error - %d\n", dn, rc);
  675. return -1;
  676. }
  677. /* modify entry */
  678. pb = slapi_pblock_new ();
  679. slapi_mod_init (&smod, 1);
  680. slapi_mod_set_type (&smod, "mail");
  681. slapi_mod_set_operation (&smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  682. bv.bv_val = "[email protected]";
  683. bv.bv_len = strlen (bv.bv_val);
  684. slapi_mod_add_value(&smod, &bv);
  685. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  686. mods[1] = NULL;
  687. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  688. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  689. slapi_modify_internal_pb (pb);
  690. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  691. slapi_mod_done (&smod);
  692. slapi_pblock_destroy (pb);
  693. if (rc != LDAP_SUCCESS)
  694. {
  695. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to modify entry (%s); "
  696. "LDAP error - %d\n", dn, rc);
  697. return -1;
  698. }
  699. /* rename entry */
  700. pb = slapi_pblock_new ();
  701. cur_time = time (NULL);
  702. PR_snprintf (newrdn, sizeof(newrdn), "cn=renamed%s", ctime(&cur_time));
  703. slapi_rename_internal_set_pb_ext (pb, dn, newrdn, NULL, 1, NULL, NULL,
  704. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  705. slapi_modrdn_internal_pb (pb);
  706. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  707. slapi_pblock_destroy (pb);
  708. if (rc != LDAP_SUCCESS)
  709. {
  710. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to rename entry (%s); "
  711. "LDAP error - %d\n", dn, rc);
  712. return -1;
  713. }
  714. /* delete the entry */
  715. pb = slapi_pblock_new ();
  716. PR_snprintf (dn, sizeof(dn), "%s,%s", newrdn, REPLICA_ROOT);
  717. slapi_delete_internal_set_pb (pb, dn, NULL, NULL,
  718. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  719. slapi_delete_internal_pb (pb);
  720. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  721. slapi_pblock_destroy (pb);
  722. if (rc != LDAP_SUCCESS)
  723. {
  724. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name_cl, "failed to delete entry (%s); "
  725. "LDAP error - %d\n", dn, rc);
  726. return -1;
  727. }
  728. return 0;
  729. }