dnp_sim.c 24 KB

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