cl5_test.c 23 KB

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