cl5_test.c 23 KB

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