cl5_test.c 25 KB

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