dnp_sim.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* dnp_simulation.c - this file varifies the correctness of dnp algorithm
  7. by generating random sequences of operations, applying
  8. the algorithm and outputing the result
  9. usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]
  10. -h - print usage information.
  11. -n <number of simulations> - how many simulations to perform; default - 1.
  12. -v - verbose mode (prints full entry state after each operation execution)
  13. -f <output file> - file where results are stored; by default results are
  14. printed to the screen.
  15. -o <op file> - file that contains operation sequence to execute; by default,
  16. random sequence is generated.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <memory.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #define MAX_OPS 12 /* maximum number of operations in a simulation */
  25. #define MAX_VALS 10 /* maximum number of values is entry or dn */
  26. #define NOT_PRESENT -1
  27. /* data types */
  28. typedef struct value_state
  29. {
  30. int value_index; /* value */
  31. int presense_csn; /* last time at which we know the value was present */
  32. int distinguished_csn; /* last time at which we know the value was distinguished */
  33. int delete_csn; /* last attempt to delete this value */
  34. int non_distinguished_csns [MAX_OPS];/* list of times at which value became non-distinguished */
  35. int present; /* flag that tells whether the value iscurrently present */
  36. } Value_State;
  37. typedef struct entry_state
  38. {
  39. int dn_index;
  40. int dn_csn;
  41. Value_State values[MAX_VALS]; /* values of the attribute */
  42. int attr_delete_csn; /* last attempt to delete the entire attribute */
  43. } Entry_State;
  44. typedef enum
  45. {
  46. OP_ADD_VALUE,
  47. OP_RENAME_ENTRY,
  48. OP_DELETE_VALUE,
  49. OP_DELETE_ATTR,
  50. OP_END
  51. } Operation_Type;
  52. typedef struct operation
  53. {
  54. Operation_Type type; /* operation type */
  55. int csn; /* operation type */
  56. int value_index; /* value to add, remove or rename from */
  57. int old_dn_index; /* new dn - rename only */
  58. }Operation;
  59. typedef struct simulator_params
  60. {
  61. int runs; /* number of runs */
  62. FILE *fout; /* output file */
  63. int value_count; /* number of values */
  64. int verbose; /* verbose mode */
  65. Operation *ops; /* operation sequence to apply */
  66. int op_count;
  67. }Simulator_Params;
  68. /* gloabl data */
  69. Simulator_Params sim;
  70. char *g_values[] =
  71. {
  72. "v",
  73. "u",
  74. "w",
  75. NULL
  76. };
  77. /* forward declarations */
  78. /* initialization */
  79. void process_cmd (int argc, char **argv);
  80. void set_default_sim_params ();
  81. int count_values ();
  82. void parse_operations_file (char *name);
  83. void parse_operation (char *line, int pos);
  84. int value2index (char *value);
  85. void print_usage ();
  86. /* simulation run */
  87. void run_simulation ();
  88. void generate_operations (Operation **ops, int *op_count);
  89. void generate_operation (Operation *op, int csn, int *last_dn_index);
  90. int* generate_operation_order (int op_count, int seq_num);
  91. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry);
  92. void init_entry_state (Entry_State *entry);
  93. void init_value_state (Value_State *val, int seq_num);
  94. void apply_operation (Entry_State *entry, Operation *op);
  95. void free_operations (Operation **ops);
  96. int ** new_perm_table (int op_count);
  97. void free_perm_table (int ***perm_table, int op_count);
  98. int get_perm_count (int op_count);
  99. void generate_perm_table (int *elements, int element_count, int static_part,
  100. int **perm_table);
  101. void apply_add_operation (Entry_State *entry, Operation *op);
  102. void apply_value_delete_operation (Entry_State *entry, Operation *op);
  103. void apply_attr_delete_operation (Entry_State *entry, Operation *op);
  104. void apply_rename_operation (Entry_State *entry, Operation *op);
  105. void make_value_distinguished (int op_csn, Entry_State *entry, int value_index);
  106. void make_value_non_distinguished (int op_csn, Entry_State *entry, int value_index);
  107. void purge_value_state (Value_State *value);
  108. void purge_non_distinguished_csns (Value_State *value);
  109. void resolve_value_state (Entry_State *entry, int value_index);
  110. int value_distinguished_at_delete (Value_State *value, int attr_delete_csn);
  111. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run);
  112. /* data tracing */
  113. void dump_operations (Operation *ops, int op_count, int *order);
  114. void dump_operation (Operation *op);
  115. void dump_perm_table (int **perm_table, int op_count);
  116. void dump_entry_state (Entry_State *entry);
  117. void dump_value_state (Value_State *value);
  118. void dump_list (int *list);
  119. /* misc functions */
  120. int max_val (int a, int b);
  121. int is_list_empty (int *list);
  122. int min_list_val (int *list);
  123. int list_equal (int *list1, int *list2);
  124. int main (int argc, char **argv)
  125. {
  126. int i;
  127. process_cmd (argc, argv);
  128. for (i = 0; i < sim.runs; i++)
  129. {
  130. fprintf (sim.fout, "*******running simulation #%d ...\n\n", i+1);
  131. run_simulation ();
  132. fprintf (sim.fout, "\n*******done with simulation #%d ...\n\n", i+1);
  133. }
  134. if (sim.fout != stdout)
  135. fclose (sim.fout);
  136. return 0;
  137. }
  138. void process_cmd (int argc, char **argv)
  139. {
  140. int i;
  141. set_default_sim_params ();
  142. if (argc == 1)
  143. {
  144. return;
  145. }
  146. if (strcmp (argv[1], "-h") == 0) /* print help */
  147. {
  148. print_usage ();
  149. exit (0);
  150. }
  151. i = 1;
  152. while (i < argc)
  153. {
  154. if (strcmp (argv[i], "-v") == 0) /* verbose mode */
  155. {
  156. sim.verbose = 1;
  157. i ++;
  158. }
  159. else if (strcmp (argv[i], "-n") == 0)
  160. {
  161. if (i < argc - 1)
  162. {
  163. int runs = atoi (argv[i + 1]);
  164. if (runs > 0)
  165. sim.runs = runs;
  166. i+=2;
  167. }
  168. else
  169. {
  170. /* ONREPL print warning */
  171. i++;
  172. }
  173. }
  174. else if (strcmp (argv[i], "-f") == 0) /* output file */
  175. {
  176. if (i < argc - 1)
  177. {
  178. FILE *f = fopen (argv[i + 1], "w");
  179. if (f == 0)
  180. {
  181. printf ("failed to open output file; error - %s, using stdout\n",
  182. strerror(errno));
  183. }
  184. else
  185. {
  186. /* ONREPL print warning */
  187. sim.fout = f;
  188. }
  189. i += 2;
  190. }
  191. else
  192. i++;
  193. }
  194. else if (strcmp (argv[i], "-o") == 0) /* file with operation sequence */
  195. {
  196. if (i < argc - 1)
  197. {
  198. parse_operations_file (argv[i+1]);
  199. i += 2;
  200. }
  201. else
  202. {
  203. /* ONREPL print warning */
  204. i ++;
  205. }
  206. }
  207. else /* unknown option */
  208. {
  209. printf ("unknown option - %s; ignored\n", argv[i]);
  210. i ++;
  211. }
  212. }
  213. }
  214. void set_default_sim_params ()
  215. {
  216. memset (&sim, 0, sizeof (sim));
  217. sim.runs = 1;
  218. sim.fout = stdout;
  219. sim.value_count = count_values ();
  220. }
  221. /* file format: <op count>
  222. add <value>
  223. delete <value>
  224. delete attribute
  225. rename <value> to <value>
  226. */
  227. void parse_operations_file (char *name)
  228. {
  229. FILE *file = fopen (name, "r");
  230. char line [256];
  231. int i;
  232. if (file == NULL)
  233. {
  234. printf ("failed to open operations file %s: error = %d\n", name, errno);
  235. print_usage ();
  236. exit (1);
  237. }
  238. i = 0;
  239. while (fgets (line, sizeof (line), file))
  240. {
  241. if (i == 0)
  242. {
  243. /* read operation count */
  244. sim.op_count = atoi (line);
  245. if (sim.op_count < 1 || sim.op_count > MAX_OPS/2)
  246. {
  247. printf ("invalid operation count - %d; value must be between 1 and %d\n",
  248. sim.op_count, MAX_OPS/2);
  249. print_usage ();
  250. exit (1);
  251. }
  252. else
  253. {
  254. sim.ops = (Operation*)malloc (sim.op_count * sizeof (Operation));
  255. }
  256. }
  257. else
  258. {
  259. if (strlen (line) == 0) /* skip empty lines */
  260. continue;
  261. parse_operation (line, i);
  262. }
  263. i ++;
  264. }
  265. }
  266. void parse_operation (char *line, int i)
  267. {
  268. sim.ops [i - 1].csn = i;
  269. if (line[strlen(line) - 1] == '\n')
  270. line[strlen(line) - 1] = '\0';
  271. if (strncmp (line, "add", 3) == 0)
  272. {
  273. sim.ops [i - 1].type = OP_ADD_VALUE;
  274. sim.ops [i - 1].value_index = value2index (&line[4]);
  275. }
  276. else if (strncmp (line, "delete attribute", 16) == 0)
  277. {
  278. sim.ops [i - 1].type = OP_DELETE_ATTR;
  279. }
  280. else if (strncmp (line, "delete", 6) == 0)
  281. {
  282. sim.ops [i - 1].type = OP_DELETE_VALUE;
  283. sim.ops [i - 1].value_index = value2index (&line[7]);
  284. }
  285. else if (strncmp (line, "rename", 6) == 0)
  286. {
  287. char *tok;
  288. sim.ops [i - 1].type = OP_RENAME_ENTRY;
  289. /* strtok() is not MT safe, but it is okay to call here because this is a program test */
  290. tok = strtok (&line[7], " ");
  291. sim.ops [i - 1].old_dn_index = value2index (tok);
  292. /* skip to */
  293. tok = strtok (NULL, " ");
  294. tok = strtok (NULL, " ");
  295. sim.ops [i - 1].value_index = value2index (tok);
  296. }
  297. else
  298. {
  299. /* invalid line */
  300. printf ("invalid operation: %s\n", line);
  301. exit (1);
  302. }
  303. }
  304. int value2index (char *value)
  305. {
  306. int i;
  307. for (i = 0; i < sim.value_count; i++)
  308. {
  309. if (strcmp (g_values[i], value) == 0)
  310. return i;
  311. }
  312. return -1;
  313. }
  314. void print_usage ()
  315. {
  316. printf ("usage: dnp_sim [-h] [-n <number of simulations> ] [-v] [-f <output file>]\n"
  317. "\t-h - print usage information\n"
  318. "\t-n <number of simulations>; default - 1\n"
  319. "\t-v - verbose mode\n"
  320. "\t-f <output file> - by default, results are printed to the screen\n"
  321. "\t-o <op file> - file that contains operation sequence to execute;\n"
  322. "\tby default, random sequence is generated.\n");
  323. }
  324. int count_values ()
  325. {
  326. int i;
  327. for (i = 0; g_values[i]; i++);
  328. return i;
  329. }
  330. void run_simulation ()
  331. {
  332. int *order;
  333. int i;
  334. int perm_count;
  335. Entry_State entry_first, entry_current;
  336. int error = 0;
  337. init_entry_state (&entry_first);
  338. fprintf (sim.fout, "initial entry state :\n");
  339. dump_entry_state (&entry_first);
  340. if (sim.ops == NULL)
  341. {
  342. generate_operations (&sim.ops, &sim.op_count);
  343. }
  344. fprintf (sim.fout, "initial operation set:\n");
  345. dump_operations (sim.ops, sim.op_count, NULL/* order */);
  346. perm_count = get_perm_count (sim.op_count);
  347. for (i = 0; i < perm_count; i++)
  348. {
  349. fprintf (sim.fout, "--------------------------------\n");
  350. fprintf (sim.fout, "simulation run %d\n", i + 1);
  351. fprintf (sim.fout, "--------------------------------\n");
  352. order = generate_operation_order (sim.op_count, i);
  353. if (i == 0)
  354. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_first);
  355. else
  356. {
  357. apply_operation_sequence (sim.ops, sim.op_count, order, &entry_current);
  358. error |= compare_entry_state (&entry_first, &entry_current, i + 1);
  359. }
  360. }
  361. switch (error)
  362. {
  363. case 0: fprintf (sim.fout, "all runs left the entry in the same state\n");
  364. break;
  365. case 1: fprintf (sim.fout, "while value presence is consistent across all runs, "
  366. "the exact state does not match\n");
  367. break;
  368. case 3: fprintf (sim.fout, "the runs left entries in an inconsistent state\n");
  369. break;
  370. }
  371. free_operations (&sim.ops);
  372. }
  373. void generate_operations (Operation **ops, int *op_count)
  374. {
  375. int i;
  376. int last_dn_index = 0;
  377. /* generate number operations in the sequence */
  378. *op_count = slapi_rand () % (MAX_OPS / 2) + 1;
  379. *ops = (Operation *)malloc (*op_count * sizeof (Operation));
  380. for (i = 0; i < *op_count; i ++)
  381. {
  382. generate_operation (&((*ops)[i]), i + 1, &last_dn_index);
  383. }
  384. }
  385. void generate_operation (Operation *op, int csn, int *last_dn_index)
  386. {
  387. /* generate operation type */
  388. op->type = slapi_rand () % OP_END;
  389. /* generate value to which operation applies */
  390. op->value_index = slapi_rand () % sim.value_count;
  391. op->csn = csn;
  392. /* generate new distinguished value */
  393. if (op->type == OP_RENAME_ENTRY)
  394. {
  395. op->old_dn_index = *last_dn_index;
  396. while (op->value_index == op->old_dn_index)
  397. op->value_index = slapi_rand () % sim.value_count;
  398. *last_dn_index = op->value_index;
  399. }
  400. }
  401. int* generate_operation_order (int op_count, int seq_num)
  402. {
  403. static int **perm_table = NULL;
  404. /* first request - generate pemutation table */
  405. if (seq_num == 0)
  406. {
  407. int elements [MAX_OPS];
  408. int i;
  409. if (perm_table)
  410. free_perm_table (&perm_table, op_count);
  411. perm_table = new_perm_table (op_count);
  412. for (i = 0; i < op_count; i++)
  413. elements [i] = i;
  414. generate_perm_table (elements, op_count, 0 /* static part */,
  415. perm_table);
  416. /* dump_perm_table (perm_table, op_count);*/
  417. }
  418. return perm_table [seq_num];
  419. }
  420. void apply_operation_sequence (Operation *ops, int op_count, int *order, Entry_State *entry)
  421. {
  422. int i;
  423. init_entry_state (entry);
  424. if (!sim.verbose)
  425. {
  426. if (!sim.verbose)
  427. {
  428. fprintf (sim.fout, "operation_sequence for this run:\n");
  429. dump_operations (ops, op_count, order);
  430. }
  431. }
  432. for (i = 0; i < op_count; i++)
  433. {
  434. apply_operation (entry, &(ops [order[i]]));
  435. }
  436. if (!sim.verbose)
  437. {
  438. fprintf (sim.fout, "final entry state :\n");
  439. dump_entry_state (entry);
  440. }
  441. }
  442. void init_entry_state (Entry_State *entry)
  443. {
  444. int i;
  445. memset (entry, 0, sizeof (*entry));
  446. entry->attr_delete_csn = NOT_PRESENT;
  447. for (i = 0; i < sim.value_count; i++)
  448. {
  449. init_value_state (&(entry->values[i]), i);
  450. }
  451. }
  452. void init_value_state (Value_State *val, int seq_num)
  453. {
  454. memset (val, 0, sizeof (*val));
  455. val->value_index = seq_num;
  456. val->present = 1;
  457. val->delete_csn = NOT_PRESENT;
  458. if (seq_num > 0) /* only first value is distinguished */
  459. val->distinguished_csn = -1;
  460. }
  461. void apply_operation (Entry_State *entry, Operation *op)
  462. {
  463. switch (op->type)
  464. {
  465. case OP_ADD_VALUE: apply_add_operation (entry, op);
  466. break;
  467. case OP_DELETE_VALUE: apply_value_delete_operation (entry, op);
  468. break;
  469. case OP_DELETE_ATTR: apply_attr_delete_operation (entry, op);
  470. break;
  471. case OP_RENAME_ENTRY: apply_rename_operation (entry, op);
  472. break;
  473. }
  474. if (sim.verbose)
  475. {
  476. fprintf (sim.fout, "operation: ");
  477. dump_operation (op);
  478. fprintf (sim.fout, "\n");
  479. dump_entry_state (entry);
  480. }
  481. }
  482. void free_operations (Operation **ops)
  483. {
  484. free (*ops);
  485. *ops = NULL;
  486. }
  487. int **new_perm_table (int op_count)
  488. {
  489. int i;
  490. int **perm_table;
  491. int perm_count = get_perm_count (op_count);
  492. perm_table = (int**)malloc (perm_count * sizeof (int*));
  493. for (i = 0; i < perm_count; i ++)
  494. perm_table [i] = (int*) malloc (op_count * sizeof (int));
  495. return perm_table;
  496. }
  497. void free_perm_table (int ***perm_table, int op_count)
  498. {
  499. int i;
  500. int perm_count = get_perm_count (op_count);
  501. for (i = 0; i < perm_count; i ++)
  502. free ((*perm_table)[i]);
  503. free (*perm_table);
  504. *perm_table = NULL;
  505. }
  506. void generate_perm_table (int *elements, int element_count, int static_part,
  507. int **perm_table)
  508. {
  509. int i;
  510. int elements_copy [MAX_OPS];
  511. int start_pos;
  512. if (element_count - 1 == static_part)
  513. {
  514. memcpy (*perm_table, elements, element_count * sizeof (int));
  515. return;
  516. }
  517. start_pos = 0;
  518. for (i = 0; i < element_count - static_part; i ++)
  519. {
  520. memcpy (elements_copy, elements, element_count * sizeof (int));
  521. elements_copy [static_part] = elements [static_part + i];
  522. elements_copy [static_part + i] = elements [static_part];
  523. generate_perm_table (elements_copy, element_count, static_part + 1,
  524. &perm_table [start_pos]);
  525. start_pos += get_perm_count (element_count - static_part - 1);
  526. }
  527. }
  528. int get_perm_count (int op_count)
  529. {
  530. int i;
  531. int perm_count = 1;
  532. for (i = 2; i <= op_count; i ++)
  533. perm_count *= i;
  534. return perm_count;
  535. }
  536. void apply_add_operation (Entry_State *entry, Operation *op)
  537. {
  538. if (entry->values[op->value_index].presense_csn < op->csn)
  539. {
  540. entry->values[op->value_index].presense_csn = op->csn;
  541. entry->values[op->value_index].present = 1;
  542. resolve_value_state (entry, op->value_index);
  543. }
  544. }
  545. void apply_value_delete_operation (Entry_State *entry, Operation *op)
  546. {
  547. if (entry->values[op->value_index].delete_csn < op->csn)
  548. {
  549. entry->values[op->value_index].delete_csn = op->csn;
  550. resolve_value_state (entry, op->value_index);
  551. }
  552. }
  553. void apply_attr_delete_operation (Entry_State *entry, Operation *op)
  554. {
  555. int i;
  556. if (entry->attr_delete_csn < op->csn)
  557. {
  558. entry->attr_delete_csn = op->csn;
  559. for (i = 0; i < sim.value_count; i++)
  560. {
  561. resolve_value_state (entry, i);
  562. }
  563. }
  564. }
  565. void apply_rename_operation (Entry_State *entry, Operation *op)
  566. {
  567. if (entry->dn_csn < op->csn)
  568. {
  569. entry->dn_index = op->value_index;
  570. entry->dn_csn = op->csn;
  571. }
  572. make_value_non_distinguished (op->csn, entry, op->old_dn_index);
  573. make_value_distinguished (op->csn, entry, op->value_index);
  574. }
  575. void make_value_distinguished (int op_csn, Entry_State *entry, int value_index)
  576. {
  577. Value_State *value = &(entry->values[value_index]);
  578. if (value->distinguished_csn < op_csn)
  579. {
  580. value->distinguished_csn = op_csn;
  581. if (value->presense_csn < op_csn)
  582. {
  583. value->present = 1;
  584. value->presense_csn = op_csn;
  585. }
  586. resolve_value_state (entry, value_index);
  587. }
  588. }
  589. void make_value_non_distinguished (int op_csn, Entry_State *entry, int value_index)
  590. {
  591. int i = 0;
  592. int index;
  593. Value_State *value = &(entry->values[value_index]);
  594. if (op_csn < value->distinguished_csn)
  595. return;
  596. /* insert into sorted list */
  597. while (value->non_distinguished_csns[i] && value->non_distinguished_csns[i] < op_csn)
  598. i++;
  599. if (value->non_distinguished_csns[i] == 0)
  600. value->non_distinguished_csns[i] = op_csn;
  601. else
  602. {
  603. index = i;
  604. while (value->non_distinguished_csns[i])
  605. i++;
  606. memcpy (&(value->non_distinguished_csns[index + 1]),
  607. &(value->non_distinguished_csns[index]), (i - index) * sizeof (int));
  608. value->non_distinguished_csns[index] = op_csn;
  609. }
  610. resolve_value_state (entry, value_index);
  611. }
  612. void purge_value_state (Value_State *value)
  613. {
  614. /* value state information can be purged once a value was
  615. readed/made distinguished because at that point we know that the value
  616. existed/was distinguished */
  617. purge_non_distinguished_csns (value);
  618. if (value->delete_csn < max_val (value->distinguished_csn, value->presense_csn))
  619. value->delete_csn = NOT_PRESENT;
  620. }
  621. void purge_non_distinguished_csns (Value_State *value)
  622. {
  623. int i = 0;
  624. int index;
  625. while (value->non_distinguished_csns[i] &&
  626. value->non_distinguished_csns[i] < value->distinguished_csn)
  627. i ++;
  628. if (i > 0)
  629. {
  630. index = i-1;
  631. while (value->non_distinguished_csns[i])
  632. i ++;
  633. if (i > index + 1)
  634. {
  635. memcpy (value->non_distinguished_csns, &value->non_distinguished_csns[index+1],
  636. (i - index - 1) * sizeof (int));
  637. memset (&(value->non_distinguished_csns[index+1]), 0, sizeof (int) * (i - index - 1));
  638. }
  639. else
  640. {
  641. memset (value->non_distinguished_csns, 0, sizeof (int) * i);
  642. }
  643. }
  644. }
  645. int is_list_empty (int *list)
  646. {
  647. return (list[0] == 0);
  648. }
  649. int min_list_val (int *list)
  650. {
  651. return (list [0]);
  652. }
  653. void resolve_value_state (Entry_State *entry, int value_index)
  654. {
  655. Value_State *value = &(entry->values[value_index]);
  656. purge_value_state (value);
  657. /* no deletes that effect the state */
  658. if (max_val (value->delete_csn, entry->attr_delete_csn) <
  659. max_val (value->distinguished_csn, value->presense_csn))
  660. return;
  661. if (value->present) /* check if it should be removed based on the current state */
  662. {
  663. if (!value_distinguished_at_delete (value, entry->attr_delete_csn))
  664. {
  665. /* note that we keep presence csn because we might have to restore
  666. the value in the future */
  667. value->present = 0;
  668. }
  669. }
  670. else /* not present - check if it should be restored */
  671. {
  672. if (value_distinguished_at_delete (value, entry->attr_delete_csn))
  673. {
  674. value->present = 1;
  675. }
  676. }
  677. }
  678. /* Note we can't trim distinguished_csn (even during regular trimming)
  679. because in some cases we would not be able to figure out whether
  680. a value was distinguished or not at the time of deletion.
  681. Example 1: Example2:
  682. csn order operation csn order operation
  683. 1 1 make V distinguished 1 1 make V distinguished
  684. 3 3 delete V 2 2 make V non distinguished
  685. 4 2 make V non-distinguished 3 4 delete V
  686. 4 3 make V non distinguished (on another server)
  687. if the csns up to 2 were triimed, when delete operation is received, the state
  688. is exactly the same in both examples but in example one delete should not go
  689. through while in example 2 it should
  690. */
  691. int value_distinguished_at_delete (Value_State *value, int attr_delete_csn)
  692. {
  693. if (value->distinguished_csn >= 0 &&
  694. (is_list_empty (value->non_distinguished_csns) ||
  695. min_list_val (value->non_distinguished_csns) >
  696. max_val (value->delete_csn, attr_delete_csn)))
  697. return 1;
  698. else
  699. return 0;
  700. }
  701. int compare_entry_state (Entry_State *entry1, Entry_State *entry2, int run)
  702. {
  703. int j;
  704. int error = 0;
  705. /* first - quick check for present / not present */
  706. for (j = 0; j < sim.value_count; j++)
  707. {
  708. if (entry1->values[j].present != entry2->values[j].present)
  709. {
  710. fprintf (sim.fout,
  711. "value %s is %s present in the first run but %s present in the %d run\n",
  712. g_values[j], entry1->values[j].present ? "" : "not",
  713. entry2->values[j].present ? "" : "not", run);
  714. error = 1;
  715. }
  716. }
  717. if (error)
  718. return 3;
  719. /* compare value state */
  720. error = 0;
  721. if (entry1->attr_delete_csn != entry2->attr_delete_csn)
  722. {
  723. fprintf (sim.fout, "attribute delete csn is %d for run 1 "
  724. "but is %d for run %d\n", entry1->attr_delete_csn,
  725. entry2->attr_delete_csn, run);
  726. error = 1;
  727. }
  728. for (j = 0; j < sim.value_count; j++)
  729. {
  730. if (entry1->values[j].presense_csn != entry2->values[j].presense_csn)
  731. {
  732. fprintf (sim.fout, "presence csn for value %s is %d in run 1 "
  733. "but is %d in run %d\n", g_values[j], entry1->values[j].presense_csn,
  734. entry2->values[j].presense_csn, run);
  735. error = 1;
  736. }
  737. if (entry1->values[j].distinguished_csn != entry2->values[j].distinguished_csn)
  738. {
  739. fprintf (sim.fout, "distinguished csn for value %s is %d in run 1 "
  740. "but is %d in run %d\n", g_values[j], entry1->values[j].distinguished_csn,
  741. entry2->values[j].distinguished_csn, run);
  742. error = 1;
  743. }
  744. if (entry1->values[j].delete_csn != entry2->values[j].delete_csn)
  745. {
  746. fprintf (sim.fout, "delete csn for value %s is %d in run 1 "
  747. "but is %d in run %d\n", g_values[j], entry1->values[j].delete_csn,
  748. entry2->values[j].delete_csn, run);
  749. error = 1;
  750. }
  751. if (!list_equal (entry1->values[j].non_distinguished_csns,
  752. entry2->values[j].non_distinguished_csns))
  753. {
  754. fprintf (sim.fout, "pending list mismatch for valye %s in runs 1 and %d\n",
  755. g_values[j], run);
  756. dump_list (entry1->values[j].non_distinguished_csns);
  757. dump_list (entry2->values[j].non_distinguished_csns);
  758. }
  759. }
  760. if (error != 0)
  761. {
  762. return 1;
  763. }
  764. else
  765. return 0;
  766. }
  767. void dump_operations (Operation *ops, int op_count, int *order)
  768. {
  769. int index;
  770. int i;
  771. for (i = 0; i < op_count; i ++)
  772. {
  773. if (order == NULL) /* current order */
  774. index = i;
  775. else
  776. index = order [i];
  777. dump_operation (&ops[index]);
  778. }
  779. fprintf (sim.fout, "\n");
  780. }
  781. void dump_operation (Operation *op)
  782. {
  783. switch (op->type)
  784. {
  785. case OP_ADD_VALUE:
  786. fprintf (sim.fout, "\t%d add value %s\n", op->csn,
  787. g_values [op->value_index]);
  788. break;
  789. case OP_DELETE_VALUE:
  790. fprintf (sim.fout, "\t%d delete value %s\n", op->csn,
  791. g_values [op->value_index]);
  792. break;
  793. case OP_DELETE_ATTR:
  794. fprintf (sim.fout, "\t%d delete attribute\n", op->csn);
  795. break;
  796. case OP_RENAME_ENTRY:
  797. fprintf (sim.fout, "\t%d rename entry from %s to %s\n", op->csn,
  798. g_values [op->old_dn_index], g_values [op->value_index]);
  799. break;
  800. }
  801. }
  802. void dump_perm_table (int **perm_table, int op_count)
  803. {
  804. int i, j;
  805. int perm_count = get_perm_count (op_count);
  806. for (i = 0; i < op_count; i++)
  807. {
  808. for (j = 0; j < perm_count; j++)
  809. {
  810. fprintf (sim.fout, "%d ", perm_table [j][i]);
  811. }
  812. fprintf (sim.fout, "\n");
  813. }
  814. }
  815. void dump_entry_state (Entry_State *entry)
  816. {
  817. int i;
  818. fprintf (sim.fout, "\tentry dn: %s; dn csn - %d\n",
  819. g_values [entry->dn_index], entry->dn_csn);
  820. if (sim.verbose)
  821. fprintf (sim.fout, "\n");
  822. for (i = 0; i < sim.value_count; i++)
  823. {
  824. dump_value_state (&(entry->values[i]));
  825. }
  826. fprintf (sim.fout, "\n");
  827. }
  828. void dump_value_state (Value_State *value)
  829. {
  830. fprintf (sim.fout, "\tvalue %s is %s\n", g_values[value->value_index],
  831. value->present ? "present" : "not present");
  832. if (sim.verbose)
  833. {
  834. fprintf (sim.fout, "\t\tpresent csn: %d\n", value->presense_csn);
  835. fprintf (sim.fout, "\t\tdistinguished csn: %d\n", value->distinguished_csn);
  836. fprintf (sim.fout, "\t\tdelete value csn: %d\n", value->delete_csn);
  837. fprintf (sim.fout, "\t\tnon distinguished csns: ");
  838. dump_list (value->non_distinguished_csns);
  839. fprintf (sim.fout, "\n");
  840. }
  841. }
  842. void dump_list (int *list)
  843. {
  844. int i = 0;
  845. while (list[i])
  846. {
  847. fprintf (sim.fout, "%d ", list[i]);
  848. i ++;
  849. }
  850. fprintf (sim.fout, "\n");
  851. }
  852. /* misc functions */
  853. int max_val (int a, int b)
  854. {
  855. if (a >= b)
  856. return a;
  857. else
  858. return b;
  859. }
  860. int list_equal (int *list1, int *list2)
  861. {
  862. int i;
  863. i = 0;
  864. while (list1[i] && list2[i])
  865. {
  866. if (list1[i] != list2[i])
  867. return 0;
  868. i ++;
  869. }
  870. if (list1[i] != list2[i])
  871. return 0;
  872. else
  873. return 1;
  874. }